mduigou | 072224c | 2013-05-02 09:18:56 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2012, 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 | import org.testng.annotations.DataProvider; |
| 25 | import org.testng.annotations.Test; |
| 26 | |
| 27 | import java.lang.Integer; |
| 28 | import java.lang.Object; |
| 29 | import java.lang.System; |
| 30 | import java.util.BitSet; |
| 31 | import java.util.OptionalInt; |
| 32 | import java.util.PrimitiveIterator; |
| 33 | import java.util.Random; |
| 34 | import java.util.function.IntSupplier; |
| 35 | import java.util.stream.Collectors; |
| 36 | import java.util.stream.IntStream; |
| 37 | |
| 38 | import static org.testng.Assert.assertEquals; |
| 39 | import static org.testng.Assert.assertFalse; |
| 40 | import static org.testng.Assert.assertTrue; |
| 41 | import static org.testng.Assert.fail; |
| 42 | |
| 43 | /** |
| 44 | * @test |
| 45 | * @summary test BitSet stream |
| 46 | * @bug 8012645 |
| 47 | * @run testng BitSetStreamTest |
| 48 | */ |
| 49 | public class BitSetStreamTest { |
| 50 | static class Fibs implements IntSupplier { |
| 51 | private int n1 = 0; |
| 52 | private int n2 = 1; |
| 53 | |
| 54 | static int fibs(int n) { |
| 55 | Fibs f = new Fibs(); |
| 56 | while (n-- > 0) f.getAsInt(); |
| 57 | return f.getAsInt(); |
| 58 | } |
| 59 | |
| 60 | public int getAsInt() { int s = n1; n1 = n2; n2 = s + n1; return s; } |
| 61 | } |
| 62 | |
| 63 | private static final Object[][] testcases = new Object[][] { |
| 64 | { "none", IntStream.empty() }, |
| 65 | { "index 0", IntStream.of(0) }, |
| 66 | { "index 255", IntStream.of(255) }, |
| 67 | { "every bit", IntStream.range(0, 255) }, |
henryjen | 8de8243 | 2013-06-12 14:07:14 -0700 | [diff] [blame] | 68 | { "step 2", IntStream.range(0, 255).map(f -> f * 2) }, |
| 69 | { "step 3", IntStream.range(0, 255).map(f -> f * 3) }, |
| 70 | { "step 5", IntStream.range(0, 255).map(f -> f * 5) }, |
| 71 | { "step 7", IntStream.range(0, 255).map(f -> f * 7) }, |
mduigou | 072224c | 2013-05-02 09:18:56 -0700 | [diff] [blame] | 72 | { "1, 10, 100, 1000", IntStream.of(1, 10, 100, 1000) }, |
| 73 | { "25 fibs", IntStream.generate(new Fibs()).limit(25) } |
| 74 | }; |
| 75 | |
| 76 | @DataProvider(name = "cases") |
| 77 | public static Object[][] produceCases() { |
| 78 | return testcases; |
| 79 | } |
| 80 | |
| 81 | @Test |
| 82 | public void testFibs() { |
| 83 | Fibs f = new Fibs(); |
| 84 | assertEquals(0, f.getAsInt()); |
| 85 | assertEquals(1, f.getAsInt()); |
| 86 | assertEquals(1, f.getAsInt()); |
| 87 | assertEquals(2, f.getAsInt()); |
| 88 | assertEquals(3, f.getAsInt()); |
| 89 | assertEquals(5, f.getAsInt()); |
| 90 | assertEquals(8, f.getAsInt()); |
| 91 | assertEquals(13, f.getAsInt()); |
| 92 | assertEquals(987, Fibs.fibs(16)); |
| 93 | } |
| 94 | |
| 95 | @Test(dataProvider = "cases") |
| 96 | public void testBitsetStream(String name, IntStream data) { |
| 97 | BitSet bs = new BitSet(); |
| 98 | long setBits = data.distinct() |
| 99 | .peek(i -> bs.set(i)) |
| 100 | .count(); |
| 101 | |
| 102 | assertEquals(bs.cardinality(), setBits); |
| 103 | assertEquals(bs.cardinality(), bs.stream().reduce(0, (s, i) -> s+1)); |
| 104 | |
| 105 | PrimitiveIterator.OfInt it = bs.stream().iterator(); |
| 106 | for (int i = bs.nextSetBit(0); i >= 0; i = bs.nextSetBit(i+1)) { |
| 107 | assertTrue(it.hasNext()); |
| 108 | assertEquals(it.nextInt(), i); |
| 109 | } |
| 110 | assertFalse(it.hasNext()); |
| 111 | } |
| 112 | |
| 113 | @Test |
| 114 | public void testRandomStream() { |
| 115 | final int size = 1024 * 1024; |
| 116 | final int[] seeds = { |
| 117 | 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, |
| 118 | 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97}; |
| 119 | final byte[] bytes = new byte[size]; |
| 120 | for (int seed : seeds) { |
| 121 | final Random random = new Random(seed); |
| 122 | random.nextBytes(bytes); |
| 123 | final BitSet bitSet = BitSet.valueOf(bytes); |
| 124 | final int cardinality = bitSet.cardinality(); |
| 125 | final IntStream stream = bitSet.stream(); |
| 126 | final int[] array = stream.toArray(); |
| 127 | assertEquals(array.length, cardinality); |
| 128 | int nextSetBit = -1; |
| 129 | for (int i=0; i < cardinality; i++) { |
| 130 | nextSetBit = bitSet.nextSetBit(nextSetBit + 1); |
| 131 | assertEquals(array[i], nextSetBit); |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | } |