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.ArrayList; 27 import java.util.Arrays; 28 import java.util.Collections; 29 import java.util.Comparator; 30 import java.util.HashSet; 31 import java.util.Iterator; 32 import java.util.List; 33 import java.util.Objects; 34 import java.util.Set; 35 36 import static org.testng.Assert.assertEquals; 37 import static org.testng.Assert.assertTrue; 38 import static org.testng.Assert.fail; 39 40 /** 41 * @library 42 * CollectionAssert -- assertion methods for lambda test cases 43 */ 44 public class CollectionAsserts { 45 46 public static void assertCountSum(Iterable<? super Integer> it, int count, int sum) { 47 assertCountSum(it.iterator(), count, sum); 48 } 49 50 public static void assertCountSum(Iterator<? super Integer> it, int count, int sum) { 51 int c = 0; 52 int s = 0; 53 while (it.hasNext()) { 54 int i = (Integer) it.next(); 55 c++; 56 s += i; 57 } 58 59 assertEquals(c, count); 60 assertEquals(s, sum); 61 } 62 63 public static void assertConcat(Iterator<Character> it, String result) { 64 StringBuilder sb = new StringBuilder(); 65 while (it.hasNext()) { 66 sb.append(it.next()); 67 } 68 69 assertEquals(result, sb.toString()); 70 } 71 72 public static<T extends Comparable<? super T>> void assertSorted(Iterator<T> i) { 73 if (!i.hasNext()) 74 return; 75 T last = i.next(); 76 while (i.hasNext()) { 77 T t = i.next(); 78 assertTrue(last.compareTo(t) <= 0); 79 assertTrue(t.compareTo(last) >= 0); 80 last = t; 81 } 82 } 83 84 public static<T> void assertSorted(Iterator<T> i, Comparator<? super T> comp) { 85 if (!i.hasNext()) 86 return; 87 T last = i.next(); 88 while (i.hasNext()) { 89 T t = i.next(); 90 assertTrue(comp.compare(last, t) <= 0); 91 assertTrue(comp.compare(t, last) >= 0); 92 last = t; 93 } 94 } 95 96 public static<T extends Comparable<? super T>> void assertSorted(Iterable<T> iter) { 97 assertSorted(iter.iterator()); 98 } 99 100 public static<T> void assertSorted(Iterable<T> iter, Comparator<? super T> comp) { 101 assertSorted(iter.iterator(), comp); 102 } 103 104 public static <T> void assertUnique(Iterable<T> iter) { 105 assertUnique(iter.iterator()); 106 } 107 108 public static<T> void assertUnique(Iterator<T> iter) { 109 if (!iter.hasNext()) { 110 return; 111 } 112 113 Set<T> uniq = new HashSet<>(); 114 while(iter.hasNext()) { 115 T each = iter.next(); 116 assertTrue(!uniq.contains(each)); 117 uniq.add(each); 118 } 119 } 120 121 public static<T> void assertContents(Iterable<T> actual, Iterable<T> expected) { 122 assertContents(actual.iterator(), expected.iterator()); 123 } 124 125 public static<T> void assertContents(Iterator<T> actual, Iterator<T> expected) { 126 List<T> history = new ArrayList<>(); 127 128 while (expected.hasNext()) { 129 if (!actual.hasNext()) { 130 List<T> expectedData = new ArrayList<>(history); 131 while (expected.hasNext()) 132 expectedData.add(expected.next()); 133 fail(String.format("Premature end of data; expected=%s, found=%s", expectedData, history)); 134 } 135 T a = actual.next(); 136 T e = expected.next(); 137 history.add(a); 138 139 if (!Objects.equals(a, e)) 140 fail(String.format("Data mismatch; preceding=%s, nextExpected=%s, nextFound=%s", history, e, a)); 141 } 142 if (actual.hasNext()) { 143 List<T> rest = new ArrayList<>(); 144 while (actual.hasNext()) 145 rest.add(actual.next()); 146 fail(String.format("Unexpected data %s after %s", rest, history)); 147 } 148 } 149 150 @SafeVarargs 151 @SuppressWarnings("varargs") 152 public static<T> void assertContents(Iterator<T> actual, T... expected) { 153 assertContents(actual, Arrays.asList(expected).iterator()); 154 } 155 156 public static <T> boolean equalsContentsUnordered(Iterable<T> a, Iterable<T> b) { 157 Set<T> sa = new HashSet<>(); 158 for (T t : a) { 159 sa.add(t); 160 } 161 162 Set<T> sb = new HashSet<>(); 163 for (T t : b) { 164 sb.add(t); 165 } 166 167 return Objects.equals(sa, sb); 168 } 169 170 public static<T extends Comparable<? super T>> void assertContentsUnordered(Iterable<T> actual, Iterable<T> expected) { 171 ArrayList<T> one = new ArrayList<>(); 172 for (T t : actual) 173 one.add(t); 174 ArrayList<T> two = new ArrayList<>(); 175 for (T t : expected) 176 two.add(t); 177 Collections.sort(one); 178 Collections.sort(two); 179 assertContents(one, two); 180 } 181 182 static <T> void assertSplitContents(Iterable<Iterable<T>> splits, Iterable<T> list) { 183 Iterator<Iterable<T>> mI = splits.iterator(); 184 Iterator<T> pI = null; 185 Iterator<T> lI = list.iterator(); 186 187 while (lI.hasNext()) { 188 if (pI == null) 189 pI = mI.next().iterator(); 190 while (!pI.hasNext()) { 191 if (!mI.hasNext()) { 192 break; 193 } 194 else { 195 pI = mI.next().iterator(); 196 } 197 } 198 assertTrue(pI.hasNext()); 199 T pT = pI.next(); 200 T lT = lI.next(); 201 assertEquals(pT, lT); 202 } 203 204 if (pI != null) { 205 assertTrue(!pI.hasNext()); 206 } 207 208 while(mI.hasNext()) { 209 pI = mI.next().iterator(); 210 assertTrue(!pI.hasNext()); 211 } 212 } 213 }