File: DateGregorianCalendarTest.java

package info (click to toggle)
openjdk-11 11.0.4%2B11-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 757,028 kB
  • sloc: java: 5,016,041; xml: 1,191,974; cpp: 934,731; ansic: 555,697; sh: 24,299; objc: 12,703; python: 3,602; asm: 3,415; makefile: 2,772; awk: 351; sed: 172; perl: 114; jsp: 24; csh: 3
file content (235 lines) | stat: -rw-r--r-- 10,695 bytes parent folder | download | duplicates (16)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/*
 * Copyright (c) 2003, 2016, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

/*
 * @test
 * @bug 4614842
 * @summary Make sure that a Date and a GregorianCalendar produce the same date/time. Both are new implementations in 1.5.
 * @run main DateGregorianCalendarTest 15
 */

import java.util.*;
import static java.util.GregorianCalendar.*;

// Usage: java DateGregorianCalendarTest [duration]

@SuppressWarnings("deprecation")
public class DateGregorianCalendarTest {
    static volatile boolean runrun = true;
    static int nThreads;

    public static void main(String[] args) {
        int duration = 600;
        if (args.length == 1) {
            duration = Math.max(10, Integer.parseInt(args[0]));
        }

        TimeZone savedTZ = TimeZone.getDefault();
        try {
            TimeZone.setDefault(TimeZone.getTimeZone("GMT"));
            Thread[] t = new Thread[10]; // for future bugs...
            int index = 0;
            t[index++] = new Thread(new Runnable() {
                    public void run() {
                        GregorianCalendar gc = new GregorianCalendar();

                        long delta = (long)(279 * 365.2422 * 24 * 60 * 60 * 1000);
                        long count = 0;
                        try {
                            for (long t = Long.MIN_VALUE; runrun && t < Long.MAX_VALUE-delta; t += delta) {
                                gc.setTimeInMillis(t);
                                Date date = new Date(t);
                                int y;
                                if (!((y = gc.get(YEAR)) == (date.getYear()+1900) &&
                                      gc.get(MONTH) == date.getMonth() &&
                                      gc.get(DAY_OF_MONTH) == date.getDate() &&
                                      gc.get(HOUR_OF_DAY) == date.getHours() &&
                                      gc.get(MINUTE) == date.getMinutes() &&
                                      gc.get(SECOND) == date.getSeconds())) {
                                    throw new RuntimeException("GregorinCalendar and Date returned different dates."
                                                               +" (millis=" + t + ")\n"
                                                               +"GC=" + gc + "\nDate=" + date);
                                }
                                ++count;
                                if (y >= 1) {
                                    delta = (long)(365.2422 * 24 * 60 * 60 * 1000);
                                }
                                if (y >= 1970) {
                                    delta = (24 * 60 * 60 * 1000);
                                }
                                if (y >= 2039) {
                                    delta = (long)(279 * 365.2422 * 24 * 60 * 60 * 1000);
                                }
                            }
                            if (runrun) {
                                System.out.println("Part I (count="+count+"): Passed");
                            } else {
                                System.out.println("Part I (count="+count+"): Incomplete");
                            }
                        } catch (RuntimeException e) {
                            System.out.println("Part I (count="+count+"): FAILED");
                            runrun = false;
                            throw e;
                        } finally {
                            decrementCounter();
                        }
                    }
                });

            t[index++] = new Thread(new Runnable() {
                    public void run() {
                        GregorianCalendar gc = new GregorianCalendar();

                        long count = 0;
                        int delta;
                        try {
                            for (long year = Integer.MIN_VALUE+1900;
                                 runrun && year <= Integer.MAX_VALUE; year += delta) {
                                checkTimes(gc, year, JANUARY, 1, 0, 0, 0);
                                ++count;
                                delta = getDelta((int)year);
                            }

                            for (long month = Integer.MIN_VALUE;
                                 runrun && month <= Integer.MAX_VALUE; month += delta) {
                                checkTimes(gc, 1900, month, 1, 0, 0, 0);
                                ++count;
                                delta = getDelta(gc.get(YEAR));
                            }

                            for (long dayOfMonth = Integer.MIN_VALUE;
                                 runrun && dayOfMonth <= Integer.MAX_VALUE; dayOfMonth += delta) {
                                checkTimes(gc, 1900, JANUARY, dayOfMonth, 0, 0, 0);
                                ++count;
                                delta = getDelta(gc.get(YEAR));
                            }
                            if (runrun) {
                                System.out.println("Part II (count="+count+"): Passed");
                            } else {
                                System.out.println("Part II (count="+count+"): Incomplete");
                            }
                        } catch (RuntimeException e) {
                            System.out.println("Part II (count="+count+"): FAILED");
                            runrun = false;
                            throw e;
                        } finally {
                            decrementCounter();
                        }
                    }
                });

            // t3 takes more than 10 minutes (on Ultra-60 450MHz) without
            // the 4936355 fix due to getting the small delta.
            t[index++] = new Thread(new Runnable() {
                    public void run() {
                        GregorianCalendar gc = new GregorianCalendar();

                        long count = 0;
                        int delta;
                        try {
                            for (long hourOfDay = Integer.MIN_VALUE;
                                 runrun && hourOfDay <= Integer.MAX_VALUE; hourOfDay += delta) {
                                checkTimes(gc, 1970, JANUARY, 1, hourOfDay, 0, 0);
                                ++count;
                                delta = getDelta(gc.get(YEAR));
                            }
                            for (long minutes = Integer.MIN_VALUE;
                                 runrun && minutes <= Integer.MAX_VALUE; minutes += delta) {
                                checkTimes(gc, 1970, JANUARY, 1, 0, minutes, 0);
                                ++count;
                                delta = getDelta(gc.get(YEAR)) * 60;
                            }
                            for (long seconds = Integer.MIN_VALUE;
                                 runrun && seconds <= Integer.MAX_VALUE; seconds += delta) {
                                checkTimes(gc, 1970, JANUARY, 1, 0, 0, seconds);
                                ++count;
                                delta = getDelta(gc.get(YEAR)) * 60 * 60;
                            }
                            if (runrun) {
                                System.out.println("Part III (count="+count+"): Passed");
                            } else {
                                System.out.println("Part III (count="+count+"): Incomplete");
                            }
                        } catch (RuntimeException e) {
                            System.out.println("Part III (count="+count+"): FAILED");
                            runrun = false;
                            throw e;
                        } finally {
                            decrementCounter();
                        }
                    }
                });

            for (int i = 0; i < index; i++) {
                incrementCounter();
                t[i].start();
            }

            try {
                for (int i = 0; getCounter() > 0 && i < duration; i++) {
                    Thread.sleep(1000);
                }
                runrun = false;
                for (int i = 0; i < index; i++) {
                    t[i].join();
                }
            } catch (InterruptedException e) {
            }
        } finally {
            TimeZone.setDefault(savedTZ);
        }
    }

    static void checkTimes(GregorianCalendar gc, long year, long month, long dayOfMonth,
                           long hourOfDay, long minutes, long seconds) {
        gc.clear();
        gc.set((int)year, (int)month, (int)dayOfMonth, (int)hourOfDay, (int)minutes, (int)seconds);
        long time = gc.getTimeInMillis();
        Date date = new Date((int)year - 1900, (int)month, (int)dayOfMonth,
                             (int)hourOfDay, (int)minutes, (int)seconds);
        long time2 = date.getTime();
        if (time != time2) {
            throw new RuntimeException("GregorinCalendar and Date returned different values.\n"
                                       +"year="+year+", month="+month+", dayOfMonth="+dayOfMonth
                                       +"\nhourOfDay="+hourOfDay+", minutes="+minutes+", seconds="+seconds
                                       +"\ntime=" + time + ", time2=" + time2
                                       +"\nGC=" + gc + "\nDate=" + date);
        }
    }

    static final int getDelta(int year) {
        return (year >= 1970 && year <= 2039) ? 1 : 1<<13;
    }

    synchronized static void incrementCounter() {
        nThreads++;
    }

    synchronized static void decrementCounter() {
        nThreads--;
    }

    synchronized static int getCounter() {
        return nThreads;
    }
}