]>
git.saurik.com Git - apple/icu.git/blob - icuSources/test/cintltst/utmstest.c
2 ****************************************************************************
3 * Copyright (c) 1997-2004, International Business Machines Corporation and *
4 * others. All Rights Reserved. *
5 ****************************************************************************
8 #include "unicode/utypes.h"
10 #if !UCONFIG_NO_FORMATTING
12 #include "unicode/utmscale.h"
19 #define LOOP_COUNT 10000
21 static void TestAPI(void);
22 static void TestData(void);
23 static void TestMonkey(void);
25 void addUtmsTest(TestNode
** root
);
27 void addUtmsTest(TestNode
** root
)
29 addTest(root
, &TestAPI
, "tsformat/utmstest/TestAPI");
30 addTest(root
, &TestData
, "tsformat/utmstest/TestData");
31 addTest(root
, &TestMonkey
, "tsformat/utmstest/TestMonkey");
35 * Return a random int64_t where U_INT64_MIN <= ran <= U_INT64_MAX.
37 static uint64_t randomInt64(void)
41 static UBool initialized
= FALSE
;
44 srand((unsigned)time(NULL
));
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);
56 static int64_t ranInt
;
57 static int64_t ranMin
;
58 static int64_t ranMax
;
60 static void initRandom(int64_t min
, int64_t max
)
62 uint64_t interval
= max
- min
;
68 if (interval
< U_INT64_MIN
) {
73 static int64_t randomInRange(void)
78 value
= randomInt64() % ranInt
;
87 value
= randomInt64();
88 } while (value
< ranMin
|| value
> ranMax
);
94 static void roundTripTest(int64_t value
, int32_t scale
)
96 UErrorCode status
= U_ZERO_ERROR
;
97 int64_t rt
= utmscale_toInt64(utmscale_fromInt64(value
, scale
, &status
), scale
, &status
);
100 log_err("Round-trip error: time scale = %d, value = %lld, round-trip = %lld.\n", scale
, value
, rt
);
104 static void toLimitTest(int64_t toLimit
, int64_t fromLimit
, int32_t scale
)
106 UErrorCode status
= U_ZERO_ERROR
;
107 int64_t result
= utmscale_toInt64(toLimit
, scale
, &status
);
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
);
115 static void epochOffsetTest(int64_t epochOffset
, int64_t units
, int32_t scale
)
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
);
123 log_err("utmscale_toInt64(epochOffset, scale, &status): scale = %d epochOffset = %lld, result = %lld.\n", scale
, epochOffset
, local
);
126 local
= utmscale_toInt64(0, scale
, &status
);
128 if (local
!= -epochOffset
) {
129 log_err("utmscale_toInt64(0, scale): scale = %d, result = %lld.\n", scale
, local
);
132 universal
= utmscale_fromInt64(-epochOffset
, scale
, &status
);
134 if (universal
!= 0) {
135 log_err("from(-epochOffest, scale): scale = %d, epochOffset = %lld, result = %lld.\n", scale
, epochOffset
, universal
);
138 universal
= utmscale_fromInt64(0, scale
, &status
);
140 if (universal
!= universalEpoch
) {
141 log_err("utmscale_fromInt64(0, scale): scale = %d, result = %lld.\n", scale
, universal
);
145 static void TestEpochOffsets(void)
147 UErrorCode status
= U_ZERO_ERROR
;
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
);
154 epochOffsetTest(epochOffset
, units
, scale
);
158 static void TestFromLimits(void)
160 UErrorCode status
= U_ZERO_ERROR
;
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
);
167 roundTripTest(fromMin
, scale
);
168 roundTripTest(fromMax
, scale
);
172 static void TestToLimits(void)
174 UErrorCode status
= U_ZERO_ERROR
;
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
);
183 toLimitTest(toMin
, fromMin
, scale
);
184 toLimitTest(toMax
, fromMax
, scale
);
188 static void TestFromInt64(void)
192 UErrorCode status
= U_ZERO_ERROR
;
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");
199 for (scale
= 0; scale
< UDTS_MAX_SCALE
; scale
+= 1) {
200 int64_t fromMin
, fromMax
;
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
);
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
);
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
);
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
);
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
);
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
);
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");
248 static void TestToInt64(void)
252 UErrorCode status
= U_ZERO_ERROR
;
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");
259 for (scale
= 0; scale
< UDTS_MAX_SCALE
; scale
+= 1) {
260 int64_t toMin
, toMax
;
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
);
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
);
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
);
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
);
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
);
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
);
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");
309 static void TestAPI(void)
315 static void TestData(void)
322 static void TestMonkey(void)
325 UErrorCode status
= U_ZERO_ERROR
;
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
);
332 initRandom(fromMin
, fromMax
);
334 for (i
= 0; i
< LOOP_COUNT
; i
+= 1) {
335 int64_t value
= randomInRange();
337 roundTripTest(value
, scale
);
342 #endif /* #if !UCONFIG_NO_FORMATTING */