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