test/java/util/Collection/testlibrary/CollectionAsserts.java

Print this page
rev 7932 : 8021591: Additional explicit null checks
Reviewed-by: psandoz, martin, alanb


  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()) {


 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();


  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()) {


 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 extends Comparable<? super T>> void assertContentsUnordered(Iterable<T> actual, Iterable<T> expected) {
 170         assertContentsUnordered(actual, expected, null);


 171     }
 172 
 173     public static<T extends Comparable<? super T>> void assertContentsUnordered(Iterable<T> actual, Iterable<T> expected, String msg) {
 174         List<T> allExpected = new ArrayList<>();
 175         for (T t : expected) {
 176             allExpected.add(t);
 177         }
 178 
 179         for (T t : actual) {
 180             assertTrue(allExpected.remove(t), msg + " element '" + String.valueOf(t) + "' not found");
 181         }
 182 
 183         assertTrue(allExpected.isEmpty(), msg + "expected contained additional elements");









 184     }
 185 
 186     static <T> void assertSplitContents(Iterable<Iterable<T>> splits, Iterable<T> list) {
 187         Iterator<Iterable<T>> mI = splits.iterator();
 188         Iterator<T> pI = null;
 189         Iterator<T> lI = list.iterator();
 190 
 191         while (lI.hasNext()) {
 192             if (pI == null)
 193                 pI = mI.next().iterator();
 194             while (!pI.hasNext()) {
 195                 if (!mI.hasNext()) {
 196                     break;
 197                 }
 198                 else {
 199                     pI = mI.next().iterator();
 200                 }
 201             }
 202             assertTrue(pI.hasNext());
 203             T pT = pI.next();