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

Print this page
rev 7485 : 8009736: Comparator API cleanup
Reviewed-by:
Contributed-by: henry.jen@oracle.com


  23 package org.openjdk.tests.java.util.stream;
  24 
  25 import org.testng.annotations.Test;
  26 
  27 import java.util.*;
  28 import java.util.Spliterators;
  29 import java.util.stream.*;
  30 
  31 import static java.util.stream.LambdaTestHelpers.*;
  32 
  33 /**
  34  * SortedOpTest
  35  *
  36  * @author Brian Goetz
  37  */
  38 @Test
  39 public class SortedOpTest extends OpTestCase {
  40     public void testSorted() {
  41         assertCountSum(countTo(0).stream().sorted(), 0, 0);
  42         assertCountSum(countTo(10).stream().sorted(), 10, 55);
  43         assertCountSum(countTo(10).stream().sorted(cInteger.reverseOrder()), 10, 55);
  44 
  45         List<Integer> to10 = countTo(10);
  46         assertSorted(to10.stream().sorted(cInteger.reverseOrder()).iterator(), cInteger.reverseOrder());
  47 
  48         Collections.reverse(to10);
  49         assertSorted(to10.stream().sorted().iterator());
  50 
  51         Spliterator<Integer> s = to10.stream().sorted().spliterator();
  52         assertTrue(s.hasCharacteristics(Spliterator.SORTED));
  53 
  54         s = to10.stream().sorted(cInteger.reverseOrder()).spliterator();
  55         assertFalse(s.hasCharacteristics(Spliterator.SORTED));
  56     }
  57 
  58     @Test(groups = { "serialization-hostile" })
  59     public void testSequentialShortCircuitTerminal() {
  60         // The sorted op for sequential evaluation will buffer all elements when accepting
  61         // then at the end sort those elements and push those elements downstream
  62 
  63         List<Integer> l = Arrays.asList(5, 4, 3, 2, 1);
  64 
  65         // Find
  66         assertEquals(l.stream().sorted().findFirst(), Optional.of(1));
  67         assertEquals(l.stream().sorted().findAny(), Optional.of(1));
  68         assertEquals(unknownSizeStream(l).sorted().findFirst(), Optional.of(1));
  69         assertEquals(unknownSizeStream(l).sorted().findAny(), Optional.of(1));
  70 
  71         // Match
  72         assertEquals(l.stream().sorted().anyMatch(i -> i == 2), true);
  73         assertEquals(l.stream().sorted().noneMatch(i -> i == 2), false);
  74         assertEquals(l.stream().sorted().allMatch(i -> i == 2), false);
  75         assertEquals(unknownSizeStream(l).sorted().anyMatch(i -> i == 2), true);
  76         assertEquals(unknownSizeStream(l).sorted().noneMatch(i -> i == 2), false);
  77         assertEquals(unknownSizeStream(l).sorted().allMatch(i -> i == 2), false);
  78     }
  79 
  80     private <T> Stream<T> unknownSizeStream(List<T> l) {
  81         return StreamSupport.stream(Spliterators.spliteratorUnknownSize(l.iterator(), 0));
  82     }
  83 
  84     @Test(dataProvider = "StreamTestData<Integer>", dataProviderClass = StreamTestDataProvider.class)
  85     public void testOps(String name, TestData.OfRef<Integer> data) {
  86         Collection<Integer> result = exerciseOpsInt(data, Stream::sorted, IntStream::sorted, LongStream::sorted, DoubleStream::sorted);
  87         assertSorted(result.iterator());
  88         assertContentsUnordered(data, result);
  89 
  90         result = exerciseOps(data, s -> s.sorted(cInteger.reverseOrder()));
  91         assertSorted(result.iterator(), cInteger.reverseOrder());
  92         assertContentsUnordered(data, result);
  93     }
  94 
  95     @Test(dataProvider = "StreamTestData<Integer>", dataProviderClass = StreamTestDataProvider.class)
  96     public void testSortSort(String name, TestData.OfRef<Integer> data) {
  97         // For parallel cases ensure the size is known
  98         Collection<Integer> result = withData(data)
  99                 .stream(s -> s.sorted().sorted(),
 100                         new CollectorOps.TestParallelSizedOp<Integer>())
 101                 .exercise();
 102 
 103         assertSorted(result);
 104         assertContentsUnordered(data, result);
 105 
 106         result = withData(data)
 107                 .stream(s -> s.sorted(cInteger.reverseOrder()).sorted(cInteger.reverseOrder()),
 108                         new CollectorOps.TestParallelSizedOp<Integer>())
 109                 .exercise();
 110 
 111         assertSorted(result, cInteger.reverseOrder());
 112         assertContentsUnordered(data, result);
 113 
 114         result = withData(data)
 115                 .stream(s -> s.sorted().sorted(cInteger.reverseOrder()),
 116                         new CollectorOps.TestParallelSizedOp<Integer>())
 117                 .exercise();
 118 
 119         assertSorted(result, cInteger.reverseOrder());
 120         assertContentsUnordered(data, result);
 121 
 122         result = withData(data)
 123                 .stream(s -> s.sorted(cInteger.reverseOrder()).sorted(),
 124                         new CollectorOps.TestParallelSizedOp<Integer>())
 125                 .exercise();
 126 
 127         assertSorted(result);
 128         assertContentsUnordered(data, result);
 129     }
 130 
 131     //
 132 
 133     @Test(groups = { "serialization-hostile" })
 134     public void testIntSequentialShortCircuitTerminal() {
 135         int[] a = new int[]{5, 4, 3, 2, 1};
 136 
 137         // Find
 138         assertEquals(Arrays.stream(a).sorted().findFirst(), OptionalInt.of(1));
 139         assertEquals(Arrays.stream(a).sorted().findAny(), OptionalInt.of(1));
 140         assertEquals(unknownSizeIntStream(a).sorted().findFirst(), OptionalInt.of(1));
 141         assertEquals(unknownSizeIntStream(a).sorted().findAny(), OptionalInt.of(1));
 142 
 143         // Match




  23 package org.openjdk.tests.java.util.stream;
  24 
  25 import org.testng.annotations.Test;
  26 
  27 import java.util.*;
  28 import java.util.Spliterators;
  29 import java.util.stream.*;
  30 
  31 import static java.util.stream.LambdaTestHelpers.*;
  32 
  33 /**
  34  * SortedOpTest
  35  *
  36  * @author Brian Goetz
  37  */
  38 @Test
  39 public class SortedOpTest extends OpTestCase {
  40     public void testSorted() {
  41         assertCountSum(countTo(0).stream().sorted(), 0, 0);
  42         assertCountSum(countTo(10).stream().sorted(), 10, 55);
  43         assertCountSum(countTo(10).stream().sorted(cInteger.reversed()), 10, 55);
  44 
  45         List<Integer> to10 = countTo(10);
  46         assertSorted(to10.stream().sorted(cInteger.reversed()).iterator(), cInteger.reversed());
  47 
  48         Collections.reverse(to10);
  49         assertSorted(to10.stream().sorted().iterator());
  50 
  51         Spliterator<Integer> s = to10.stream().sorted().spliterator();
  52         assertTrue(s.hasCharacteristics(Spliterator.SORTED));
  53 
  54         s = to10.stream().sorted(cInteger.reversed()).spliterator();
  55         assertFalse(s.hasCharacteristics(Spliterator.SORTED));
  56     }
  57 
  58     @Test(groups = { "serialization-hostile" })
  59     public void testSequentialShortCircuitTerminal() {
  60         // The sorted op for sequential evaluation will buffer all elements when accepting
  61         // then at the end sort those elements and push those elements downstream
  62 
  63         List<Integer> l = Arrays.asList(5, 4, 3, 2, 1);
  64 
  65         // Find
  66         assertEquals(l.stream().sorted().findFirst(), Optional.of(1));
  67         assertEquals(l.stream().sorted().findAny(), Optional.of(1));
  68         assertEquals(unknownSizeStream(l).sorted().findFirst(), Optional.of(1));
  69         assertEquals(unknownSizeStream(l).sorted().findAny(), Optional.of(1));
  70 
  71         // Match
  72         assertEquals(l.stream().sorted().anyMatch(i -> i == 2), true);
  73         assertEquals(l.stream().sorted().noneMatch(i -> i == 2), false);
  74         assertEquals(l.stream().sorted().allMatch(i -> i == 2), false);
  75         assertEquals(unknownSizeStream(l).sorted().anyMatch(i -> i == 2), true);
  76         assertEquals(unknownSizeStream(l).sorted().noneMatch(i -> i == 2), false);
  77         assertEquals(unknownSizeStream(l).sorted().allMatch(i -> i == 2), false);
  78     }
  79 
  80     private <T> Stream<T> unknownSizeStream(List<T> l) {
  81         return StreamSupport.stream(Spliterators.spliteratorUnknownSize(l.iterator(), 0));
  82     }
  83 
  84     @Test(dataProvider = "StreamTestData<Integer>", dataProviderClass = StreamTestDataProvider.class)
  85     public void testOps(String name, TestData.OfRef<Integer> data) {
  86         Collection<Integer> result = exerciseOpsInt(data, Stream::sorted, IntStream::sorted, LongStream::sorted, DoubleStream::sorted);
  87         assertSorted(result.iterator());
  88         assertContentsUnordered(data, result);
  89 
  90         result = exerciseOps(data, s -> s.sorted(cInteger.reversed()));
  91         assertSorted(result.iterator(), cInteger.reversed());
  92         assertContentsUnordered(data, result);
  93     }
  94 
  95     @Test(dataProvider = "StreamTestData<Integer>", dataProviderClass = StreamTestDataProvider.class)
  96     public void testSortSort(String name, TestData.OfRef<Integer> data) {
  97         // For parallel cases ensure the size is known
  98         Collection<Integer> result = withData(data)
  99                 .stream(s -> s.sorted().sorted(),
 100                         new CollectorOps.TestParallelSizedOp<Integer>())
 101                 .exercise();
 102 
 103         assertSorted(result);
 104         assertContentsUnordered(data, result);
 105 
 106         result = withData(data)
 107                 .stream(s -> s.sorted(cInteger.reversed()).sorted(cInteger.reversed()),
 108                         new CollectorOps.TestParallelSizedOp<Integer>())
 109                 .exercise();
 110 
 111         assertSorted(result, cInteger.reversed());
 112         assertContentsUnordered(data, result);
 113 
 114         result = withData(data)
 115                 .stream(s -> s.sorted().sorted(cInteger.reversed()),
 116                         new CollectorOps.TestParallelSizedOp<Integer>())
 117                 .exercise();
 118 
 119         assertSorted(result, cInteger.reversed());
 120         assertContentsUnordered(data, result);
 121 
 122         result = withData(data)
 123                 .stream(s -> s.sorted(cInteger.reversed()).sorted(),
 124                         new CollectorOps.TestParallelSizedOp<Integer>())
 125                 .exercise();
 126 
 127         assertSorted(result);
 128         assertContentsUnordered(data, result);
 129     }
 130 
 131     //
 132 
 133     @Test(groups = { "serialization-hostile" })
 134     public void testIntSequentialShortCircuitTerminal() {
 135         int[] a = new int[]{5, 4, 3, 2, 1};
 136 
 137         // Find
 138         assertEquals(Arrays.stream(a).sorted().findFirst(), OptionalInt.of(1));
 139         assertEquals(Arrays.stream(a).sorted().findAny(), OptionalInt.of(1));
 140         assertEquals(unknownSizeIntStream(a).sorted().findFirst(), OptionalInt.of(1));
 141         assertEquals(unknownSizeIntStream(a).sorted().findAny(), OptionalInt.of(1));
 142 
 143         // Match