]> git.saurik.com Git - apple/icu.git/blame - icuSources/test/intltest/sdtfmtts.cpp
ICU-511.27.tar.gz
[apple/icu.git] / icuSources / test / intltest / sdtfmtts.cpp
CommitLineData
b75a7d8f
A
1
2/********************************************************************
3 * COPYRIGHT:
51004dcb 4 * Copyright (c) 1997-2012, International Business Machines Corporation and
b75a7d8f
A
5 * others. All Rights Reserved.
6 ********************************************************************/
7
8#include "unicode/utypes.h"
9
10#if !UCONFIG_NO_FORMATTING
11
12#include "sdtfmtts.h"
13
14#include "unicode/smpdtfmt.h"
15#include "unicode/dtfmtsym.h"
16
17// This is an API test, not a unit test. It doesn't test very many cases, and doesn't
18// try to test the full functionality. It just calls each function in the class and
19// verifies that it works on a basic level.
20
21void IntlTestSimpleDateFormatAPI::runIndexedTest( int32_t index, UBool exec, const char* &name, char* /*par*/ )
22{
23 if (exec) logln("TestSuite SimpleDateFormatAPI");
24 switch (index) {
25 case 0: name = "SimpleDateFormat API test";
26 if (exec) {
27 logln("SimpleDateFormat API test---"); logln("");
28 UErrorCode status = U_ZERO_ERROR;
73c04bcf 29 Locale saveLocale;
b75a7d8f
A
30 Locale::setDefault(Locale::getEnglish(), status);
31 if(U_FAILURE(status)) {
32 errln("ERROR: Could not set default locale, test may not give correct results");
33 }
34 testAPI(/*par*/);
73c04bcf 35 Locale::setDefault(saveLocale, status);
b75a7d8f
A
36 }
37 break;
38
39 default: name = ""; break;
40 }
41}
42
43/**
44 * Test various generic API methods of SimpleDateFormat for API coverage.
45 */
46void IntlTestSimpleDateFormatAPI::testAPI(/*char *par*/)
47{
48 UErrorCode status = U_ZERO_ERROR;
49
50// ======= Test constructors
51
52 logln("Testing SimpleDateFormat constructors");
53
54 SimpleDateFormat def(status);
55 if(U_FAILURE(status)) {
73c04bcf
A
56 dataerrln("ERROR: Could not create SimpleDateFormat (default) - exitting");
57 return;
b75a7d8f
A
58 }
59
60 status = U_ZERO_ERROR;
729e4ab9
A
61 const UnicodeString pattern("yyyy.MM.dd G 'at' hh:mm:ss z", "");
62 const UnicodeString override("y=hebr;d=thai;s=arab", ""); /* use invariant converter */
63 const UnicodeString override_bogus("y=hebr;d=thai;s=bogus", "");
64
b75a7d8f
A
65 SimpleDateFormat pat(pattern, status);
66 if(U_FAILURE(status)) {
729e4ab9 67 errln("ERROR: Could not create SimpleDateFormat (pattern) - %s", u_errorName(status));
b75a7d8f
A
68 }
69
70 status = U_ZERO_ERROR;
71 SimpleDateFormat pat_fr(pattern, Locale::getFrench(), status);
72 if(U_FAILURE(status)) {
73 errln("ERROR: Could not create SimpleDateFormat (pattern French)");
74 }
75
76 status = U_ZERO_ERROR;
77 DateFormatSymbols *symbols = new DateFormatSymbols(Locale::getFrench(), status);
78 if(U_FAILURE(status)) {
79 errln("ERROR: Could not create DateFormatSymbols (French)");
80 }
81
82 status = U_ZERO_ERROR;
83 SimpleDateFormat cust1(pattern, symbols, status);
84 if(U_FAILURE(status)) {
73c04bcf
A
85 dataerrln("ERROR: Could not create SimpleDateFormat (pattern, symbols*) - exitting");
86 return;
b75a7d8f
A
87 }
88
89 status = U_ZERO_ERROR;
90 SimpleDateFormat cust2(pattern, *symbols, status);
91 if(U_FAILURE(status)) {
92 errln("ERROR: Could not create SimpleDateFormat (pattern, symbols)");
93 }
94
729e4ab9
A
95 status = U_ZERO_ERROR;
96 logln(UnicodeString("Override with: ") + override);
97 SimpleDateFormat ovr1(pattern, override, status);
98 if(U_FAILURE(status)) {
99 errln("ERROR: Could not create SimpleDateFormat (pattern, override) - %s", u_errorName(status));
100 }
101
102 status = U_ZERO_ERROR;
103 SimpleDateFormat ovr2(pattern, override, Locale::getGerman(), status);
104 if(U_FAILURE(status)) {
105 errln("ERROR: Could not create SimpleDateFormat (pattern, override, locale) - %s", u_errorName(status));
106 }
107
108 status = U_ZERO_ERROR;
109 logln(UnicodeString("Override with: ") + override_bogus);
110 SimpleDateFormat ovr3(pattern, override_bogus, Locale::getGerman(), status);
111 if(U_SUCCESS(status)) {
112 errln("ERROR: Should not have been able to create SimpleDateFormat (pattern, override, locale) with a bogus override");
113 }
114
115
b75a7d8f
A
116 SimpleDateFormat copy(pat);
117
118// ======= Test clone(), assignment, and equality
119
120 logln("Testing clone(), assignment and equality operators");
121
122 if( ! (copy == pat) || copy != pat) {
123 errln("ERROR: Copy constructor (or ==) failed");
124 }
125
126 copy = cust1;
127 if(copy != cust1) {
128 errln("ERROR: Assignment (or !=) failed");
129 }
130
131 Format *clone = def.clone();
132 if( ! (*clone == def) ) {
133 errln("ERROR: Clone() (or ==) failed");
134 }
135 delete clone;
136
137// ======= Test various format() methods
138
139 logln("Testing various format() methods");
140
141 UDate d = 837039928046.0;
142 Formattable fD(d, Formattable::kIsDate);
143
144 UnicodeString res1, res2;
145 FieldPosition pos1(0), pos2(0);
146
147 res1 = def.format(d, res1, pos1);
148 logln( (UnicodeString) "" + d + " formatted to " + res1);
149
150 status = U_ZERO_ERROR;
151 res2 = cust1.format(fD, res2, pos2, status);
152 if(U_FAILURE(status)) {
153 errln("ERROR: format(Formattable [Date]) failed");
154 }
155 logln((UnicodeString) "" + fD.getDate() + " formatted to " + res2);
156
157// ======= Test parse()
158
159 logln("Testing parse()");
160
161 UnicodeString text("02/03/76 2:50 AM, CST");
162 UDate result1, result2;
163 ParsePosition pos(0);
164 result1 = def.parse(text, pos);
165 logln(text + " parsed into " + result1);
166
167 status = U_ZERO_ERROR;
168 result2 = def.parse(text, status);
169 if(U_FAILURE(status)) {
170 errln("ERROR: parse() failed");
171 }
172 logln(text + " parsed into " + result2);
173
174// ======= Test getters and setters
175
176 logln("Testing getters and setters");
177
178 const DateFormatSymbols *syms = pat.getDateFormatSymbols();
179 if(!syms) {
180 errln("Couldn't obtain DateFormatSymbols. Quitting test!");
181 return;
182 }
183 if(syms->getDynamicClassID() != DateFormatSymbols::getStaticClassID()) {
184 errln("ERROR: format->getDateFormatSymbols()->getDynamicClassID() != DateFormatSymbols::getStaticClassID()");
185 }
186 DateFormatSymbols *newSyms = new DateFormatSymbols(*syms);
187 def.adoptDateFormatSymbols(newSyms);
188 pat_fr.setDateFormatSymbols(*newSyms);
189 if( *(pat.getDateFormatSymbols()) != *(def.getDateFormatSymbols())) {
190 errln("ERROR: adopt or set DateFormatSymbols() failed");
191 }
192
193 status = U_ZERO_ERROR;
194 UDate startDate = pat.get2DigitYearStart(status);
195 if(U_FAILURE(status)) {
196 errln("ERROR: getTwoDigitStartDate() failed");
197 }
198
199 status = U_ZERO_ERROR;
200 pat_fr.set2DigitYearStart(startDate, status);
201 if(U_FAILURE(status)) {
202 errln("ERROR: setTwoDigitStartDate() failed");
203 }
204
205// ======= Test DateFormatSymbols constructor
206 newSyms =new DateFormatSymbols("gregorian", status);
207 if(U_FAILURE(status)) {
208 errln("ERROR: new DateFormatSymbols() failed");
209 }
210 def.adoptDateFormatSymbols(newSyms);
211
212// ======= Test applyPattern()
213
214 logln("Testing applyPattern()");
215
216 UnicodeString p1("yyyy.MM.dd G 'at' hh:mm:ss z");
217 logln("Applying pattern " + p1);
218 status = U_ZERO_ERROR;
219 pat.applyPattern(p1);
220
221 UnicodeString s2;
222 s2 = pat.toPattern(s2);
223 logln("Extracted pattern is " + s2);
224 if(s2 != p1) {
225 errln("ERROR: toPattern() result did not match pattern applied");
226 }
227
228 logln("Applying pattern " + p1);
229 status = U_ZERO_ERROR;
230 pat.applyLocalizedPattern(p1, status);
231 if(U_FAILURE(status)) {
51004dcb 232 errln("ERROR: applyPattern() failed with %s", u_errorName(status));
b75a7d8f
A
233 }
234 UnicodeString s3;
235 status = U_ZERO_ERROR;
236 s3 = pat.toLocalizedPattern(s3, status);
237 if(U_FAILURE(status)) {
238 errln("ERROR: toLocalizedPattern() failed");
239 }
240 logln("Extracted pattern is " + s3);
241 if(s3 != p1) {
242 errln("ERROR: toLocalizedPattern() result did not match pattern applied");
243 }
244
245// ======= Test getStaticClassID()
246
247 logln("Testing getStaticClassID()");
248
249 status = U_ZERO_ERROR;
250 DateFormat *test = new SimpleDateFormat(status);
251 if(U_FAILURE(status)) {
252 errln("ERROR: Couldn't create a SimpleDateFormat");
253 }
254
255 if(test->getDynamicClassID() != SimpleDateFormat::getStaticClassID()) {
256 errln("ERROR: getDynamicClassID() didn't return the expected value");
257 }
46f4442e 258
b75a7d8f 259 delete test;
46f4442e
A
260
261// ======= Test Ticket 5684 (Parsing with 'e' and 'Y')
729e4ab9 262 SimpleDateFormat object(UNICODE_STRING_SIMPLE("YYYY'W'wwe"), status);
46f4442e
A
263 if(U_FAILURE(status)) {
264 errln("ERROR: Couldn't create a SimpleDateFormat");
265 }
266 object.setLenient(false);
267 ParsePosition pp(0);
46f4442e
A
268 UDate udDate = object.parse("2007W014", pp);
269 if ((double)udDate == 0.0) {
270 errln("ERROR: Parsing failed using 'Y' and 'e'");
271 }
b75a7d8f
A
272}
273
274#endif /* #if !UCONFIG_NO_FORMATTING */