]> git.saurik.com Git - apple/icu.git/blob - icuSources/test/intltest/sdtfmtts.cpp
ICU-6.2.4.tar.gz
[apple/icu.git] / icuSources / test / intltest / sdtfmtts.cpp
1
2 /********************************************************************
3 * COPYRIGHT:
4 * Copyright (c) 1997-2003, International Business Machines Corporation and
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
21 void 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;
29 Locale::setDefault(Locale::getEnglish(), status);
30 if(U_FAILURE(status)) {
31 errln("ERROR: Could not set default locale, test may not give correct results");
32 }
33 testAPI(/*par*/);
34 }
35 break;
36
37 default: name = ""; break;
38 }
39 }
40
41 /**
42 * Test various generic API methods of SimpleDateFormat for API coverage.
43 */
44 void IntlTestSimpleDateFormatAPI::testAPI(/*char *par*/)
45 {
46 UErrorCode status = U_ZERO_ERROR;
47
48 // ======= Test constructors
49
50 logln("Testing SimpleDateFormat constructors");
51
52 SimpleDateFormat def(status);
53 if(U_FAILURE(status)) {
54 errln("ERROR: Could not create SimpleDateFormat (default)");
55 }
56
57 status = U_ZERO_ERROR;
58 const UnicodeString pattern("yyyy.MM.dd G 'at' hh:mm:ss z");
59 SimpleDateFormat pat(pattern, status);
60 if(U_FAILURE(status)) {
61 errln("ERROR: Could not create SimpleDateFormat (pattern)");
62 }
63
64 status = U_ZERO_ERROR;
65 SimpleDateFormat pat_fr(pattern, Locale::getFrench(), status);
66 if(U_FAILURE(status)) {
67 errln("ERROR: Could not create SimpleDateFormat (pattern French)");
68 }
69
70 status = U_ZERO_ERROR;
71 DateFormatSymbols *symbols = new DateFormatSymbols(Locale::getFrench(), status);
72 if(U_FAILURE(status)) {
73 errln("ERROR: Could not create DateFormatSymbols (French)");
74 }
75
76 status = U_ZERO_ERROR;
77 SimpleDateFormat cust1(pattern, symbols, status);
78 if(U_FAILURE(status)) {
79 errln("ERROR: Could not create SimpleDateFormat (pattern, symbols*)");
80 }
81
82 status = U_ZERO_ERROR;
83 SimpleDateFormat cust2(pattern, *symbols, status);
84 if(U_FAILURE(status)) {
85 errln("ERROR: Could not create SimpleDateFormat (pattern, symbols)");
86 }
87
88 SimpleDateFormat copy(pat);
89
90 // ======= Test clone(), assignment, and equality
91
92 logln("Testing clone(), assignment and equality operators");
93
94 if( ! (copy == pat) || copy != pat) {
95 errln("ERROR: Copy constructor (or ==) failed");
96 }
97
98 copy = cust1;
99 if(copy != cust1) {
100 errln("ERROR: Assignment (or !=) failed");
101 }
102
103 Format *clone = def.clone();
104 if( ! (*clone == def) ) {
105 errln("ERROR: Clone() (or ==) failed");
106 }
107 delete clone;
108
109 // ======= Test various format() methods
110
111 logln("Testing various format() methods");
112
113 UDate d = 837039928046.0;
114 Formattable fD(d, Formattable::kIsDate);
115
116 UnicodeString res1, res2;
117 FieldPosition pos1(0), pos2(0);
118
119 res1 = def.format(d, res1, pos1);
120 logln( (UnicodeString) "" + d + " formatted to " + res1);
121
122 status = U_ZERO_ERROR;
123 res2 = cust1.format(fD, res2, pos2, status);
124 if(U_FAILURE(status)) {
125 errln("ERROR: format(Formattable [Date]) failed");
126 }
127 logln((UnicodeString) "" + fD.getDate() + " formatted to " + res2);
128
129 // ======= Test parse()
130
131 logln("Testing parse()");
132
133 UnicodeString text("02/03/76 2:50 AM, CST");
134 UDate result1, result2;
135 ParsePosition pos(0);
136 result1 = def.parse(text, pos);
137 logln(text + " parsed into " + result1);
138
139 status = U_ZERO_ERROR;
140 result2 = def.parse(text, status);
141 if(U_FAILURE(status)) {
142 errln("ERROR: parse() failed");
143 }
144 logln(text + " parsed into " + result2);
145
146 // ======= Test getters and setters
147
148 logln("Testing getters and setters");
149
150 const DateFormatSymbols *syms = pat.getDateFormatSymbols();
151 if(!syms) {
152 errln("Couldn't obtain DateFormatSymbols. Quitting test!");
153 return;
154 }
155 if(syms->getDynamicClassID() != DateFormatSymbols::getStaticClassID()) {
156 errln("ERROR: format->getDateFormatSymbols()->getDynamicClassID() != DateFormatSymbols::getStaticClassID()");
157 }
158 DateFormatSymbols *newSyms = new DateFormatSymbols(*syms);
159 def.adoptDateFormatSymbols(newSyms);
160 pat_fr.setDateFormatSymbols(*newSyms);
161 if( *(pat.getDateFormatSymbols()) != *(def.getDateFormatSymbols())) {
162 errln("ERROR: adopt or set DateFormatSymbols() failed");
163 }
164
165 status = U_ZERO_ERROR;
166 UDate startDate = pat.get2DigitYearStart(status);
167 if(U_FAILURE(status)) {
168 errln("ERROR: getTwoDigitStartDate() failed");
169 }
170
171 status = U_ZERO_ERROR;
172 pat_fr.set2DigitYearStart(startDate, status);
173 if(U_FAILURE(status)) {
174 errln("ERROR: setTwoDigitStartDate() failed");
175 }
176
177 // ======= Test DateFormatSymbols constructor
178 newSyms =new DateFormatSymbols("gregorian", status);
179 if(U_FAILURE(status)) {
180 errln("ERROR: new DateFormatSymbols() failed");
181 }
182 def.adoptDateFormatSymbols(newSyms);
183
184 // ======= Test applyPattern()
185
186 logln("Testing applyPattern()");
187
188 UnicodeString p1("yyyy.MM.dd G 'at' hh:mm:ss z");
189 logln("Applying pattern " + p1);
190 status = U_ZERO_ERROR;
191 pat.applyPattern(p1);
192
193 UnicodeString s2;
194 s2 = pat.toPattern(s2);
195 logln("Extracted pattern is " + s2);
196 if(s2 != p1) {
197 errln("ERROR: toPattern() result did not match pattern applied");
198 }
199
200 logln("Applying pattern " + p1);
201 status = U_ZERO_ERROR;
202 pat.applyLocalizedPattern(p1, status);
203 if(U_FAILURE(status)) {
204 errln("ERROR: applyPattern() failed with " + (int32_t) status);
205 }
206 UnicodeString s3;
207 status = U_ZERO_ERROR;
208 s3 = pat.toLocalizedPattern(s3, status);
209 if(U_FAILURE(status)) {
210 errln("ERROR: toLocalizedPattern() failed");
211 }
212 logln("Extracted pattern is " + s3);
213 if(s3 != p1) {
214 errln("ERROR: toLocalizedPattern() result did not match pattern applied");
215 }
216
217 // ======= Test getStaticClassID()
218
219 logln("Testing getStaticClassID()");
220
221 status = U_ZERO_ERROR;
222 DateFormat *test = new SimpleDateFormat(status);
223 if(U_FAILURE(status)) {
224 errln("ERROR: Couldn't create a SimpleDateFormat");
225 }
226
227 if(test->getDynamicClassID() != SimpleDateFormat::getStaticClassID()) {
228 errln("ERROR: getDynamicClassID() didn't return the expected value");
229 }
230
231 delete test;
232 }
233
234 #endif /* #if !UCONFIG_NO_FORMATTING */