~ubuntu-branches/ubuntu/quantal/icu/quantal

« back to all changes in this revision

Viewing changes to source/test/intltest/callimts.cpp

  • Committer: Package Import Robot
  • Author(s): Yves Arrouye
  • Date: 2002-03-03 15:31:13 UTC
  • Revision ID: package-import@ubuntu.com-20020303153113-3ssceqlq45xbmbnc
Tags: upstream-2.0-2.1pre20020303
ImportĀ upstreamĀ versionĀ 2.0-2.1pre20020303

Show diffs side-by-side

added added

removed removed

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