1 /* 2 * Copyright (c) 1997, 2010, 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. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 import java.util.*; 27 import java.util.Iterator; 28 import java.util.function.*; 29 30 import static org.testng.Assert.assertEquals; 31 import static org.testng.Assert.assertTrue; 32 import static org.testng.Assert.fail; 33 34 /** 35 * @library 36 * CollectionAssert -- assertion methods for lambda test cases 37 */ 38 public class CollectionAsserts { 39 40 public static void assertCountSum(Iterable<? super Integer> it, int count, int sum) { 41 assertCountSum(it.iterator(), count, sum); 42 } 43 44 public static void assertCountSum(Iterator<? super Integer> it, int count, int sum) { 45 int c = 0; 46 int s = 0; 47 while (it.hasNext()) { 48 int i = (Integer) it.next(); 49 c++; 50 s += i; 51 } 52 53 assertEquals(c, count); 54 assertEquals(s, sum); 55 } 56 57 public static void assertConcat(Iterator<Character> it, String result) { 58 StringBuilder sb = new StringBuilder(); 59 while (it.hasNext()) { 60 sb.append(it.next()); 61 } 62 63 assertEquals(result, sb.toString()); 64 } 65 66 public static<T extends Comparable<? super T>> void assertSorted(Iterator<T> i) { 67 if (!i.hasNext()) 68 return; 69 T last = i.next(); 70 while (i.hasNext()) { 71 T t = i.next(); 72 assertTrue(last.compareTo(t) <= 0); 73 assertTrue(t.compareTo(last) >= 0); 74 last = t; 75 } 76 } 77 78 public static<T> void assertSorted(Iterator<T> i, Comparator<? super T> comp) { 79 if (!i.hasNext()) 80 return; 81 T last = i.next(); 82 while (i.hasNext()) { 83 T t = i.next(); 84 assertTrue(comp.compare(last, t) <= 0); 85 assertTrue(comp.compare(t, last) >= 0); 86 last = t; 87 } 88 } 89 90 public static<T extends Comparable<? super T>> void assertSorted(Iterable<T> iter) { 91 assertSorted(iter.iterator()); 92 } 93 94 public static<T> void assertSorted(Iterable<T> iter, Comparator<? super T> comp) { 95 assertSorted(iter.iterator(), comp); 96 } 97 98 public static <T> void assertUnique(Iterable<T> iter) { 99 assertUnique(iter.iterator()); 100 } 101 102 public static<T> void assertUnique(Iterator<T> iter) { 103 if (!iter.hasNext()) { 104 return; 105 } 106 107 Set<T> uniq = new HashSet<>(); 108 while(iter.hasNext()) { 109 T each = iter.next(); 110 assertTrue(!uniq.contains(each)); 111 uniq.add(each); 112 } 113 } 114 115 public static<T> void assertContents(Iterable<T> actual, Iterable<T> expected) { 116 assertContents(actual.iterator(), expected.iterator()); 117 } 118 119 public static<T> void assertContents(Iterator<T> actual, Iterator<T> expected) { 120 List<T> history = new ArrayList<>(); 121 122 while (expected.hasNext()) { 123 if (!actual.hasNext()) { 124 List<T> expectedData = new ArrayList<>(history); 125 while (expected.hasNext()) 126 expectedData.add(expected.next()); 127 fail(String.format("Premature end of data; expected=%s, found=%s", expectedData, history)); 128 } 129 T a = actual.next(); 130 T e = expected.next(); 131 history.add(a); 132 133 if (!Objects.equals(a, e)) 134 fail(String.format("Data mismatch; preceding=%s, nextExpected=%s, nextFound=%s", history, e, a)); 135 } 136 if (actual.hasNext()) { 137 List<T> rest = new ArrayList<>(); 138 while (actual.hasNext()) 139 rest.add(actual.next()); 140 fail(String.format("Unexpected data %s after %s", rest, history)); 141 } 142 } 143 144 @SafeVarargs 145 @SuppressWarnings("varargs") 146 public static<T> void assertContents(Iterator<T> actual, T... expected) { 147 assertContents(actual, Arrays.asList(expected).iterator()); 148 } 149 150 public static <T> boolean equalsContentsUnordered(Iterable<T> a, Iterable<T> b) { 151 Set<T> sa = new HashSet<>(); 152 for (T t : a) { 153 sa.add(t); 154 } 155 156 Set<T> sb = new HashSet<>(); 157 for (T t : b) { 158 sb.add(t); 159 } 160 161 return Objects.equals(sa, sb); 162 } 163 164 public static<T extends Comparable<? super T>> void assertContentsUnordered(Iterable<T> actual, Iterable<T> expected) { 165 ArrayList<T> one = new ArrayList<>(); 166 for (T t : actual) 167 one.add(t); 168 ArrayList<T> two = new ArrayList<>(); 169 for (T t : expected) 170 two.add(t); 171 Collections.sort(one); 172 Collections.sort(two); 173 assertContents(one, two); 174 } 175 176 static <T> void assertSplitContents(Iterable<Iterable<T>> splits, Iterable<T> list) { 177 Iterator<Iterable<T>> mI = splits.iterator(); 178 Iterator<T> pI = null; 179 Iterator<T> lI = list.iterator(); 180 181 while (lI.hasNext()) { 182 if (pI == null) 183 pI = mI.next().iterator(); 184 while (!pI.hasNext()) { 185 if (!mI.hasNext()) { 186 break; 187 } 188 else { 189 pI = mI.next().iterator(); 190 } 191 } 192 assertTrue(pI.hasNext()); 193 T pT = pI.next(); 194 T lT = lI.next(); 195 assertEquals(pT, lT); 196 } 197 198 if (pI != null) { 199 assertTrue(!pI.hasNext()); 200 } 201 202 while(mI.hasNext()) { 203 pI = mI.next().iterator(); 204 assertTrue(!pI.hasNext()); 205 } 206 } 207 }