1 /***********************************************************************
3 * Copyright (c) 1997-2004, International Business Machines Corporation
4 * and others. All Rights Reserved.
5 ***********************************************************************/
7 #include "unicode/utypes.h"
9 #if !UCONFIG_NO_FORMATTING
13 #include "unicode/calendar.h"
14 #include "unicode/gregocal.h"
15 #include "unicode/datefmt.h"
16 #include "unicode/smpdtfmt.h"
20 void CalendarLimitTest::runIndexedTest( int32_t index
, UBool exec
, const char* &name
, char* /*par*/ )
22 if (exec
) logln("TestSuite TestCalendarLimit");
24 // Re-enable this later
26 name
= "TestCalendarLimit";
28 logln("TestCalendarLimit---"); logln("");
32 default: name
= ""; break;
37 // *****************************************************************************
38 // class CalendarLimitTest
39 // *****************************************************************************
41 // -------------------------------------
43 CalendarLimitTest::test(UDate millis
, U_NAMESPACE_QUALIFIER Calendar
* cal
, U_NAMESPACE_QUALIFIER DateFormat
* fmt
)
45 static const UDate kDrift
= 1e-10;
46 UErrorCode exception
= U_ZERO_ERROR
;
47 UnicodeString theDate
;
48 UErrorCode status
= U_ZERO_ERROR
;
49 cal
->setTime(millis
, exception
);
50 if (U_SUCCESS(exception
)) {
51 fmt
->format(millis
, theDate
);
52 UDate dt
= fmt
->parse(theDate
, status
);
53 // allow a small amount of error (drift)
54 if(! withinErr(dt
, millis
, kDrift
)) {
55 errln("FAIL:round trip for large milli, got: %.1lf wanted: %.1lf. (delta %.2lf greater than %.2lf)",
56 dt
, millis
, uprv_fabs(millis
-dt
), uprv_fabs(dt
*kDrift
));
57 logln(UnicodeString(" ") + theDate
+ " " + CalendarTest::calToStr(*cal
));
59 logln(UnicodeString("OK: got ") + dt
+ ", wanted " + millis
);
60 logln(UnicodeString(" ") + theDate
);
65 // -------------------------------------
67 // bug 986c: deprecate nextDouble/previousDouble
69 //|CalendarLimitTest::nextDouble(double a)
71 //| return uprv_nextDouble(a, TRUE);
75 //|CalendarLimitTest::previousDouble(double a)
77 //| return uprv_nextDouble(a, FALSE);
81 CalendarLimitTest::withinErr(double a
, double b
, double err
)
83 return ( uprv_fabs(a
- b
) < uprv_fabs(a
* err
) );
87 CalendarLimitTest::TestCalendarLimit()
89 UErrorCode status
= U_ZERO_ERROR
;
90 Calendar
*cal
= Calendar::createInstance(status
);
91 if (failure(status
, "Calendar::createInstance")) return;
92 cal
->adoptTimeZone(TimeZone::createTimeZone("GMT"));
93 DateFormat
*fmt
= DateFormat::createDateTimeInstance();
94 fmt
->adoptCalendar(cal
);
95 ((SimpleDateFormat
*) fmt
)->applyPattern("HH:mm:ss.SSS zzz, EEEE, MMMM d, yyyy G");
98 // This test used to test the algorithmic limits of the dates that
99 // GregorianCalendar could handle. However, the algorithm has
100 // been rewritten completely since then and the prior limits no
101 // longer apply. Instead, we now do basic round-trip testing of
102 // some extreme (but still manageable) dates.
104 logln("checking 1e16..1e17");
105 for ( m
= 1e16
; m
< 1e17
; m
*= 1.1) {
108 logln("checking -1e14..-1e15");
109 for ( m
= -1e14
; m
> -1e15
; m
*= 1.1) {
113 // This is 2^52 - 1, the largest allowable mantissa with a 0
114 // exponent in a 64-bit double
115 UDate VERY_EARLY_MILLIS
= - 4503599627370495.0;
116 UDate VERY_LATE_MILLIS
= 4503599627370495.0;
118 // I am removing the previousDouble and nextDouble calls below for
119 // two reasons: 1. As part of jitterbug 986, I am deprecating
120 // these methods and removing calls to them. 2. This test is a
121 // non-critical boundary behavior test.
122 test(VERY_EARLY_MILLIS
, cal
, fmt
);
123 //test(previousDouble(VERY_EARLY_MILLIS), cal, fmt);
124 test(VERY_LATE_MILLIS
, cal
, fmt
);
125 //test(nextDouble(VERY_LATE_MILLIS), cal, fmt);
129 #endif /* #if !UCONFIG_NO_FORMATTING */