• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 package test.java.util.Collection.testlibrary;
24 
25 import java.util.ArrayList;
26 import java.util.Arrays;
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 
CollectionAsserts()44     private CollectionAsserts() {
45         // no instances
46     }
47 
assertCountSum(Iterable<? super Integer> it, int count, int sum)48     public static void assertCountSum(Iterable<? super Integer> it, int count, int sum) {
49         assertCountSum(it.iterator(), count, sum);
50     }
51 
assertCountSum(Iterator<? super Integer> it, int count, int sum)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 
assertConcat(Iterator<Character> it, String result)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 
assertSorted(Iterator<T> i)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 
assertSorted(Iterator<T> i, Comparator<? super T> comp)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 
assertSorted(Iterable<T> iter)98     public static<T extends Comparable<? super T>> void assertSorted(Iterable<T> iter) {
99         assertSorted(iter.iterator());
100     }
101 
assertSorted(Iterable<T> iter, Comparator<? super T> comp)102     public static<T> void assertSorted(Iterable<T> iter, Comparator<? super T> comp) {
103         assertSorted(iter.iterator(), comp);
104     }
105 
assertUnique(Iterable<T> iter)106     public static <T> void assertUnique(Iterable<T> iter) {
107         assertUnique(iter.iterator());
108     }
109 
assertUnique(Iterator<T> iter)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 
assertContents(Iterable<T> actual, Iterable<T> expected)123     public static<T> void assertContents(Iterable<T> actual, Iterable<T> expected) {
124         assertContents(actual, expected, null);
125     }
126 
assertContents(Iterable<T> actual, Iterable<T> expected, String msg)127     public static<T> void assertContents(Iterable<T> actual, Iterable<T> expected, String msg) {
128         assertContents(actual.iterator(), expected.iterator(), msg);
129     }
130 
assertContents(Iterator<T> actual, Iterator<T> expected)131     public static<T> void assertContents(Iterator<T> actual, Iterator<T> expected) {
132         assertContents(actual, expected, null);
133     }
134 
assertContents(Iterator<T> actual, Iterator<T> expected, String msg)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")
assertContents(Iterator<T> actual, T... expected)165     public static<T> void assertContents(Iterator<T> actual, T... expected) {
166         assertContents(actual, Arrays.asList(expected).iterator());
167     }
168 
assertContentsUnordered(Iterable<T> actual, Iterable<T> expected)169     public static<T extends Comparable<? super T>> void assertContentsUnordered(Iterable<T> actual, Iterable<T> expected) {
170         assertContentsUnordered(actual, expected, null);
171     }
172 
assertContentsUnordered(Iterable<T> actual, Iterable<T> expected, String msg)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 
assertSplitContents(Iterable<Iterable<T>> splits, Iterable<T> list)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();
204             T lT = lI.next();
205             assertEquals(pT, lT);
206         }
207 
208         if (pI != null) {
209             assertTrue(!pI.hasNext());
210         }
211 
212         while (mI.hasNext()) {
213             pI = mI.next().iterator();
214             assertTrue(!pI.hasNext());
215         }
216     }
217 }
218