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