]>
Commit | Line | Data |
---|---|---|
f3c0d7a5 A |
1 | // © 2016 and later: Unicode, Inc. and others. |
2 | // License & terms of use: http://www.unicode.org/copyright.html | |
b75a7d8f A |
3 | |
4 | /******************************************************************** | |
2ca993e8 A |
5 | * COPYRIGHT: |
6 | * Copyright (c) 1997-2016, International Business Machines Corporation and | |
b75a7d8f A |
7 | * others. All Rights Reserved. |
8 | ********************************************************************/ | |
9 | ||
10 | #include "unicode/utypes.h" | |
11 | ||
12 | #if !UCONFIG_NO_FORMATTING | |
13 | ||
14 | #include "sdtfmtts.h" | |
15 | ||
16 | #include "unicode/smpdtfmt.h" | |
17 | #include "unicode/dtfmtsym.h" | |
18 | ||
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. | |
22 | ||
23 | void IntlTestSimpleDateFormatAPI::runIndexedTest( int32_t index, UBool exec, const char* &name, char* /*par*/ ) | |
24 | { | |
25 | if (exec) logln("TestSuite SimpleDateFormatAPI"); | |
26 | switch (index) { | |
2ca993e8 | 27 | case 0: name = "SimpleDateFormat API test"; |
b75a7d8f A |
28 | if (exec) { |
29 | logln("SimpleDateFormat API test---"); logln(""); | |
30 | UErrorCode status = U_ZERO_ERROR; | |
73c04bcf | 31 | Locale saveLocale; |
b75a7d8f A |
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"); | |
35 | } | |
36 | testAPI(/*par*/); | |
73c04bcf | 37 | Locale::setDefault(saveLocale, status); |
b75a7d8f A |
38 | } |
39 | break; | |
40 | ||
41 | default: name = ""; break; | |
42 | } | |
43 | } | |
44 | ||
45 | /** | |
46 | * Test various generic API methods of SimpleDateFormat for API coverage. | |
47 | */ | |
48 | void IntlTestSimpleDateFormatAPI::testAPI(/*char *par*/) | |
49 | { | |
50 | UErrorCode status = U_ZERO_ERROR; | |
51 | ||
52 | // ======= Test constructors | |
53 | ||
54 | logln("Testing SimpleDateFormat constructors"); | |
55 | ||
56 | SimpleDateFormat def(status); | |
57 | if(U_FAILURE(status)) { | |
73c04bcf A |
58 | dataerrln("ERROR: Could not create SimpleDateFormat (default) - exitting"); |
59 | return; | |
b75a7d8f A |
60 | } |
61 | ||
62 | status = U_ZERO_ERROR; | |
729e4ab9 A |
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", ""); | |
66 | ||
b75a7d8f A |
67 | SimpleDateFormat pat(pattern, status); |
68 | if(U_FAILURE(status)) { | |
729e4ab9 | 69 | errln("ERROR: Could not create SimpleDateFormat (pattern) - %s", u_errorName(status)); |
b75a7d8f A |
70 | } |
71 | ||
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)"); | |
76 | } | |
77 | ||
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)"); | |
82 | } | |
83 | ||
84 | status = U_ZERO_ERROR; | |
85 | SimpleDateFormat cust1(pattern, symbols, status); | |
86 | if(U_FAILURE(status)) { | |
73c04bcf A |
87 | dataerrln("ERROR: Could not create SimpleDateFormat (pattern, symbols*) - exitting"); |
88 | return; | |
b75a7d8f A |
89 | } |
90 | ||
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)"); | |
95 | } | |
96 | ||
729e4ab9 A |
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)); | |
102 | } | |
103 | ||
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)); | |
108 | } | |
109 | ||
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"); | |
115 | } | |
116 | ||
117 | ||
b75a7d8f A |
118 | SimpleDateFormat copy(pat); |
119 | ||
120 | // ======= Test clone(), assignment, and equality | |
121 | ||
122 | logln("Testing clone(), assignment and equality operators"); | |
123 | ||
124 | if( ! (copy == pat) || copy != pat) { | |
125 | errln("ERROR: Copy constructor (or ==) failed"); | |
126 | } | |
127 | ||
128 | copy = cust1; | |
129 | if(copy != cust1) { | |
130 | errln("ERROR: Assignment (or !=) failed"); | |
131 | } | |
132 | ||
133 | Format *clone = def.clone(); | |
134 | if( ! (*clone == def) ) { | |
135 | errln("ERROR: Clone() (or ==) failed"); | |
136 | } | |
137 | delete clone; | |
138 | ||
139 | // ======= Test various format() methods | |
140 | ||
141 | logln("Testing various format() methods"); | |
142 | ||
143 | UDate d = 837039928046.0; | |
144 | Formattable fD(d, Formattable::kIsDate); | |
145 | ||
146 | UnicodeString res1, res2; | |
f3c0d7a5 | 147 | FieldPosition pos1(FieldPosition::DONT_CARE), pos2(FieldPosition::DONT_CARE); |
b75a7d8f A |
148 | |
149 | res1 = def.format(d, res1, pos1); | |
150 | logln( (UnicodeString) "" + d + " formatted to " + res1); | |
151 | ||
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"); | |
156 | } | |
157 | logln((UnicodeString) "" + fD.getDate() + " formatted to " + res2); | |
158 | ||
159 | // ======= Test parse() | |
160 | ||
161 | logln("Testing parse()"); | |
162 | ||
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); | |
168 | ||
169 | status = U_ZERO_ERROR; | |
170 | result2 = def.parse(text, status); | |
171 | if(U_FAILURE(status)) { | |
172 | errln("ERROR: parse() failed"); | |
173 | } | |
174 | logln(text + " parsed into " + result2); | |
175 | ||
176 | // ======= Test getters and setters | |
177 | ||
178 | logln("Testing getters and setters"); | |
179 | ||
180 | const DateFormatSymbols *syms = pat.getDateFormatSymbols(); | |
181 | if(!syms) { | |
182 | errln("Couldn't obtain DateFormatSymbols. Quitting test!"); | |
183 | return; | |
184 | } | |
185 | if(syms->getDynamicClassID() != DateFormatSymbols::getStaticClassID()) { | |
186 | errln("ERROR: format->getDateFormatSymbols()->getDynamicClassID() != DateFormatSymbols::getStaticClassID()"); | |
187 | } | |
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"); | |
193 | } | |
194 | ||
195 | status = U_ZERO_ERROR; | |
196 | UDate startDate = pat.get2DigitYearStart(status); | |
197 | if(U_FAILURE(status)) { | |
198 | errln("ERROR: getTwoDigitStartDate() failed"); | |
199 | } | |
200 | ||
201 | status = U_ZERO_ERROR; | |
202 | pat_fr.set2DigitYearStart(startDate, status); | |
203 | if(U_FAILURE(status)) { | |
204 | errln("ERROR: setTwoDigitStartDate() failed"); | |
205 | } | |
206 | ||
207 | // ======= Test DateFormatSymbols constructor | |
208 | newSyms =new DateFormatSymbols("gregorian", status); | |
209 | if(U_FAILURE(status)) { | |
210 | errln("ERROR: new DateFormatSymbols() failed"); | |
211 | } | |
212 | def.adoptDateFormatSymbols(newSyms); | |
213 | ||
214 | // ======= Test applyPattern() | |
215 | ||
216 | logln("Testing applyPattern()"); | |
217 | ||
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); | |
222 | ||
223 | UnicodeString s2; | |
224 | s2 = pat.toPattern(s2); | |
225 | logln("Extracted pattern is " + s2); | |
226 | if(s2 != p1) { | |
227 | errln("ERROR: toPattern() result did not match pattern applied"); | |
228 | } | |
229 | ||
230 | logln("Applying pattern " + p1); | |
231 | status = U_ZERO_ERROR; | |
232 | pat.applyLocalizedPattern(p1, status); | |
233 | if(U_FAILURE(status)) { | |
51004dcb | 234 | errln("ERROR: applyPattern() failed with %s", u_errorName(status)); |
b75a7d8f A |
235 | } |
236 | UnicodeString s3; | |
237 | status = U_ZERO_ERROR; | |
238 | s3 = pat.toLocalizedPattern(s3, status); | |
239 | if(U_FAILURE(status)) { | |
240 | errln("ERROR: toLocalizedPattern() failed"); | |
241 | } | |
242 | logln("Extracted pattern is " + s3); | |
243 | if(s3 != p1) { | |
244 | errln("ERROR: toLocalizedPattern() result did not match pattern applied"); | |
245 | } | |
246 | ||
247 | // ======= Test getStaticClassID() | |
248 | ||
249 | logln("Testing getStaticClassID()"); | |
250 | ||
251 | status = U_ZERO_ERROR; | |
252 | DateFormat *test = new SimpleDateFormat(status); | |
253 | if(U_FAILURE(status)) { | |
254 | errln("ERROR: Couldn't create a SimpleDateFormat"); | |
255 | } | |
256 | ||
257 | if(test->getDynamicClassID() != SimpleDateFormat::getStaticClassID()) { | |
258 | errln("ERROR: getDynamicClassID() didn't return the expected value"); | |
259 | } | |
46f4442e | 260 | |
b75a7d8f | 261 | delete test; |
46f4442e A |
262 | |
263 | // ======= Test Ticket 5684 (Parsing with 'e' and 'Y') | |
729e4ab9 | 264 | SimpleDateFormat object(UNICODE_STRING_SIMPLE("YYYY'W'wwe"), status); |
46f4442e A |
265 | if(U_FAILURE(status)) { |
266 | errln("ERROR: Couldn't create a SimpleDateFormat"); | |
267 | } | |
268 | object.setLenient(false); | |
269 | ParsePosition pp(0); | |
46f4442e A |
270 | UDate udDate = object.parse("2007W014", pp); |
271 | if ((double)udDate == 0.0) { | |
272 | errln("ERROR: Parsing failed using 'Y' and 'e'"); | |
273 | } | |
b331163b A |
274 | |
275 | // ====== Test ticket 11295 getNumberFormatForField returns wild pointer | |
2ca993e8 A |
276 | if (object.getNumberFormatForField('N') != NULL) { |
277 | errln("N is not a valid field, " | |
b331163b A |
278 | "getNumberFormatForField should return NULL"); |
279 | } | |
b75a7d8f A |
280 | } |
281 | ||
282 | #endif /* #if !UCONFIG_NO_FORMATTING */ |