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