]>
git.saurik.com Git - apple/icu.git/blob - icuSources/test/intltest/sdtfmtts.cpp
1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 /********************************************************************
6 * Copyright (c) 1997-2016, International Business Machines Corporation and
7 * others. All Rights Reserved.
8 ********************************************************************/
10 #include "unicode/utypes.h"
12 #if !UCONFIG_NO_FORMATTING
16 #include "unicode/smpdtfmt.h"
17 #include "unicode/dtfmtsym.h"
19 // This is an API test, not a unit test. It doesn't test very many cases, and doesn't
20 // try to test the full functionality. It just calls each function in the class and
21 // verifies that it works on a basic level.
23 void IntlTestSimpleDateFormatAPI::runIndexedTest( int32_t index
, UBool exec
, const char* &name
, char* /*par*/ )
25 if (exec
) logln("TestSuite SimpleDateFormatAPI");
27 case 0: name
= "SimpleDateFormat API test";
29 logln("SimpleDateFormat API test---"); logln("");
30 UErrorCode status
= U_ZERO_ERROR
;
32 Locale::setDefault(Locale::getEnglish(), status
);
33 if(U_FAILURE(status
)) {
34 errln("ERROR: Could not set default locale, test may not give correct results");
37 Locale::setDefault(saveLocale
, status
);
41 default: name
= ""; break;
46 * Test various generic API methods of SimpleDateFormat for API coverage.
48 void IntlTestSimpleDateFormatAPI::testAPI(/*char *par*/)
50 UErrorCode status
= U_ZERO_ERROR
;
52 // ======= Test constructors
54 logln("Testing SimpleDateFormat constructors");
56 SimpleDateFormat
def(status
);
57 if(U_FAILURE(status
)) {
58 dataerrln("ERROR: Could not create SimpleDateFormat (default) - exitting");
62 status
= U_ZERO_ERROR
;
63 const UnicodeString
pattern("yyyy.MM.dd G 'at' hh:mm:ss z", "");
64 const UnicodeString
override("y=hebr;d=thai;s=arab", ""); /* use invariant converter */
65 const UnicodeString
override_bogus("y=hebr;d=thai;s=bogus", "");
67 SimpleDateFormat
pat(pattern
, status
);
68 if(U_FAILURE(status
)) {
69 errln("ERROR: Could not create SimpleDateFormat (pattern) - %s", u_errorName(status
));
72 status
= U_ZERO_ERROR
;
73 SimpleDateFormat
pat_fr(pattern
, Locale::getFrench(), status
);
74 if(U_FAILURE(status
)) {
75 errln("ERROR: Could not create SimpleDateFormat (pattern French)");
78 status
= U_ZERO_ERROR
;
79 DateFormatSymbols
*symbols
= new DateFormatSymbols(Locale::getFrench(), status
);
80 if(U_FAILURE(status
)) {
81 errln("ERROR: Could not create DateFormatSymbols (French)");
84 status
= U_ZERO_ERROR
;
85 SimpleDateFormat
cust1(pattern
, symbols
, status
);
86 if(U_FAILURE(status
)) {
87 dataerrln("ERROR: Could not create SimpleDateFormat (pattern, symbols*) - exitting");
91 status
= U_ZERO_ERROR
;
92 SimpleDateFormat
cust2(pattern
, *symbols
, status
);
93 if(U_FAILURE(status
)) {
94 errln("ERROR: Could not create SimpleDateFormat (pattern, symbols)");
97 status
= U_ZERO_ERROR
;
98 logln(UnicodeString("Override with: ") + override
);
99 SimpleDateFormat
ovr1(pattern
, override
, status
);
100 if(U_FAILURE(status
)) {
101 errln("ERROR: Could not create SimpleDateFormat (pattern, override) - %s", u_errorName(status
));
104 status
= U_ZERO_ERROR
;
105 SimpleDateFormat
ovr2(pattern
, override
, Locale::getGerman(), status
);
106 if(U_FAILURE(status
)) {
107 errln("ERROR: Could not create SimpleDateFormat (pattern, override, locale) - %s", u_errorName(status
));
110 status
= U_ZERO_ERROR
;
111 logln(UnicodeString("Override with: ") + override_bogus
);
112 SimpleDateFormat
ovr3(pattern
, override_bogus
, Locale::getGerman(), status
);
113 if(U_SUCCESS(status
)) {
114 errln("ERROR: Should not have been able to create SimpleDateFormat (pattern, override, locale) with a bogus override");
118 SimpleDateFormat
copy(pat
);
120 // ======= Test clone(), assignment, and equality
122 logln("Testing clone(), assignment and equality operators");
124 if( ! (copy
== pat
) || copy
!= pat
) {
125 errln("ERROR: Copy constructor (or ==) failed");
130 errln("ERROR: Assignment (or !=) failed");
133 Format
*clone
= def
.clone();
134 if( ! (*clone
== def
) ) {
135 errln("ERROR: Clone() (or ==) failed");
139 // ======= Test various format() methods
141 logln("Testing various format() methods");
143 UDate d
= 837039928046.0;
144 Formattable
fD(d
, Formattable::kIsDate
);
146 UnicodeString res1
, res2
;
147 FieldPosition
pos1(FieldPosition::DONT_CARE
), pos2(FieldPosition::DONT_CARE
);
149 res1
= def
.format(d
, res1
, pos1
);
150 logln( (UnicodeString
) "" + d
+ " formatted to " + res1
);
152 status
= U_ZERO_ERROR
;
153 res2
= cust1
.format(fD
, res2
, pos2
, status
);
154 if(U_FAILURE(status
)) {
155 errln("ERROR: format(Formattable [Date]) failed");
157 logln((UnicodeString
) "" + fD
.getDate() + " formatted to " + res2
);
159 // ======= Test parse()
161 logln("Testing parse()");
163 UnicodeString
text("02/03/76 2:50 AM, CST");
164 UDate result1
, result2
;
165 ParsePosition
pos(0);
166 result1
= def
.parse(text
, pos
);
167 logln(text
+ " parsed into " + result1
);
169 status
= U_ZERO_ERROR
;
170 result2
= def
.parse(text
, status
);
171 if(U_FAILURE(status
)) {
172 errln("ERROR: parse() failed");
174 logln(text
+ " parsed into " + result2
);
176 // ======= Test getters and setters
178 logln("Testing getters and setters");
180 const DateFormatSymbols
*syms
= pat
.getDateFormatSymbols();
182 errln("Couldn't obtain DateFormatSymbols. Quitting test!");
185 if(syms
->getDynamicClassID() != DateFormatSymbols::getStaticClassID()) {
186 errln("ERROR: format->getDateFormatSymbols()->getDynamicClassID() != DateFormatSymbols::getStaticClassID()");
188 DateFormatSymbols
*newSyms
= new DateFormatSymbols(*syms
);
189 def
.adoptDateFormatSymbols(newSyms
);
190 pat_fr
.setDateFormatSymbols(*newSyms
);
191 if( *(pat
.getDateFormatSymbols()) != *(def
.getDateFormatSymbols())) {
192 errln("ERROR: adopt or set DateFormatSymbols() failed");
195 status
= U_ZERO_ERROR
;
196 UDate startDate
= pat
.get2DigitYearStart(status
);
197 if(U_FAILURE(status
)) {
198 errln("ERROR: getTwoDigitStartDate() failed");
201 status
= U_ZERO_ERROR
;
202 pat_fr
.set2DigitYearStart(startDate
, status
);
203 if(U_FAILURE(status
)) {
204 errln("ERROR: setTwoDigitStartDate() failed");
207 // ======= Test DateFormatSymbols constructor
208 newSyms
=new DateFormatSymbols("gregorian", status
);
209 if(U_FAILURE(status
)) {
210 errln("ERROR: new DateFormatSymbols() failed");
212 def
.adoptDateFormatSymbols(newSyms
);
214 // ======= Test applyPattern()
216 logln("Testing applyPattern()");
218 UnicodeString
p1("yyyy.MM.dd G 'at' hh:mm:ss z");
219 logln("Applying pattern " + p1
);
220 status
= U_ZERO_ERROR
;
221 pat
.applyPattern(p1
);
224 s2
= pat
.toPattern(s2
);
225 logln("Extracted pattern is " + s2
);
227 errln("ERROR: toPattern() result did not match pattern applied");
230 logln("Applying pattern " + p1
);
231 status
= U_ZERO_ERROR
;
232 pat
.applyLocalizedPattern(p1
, status
);
233 if(U_FAILURE(status
)) {
234 errln("ERROR: applyPattern() failed with %s", u_errorName(status
));
237 status
= U_ZERO_ERROR
;
238 s3
= pat
.toLocalizedPattern(s3
, status
);
239 if(U_FAILURE(status
)) {
240 errln("ERROR: toLocalizedPattern() failed");
242 logln("Extracted pattern is " + s3
);
244 errln("ERROR: toLocalizedPattern() result did not match pattern applied");
247 // ======= Test getStaticClassID()
249 logln("Testing getStaticClassID()");
251 status
= U_ZERO_ERROR
;
252 DateFormat
*test
= new SimpleDateFormat(status
);
253 if(U_FAILURE(status
)) {
254 errln("ERROR: Couldn't create a SimpleDateFormat");
257 if(test
->getDynamicClassID() != SimpleDateFormat::getStaticClassID()) {
258 errln("ERROR: getDynamicClassID() didn't return the expected value");
263 // ======= Test Ticket 5684 (Parsing with 'e' and 'Y')
264 SimpleDateFormat
object(UNICODE_STRING_SIMPLE("YYYY'W'wwe"), status
);
265 if(U_FAILURE(status
)) {
266 errln("ERROR: Couldn't create a SimpleDateFormat");
268 object
.setLenient(false);
270 UDate udDate
= object
.parse("2007W014", pp
);
271 if ((double)udDate
== 0.0) {
272 errln("ERROR: Parsing failed using 'Y' and 'e'");
275 // ====== Test ticket 11295 getNumberFormatForField returns wild pointer
276 if (object
.getNumberFormatForField('N') != NULL
) {
277 errln("N is not a valid field, "
278 "getNumberFormatForField should return NULL");
282 #endif /* #if !UCONFIG_NO_FORMATTING */