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