ICU-20799 delete calendarToAdopt upon error

ICU-20799 add unit tests

ICU-20799 initialize error and add comments.
diff --git a/icu4c/source/i18n/smpdtfmt.cpp b/icu4c/source/i18n/smpdtfmt.cpp
index 0b4939f..b8edff4 100644
--- a/icu4c/source/i18n/smpdtfmt.cpp
+++ b/icu4c/source/i18n/smpdtfmt.cpp
@@ -3999,6 +3999,7 @@
   DateFormatSymbols *newSymbols =
           DateFormatSymbols::createForLocale(calLocale, status);
   if (U_FAILURE(status)) {
+      delete calendarToAdopt;
       return;
   }
   DateFormat::adoptCalendar(calendarToAdopt);
diff --git a/icu4c/source/test/intltest/dtfmttst.cpp b/icu4c/source/test/intltest/dtfmttst.cpp
index 197a3c3..128edba 100644
--- a/icu4c/source/test/intltest/dtfmttst.cpp
+++ b/icu4c/source/test/intltest/dtfmttst.cpp
@@ -127,6 +127,7 @@
     TESTCASE_AUTO(TestMinuteSecondFieldsInOddPlaces);
     TESTCASE_AUTO(TestDayPeriodParsing);
     TESTCASE_AUTO(TestParseRegression13744);
+    TESTCASE_AUTO(TestAdoptCalendarLeak);
 
     TESTCASE_AUTO_END;
 }
@@ -5540,6 +5541,19 @@
     assertEquals("Error index", inDate.length(), pos.getErrorIndex());
 }
 
+void DateFormatTest::TestAdoptCalendarLeak() {
+    UErrorCode status = U_ZERO_ERROR;
+    // This test relies on the locale fullName exceeding ULOC_FULLNAME_CAPACITY
+    // in order for setKeywordValue to fail.
+    SimpleDateFormat sdf(
+        "d.M.y",
+        Locale("de__POSIX@colstrength=primary;currency=eur;em=default;"
+               "hours=h23;lb=strict;lw=normal;measure=metric;numbers=latn;"
+               "rg=atzzzz;sd=atat1;ss=none;timezone=Europe/Vienna"),
+        status);
+    sdf.adoptCalendar(Calendar::createInstance(status));
+}
+
 #endif /* #if !UCONFIG_NO_FORMATTING */
 
 //eof
diff --git a/icu4c/source/test/intltest/dtfmttst.h b/icu4c/source/test/intltest/dtfmttst.h
index efc5673..fd07b5a 100644
--- a/icu4c/source/test/intltest/dtfmttst.h
+++ b/icu4c/source/test/intltest/dtfmttst.h
@@ -262,6 +262,7 @@
     void TestMinuteSecondFieldsInOddPlaces();
     void TestDayPeriodParsing();
     void TestParseRegression13744();
+    void TestAdoptCalendarLeak();
 
 private:
     UBool showParse(DateFormat &format, const UnicodeString &formattedString);