< prev index next >

test/java/util/stream/test/org/openjdk/tests/java/util/stream/IntPrimitiveOpsTests.java

Print this page

        

*** 26,42 **** --- 26,50 ---- import org.testng.annotations.Test; import java.util.Arrays; import java.util.List; import java.util.Random; + import java.util.Spliterator; + import java.util.TreeSet; import java.util.concurrent.atomic.AtomicInteger; import java.util.function.IntConsumer; import java.util.stream.Collectors; import java.util.stream.IntStream; import static org.testng.Assert.assertEquals; + import static org.testng.Assert.assertFalse; + import static org.testng.Assert.assertTrue; + /** + * @test + * @bug 8153293 + */ @Test public class IntPrimitiveOpsTests { public void testSum() { long sum = IntStream.range(1, 10).filter(i -> i % 2 == 0).sum();
*** 83,92 **** --- 91,123 ---- public void testUnBox() { long sum = Arrays.asList(1, 2, 3, 4, 5).stream().mapToInt(i -> (int) i).sum(); assertEquals(sum, 15); } + public void testFlags() { + assertTrue(IntStream.range(1, 10).boxed().spliterator() + .hasCharacteristics(Spliterator.SORTED | Spliterator.DISTINCT)); + assertFalse(IntStream.of(1, 10).boxed().spliterator() + .hasCharacteristics(Spliterator.SORTED)); + assertFalse(IntStream.of(1, 10).boxed().spliterator() + .hasCharacteristics(Spliterator.DISTINCT)); + + assertTrue(IntStream.range(1, 10).asLongStream().spliterator() + .hasCharacteristics(Spliterator.SORTED | Spliterator.DISTINCT)); + assertFalse(IntStream.of(1, 10).asLongStream().spliterator() + .hasCharacteristics(Spliterator.SORTED)); + assertFalse(IntStream.of(1, 10).asLongStream().spliterator() + .hasCharacteristics(Spliterator.DISTINCT)); + + assertTrue(IntStream.range(1, 10).asDoubleStream().spliterator() + .hasCharacteristics(Spliterator.SORTED | Spliterator.DISTINCT)); + assertFalse(IntStream.of(1, 10).asDoubleStream().spliterator() + .hasCharacteristics(Spliterator.SORTED)); + assertFalse(IntStream.of(1, 10).asDoubleStream().spliterator() + .hasCharacteristics(Spliterator.DISTINCT)); + } + public void testToArray() { { int[] array = IntStream.range(1, 10).map(i -> i * 2).toArray(); assertEquals(array, new int[]{2, 4, 6, 8, 10, 12, 14, 16, 18}); }
*** 113,122 **** --- 144,182 ---- int[] array = Arrays.stream(content).parallel().sorted().toArray(); assertEquals(array, sortedContent); } } + public void testSortDistinct() { + { + int[] range = IntStream.range(0, 10).toArray(); + + assertEquals(IntStream.range(0, 10).sorted().distinct().toArray(), range); + assertEquals(IntStream.range(0, 10).parallel().sorted().distinct().toArray(), range); + + int[] data = {5, 3, 1, 1, 5, 3, 9, 2, 9, 1, 0, 8}; + int[] expected = {0, 1, 2, 3, 5, 8, 9}; + assertEquals(IntStream.of(data).sorted().distinct().toArray(), expected); + assertEquals(IntStream.of(data).parallel().sorted().distinct().toArray(), expected); + } + + { + int[] input = new Random().ints(100, -10, 10).map(x -> x+Integer.MAX_VALUE).toArray(); + TreeSet<Long> longs = new TreeSet<>(); + for(int i : input) longs.add((long)i); + long[] expectedLongs = longs.stream().mapToLong(Long::longValue).toArray(); + assertEquals(IntStream.of(input).sorted().asLongStream().sorted().distinct().toArray(), + expectedLongs); + + TreeSet<Double> doubles = new TreeSet<>(); + for(int i : input) doubles.add((double)i); + double[] expectedDoubles = doubles.stream().mapToDouble(Double::doubleValue).toArray(); + assertEquals(IntStream.of(input).sorted().distinct().asDoubleStream() + .sorted().distinct().toArray(), expectedDoubles); + } + } + public void testSortSort() { Random r = new Random(); int[] content = IntStream.generate(() -> r.nextInt(100)).limit(10).toArray(); int[] sortedContent = content.clone();
< prev index next >