1 /*
2  * Copyright (C) 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package libcore.java.util;
18 
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertThrows;
21 
22 import java.util.SplittableRandom;
23 import java.util.random.RandomGenerator.SplittableGenerator;
24 
25 import org.junit.Assert;
26 import org.junit.Test;
27 import org.junit.runner.RunWith;
28 import org.junit.runners.JUnit4;
29 
30 @RunWith(JUnit4.class)
31 public class SplittableRandomTest {
32 
33     @Test
testNextBoolean()34     public void testNextBoolean() {
35         long seed = 0x1234567890L;
36         SplittableRandom random1 = new SplittableRandom(seed);
37         SplittableRandom random2 = new SplittableRandom(seed);
38 
39         assertGeneratorsAreEquals(random1, random2);
40     }
41 
42     @Test
split_throwsNPE_whenSourceIsNull()43     public void split_throwsNPE_whenSourceIsNull() {
44         SplittableRandom random = new SplittableRandom(42);
45 
46         assertThrows(NullPointerException.class, () -> random.split(/* source= */ null));
47         assertThrows(NullPointerException.class, () -> random.splits(1, /* source= */ null));
48         assertThrows(NullPointerException.class, () -> random.splits(/* source= */ null));
49     }
50 
51     @Test
split_throwsIAE_whenSizeIsNonPositive()52     public void split_throwsIAE_whenSizeIsNonPositive() {
53         SplittableRandom random = new SplittableRandom(42);
54 
55         assertThrows(IllegalArgumentException.class, () -> random.splits(-1, random));
56         assertThrows(IllegalArgumentException.class, () -> random.splits(-1));
57     }
58 
59     @Test
splits_streamOfSizeZero_areEmpty()60     public void splits_streamOfSizeZero_areEmpty() {
61         var random = new SplittableRandom();
62 
63         assertEquals(0L, random.splits(0).count());
64         assertEquals(0L, random.splits(0, random).count());
65     }
66 
67     @Test
splitInstances_areTheSame_whenTheyAreSplitWithIdenticalSource()68     public void splitInstances_areTheSame_whenTheyAreSplitWithIdenticalSource() {
69         var seed = 1001;
70         var random1 = new SplittableRandom(seed);
71         var random2 = new SplittableRandom(seed);
72 
73         var sourceSeed = 9999;
74 
75         var splitRandom1 = random1.split(new SplittableRandom(sourceSeed));
76         var splitRandom2 = random2.split(new SplittableRandom(sourceSeed));
77 
78         assertGeneratorsAreEquals(splitRandom1, splitRandom2);
79     }
80 
81     @Test
splitsInstances_areTheSame_whenTheyAreSplitWithIdenticalSource_boundedStream()82     public void splitsInstances_areTheSame_whenTheyAreSplitWithIdenticalSource_boundedStream() {
83         var seed = 1001;
84 
85         var random1 = new SplittableRandom(seed);
86         var random2 = new SplittableRandom(seed);
87 
88         var sourceSeed = 9999;
89         var size = 10;
90 
91         var splitRandom1 = random1.splits(size, new SplittableRandom(sourceSeed)).toList();
92         var splitRandom2 = random2.splits(size, new SplittableRandom(sourceSeed)).toList();
93 
94         assertEquals(size, splitRandom1.size());
95         assertEquals(size, splitRandom2.size());
96 
97         for (int i = 0; i < size; ++i) {
98             assertGeneratorsAreEquals(splitRandom1.get(i), splitRandom2.get(i));
99         }
100     }
101 
102 
103     @Test
splitsInstances_areTheSame_whenTheyAreSplitWithIdenticalSource_unboundedStream()104     public void splitsInstances_areTheSame_whenTheyAreSplitWithIdenticalSource_unboundedStream() {
105         var seed = 1001;
106 
107         var random1 = new SplittableRandom(seed);
108         var random2 = new SplittableRandom(seed);
109 
110         var sourceSeed = 9999;
111         var size = 10;
112 
113         var splitRandom1 = random1.splits(new SplittableRandom(sourceSeed)).limit(size).toList();
114         var splitRandom2 = random2.splits(new SplittableRandom(sourceSeed)).limit(size).toList();
115 
116         assertEquals(size, splitRandom1.size());
117         assertEquals(size, splitRandom2.size());
118 
119         for (int i = 0; i < size; ++i) {
120             assertGeneratorsAreEquals(splitRandom1.get(i), splitRandom2.get(i));
121         }
122     }
123 
124     @Test
splitsInstances_areTheSame_whenSourceIsIdentical()125     public void splitsInstances_areTheSame_whenSourceIsIdentical() {
126         var seed = 1001;
127 
128         var random1 = new SplittableRandom(seed);
129         var random2 = new SplittableRandom(seed);
130 
131         var size = 10;
132 
133         var splitRandom1 = random1.splits(size).toList();
134         var splitRandom2 = random2.splits(size).toList();
135 
136         assertEquals(size, splitRandom1.size());
137         assertEquals(size, splitRandom2.size());
138 
139         for (int i = 0; i < size; ++i) {
140             assertGeneratorsAreEquals(splitRandom1.get(i), splitRandom2.get(i));
141         }
142     }
143 
assertGeneratorsAreEquals(SplittableGenerator random1, SplittableGenerator random2)144     private static void assertGeneratorsAreEquals(SplittableGenerator random1,
145                                                   SplittableGenerator random2) {
146         for (int i = 0; i < 1_000; ++i) {
147             assertEquals(random1.nextLong(), random2.nextLong());
148         }
149     }
150 
151 }
152