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 package java.util.stream; 24 25 import org.testng.annotations.Test; 26 27 import java.util.*; 28 import java.util.stream.Stream; 29 import java.util.stream.StreamOpFlag; 30 import java.util.stream.Streams; 31 32 import static java.util.stream.StreamOpFlag.*; 33 import static org.testng.Assert.assertEquals; 34 import static org.testng.Assert.assertTrue; 35 36 /** 37 * StreamFlagsTest 38 * 39 * @author Brian Goetz 40 */ 41 @Test 42 public class StreamFlagsTest { 43 Stream<String> arrayList = new ArrayList<String>().stream(); 44 Stream<String> linkedList = new LinkedList<String>().stream(); 45 Stream<String> hashSet = new HashSet<String>().stream(); 46 Stream<String> treeSet = new TreeSet<String>().stream(); 47 Stream<String> linkedHashSet = new LinkedHashSet<String>().stream(); 48 Stream<String> repeat = Stream.generate(() -> ""); 49 50 Stream<?>[] streams = { arrayList, linkedList, hashSet, treeSet, linkedHashSet, repeat }; 51 assertFlags(int value, EnumSet<StreamOpFlag> setFlags, EnumSet<StreamOpFlag> clearFlags)52 private void assertFlags(int value, EnumSet<StreamOpFlag> setFlags, EnumSet<StreamOpFlag> clearFlags) { 53 for (StreamOpFlag flag : setFlags) 54 assertTrue(flag.isKnown(value)); 55 for (StreamOpFlag flag : clearFlags) 56 assertTrue(!flag.isKnown(value)); 57 } 58 testBaseStreams()59 public void testBaseStreams() { 60 Stream<String> arrayList = new ArrayList<String>().stream(); 61 Stream<String> linkedList = new LinkedList<String>().stream(); 62 Stream<String> hashSet = new HashSet<String>().stream(); 63 Stream<String> treeSet = new TreeSet<String>().stream(); 64 Stream<String> linkedHashSet = new LinkedHashSet<String>().stream(); 65 Stream<String> repeat = Stream.generate(() -> ""); 66 67 assertFlags(OpTestCase.getStreamFlags(arrayList), 68 EnumSet.of(ORDERED, SIZED), 69 EnumSet.of(DISTINCT, SORTED, SHORT_CIRCUIT)); 70 assertFlags(OpTestCase.getStreamFlags(linkedList), 71 EnumSet.of(ORDERED, SIZED), 72 EnumSet.of(DISTINCT, SORTED, SHORT_CIRCUIT)); 73 assertFlags(OpTestCase.getStreamFlags(hashSet), 74 EnumSet.of(SIZED, DISTINCT), 75 EnumSet.of(ORDERED, SORTED, SHORT_CIRCUIT)); 76 assertFlags(OpTestCase.getStreamFlags(treeSet), 77 EnumSet.of(ORDERED, SIZED, DISTINCT, SORTED), 78 EnumSet.of(SHORT_CIRCUIT)); 79 assertFlags(OpTestCase.getStreamFlags(linkedHashSet), 80 EnumSet.of(ORDERED, DISTINCT, SIZED), 81 EnumSet.of(SORTED, SHORT_CIRCUIT)); 82 assertFlags(OpTestCase.getStreamFlags(repeat), 83 EnumSet.noneOf(StreamOpFlag.class), 84 EnumSet.of(DISTINCT, SORTED, SHORT_CIRCUIT)); 85 } 86 testFilter()87 public void testFilter() { 88 for (Stream<?> s : streams) { 89 int baseFlags = OpTestCase.getStreamFlags(s); 90 int filteredFlags = OpTestCase.getStreamFlags(s.filter((Object e) -> true)); 91 int expectedFlags = baseFlags & ~SIZED.set(); 92 93 assertEquals(filteredFlags, expectedFlags); 94 } 95 } 96 } 97