• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3  *
4  * This code is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License version 2 only, as
6  * published by the Free Software Foundation.
7  *
8  * This code is distributed in the hope that it will be useful, but WITHOUT
9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
11  * version 2 for more details (a copy is included in the LICENSE file that
12  * accompanied this code).
13  *
14  * You should have received a copy of the GNU General Public License version
15  * 2 along with this work; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
17  *
18  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
19  * or visit www.oracle.com if you need additional information or have any
20  * questions.
21  */
22 
23 /*
24  * This file is available under and governed by the GNU General Public
25  * License version 2 only, as published by the Free Software Foundation.
26  * However, the following notice accompanied the original version of this
27  * file:
28  *
29  * Written by Doug Lea and Martin Buchholz with assistance from
30  * members of JCP JSR-166 Expert Group and released to the public
31  * domain, as explained at
32  * http://creativecommons.org/publicdomain/zero/1.0/
33  */
34 
35 package test.java.util.concurrent.tck;
36 import static java.util.concurrent.TimeUnit.DAYS;
37 import static java.util.concurrent.TimeUnit.HOURS;
38 import static java.util.concurrent.TimeUnit.MICROSECONDS;
39 import static java.util.concurrent.TimeUnit.MILLISECONDS;
40 import static java.util.concurrent.TimeUnit.MINUTES;
41 import static java.util.concurrent.TimeUnit.NANOSECONDS;
42 import static java.util.concurrent.TimeUnit.SECONDS;
43 import static org.junit.Assert.assertEquals;
44 import static org.junit.Assert.assertFalse;
45 import static org.junit.Assert.assertNull;
46 import static org.junit.Assert.assertNotNull;
47 import static org.junit.Assert.assertSame;
48 import static org.junit.Assert.assertNotSame;
49 import static org.junit.Assert.assertTrue;
50 import static org.junit.Assert.fail;
51 
52 import java.time.Duration;
53 import java.time.temporal.ChronoUnit;
54 import java.util.Arrays;
55 import java.util.concurrent.ThreadLocalRandom;
56 import java.util.concurrent.TimeUnit;
57 import java.util.stream.LongStream;
58 
59 import org.junit.Test;
60 import org.junit.runner.RunWith;
61 import org.junit.runners.JUnit4;
62 
63 // Android-changed: Use JUnit4.
64 @RunWith(JUnit4.class)
65 public class TimeUnit8Test extends JSR166TestCase {
66 
67     // Android-changed: Use JUnitCore.main.
main(String[] args)68     public static void main(String[] args) {
69         // main(suite(), args);
70         org.junit.runner.JUnitCore.main("test.java.util.concurrent.tck.TimeUnit8Test");
71     }
72     // public static Test suite() {
73     //     return new TestSuite(TimeUnit8Test.class);
74     // }
75 
76     /**
77      * tests for toChronoUnit.
78      */
79     @Test
testToChronoUnit()80     public void testToChronoUnit() throws Exception {
81         assertSame(ChronoUnit.NANOS,   NANOSECONDS.toChronoUnit());
82         assertSame(ChronoUnit.MICROS,  MICROSECONDS.toChronoUnit());
83         assertSame(ChronoUnit.MILLIS,  MILLISECONDS.toChronoUnit());
84         assertSame(ChronoUnit.SECONDS, SECONDS.toChronoUnit());
85         assertSame(ChronoUnit.MINUTES, MINUTES.toChronoUnit());
86         assertSame(ChronoUnit.HOURS,   HOURS.toChronoUnit());
87         assertSame(ChronoUnit.DAYS,    DAYS.toChronoUnit());
88 
89         // Every TimeUnit has a defined ChronoUnit equivalent
90         for (TimeUnit x : TimeUnit.values())
91             assertSame(x, TimeUnit.of(x.toChronoUnit()));
92     }
93 
94     /**
95      * tests for TimeUnit.of(ChronoUnit).
96      */
97     @Test
testTimeUnitOf()98     public void testTimeUnitOf() throws Exception {
99         assertSame(NANOSECONDS,  TimeUnit.of(ChronoUnit.NANOS));
100         assertSame(MICROSECONDS, TimeUnit.of(ChronoUnit.MICROS));
101         assertSame(MILLISECONDS, TimeUnit.of(ChronoUnit.MILLIS));
102         assertSame(SECONDS,      TimeUnit.of(ChronoUnit.SECONDS));
103         assertSame(MINUTES,      TimeUnit.of(ChronoUnit.MINUTES));
104         assertSame(HOURS,        TimeUnit.of(ChronoUnit.HOURS));
105         assertSame(DAYS,         TimeUnit.of(ChronoUnit.DAYS));
106 
107         assertThrows(NullPointerException.class,
108                      () -> TimeUnit.of((ChronoUnit)null));
109 
110         // ChronoUnits either round trip to their TimeUnit
111         // equivalents, or throw IllegalArgumentException.
112         for (ChronoUnit cu : ChronoUnit.values()) {
113             final TimeUnit tu;
114             try {
115                 tu = TimeUnit.of(cu);
116             } catch (IllegalArgumentException acceptable) {
117                 continue;
118             }
119             assertSame(cu, tu.toChronoUnit());
120         }
121     }
122 
123     /**
124      * convert(Duration) roundtrips with Duration.ofXXXX and Duration.of(long, ChronoUnit)
125      */
126     @Test
testConvertDuration_roundtripDurationOf()127     public void testConvertDuration_roundtripDurationOf() {
128         long n = ThreadLocalRandom.current().nextLong();
129 
130         assertEquals(n, NANOSECONDS.convert(Duration.ofNanos(n)));
131         assertEquals(n, NANOSECONDS.convert(Duration.of(n, ChronoUnit.NANOS)));
132         assertEquals(n, MILLISECONDS.convert(Duration.ofMillis(n)));
133         assertEquals(n, MILLISECONDS.convert(Duration.of(n, ChronoUnit.MILLIS)));
134         assertEquals(n, SECONDS.convert(Duration.ofSeconds(n)));
135         assertEquals(n, SECONDS.convert(Duration.of(n, ChronoUnit.SECONDS)));
136         n /= 60;
137         assertEquals(n, MINUTES.convert(Duration.ofMinutes(n)));
138         assertEquals(n, MINUTES.convert(Duration.of(n, ChronoUnit.MINUTES)));
139         n /= 60;
140         assertEquals(n, HOURS.convert(Duration.ofHours(n)));
141         assertEquals(n, HOURS.convert(Duration.of(n, ChronoUnit.HOURS)));
142         n /= 24;
143         assertEquals(n, DAYS.convert(Duration.ofDays(n)));
144         assertEquals(n, DAYS.convert(Duration.of(n, ChronoUnit.DAYS)));
145     }
146 
147     /**
148      * convert(Duration.ofNanos(n)) agrees with convert(n, NANOSECONDS)
149      */
150     @Test
testConvertDuration_roundtripDurationOfNanos()151     public void testConvertDuration_roundtripDurationOfNanos() {
152         // Test values near unit transitions and near overflow.
153         LongStream.concat(
154                 Arrays.stream(TimeUnit.values()).mapToLong(u -> u.toNanos(1)),
155                 LongStream.of(Long.MAX_VALUE, Long.MIN_VALUE))
156             .flatMap(n -> LongStream.of(n, n + 1, n - 1))
157             .flatMap(n -> LongStream.of(n, n + 1_000_000_000, n - 1_000_000_000))
158             .flatMap(n -> LongStream.of(n, -n))
159             // .peek(System.err::println)
160             .forEach(n -> Arrays.stream(TimeUnit.values()).forEach(
161                 u -> assertEquals(u.convert(n, NANOSECONDS),
162                                   u.convert(Duration.ofNanos(n)))));
163     }
164 
165     /**
166      * convert(Duration) doesn't misbehave near Long.MAX_VALUE and Long.MIN_VALUE.
167      */
168     @Test
testConvertDuration_nearOverflow()169     public void testConvertDuration_nearOverflow() {
170         ChronoUnit NANOS = ChronoUnit.NANOS;
171         Duration maxDuration = Duration.ofSeconds(Long.MAX_VALUE, 999_999_999);
172         Duration minDuration = Duration.ofSeconds(Long.MIN_VALUE, 0);
173 
174         for (TimeUnit u : TimeUnit.values()) {
175             ChronoUnit cu = u.toChronoUnit();
176             long r;
177             if (u.toNanos(1) > SECONDS.toNanos(1)) {
178                 r = u.toNanos(1) / SECONDS.toNanos(1);
179 
180                 assertThrows(ArithmeticException.class,
181                              () -> Duration.of(Long.MAX_VALUE, cu),
182                              () -> Duration.of(Long.MIN_VALUE, cu));
183             } else {
184                 r = 1;
185 
186                 Duration max = Duration.of(Long.MAX_VALUE, cu);
187                 Duration min = Duration.of(Long.MIN_VALUE, cu);
188                 assertEquals(Long.MAX_VALUE, u.convert(max));
189                 assertEquals(Long.MAX_VALUE - 1, u.convert(max.minus(1, NANOS)));
190                 assertEquals(Long.MAX_VALUE - 1, u.convert(max.minus(1, cu)));
191                 assertEquals(Long.MIN_VALUE, u.convert(min));
192                 assertEquals(Long.MIN_VALUE + 1, u.convert(min.plus(1, NANOS)));
193                 assertEquals(Long.MIN_VALUE + 1, u.convert(min.plus(1, cu)));
194                 assertEquals(Long.MAX_VALUE, u.convert(max.plus(1, NANOS)));
195                 if (u != SECONDS) {
196                     assertEquals(Long.MAX_VALUE, u.convert(max.plus(1, cu)));
197                     assertEquals(Long.MIN_VALUE, u.convert(min.minus(1, NANOS)));
198                     assertEquals(Long.MIN_VALUE, u.convert(min.minus(1, cu)));
199                 }
200             }
201 
202             assertEquals(Long.MAX_VALUE / r, u.convert(maxDuration));
203             assertEquals(Long.MIN_VALUE / r, u.convert(minDuration));
204         }
205     }
206 
207 }
208