]>
git.saurik.com Git - apple/icu.git/blob - icuSources/test/intltest/sdtfmtts.cpp
2 /********************************************************************
4 * Copyright (c) 1997-2003, 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
;
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");
37 default: name
= ""; break;
42 * Test various generic API methods of SimpleDateFormat for API coverage.
44 void IntlTestSimpleDateFormatAPI::testAPI(/*char *par*/)
46 UErrorCode status
= U_ZERO_ERROR
;
48 // ======= Test constructors
50 logln("Testing SimpleDateFormat constructors");
52 SimpleDateFormat
def(status
);
53 if(U_FAILURE(status
)) {
54 errln("ERROR: Could not create SimpleDateFormat (default)");
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)");
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)");
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)");
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*)");
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)");
88 SimpleDateFormat
copy(pat
);
90 // ======= Test clone(), assignment, and equality
92 logln("Testing clone(), assignment and equality operators");
94 if( ! (copy
== pat
) || copy
!= pat
) {
95 errln("ERROR: Copy constructor (or ==) failed");
100 errln("ERROR: Assignment (or !=) failed");
103 Format
*clone
= def
.clone();
104 if( ! (*clone
== def
) ) {
105 errln("ERROR: Clone() (or ==) failed");
109 // ======= Test various format() methods
111 logln("Testing various format() methods");
113 UDate d
= 837039928046.0;
114 Formattable
fD(d
, Formattable::kIsDate
);
116 UnicodeString res1
, res2
;
117 FieldPosition
pos1(0), pos2(0);
119 res1
= def
.format(d
, res1
, pos1
);
120 logln( (UnicodeString
) "" + d
+ " formatted to " + res1
);
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");
127 logln((UnicodeString
) "" + fD
.getDate() + " formatted to " + res2
);
129 // ======= Test parse()
131 logln("Testing parse()");
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
);
139 status
= U_ZERO_ERROR
;
140 result2
= def
.parse(text
, status
);
141 if(U_FAILURE(status
)) {
142 errln("ERROR: parse() failed");
144 logln(text
+ " parsed into " + result2
);
146 // ======= Test getters and setters
148 logln("Testing getters and setters");
150 const DateFormatSymbols
*syms
= pat
.getDateFormatSymbols();
152 errln("Couldn't obtain DateFormatSymbols. Quitting test!");
155 if(syms
->getDynamicClassID() != DateFormatSymbols::getStaticClassID()) {
156 errln("ERROR: format->getDateFormatSymbols()->getDynamicClassID() != DateFormatSymbols::getStaticClassID()");
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");
165 status
= U_ZERO_ERROR
;
166 UDate startDate
= pat
.get2DigitYearStart(status
);
167 if(U_FAILURE(status
)) {
168 errln("ERROR: getTwoDigitStartDate() failed");
171 status
= U_ZERO_ERROR
;
172 pat_fr
.set2DigitYearStart(startDate
, status
);
173 if(U_FAILURE(status
)) {
174 errln("ERROR: setTwoDigitStartDate() failed");
177 // ======= Test DateFormatSymbols constructor
178 newSyms
=new DateFormatSymbols("gregorian", status
);
179 if(U_FAILURE(status
)) {
180 errln("ERROR: new DateFormatSymbols() failed");
182 def
.adoptDateFormatSymbols(newSyms
);
184 // ======= Test applyPattern()
186 logln("Testing applyPattern()");
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
);
194 s2
= pat
.toPattern(s2
);
195 logln("Extracted pattern is " + s2
);
197 errln("ERROR: toPattern() result did not match pattern applied");
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
);
207 status
= U_ZERO_ERROR
;
208 s3
= pat
.toLocalizedPattern(s3
, status
);
209 if(U_FAILURE(status
)) {
210 errln("ERROR: toLocalizedPattern() failed");
212 logln("Extracted pattern is " + s3
);
214 errln("ERROR: toLocalizedPattern() result did not match pattern applied");
217 // ======= Test getStaticClassID()
219 logln("Testing getStaticClassID()");
221 status
= U_ZERO_ERROR
;
222 DateFormat
*test
= new SimpleDateFormat(status
);
223 if(U_FAILURE(status
)) {
224 errln("ERROR: Couldn't create a SimpleDateFormat");
227 if(test
->getDynamicClassID() != SimpleDateFormat::getStaticClassID()) {
228 errln("ERROR: getDynamicClassID() didn't return the expected value");
234 #endif /* #if !UCONFIG_NO_FORMATTING */