Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/nio/file/attribute/FileTime/Basic.java
41159 views
1
/*
2
* Copyright (c) 2009, 2013, 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
/* @test
25
* @bug 6844313 8011647
26
* @summary Unit test for java.nio.file.FileTime
27
* @key randomness
28
*/
29
30
31
import java.nio.file.attribute.FileTime;
32
import java.time.Instant;
33
import java.util.concurrent.TimeUnit;
34
import static java.util.concurrent.TimeUnit.*;
35
import java.util.Random;
36
import java.util.EnumSet;
37
38
public class Basic {
39
40
static final Random rand = new Random();
41
42
public static void main(String[] args) {
43
long now = System.currentTimeMillis();
44
long tomorrowInDays = TimeUnit.DAYS.convert(now, MILLISECONDS) + 1;
45
long yesterdayInDays = TimeUnit.DAYS.convert(now, MILLISECONDS) - 1;
46
47
Instant nowInstant = Instant.ofEpochMilli(now);
48
49
// equals
50
eq(now, MILLISECONDS, now, MILLISECONDS);
51
eq(now, MILLISECONDS, now*1000L, MICROSECONDS);
52
neq(now, MILLISECONDS, 0, MILLISECONDS);
53
neq(now, MILLISECONDS, 0, MICROSECONDS);
54
55
eq(nowInstant, now, MILLISECONDS);
56
eq(nowInstant, now*1000L, MICROSECONDS);
57
neq(nowInstant, 0, MILLISECONDS);
58
neq(nowInstant, 0, MICROSECONDS);
59
60
// compareTo
61
cmp(now, MILLISECONDS, now, MILLISECONDS, 0);
62
cmp(now, MILLISECONDS, now*1000L, MICROSECONDS, 0);
63
cmp(now, MILLISECONDS, now-1234, MILLISECONDS, 1);
64
cmp(now, MILLISECONDS, now+1234, MILLISECONDS, -1);
65
66
cmp(tomorrowInDays, DAYS, now, MILLISECONDS, 1);
67
cmp(now, MILLISECONDS, tomorrowInDays, DAYS, -1);
68
cmp(yesterdayInDays, DAYS, now, MILLISECONDS, -1);
69
cmp(now, MILLISECONDS, yesterdayInDays, DAYS, 1);
70
cmp(yesterdayInDays, DAYS, now, MILLISECONDS, -1);
71
72
cmp(Long.MAX_VALUE, DAYS, Long.MAX_VALUE, NANOSECONDS, 1);
73
cmp(Long.MAX_VALUE, DAYS, Long.MIN_VALUE, NANOSECONDS, 1);
74
cmp(Long.MIN_VALUE, DAYS, Long.MIN_VALUE, NANOSECONDS, -1);
75
cmp(Long.MIN_VALUE, DAYS, Long.MAX_VALUE, NANOSECONDS, -1);
76
77
cmp(Instant.MIN, Long.MIN_VALUE, DAYS, 1);
78
cmp(Instant.MIN, Long.MIN_VALUE, HOURS, 1);
79
cmp(Instant.MIN, Long.MIN_VALUE, MINUTES, 1);
80
cmp(Instant.MIN, Long.MIN_VALUE, SECONDS, 1);
81
cmp(Instant.MIN, Instant.MIN.getEpochSecond() - 1, SECONDS, 1);
82
cmp(Instant.MIN, Instant.MIN.getEpochSecond() - 100, SECONDS, 1);
83
cmp(Instant.MIN, Instant.MIN.getEpochSecond(), SECONDS, 0);
84
85
cmp(Instant.MAX, Long.MAX_VALUE, DAYS, -1);
86
cmp(Instant.MAX, Long.MAX_VALUE, HOURS, -1);
87
cmp(Instant.MAX, Long.MAX_VALUE, MINUTES, -1);
88
cmp(Instant.MAX, Long.MAX_VALUE, SECONDS, -1);
89
cmp(Instant.MAX, Instant.MAX.getEpochSecond() + 1, SECONDS, -1);
90
cmp(Instant.MAX, Instant.MAX.getEpochSecond() + 100, SECONDS, -1);
91
cmp(Instant.MAX, Instant.MAX.getEpochSecond(), SECONDS, 0);
92
93
cmp(nowInstant, now, MILLISECONDS, 0);
94
cmp(nowInstant, now*1000L, MICROSECONDS, 0);
95
cmp(nowInstant, now-1234, MILLISECONDS, 1);
96
cmp(nowInstant, now+1234, MILLISECONDS, -1);
97
cmp(nowInstant, tomorrowInDays, DAYS, -1);
98
cmp(nowInstant, yesterdayInDays, DAYS, 1);
99
100
// to(TimeUnit)
101
to(MILLISECONDS.convert(1, DAYS) - 1, MILLISECONDS);
102
to(MILLISECONDS.convert(1, DAYS) + 0, MILLISECONDS);
103
to(MILLISECONDS.convert(1, DAYS) + 1, MILLISECONDS);
104
to(1, MILLISECONDS);
105
to(0, MILLISECONDS);
106
to(1, MILLISECONDS);
107
to(MILLISECONDS.convert(-1, DAYS) - 1, MILLISECONDS);
108
to(MILLISECONDS.convert(-1, DAYS) + 0, MILLISECONDS);
109
to(MILLISECONDS.convert(-1, DAYS) + 1, MILLISECONDS);
110
for (TimeUnit unit: TimeUnit.values()) {
111
for (int i=0; i<100; i++) { to(rand.nextLong(), unit); }
112
to(Long.MIN_VALUE, unit);
113
to(Long.MAX_VALUE, unit);
114
}
115
116
// toInstant()
117
int N = 1000;
118
for (TimeUnit unit : EnumSet.allOf(TimeUnit.class)) {
119
for (int i = 0; i < N; i++) {
120
long value = rand.nextLong();
121
FileTime ft = FileTime.from(value, unit);
122
Instant instant = ft.toInstant();
123
if (instant != Instant.MIN && instant != Instant.MAX) {
124
eqTime(value, unit, instant);
125
}
126
}
127
}
128
for (TimeUnit unit : EnumSet.allOf(TimeUnit.class)) {
129
long value = Long.MIN_VALUE;
130
FileTime ft = FileTime.from(value, unit);
131
Instant instant = ft.toInstant();
132
if (unit.compareTo(TimeUnit.SECONDS) < 0) {
133
eqTime(value, unit, instant);
134
} else if (!instant.equals(Instant.MIN)) {
135
throw new RuntimeException("should overflow to MIN");
136
}
137
value = Long.MAX_VALUE;
138
ft = FileTime.from(value, unit);
139
instant = ft.toInstant();
140
if (unit.compareTo(TimeUnit.SECONDS) < 0) {
141
eqTime(value, unit, instant);
142
} else if (!instant.equals(Instant.MAX)) {
143
throw new RuntimeException("should overflow to MAX");
144
}
145
}
146
147
// from(Instant)
148
final long MAX_SECOND = 31556889864403199L;
149
for (int i = 0; i < N; i++) {
150
long v = rand.nextLong();
151
long secs = v % MAX_SECOND;
152
Instant instant = Instant.ofEpochSecond(secs, rand.nextInt(1000_000_000));
153
FileTime ft = FileTime.from(instant);
154
if (!ft.toInstant().equals(instant) || ft.to(SECONDS) != secs) {
155
throw new RuntimeException("from(Instant) failed");
156
}
157
long millis = v;
158
instant = Instant.ofEpochMilli(millis);
159
ft = FileTime.from(instant);
160
if (!ft.toInstant().equals(instant) ||
161
ft.toMillis() != instant.toEpochMilli()) {
162
throw new RuntimeException("from(Instant) failed");
163
}
164
long nanos = v;
165
ft = FileTime.from(nanos, NANOSECONDS);
166
secs = nanos / 1000_000_000;
167
nanos = nanos % 1000_000_000;
168
instant = Instant.ofEpochSecond(secs, nanos);
169
if (!ft.equals(FileTime.from(instant))) {
170
throw new RuntimeException("from(Instant) failed");
171
}
172
}
173
174
// toString
175
ts(1L, DAYS, "1970-01-02T00:00:00Z");
176
ts(1L, HOURS, "1970-01-01T01:00:00Z");
177
ts(1L, MINUTES, "1970-01-01T00:01:00Z");
178
ts(1L, SECONDS, "1970-01-01T00:00:01Z");
179
ts(1L, MILLISECONDS, "1970-01-01T00:00:00.001Z");
180
ts(1L, MICROSECONDS, "1970-01-01T00:00:00.000001Z");
181
ts(1L, NANOSECONDS, "1970-01-01T00:00:00.000000001Z");
182
ts(999999999L, NANOSECONDS, "1970-01-01T00:00:00.999999999Z");
183
ts(9999999999L, NANOSECONDS, "1970-01-01T00:00:09.999999999Z");
184
185
ts(-1L, DAYS, "1969-12-31T00:00:00Z");
186
ts(-1L, HOURS, "1969-12-31T23:00:00Z");
187
ts(-1L, MINUTES, "1969-12-31T23:59:00Z");
188
ts(-1L, SECONDS, "1969-12-31T23:59:59Z");
189
ts(-1L, MILLISECONDS, "1969-12-31T23:59:59.999Z");
190
ts(-1L, MICROSECONDS, "1969-12-31T23:59:59.999999Z");
191
ts(-1L, NANOSECONDS, "1969-12-31T23:59:59.999999999Z");
192
ts(-999999999L, NANOSECONDS, "1969-12-31T23:59:59.000000001Z");
193
ts(-9999999999L, NANOSECONDS, "1969-12-31T23:59:50.000000001Z");
194
195
ts(-62135596799999L, MILLISECONDS, "0001-01-01T00:00:00.001Z");
196
ts(-62135596800000L, MILLISECONDS, "0001-01-01T00:00:00Z");
197
ts(-62135596800001L, MILLISECONDS, "-0001-12-31T23:59:59.999Z");
198
199
ts(253402300799999L, MILLISECONDS, "9999-12-31T23:59:59.999Z");
200
ts(-377642044800001L, MILLISECONDS, "-9999-12-31T23:59:59.999Z");
201
202
// NTFS epoch in usec.
203
ts(-11644473600000000L, MICROSECONDS, "1601-01-01T00:00:00Z");
204
205
ts(Instant.MIN, "-1000000001-01-01T00:00:00Z");
206
ts(Instant.MAX, "1000000000-12-31T23:59:59.999999999Z");
207
208
try {
209
FileTime.from(0L, null);
210
throw new RuntimeException("NullPointerException expected");
211
} catch (NullPointerException npe) { }
212
try {
213
FileTime.from(null);
214
throw new RuntimeException("NullPointerException expected");
215
} catch (NullPointerException npe) { }
216
217
FileTime time = FileTime.fromMillis(now);
218
if (time.equals(null))
219
throw new RuntimeException("should not be equal to null");
220
try {
221
time.compareTo(null);
222
throw new RuntimeException("NullPointerException expected");
223
} catch (NullPointerException npe) { }
224
225
// Instant + toMilli() overflow
226
overflow(Long.MAX_VALUE,
227
FileTime.from(Instant.MAX).toMillis());
228
overflow(Long.MAX_VALUE,
229
FileTime.from(Instant.ofEpochSecond(Long.MAX_VALUE / 1000 + 1))
230
.toMillis());
231
overflow(Long.MIN_VALUE,
232
FileTime.from(Instant.MIN).toMillis());
233
overflow(Long.MIN_VALUE,
234
FileTime.from(Instant.ofEpochSecond(Long.MIN_VALUE / 1000 - 1))
235
.toMillis());
236
237
// Instant + to(TimeUnit) overflow
238
overflow(Long.MAX_VALUE,
239
FileTime.from(Instant.ofEpochSecond(Long.MAX_VALUE / 1000 + 1))
240
.to(MILLISECONDS));
241
overflow(Long.MAX_VALUE,
242
FileTime.from(Instant.ofEpochSecond(Long.MAX_VALUE / 1000,
243
MILLISECONDS.toNanos(1000)))
244
.to(MILLISECONDS));
245
overflow(Long.MIN_VALUE,
246
FileTime.from(Instant.ofEpochSecond(Long.MIN_VALUE / 1000 - 1))
247
.to(MILLISECONDS));
248
overflow(Long.MIN_VALUE,
249
FileTime.from(Instant.ofEpochSecond(Long.MIN_VALUE / 1000,
250
-MILLISECONDS.toNanos(1)))
251
.to(MILLISECONDS));
252
}
253
254
static void overflow(long minmax, long v) {
255
if (v != minmax)
256
throw new RuntimeException("saturates to Long.MIN/MAX_VALUE expected");
257
}
258
259
static void cmp(long v1, TimeUnit u1, long v2, TimeUnit u2, int expected) {
260
int result = FileTime.from(v1, u1).compareTo(FileTime.from(v2, u2));
261
if (result != expected)
262
throw new RuntimeException("unexpected order");
263
}
264
265
static void cmp(Instant ins, long v2, TimeUnit u2, int expected) {
266
int result = FileTime.from(ins).compareTo(FileTime.from(v2, u2));
267
if (result != expected)
268
throw new RuntimeException("unexpected order");
269
}
270
271
static void eq(long v1, TimeUnit u1, long v2, TimeUnit u2) {
272
FileTime t1 = FileTime.from(v1, u1);
273
FileTime t2 = FileTime.from(v2, u2);
274
if (!t1.equals(t2))
275
throw new RuntimeException("not equal");
276
if (t1.hashCode() != t2.hashCode())
277
throw new RuntimeException("hashCodes should be equal");
278
}
279
280
static void eq(Instant ins, long v2, TimeUnit u2) {
281
FileTime t1 = FileTime.from(ins);
282
FileTime t2 = FileTime.from(v2, u2);
283
if (!t1.equals(t2))
284
throw new RuntimeException("not equal");
285
if (t1.hashCode() != t2.hashCode())
286
throw new RuntimeException("hashCodes should be equal");
287
}
288
289
static void eqTime(long value, TimeUnit unit, Instant instant) {
290
long secs = SECONDS.convert(value, unit);
291
long nanos = NANOSECONDS.convert(value - unit.convert(secs, SECONDS), unit);
292
if (nanos < 0) { // normalize nanoOfSecond to positive
293
secs -= 1;
294
nanos += 1000_000_000;
295
}
296
if (secs != instant.getEpochSecond() || (int)nanos != instant.getNano()) {
297
System.err.println(" ins=" + instant);
298
throw new RuntimeException("ft and instant are not the same time point");
299
}
300
}
301
302
static void neq(long v1, TimeUnit u1, long v2, TimeUnit u2) {
303
FileTime t1 = FileTime.from(v1, u1);
304
FileTime t2 = FileTime.from(v2, u2);
305
if (t1.equals(t2))
306
throw new RuntimeException("should not be equal");
307
}
308
309
static void neq(Instant ins, long v2, TimeUnit u2) {
310
FileTime t1 = FileTime.from(ins);
311
FileTime t2 = FileTime.from(v2, u2);
312
if (t1.equals(t2))
313
throw new RuntimeException("should not be equal");
314
}
315
316
static void to(long v, TimeUnit unit) {
317
FileTime t = FileTime.from(v, unit);
318
for (TimeUnit u: TimeUnit.values()) {
319
long result = t.to(u);
320
long expected = u.convert(v, unit);
321
if (result != expected) {
322
throw new RuntimeException("unexpected result");
323
}
324
}
325
}
326
327
static void ts(long v, TimeUnit unit, String expected) {
328
String result = FileTime.from(v, unit).toString();
329
if (!result.equals(expected)) {
330
System.err.format("FileTime.from(%d, %s).toString() failed\n", v, unit);
331
System.err.format("Expected: %s\n", expected);
332
System.err.format(" Got: %s\n", result);
333
throw new RuntimeException();
334
}
335
}
336
337
static void ts(Instant instant, String expected) {
338
String result = FileTime.from(instant).toString();
339
if (!result.equals(expected)) {
340
System.err.format("FileTime.from(%s).toString() failed\n", instant);
341
System.err.format("Expected: %s\n", expected);
342
System.err.format(" Got: %s\n", result);
343
throw new RuntimeException();
344
}
345
}
346
}
347
348