]> git.saurik.com Git - apple/icu.git/blob - icuSources/test/perf/normperf/dtfmtrtperf.h
ICU-57132.0.1.tar.gz
[apple/icu.git] / icuSources / test / perf / normperf / dtfmtrtperf.h
1 /*
2 **********************************************************************
3 * Copyright (c) 2010-2016,International Business Machines
4 * Corporation and others. All Rights Reserved.
5 **********************************************************************
6 **********************************************************************
7 */
8
9 #ifndef _DTFMTRTPERF_H
10 #define _DTFMTRTPERF_H
11
12 #include "unicode/utypes.h"
13 #include "unicode/uperf.h"
14 #include "unicode/timezone.h"
15 #include "unicode/simpletz.h"
16 #include "unicode/calendar.h"
17 #include "unicode/strenum.h"
18 #include "unicode/smpdtfmt.h"
19 #include "unicode/uchar.h"
20 #include "unicode/basictz.h"
21 #include "cmemory.h"
22 #include "cstring.h"
23
24 #include "unicode/uperf.h"
25 #include "unicode/unistr.h"
26 #include "unicode/datefmt.h"
27 #include "unicode/calendar.h"
28 #include "unicode/uclean.h"
29 #include "unicode/brkiter.h"
30 #include "util.h"
31
32 static const char* PATTERNS[] = {"z", "zzzz", "Z", "ZZZZ", "v", "vvvv", "V", "VVVV"};
33 static const int NUM_PATTERNS = UPRV_LENGTHOF(PATTERNS);
34
35 #include <iostream>
36 #include <stdlib.h>
37 #include <fstream>
38 #include <string>
39 using namespace std;
40
41 // Stubs for Windows API functions when building on UNIXes.
42 //
43 #if U_PLATFORM_USES_ONLY_WIN32_API
44 // do nothing
45 #else
46 #define _UNICODE
47 typedef int DWORD;
48 inline int FoldStringW(DWORD dwMapFlags, const UChar* lpSrcStr,int cchSrc, UChar* lpDestStr,int cchDest);
49 #endif
50
51 class DateTimeRoundTripFunction : public UPerfFunction
52 {
53 private:
54 int nLocales;
55 public:
56
57 DateTimeRoundTripFunction()
58 {
59 nLocales = 0;
60 }
61
62 DateTimeRoundTripFunction(int locs)
63 {
64 nLocales = locs;
65 }
66
67 virtual void call(UErrorCode* status)
68 {
69 *status = U_ZERO_ERROR;
70
71 SimpleTimeZone unknownZone(-31415, (UnicodeString)"Etc/Unknown");
72 int32_t badDstOffset = -1234;
73 int32_t badZoneOffset = -2345;
74
75 int32_t testDateData[][3] = {
76 {2007, 1, 15},
77 {2007, 6, 15},
78 {1990, 1, 15},
79 {1990, 6, 15},
80 {1960, 1, 15},
81 {1960, 6, 15},
82 };
83
84 Calendar *cal = Calendar::createInstance(*status);
85 if (U_FAILURE(*status)) {
86 //dataerrln("Calendar::createInstance failed: %s", u_errorName(*status));
87 return;
88 }
89
90 // Set up rule equivalency test range
91 UDate low, high;
92 cal->set(1900, UCAL_JANUARY, 1);
93 low = cal->getTime(*status);
94 cal->set(2040, UCAL_JANUARY, 1);
95 high = cal->getTime(*status);
96 if (U_FAILURE(*status)) {
97 //errln("getTime failed");
98 return;
99 }
100
101 // Set up test dates
102 UDate DATES[UPRV_LENGTHOF(testDateData)/3];
103 const int32_t nDates = UPRV_LENGTHOF(testDateData)/3;
104 cal->clear();
105 for (int32_t i = 0; i < nDates; i++) {
106 cal->set(testDateData[i][0], testDateData[i][1], testDateData[i][2]);
107 DATES[i] = cal->getTime(*status);
108 if (U_FAILURE(*status)) {
109 //errln("getTime failed");
110 return;
111 }
112 }
113
114 // Set up test locales
115 const Locale testLocales[] = {
116 Locale("en"),
117 Locale("en_US"),
118 Locale("en_AU"),
119 Locale("de_DE"),
120 Locale("fr"),
121 Locale("ja_JP"),
122 Locale("ko"),
123 Locale("pt"),
124 Locale("th_TH"),
125 Locale("zh_Hans"),
126
127 Locale("it"),
128
129 Locale("en"),
130 Locale("en_US"),
131 Locale("en_AU"),
132 Locale("de_DE"),
133 Locale("fr"),
134 Locale("ja_JP"),
135 Locale("ko"),
136 Locale("pt"),
137 Locale("th_TH"),
138 Locale("zh_Hans"),
139 };
140
141 const Locale *LOCALES;
142 LOCALES = testLocales;
143
144 StringEnumeration *tzids = TimeZone::createEnumeration();
145 if (U_FAILURE(*status)) {
146 //errln("tzids->count failed");
147 return;
148 }
149
150 // Run the roundtrip test
151 for (int32_t locidx = 0; locidx < nLocales; locidx++) {
152 for (int32_t patidx = 0; patidx < NUM_PATTERNS; patidx++) {
153 SimpleDateFormat *sdf = new SimpleDateFormat((UnicodeString)PATTERNS[patidx], LOCALES[locidx], *status);
154 if (U_FAILURE(*status)) {
155 //errcheckln(*status, (UnicodeString)"new SimpleDateFormat failed for pattern " +
156 // PATTERNS[patidx] + " for locale " + LOCALES[locidx].getName() + " - " + u_errorName(*status));
157 *status = U_ZERO_ERROR;
158 continue;
159 }
160
161 tzids->reset(*status);
162 const UnicodeString *tzid;
163 while ((tzid = tzids->snext(*status))) {
164 TimeZone *tz = TimeZone::createTimeZone(*tzid);
165
166 for (int32_t datidx = 0; datidx < nDates; datidx++) {
167 UnicodeString tzstr;
168 FieldPosition fpos(0);
169
170 // Format
171 sdf->setTimeZone(*tz);
172 sdf->format(DATES[datidx], tzstr, fpos);
173
174 // Before parse, set unknown zone to SimpleDateFormat instance
175 // just for making sure that it does not depends on the time zone
176 // originally set.
177 sdf->setTimeZone(unknownZone);
178
179 // Parse
180 ParsePosition pos(0);
181 Calendar *outcal = Calendar::createInstance(unknownZone, *status);
182 if (U_FAILURE(*status)) {
183 //errln("Failed to create an instance of calendar for receiving parse result.");
184 *status = U_ZERO_ERROR;
185 continue;
186 }
187 outcal->set(UCAL_DST_OFFSET, badDstOffset);
188 outcal->set(UCAL_ZONE_OFFSET, badZoneOffset);
189 sdf->parse(tzstr, *outcal, pos);
190
191 // clean loop
192 delete outcal;
193
194 }
195 delete tz;
196 // break time zone loop
197 break;
198
199 }
200 delete sdf;
201 }
202 }
203 delete cal;
204 delete tzids;
205
206 }
207
208 virtual long getOperationsPerIteration()
209 {
210 return NUM_PATTERNS * nLocales * 6;
211 }
212 };
213
214
215 class DateTimeRoundTripPerfTest : public UPerfTest
216 {
217 private:
218
219 public:
220
221 DateTimeRoundTripPerfTest(int32_t argc, const char* argv[], UErrorCode& status);
222 ~DateTimeRoundTripPerfTest();
223 virtual UPerfFunction* runIndexedTest(int32_t index, UBool exec,const char* &name, char* par);
224
225 UPerfFunction* RoundTripLocale1();
226 UPerfFunction* RoundTripLocale10();
227 UPerfFunction* RoundTripLocale11();
228 UPerfFunction* RoundTripLocale21();
229 };
230
231
232 #endif // DateTimeRoundTripPerfTest