]>
git.saurik.com Git - apple/icu.git/blob - icuSources/test/intltest/sdtfmtts.cpp
2 /********************************************************************
4 * Copyright (c) 1997-2010, International Business Machines Corporation and
5 * others. All Rights Reserved.
6 ********************************************************************/
8 #include "unicode/utypes.h"
10 #if !UCONFIG_NO_FORMATTING
14 #include "unicode/smpdtfmt.h"
15 #include "unicode/dtfmtsym.h"
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.
21 void IntlTestSimpleDateFormatAPI::runIndexedTest( int32_t index
, UBool exec
, const char* &name
, char* /*par*/ )
23 if (exec
) logln("TestSuite SimpleDateFormatAPI");
25 case 0: name
= "SimpleDateFormat API test";
27 logln("SimpleDateFormat API test---"); logln("");
28 UErrorCode status
= U_ZERO_ERROR
;
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");
35 Locale::setDefault(saveLocale
, status
);
39 default: name
= ""; break;
44 * Test various generic API methods of SimpleDateFormat for API coverage.
46 void IntlTestSimpleDateFormatAPI::testAPI(/*char *par*/)
48 UErrorCode status
= U_ZERO_ERROR
;
50 // ======= Test constructors
52 logln("Testing SimpleDateFormat constructors");
54 SimpleDateFormat
def(status
);
55 if(U_FAILURE(status
)) {
56 dataerrln("ERROR: Could not create SimpleDateFormat (default) - exitting");
60 status
= U_ZERO_ERROR
;
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", "");
65 SimpleDateFormat
pat(pattern
, status
);
66 if(U_FAILURE(status
)) {
67 errln("ERROR: Could not create SimpleDateFormat (pattern) - %s", u_errorName(status
));
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)");
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)");
82 status
= U_ZERO_ERROR
;
83 SimpleDateFormat
cust1(pattern
, symbols
, status
);
84 if(U_FAILURE(status
)) {
85 dataerrln("ERROR: Could not create SimpleDateFormat (pattern, symbols*) - exitting");
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)");
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
));
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
));
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");
116 SimpleDateFormat
copy(pat
);
118 // ======= Test clone(), assignment, and equality
120 logln("Testing clone(), assignment and equality operators");
122 if( ! (copy
== pat
) || copy
!= pat
) {
123 errln("ERROR: Copy constructor (or ==) failed");
128 errln("ERROR: Assignment (or !=) failed");
131 Format
*clone
= def
.clone();
132 if( ! (*clone
== def
) ) {
133 errln("ERROR: Clone() (or ==) failed");
137 // ======= Test various format() methods
139 logln("Testing various format() methods");
141 UDate d
= 837039928046.0;
142 Formattable
fD(d
, Formattable::kIsDate
);
144 UnicodeString res1
, res2
;
145 FieldPosition
pos1(0), pos2(0);
147 res1
= def
.format(d
, res1
, pos1
);
148 logln( (UnicodeString
) "" + d
+ " formatted to " + res1
);
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");
155 logln((UnicodeString
) "" + fD
.getDate() + " formatted to " + res2
);
157 // ======= Test parse()
159 logln("Testing parse()");
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
);
167 status
= U_ZERO_ERROR
;
168 result2
= def
.parse(text
, status
);
169 if(U_FAILURE(status
)) {
170 errln("ERROR: parse() failed");
172 logln(text
+ " parsed into " + result2
);
174 // ======= Test getters and setters
176 logln("Testing getters and setters");
178 const DateFormatSymbols
*syms
= pat
.getDateFormatSymbols();
180 errln("Couldn't obtain DateFormatSymbols. Quitting test!");
183 if(syms
->getDynamicClassID() != DateFormatSymbols::getStaticClassID()) {
184 errln("ERROR: format->getDateFormatSymbols()->getDynamicClassID() != DateFormatSymbols::getStaticClassID()");
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");
193 status
= U_ZERO_ERROR
;
194 UDate startDate
= pat
.get2DigitYearStart(status
);
195 if(U_FAILURE(status
)) {
196 errln("ERROR: getTwoDigitStartDate() failed");
199 status
= U_ZERO_ERROR
;
200 pat_fr
.set2DigitYearStart(startDate
, status
);
201 if(U_FAILURE(status
)) {
202 errln("ERROR: setTwoDigitStartDate() failed");
205 // ======= Test DateFormatSymbols constructor
206 newSyms
=new DateFormatSymbols("gregorian", status
);
207 if(U_FAILURE(status
)) {
208 errln("ERROR: new DateFormatSymbols() failed");
210 def
.adoptDateFormatSymbols(newSyms
);
212 // ======= Test applyPattern()
214 logln("Testing applyPattern()");
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
);
222 s2
= pat
.toPattern(s2
);
223 logln("Extracted pattern is " + s2
);
225 errln("ERROR: toPattern() result did not match pattern applied");
228 logln("Applying pattern " + p1
);
229 status
= U_ZERO_ERROR
;
230 pat
.applyLocalizedPattern(p1
, status
);
231 if(U_FAILURE(status
)) {
232 errln("ERROR: applyPattern() failed with " + (int32_t) status
);
235 status
= U_ZERO_ERROR
;
236 s3
= pat
.toLocalizedPattern(s3
, status
);
237 if(U_FAILURE(status
)) {
238 errln("ERROR: toLocalizedPattern() failed");
240 logln("Extracted pattern is " + s3
);
242 errln("ERROR: toLocalizedPattern() result did not match pattern applied");
245 // ======= Test getStaticClassID()
247 logln("Testing getStaticClassID()");
249 status
= U_ZERO_ERROR
;
250 DateFormat
*test
= new SimpleDateFormat(status
);
251 if(U_FAILURE(status
)) {
252 errln("ERROR: Couldn't create a SimpleDateFormat");
255 if(test
->getDynamicClassID() != SimpleDateFormat::getStaticClassID()) {
256 errln("ERROR: getDynamicClassID() didn't return the expected value");
261 // ======= Test Ticket 5684 (Parsing with 'e' and 'Y')
262 SimpleDateFormat
object(UNICODE_STRING_SIMPLE("YYYY'W'wwe"), status
);
263 if(U_FAILURE(status
)) {
264 errln("ERROR: Couldn't create a SimpleDateFormat");
266 object
.setLenient(false);
268 UDate udDate
= object
.parse("2007W014", pp
);
269 if ((double)udDate
== 0.0) {
270 errln("ERROR: Parsing failed using 'Y' and 'e'");
274 #endif /* #if !UCONFIG_NO_FORMATTING */