Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/util/Calendar/StampOverflow.java
47182 views
1
/*
2
* Copyright (c) 2001, 2016, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
/*
25
* @test
26
* @bug 4404619 6348819
27
* @summary Make sure that Calendar doesn't cause nextStamp overflow.
28
*/
29
30
import java.lang.reflect.*;
31
import java.util.*;
32
import static java.util.Calendar.*;
33
34
// Calendar fails when turning negative to positive (zero), not
35
// positive to negative with nextStamp. If a negative value was set to
36
// nextStamp, it would fail even with the fix. So, there's no way to
37
// reproduce the symptom in a short time -- at leaset it would take a
38
// couple of hours even if we started with Integer.MAX_VALUE. So, this
39
// test case just checks that set() calls don't cause any nextStamp
40
// overflow.
41
42
public class StampOverflow {
43
public static void main(String[] args) throws IllegalAccessException {
44
// Get a Field for "nextStamp".
45
Field nextstamp = null;
46
try {
47
nextstamp = Calendar.class.getDeclaredField("nextStamp");
48
} catch (NoSuchFieldException e) {
49
throw new RuntimeException("implementation changed?", e);
50
}
51
52
nextstamp.setAccessible(true);
53
54
Calendar cal = new GregorianCalendar();
55
int initialValue = nextstamp.getInt(cal);
56
// Set nextStamp to a very large number
57
nextstamp.setInt(cal, Integer.MAX_VALUE - 100);
58
59
for (int i = 0; i < 1000; i++) {
60
invoke(cal);
61
int stampValue = nextstamp.getInt(cal);
62
// nextStamp must not be less than initialValue.
63
if (stampValue < initialValue) {
64
throw new RuntimeException("invalid nextStamp: " + stampValue);
65
}
66
}
67
}
68
69
static void invoke(Calendar cal) {
70
cal.clear();
71
cal.set(2000, NOVEMBER, 2, 0, 0, 0);
72
int y = cal.get(YEAR);
73
int m = cal.get(MONTH);
74
int d = cal.get(DAY_OF_MONTH);
75
if (y != 2000 || m != NOVEMBER || d != 2) {
76
throw new RuntimeException("wrong date produced ("
77
+ y + "/" + (m+1) + "/" + d + ")");
78
}
79
}
80
}
81
82