]>
Commit | Line | Data |
---|---|---|
374ca955 A |
1 | /* |
2 | **************************************************************************** | |
3 | * Copyright (c) 1997-2004, International Business Machines Corporation and * | |
4 | * others. All Rights Reserved. * | |
5 | **************************************************************************** | |
6 | */ | |
7 | ||
8 | #include "unicode/utypes.h" | |
9 | ||
10 | #if !UCONFIG_NO_FORMATTING | |
11 | ||
12 | #include "unicode/utmscale.h" | |
13 | ||
14 | #include "cintltst.h" | |
15 | ||
16 | #include <stdlib.h> | |
17 | #include <time.h> | |
18 | ||
19 | #define LOOP_COUNT 10000 | |
20 | ||
21 | static void TestAPI(void); | |
22 | static void TestData(void); | |
23 | static void TestMonkey(void); | |
24 | ||
25 | void addUtmsTest(TestNode** root); | |
26 | ||
27 | void addUtmsTest(TestNode** root) | |
28 | { | |
29 | addTest(root, &TestAPI, "tsformat/utmstest/TestAPI"); | |
30 | addTest(root, &TestData, "tsformat/utmstest/TestData"); | |
31 | addTest(root, &TestMonkey, "tsformat/utmstest/TestMonkey"); | |
32 | } | |
33 | ||
34 | /** | |
35 | * Return a random int64_t where U_INT64_MIN <= ran <= U_INT64_MAX. | |
36 | */ | |
37 | static uint64_t randomInt64(void) | |
38 | { | |
39 | int64_t ran = 0; | |
40 | int32_t i; | |
41 | static UBool initialized = FALSE; | |
42 | ||
43 | if (!initialized) { | |
44 | srand((unsigned)time(NULL)); | |
45 | initialized = TRUE; | |
46 | } | |
47 | ||
48 | /* Assume rand has at least 12 bits of precision */ | |
49 | for (i = 0; i < sizeof(ran); i += 1) { | |
50 | ((char*)&ran)[i] = (char)((rand() & 0x0FF0) >> 4); | |
51 | } | |
52 | ||
53 | return ran; | |
54 | } | |
55 | ||
56 | static int64_t ranInt; | |
57 | static int64_t ranMin; | |
58 | static int64_t ranMax; | |
59 | ||
60 | static void initRandom(int64_t min, int64_t max) | |
61 | { | |
62 | uint64_t interval = max - min; | |
63 | ||
64 | ranMin = min; | |
65 | ranMax = max; | |
66 | ranInt = 0; | |
67 | ||
68 | if (interval < U_INT64_MIN) { | |
69 | ranInt = interval; | |
70 | } | |
71 | } | |
72 | ||
73 | static int64_t randomInRange(void) | |
74 | { | |
75 | int64_t value; | |
76 | ||
77 | if (ranInt != 0) { | |
78 | value = randomInt64() % ranInt; | |
79 | ||
80 | if (value < 0) { | |
81 | value = -value; | |
82 | } | |
83 | ||
84 | value += ranMin; | |
85 | } else { | |
86 | do { | |
87 | value = randomInt64(); | |
88 | } while (value < ranMin || value > ranMax); | |
89 | } | |
90 | ||
91 | return value; | |
92 | } | |
93 | ||
94 | static void roundTripTest(int64_t value, int32_t scale) | |
95 | { | |
96 | UErrorCode status = U_ZERO_ERROR; | |
97 | int64_t rt = utmscale_toInt64(utmscale_fromInt64(value, scale, &status), scale, &status); | |
98 | ||
99 | if (rt != value) { | |
100 | log_err("Round-trip error: time scale = %d, value = %lld, round-trip = %lld.\n", scale, value, rt); | |
101 | } | |
102 | } | |
103 | ||
104 | static void toLimitTest(int64_t toLimit, int64_t fromLimit, int32_t scale) | |
105 | { | |
106 | UErrorCode status = U_ZERO_ERROR; | |
107 | int64_t result = utmscale_toInt64(toLimit, scale, &status); | |
108 | ||
109 | if (result != fromLimit) { | |
110 | log_err("toLimit failure: scale = %d, toLimit = %lld , utmscale_toInt64(toLimit, scale, &status) = %lld, fromLimit = %lld.\n", | |
111 | scale, toLimit, result, fromLimit); | |
112 | } | |
113 | } | |
114 | ||
115 | static void epochOffsetTest(int64_t epochOffset, int64_t units, int32_t scale) | |
116 | { | |
117 | UErrorCode status = U_ZERO_ERROR; | |
118 | int64_t universal = 0; | |
119 | int64_t universalEpoch = epochOffset * units; | |
120 | int64_t local = utmscale_toInt64(universalEpoch, scale, &status); | |
121 | ||
122 | if (local != 0) { | |
123 | log_err("utmscale_toInt64(epochOffset, scale, &status): scale = %d epochOffset = %lld, result = %lld.\n", scale, epochOffset, local); | |
124 | } | |
125 | ||
126 | local = utmscale_toInt64(0, scale, &status); | |
127 | ||
128 | if (local != -epochOffset) { | |
129 | log_err("utmscale_toInt64(0, scale): scale = %d, result = %lld.\n", scale, local); | |
130 | } | |
131 | ||
132 | universal = utmscale_fromInt64(-epochOffset, scale, &status); | |
133 | ||
134 | if (universal != 0) { | |
135 | log_err("from(-epochOffest, scale): scale = %d, epochOffset = %lld, result = %lld.\n", scale, epochOffset, universal); | |
136 | } | |
137 | ||
138 | universal = utmscale_fromInt64(0, scale, &status); | |
139 | ||
140 | if (universal != universalEpoch) { | |
141 | log_err("utmscale_fromInt64(0, scale): scale = %d, result = %lld.\n", scale, universal); | |
142 | } | |
143 | } | |
144 | ||
145 | static void TestEpochOffsets(void) | |
146 | { | |
147 | UErrorCode status = U_ZERO_ERROR; | |
148 | int32_t scale; | |
149 | ||
150 | for (scale = 0; scale < UDTS_MAX_SCALE; scale += 1) { | |
151 | int64_t units = utmscale_getTimeScaleValue(scale, UTSV_UNITS_VALUE, &status); | |
152 | int64_t epochOffset = utmscale_getTimeScaleValue(scale, UTSV_EPOCH_OFFSET_VALUE, &status); | |
153 | ||
154 | epochOffsetTest(epochOffset, units, scale); | |
155 | } | |
156 | } | |
157 | ||
158 | static void TestFromLimits(void) | |
159 | { | |
160 | UErrorCode status = U_ZERO_ERROR; | |
161 | int32_t scale; | |
162 | ||
163 | for (scale = 0; scale < UDTS_MAX_SCALE; scale += 1) { | |
164 | int64_t fromMin = utmscale_getTimeScaleValue(scale, UTSV_FROM_MIN_VALUE, &status); | |
165 | int64_t fromMax = utmscale_getTimeScaleValue(scale, UTSV_FROM_MAX_VALUE, &status); | |
166 | ||
167 | roundTripTest(fromMin, scale); | |
168 | roundTripTest(fromMax, scale); | |
169 | } | |
170 | } | |
171 | ||
172 | static void TestToLimits(void) | |
173 | { | |
174 | UErrorCode status = U_ZERO_ERROR; | |
175 | int32_t scale; | |
176 | ||
177 | for (scale = 0; scale < UDTS_MAX_SCALE; scale += 1) { | |
178 | int64_t fromMin = utmscale_getTimeScaleValue(scale, UTSV_FROM_MIN_VALUE, &status); | |
179 | int64_t fromMax = utmscale_getTimeScaleValue(scale, UTSV_FROM_MAX_VALUE, &status); | |
180 | int64_t toMin = utmscale_getTimeScaleValue(scale, UTSV_TO_MIN_VALUE, &status); | |
181 | int64_t toMax = utmscale_getTimeScaleValue(scale, UTSV_TO_MAX_VALUE, &status); | |
182 | ||
183 | toLimitTest(toMin, fromMin, scale); | |
184 | toLimitTest(toMax, fromMax, scale); | |
185 | } | |
186 | } | |
187 | ||
188 | static void TestFromInt64(void) | |
189 | { | |
190 | int32_t scale; | |
191 | int64_t result; | |
192 | UErrorCode status = U_ZERO_ERROR; | |
193 | ||
194 | result = utmscale_fromInt64(0, -1, &status); | |
195 | if (status != U_ILLEGAL_ARGUMENT_ERROR) { | |
196 | log_err("utmscale_fromInt64(0, -1, status) did not set status to U_ILLEGAL_ARGUMENT_ERROR.\n"); | |
197 | } | |
198 | ||
199 | for (scale = 0; scale < UDTS_MAX_SCALE; scale += 1) { | |
200 | int64_t fromMin, fromMax; | |
201 | ||
202 | status = U_ZERO_ERROR; | |
203 | fromMin = utmscale_getTimeScaleValue(scale, UTSV_FROM_MIN_VALUE, &status); | |
204 | fromMax = utmscale_getTimeScaleValue(scale, UTSV_FROM_MAX_VALUE, &status); | |
205 | ||
206 | status = U_ZERO_ERROR; | |
207 | result = utmscale_fromInt64(0, scale, &status); | |
208 | if (status == U_ILLEGAL_ARGUMENT_ERROR) { | |
209 | log_err("utmscale_fromInt64(0, %d, &status) generated U_ILLEGAL_ARGUMENT_ERROR.\n", scale); | |
210 | } | |
211 | ||
212 | status = U_ZERO_ERROR; | |
213 | result = utmscale_fromInt64(fromMin, scale, &status); | |
214 | if (status == U_ILLEGAL_ARGUMENT_ERROR) { | |
215 | log_err("utmscale_fromInt64(fromMin, %d, &status) generated U_ILLEGAL_ARGUMENT_ERROR.\n", scale); | |
216 | } | |
217 | ||
218 | if (fromMin > U_INT64_MIN) { | |
219 | status = U_ZERO_ERROR; | |
220 | result = utmscale_fromInt64(fromMin - 1, scale, &status); | |
221 | if (status != U_ILLEGAL_ARGUMENT_ERROR) { | |
222 | log_err("utmscale_fromInt64(fromMin - 1, %d, &status) did not generate U_ILLEGAL_ARGUMENT_ERROR.\n", scale); | |
223 | } | |
224 | } | |
225 | ||
226 | status = U_ZERO_ERROR; | |
227 | result = utmscale_fromInt64(fromMax, scale, &status); | |
228 | if (status == U_ILLEGAL_ARGUMENT_ERROR) { | |
229 | log_err("utmscale_fromInt64(fromMax, %d, &status) generated U_ILLEGAL_ARGUMENT_ERROR.\n", scale); | |
230 | } | |
231 | ||
232 | if (fromMax < U_INT64_MAX) { | |
233 | status = U_ZERO_ERROR; | |
234 | result = utmscale_fromInt64(fromMax + 1, scale, &status); | |
235 | if (status != U_ILLEGAL_ARGUMENT_ERROR) { | |
236 | log_err("utmscale_fromInt64(fromMax + 1, %d, &status) didn't generate U_ILLEGAL_ARGUMENT_ERROR.\n", scale); | |
237 | } | |
238 | } | |
239 | } | |
240 | ||
241 | status = U_ZERO_ERROR; | |
242 | result = utmscale_fromInt64(0, UDTS_MAX_SCALE, &status); | |
243 | if (status != U_ILLEGAL_ARGUMENT_ERROR) { | |
244 | log_err("utmscale_fromInt64(0, UDTS_MAX_SCALE, &status) did not generate U_ILLEGAL_ARGUMENT_ERROR.\n"); | |
245 | } | |
246 | } | |
247 | ||
248 | static void TestToInt64(void) | |
249 | { | |
250 | int32_t scale; | |
251 | int64_t result; | |
252 | UErrorCode status = U_ZERO_ERROR; | |
253 | ||
254 | result = utmscale_toInt64(0, -1, &status); | |
255 | if (status != U_ILLEGAL_ARGUMENT_ERROR) { | |
256 | log_err("utmscale_toInt64(0, -1, &status) did not generate U_ILLEGAL_ARGUMENT_ERROR.\n"); | |
257 | } | |
258 | ||
259 | for (scale = 0; scale < UDTS_MAX_SCALE; scale += 1) { | |
260 | int64_t toMin, toMax; | |
261 | ||
262 | status = U_ZERO_ERROR; | |
263 | toMin = utmscale_getTimeScaleValue(scale, UTSV_TO_MIN_VALUE, &status); | |
264 | toMax = utmscale_getTimeScaleValue(scale, UTSV_TO_MAX_VALUE, &status); | |
265 | ||
266 | status = U_ZERO_ERROR; | |
267 | result = utmscale_toInt64(0, scale, &status); | |
268 | if (status == U_ILLEGAL_ARGUMENT_ERROR) { | |
269 | log_err("utmscale_toInt64(0, %d, &status) generated U_ILLEGAL_ARGUMENT_ERROR.\n", scale); | |
270 | } | |
271 | ||
272 | status = U_ZERO_ERROR; | |
273 | result = utmscale_toInt64(toMin, scale, &status); | |
274 | if (status == U_ILLEGAL_ARGUMENT_ERROR) { | |
275 | log_err("utmscale_toInt64(toMin, %d, &status) generated U_ILLEGAL_ARGUMENT_ERROR.\n", scale); | |
276 | } | |
277 | ||
278 | if (toMin > U_INT64_MIN) { | |
279 | status = U_ZERO_ERROR; | |
280 | result = utmscale_toInt64(toMin - 1, scale, &status); | |
281 | if (status != U_ILLEGAL_ARGUMENT_ERROR) { | |
282 | log_err("utmscale_toInt64(toMin - 1, %d, &status) did not generate U_ILLEGAL_ARGUMENT_ERROR.\n", scale); | |
283 | } | |
284 | } | |
285 | ||
286 | ||
287 | status = U_ZERO_ERROR; | |
288 | result = utmscale_toInt64(toMax, scale, &status); | |
289 | if (status == U_ILLEGAL_ARGUMENT_ERROR) { | |
290 | log_err("utmscale_toInt64(toMax, %d, &status) generated U_ILLEGAL_ARGUMENT_ERROR.\n", scale); | |
291 | } | |
292 | ||
293 | if (toMax < U_INT64_MAX) { | |
294 | status = U_ZERO_ERROR; | |
295 | result = utmscale_toInt64(toMax + 1, scale, &status); | |
296 | if (status != U_ILLEGAL_ARGUMENT_ERROR) { | |
297 | log_err("utmscale_toInt64(toMax + 1, %d, &status) did not generate U_ILLEGAL_ARGUMENT_ERROR.\n", scale); | |
298 | } | |
299 | } | |
300 | } | |
301 | ||
302 | status = U_ZERO_ERROR; | |
303 | result = utmscale_toInt64(0, UDTS_MAX_SCALE, &status); | |
304 | if (status != U_ILLEGAL_ARGUMENT_ERROR) { | |
305 | log_err("utmscale_toInt64(0, UDTS_MAX_SCALE, &status) did not generate U_ILLEGAL_ARGUMENT_ERROR.\n"); | |
306 | } | |
307 | } | |
308 | ||
309 | static void TestAPI(void) | |
310 | { | |
311 | TestFromInt64(); | |
312 | TestToInt64(); | |
313 | } | |
314 | ||
315 | static void TestData(void) | |
316 | { | |
317 | TestEpochOffsets(); | |
318 | TestFromLimits(); | |
319 | TestToLimits(); | |
320 | } | |
321 | ||
322 | static void TestMonkey(void) | |
323 | { | |
324 | int32_t scale; | |
325 | UErrorCode status = U_ZERO_ERROR; | |
326 | ||
327 | for (scale = 0; scale < UDTS_MAX_SCALE; scale += 1) { | |
328 | int64_t fromMin = utmscale_getTimeScaleValue(scale, UTSV_FROM_MIN_VALUE, &status); | |
329 | int64_t fromMax = utmscale_getTimeScaleValue(scale, UTSV_FROM_MAX_VALUE, &status); | |
330 | int32_t i; | |
331 | ||
332 | initRandom(fromMin, fromMax); | |
333 | ||
334 | for (i = 0; i < LOOP_COUNT; i += 1) { | |
335 | int64_t value = randomInRange(); | |
336 | ||
337 | roundTripTest(value, scale); | |
338 | } | |
339 | } | |
340 | } | |
341 | ||
342 | #endif /* #if !UCONFIG_NO_FORMATTING */ |