Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/util/Calendar/StampOverflow.java
41149 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
* @modules java.base/java.util:open
29
*/
30
31
import java.lang.reflect.*;
32
import java.util.*;
33
import static java.util.Calendar.*;
34
35
// Calendar fails when turning negative to positive (zero), not
36
// positive to negative with nextStamp. If a negative value was set to
37
// nextStamp, it would fail even with the fix. So, there's no way to
38
// reproduce the symptom in a short time -- at leaset it would take a
39
// couple of hours even if we started with Integer.MAX_VALUE. So, this
40
// test case just checks that set() calls don't cause any nextStamp
41
// overflow.
42
43
public class StampOverflow {
44
public static void main(String[] args) throws IllegalAccessException {
45
// Get a Field for "nextStamp".
46
Field nextstamp = null;
47
try {
48
nextstamp = Calendar.class.getDeclaredField("nextStamp");
49
} catch (NoSuchFieldException e) {
50
throw new RuntimeException("implementation changed?", e);
51
}
52
53
nextstamp.setAccessible(true);
54
55
Calendar cal = new GregorianCalendar();
56
int initialValue = nextstamp.getInt(cal);
57
// Set nextStamp to a very large number
58
nextstamp.setInt(cal, Integer.MAX_VALUE - 100);
59
60
for (int i = 0; i < 1000; i++) {
61
invoke(cal);
62
int stampValue = nextstamp.getInt(cal);
63
// nextStamp must not be less than initialValue.
64
if (stampValue < initialValue) {
65
throw new RuntimeException("invalid nextStamp: " + stampValue);
66
}
67
}
68
}
69
70
static void invoke(Calendar cal) {
71
cal.clear();
72
cal.set(2000, NOVEMBER, 2, 0, 0, 0);
73
int y = cal.get(YEAR);
74
int m = cal.get(MONTH);
75
int d = cal.get(DAY_OF_MONTH);
76
if (y != 2000 || m != NOVEMBER || d != 2) {
77
throw new RuntimeException("wrong date produced ("
78
+ y + "/" + (m+1) + "/" + d + ")");
79
}
80
}
81
}
82
83