]>
Commit | Line | Data |
---|---|---|
b75a7d8f A |
1 | /******************************************************************** |
2 | * COPYRIGHT: | |
3 | * Copyright (c) 1997-2001, International Business Machines Corporation and | |
4 | * others. All Rights Reserved. | |
5 | ********************************************************************/ | |
6 | ||
7 | #include "unicode/utypes.h" | |
8 | ||
9 | #if !UCONFIG_NO_FORMATTING | |
10 | ||
11 | #include "callimts.h" | |
12 | #include "unicode/calendar.h" | |
13 | #include "unicode/gregocal.h" | |
14 | #include "unicode/datefmt.h" | |
15 | #include "unicode/smpdtfmt.h" | |
16 | ||
17 | U_NAMESPACE_USE | |
18 | void CalendarLimitTest::runIndexedTest( int32_t index, UBool exec, const char* &name, char* /*par*/ ) | |
19 | { | |
20 | if (exec) logln("TestSuite TestCalendarLimit"); | |
21 | switch (index) { | |
22 | // Re-enable this later | |
23 | case 0: | |
24 | name = "TestCalendarLimit"; | |
25 | if (exec) { | |
26 | logln("TestCalendarLimit---"); logln(""); | |
27 | TestCalendarLimit(); | |
28 | } | |
29 | break; | |
30 | default: name = ""; break; | |
31 | } | |
32 | } | |
33 | ||
34 | ||
35 | // ***************************************************************************** | |
36 | // class CalendarLimitTest | |
37 | // ***************************************************************************** | |
38 | ||
39 | // ------------------------------------- | |
40 | void | |
41 | CalendarLimitTest::test(UDate millis, U_NAMESPACE_QUALIFIER Calendar* cal, U_NAMESPACE_QUALIFIER DateFormat* fmt) | |
42 | { | |
43 | UErrorCode exception = U_ZERO_ERROR; | |
44 | UnicodeString theDate; | |
45 | UErrorCode status = U_ZERO_ERROR; | |
46 | cal->setTime(millis, exception); | |
47 | if (U_SUCCESS(exception)) { | |
48 | fmt->format(millis, theDate); | |
49 | UDate dt = fmt->parse(theDate, status); | |
50 | // allow a small amount of error (drift) | |
51 | if(! withinErr(dt, millis, 1e-10)) | |
52 | errln(UnicodeString("FAIL:round trip for large milli, got: ") + dt + " wanted: " + millis); | |
53 | else { | |
54 | logln(UnicodeString("OK: got ") + dt + ", wanted " + millis); | |
55 | logln(UnicodeString(" ") + theDate); | |
56 | } | |
57 | } | |
58 | } | |
59 | ||
60 | // ------------------------------------- | |
61 | ||
62 | // bug 986c: deprecate nextDouble/previousDouble | |
63 | //|double | |
64 | //|CalendarLimitTest::nextDouble(double a) | |
65 | //|{ | |
66 | //| return uprv_nextDouble(a, TRUE); | |
67 | //|} | |
68 | //| | |
69 | //|double | |
70 | //|CalendarLimitTest::previousDouble(double a) | |
71 | //|{ | |
72 | //| return uprv_nextDouble(a, FALSE); | |
73 | //|} | |
74 | ||
75 | UBool | |
76 | CalendarLimitTest::withinErr(double a, double b, double err) | |
77 | { | |
78 | return ( uprv_fabs(a - b) < uprv_fabs(a * err) ); | |
79 | } | |
80 | ||
81 | void | |
82 | CalendarLimitTest::TestCalendarLimit() | |
83 | { | |
84 | UErrorCode status = U_ZERO_ERROR; | |
85 | Calendar *cal = Calendar::createInstance(status); | |
86 | if (failure(status, "Calendar::createInstance")) return; | |
87 | cal->adoptTimeZone(TimeZone::createTimeZone("GMT")); | |
88 | DateFormat *fmt = DateFormat::createDateTimeInstance(); | |
89 | fmt->adoptCalendar(cal); | |
90 | ((SimpleDateFormat*) fmt)->applyPattern("HH:mm:ss.SSS zzz, EEEE, MMMM d, yyyy G"); | |
91 | ||
92 | // This test used to test the algorithmic limits of the dates that | |
93 | // GregorianCalendar could handle. However, the algorithm has | |
94 | // been rewritten completely since then and the prior limits no | |
95 | // longer apply. Instead, we now do basic round-trip testing of | |
96 | // some extreme (but still manageable) dates. | |
97 | UDate m; | |
98 | for ( m = 1e17; m < 1e18; m *= 1.1) { | |
99 | test(m, cal, fmt); | |
100 | } | |
101 | for ( m = -1e14; m > -1e15; m *= 1.1) { | |
102 | test(m, cal, fmt); | |
103 | } | |
104 | ||
105 | // This is 2^52 - 1, the largest allowable mantissa with a 0 | |
106 | // exponent in a 64-bit double | |
107 | UDate VERY_EARLY_MILLIS = - 4503599627370495.0; | |
108 | UDate VERY_LATE_MILLIS = 4503599627370495.0; | |
109 | ||
110 | // I am removing the previousDouble and nextDouble calls below for | |
111 | // two reasons: 1. As part of jitterbug 986, I am deprecating | |
112 | // these methods and removing calls to them. 2. This test is a | |
113 | // non-critical boundary behavior test. | |
114 | test(VERY_EARLY_MILLIS, cal, fmt); | |
115 | //test(previousDouble(VERY_EARLY_MILLIS), cal, fmt); | |
116 | test(VERY_LATE_MILLIS, cal, fmt); | |
117 | //test(nextDouble(VERY_LATE_MILLIS), cal, fmt); | |
118 | delete fmt; | |
119 | } | |
120 | ||
121 | #endif /* #if !UCONFIG_NO_FORMATTING */ | |
122 | ||
123 | // eof |