Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/util/Calendar/ResolutionTest.java
41149 views
1
/*
2
* Copyright (c) 2007, 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 6452848
27
* @summary Make sure that the resolution of (WEKK_OF_MONTH +
28
* DAY_OF_WEEK) and (DAY_OF_WEEK_IN_MONTH + DAY_OF_WEEK) works as
29
* specified in the API.
30
* @key randomness
31
*/
32
33
import java.util.*;
34
import static java.util.Calendar.*;
35
36
public class ResolutionTest {
37
static Random rand = new Random();
38
39
public static void main(String[] args) {
40
for (int year = 1995; year < 2011; year++) {
41
for (int month = JANUARY; month <= DECEMBER; month++) {
42
for (int dow = SUNDAY; dow <= SATURDAY; dow++) {
43
test(year, month, dow);
44
}
45
}
46
}
47
}
48
49
static void test(int year, int month, int dow) {
50
Calendar cal = new GregorianCalendar(year, month, 1);
51
int max = cal.getActualMaximum(DAY_OF_MONTH);
52
ArrayList<Integer> list = new ArrayList<Integer>();
53
for (int d = 1; d <= max; d++) {
54
cal.clear();
55
cal.set(year, month, d);
56
if (cal.get(DAY_OF_WEEK) == dow) {
57
list.add(d);
58
}
59
}
60
for (int i = 0; i < 100; i++) {
61
int nth = rand.nextInt(list.size()); // 0-based
62
int day = list.get(nth);
63
nth++; // 1-based
64
testDayOfWeekInMonth(year, month, nth, dow, day);
65
}
66
67
// Put WEEK_OF_MONTH-DAY_OF_MONTH pairs
68
list = new ArrayList<Integer>();
69
for (int d = 1; d <= max; d++) {
70
cal.clear();
71
cal.set(year, month, d);
72
if (cal.get(DAY_OF_WEEK) == dow) {
73
list.add(cal.get(WEEK_OF_MONTH));
74
list.add(d);
75
}
76
}
77
for (int i = 0; i < list.size(); i++) {
78
int nth = list.get(i++);
79
int day = list.get(i);
80
testWeekOfMonth(year, month, nth, dow, day);
81
}
82
}
83
84
static Koyomi cal = new Koyomi();
85
86
static void testDayOfWeekInMonth(int year, int month, int nth, int dow, int expected) {
87
// don't call clear() here
88
cal.set(YEAR, year);
89
cal.set(MONTH, month);
90
// Set DAY_OF_WEEK_IN_MONTH before DAY_OF_WEEK
91
cal.set(DAY_OF_WEEK_IN_MONTH, nth);
92
cal.set(DAY_OF_WEEK, dow);
93
if (!cal.checkDate(year, month, expected)) {
94
throw new RuntimeException(String.format("DOWIM: year=%d, month=%d, nth=%d, dow=%d:%s%n",
95
year, month+1, nth, dow, cal.getMessage()));
96
}
97
}
98
99
static void testWeekOfMonth(int year, int month, int nth, int dow, int expected) {
100
// don't call clear() here
101
cal.set(YEAR, year);
102
cal.set(MONTH, month);
103
// Set WEEK_OF_MONTH before DAY_OF_WEEK
104
cal.set(WEEK_OF_MONTH, nth);
105
cal.set(DAY_OF_WEEK, dow);
106
if (!cal.checkDate(year, month, expected)) {
107
throw new RuntimeException(String.format("WOM: year=%d, month=%d, nth=%d, dow=%d:%s%n",
108
year, month+1, nth, dow, cal.getMessage()));
109
}
110
}
111
}
112
113