summaryrefslogtreecommitdiff
path: root/src/test/java/bjc/TestUtils.java
blob: a8cbf43c0abbd4d4b0128896f91a105ecbb1fca5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package bjc;

import java.util.*;

import static org.junit.Assert.*;

/**
 * Utility methods for doing testing
 * 
 * @author Ben Culkin
 */
public class TestUtils {
	/**
	 * Assert an iterator provides a particular sequence of values.
	 * 
	 * @param src
	 *                The iterator to pull values from.
	 * @param vals
	 *                The values to expect from the iterator.
	 */
	@SafeVarargs
	public static <T> void assertIteratorEquals(Iterator<T> src, T... vals) {
		for (int i = 0; i < vals.length; i++) {
			if (src.hasNext()) {
				assertEquals(vals[i], src.next());
			} else {
				String msg = String.format("not enough values: got %d, wanted %d",
						i, vals.length);
					
				assertTrue(msg, false);
			}
		}
	}

	/**
	 * Assert an iterator provides a particular sequence of values.
	 * 
	 * @param src
	 *                The iterator to pull values from.
	 * @param hasMore
	 *                The expected value of hasNext for the iterator.
	 * @param vals
	 *                The values to expect from the iterator.
	 */
	@SafeVarargs
	public static <T> void assertIteratorEquals(boolean hasMore, Iterator<T> src, T... vals) {
		/*
		 * @NOTE
		 * 
		 * Even though it's awkward, the boolean has to come first.
		 * Otherwise, there are cases where the compiler will get
		 * confused as to what the right value for T is, and be unable
		 * to pick an overload.
		 */
		assertIteratorEquals(src, vals);

		assertEquals("iterator not exhausted", hasMore, src.hasNext());
	}

	/**
	 * Assert an iterator provides a particular sequence of values.
	 * 
	 * @param src
	 *                The iterator to pull values from.
	 * @param vals
	 *                The values to expect from the iterator.
	 */
	@SafeVarargs
	public static <T> void assertIteratorSet(Iterator<T> src, T... vals) {
		Set<T> s1 = new HashSet<>();
		Set<T> s2 = new HashSet<>();
		
		for (int i = 0; i < vals.length; i++) {
			if (src.hasNext()) {
				s1.add(vals[i]);
				s2.add(src.next());
			} else {
				String msg = String.format("not enough values: got %d, wanted %d",
						i, vals.length);
					
				assertTrue(msg, false);
			}
		}
		
		assertEquals(s1, s2);
	}

	/**
	 * Assert an iterator provides a particular sequence of values.
	 * 
	 * @param src
	 *                The iterator to pull values from.
	 * @param hasMore
	 *                The expected value of hasNext for the iterator.
	 * @param vals
	 *                The values to expect from the iterator.
	 */
	@SafeVarargs
	public static <T> void assertIteratorSet(boolean hasMore, Iterator<T> src, T... vals) {
		/*
		 * @NOTE
		 * 
		 * Even though it's awkward, the boolean has to come first.
		 * Otherwise, there are cases where the compiler will get
		 * confused as to what the right value for T is, and be unable
		 * to pick an overload.
		 */
		assertIteratorSet(src, vals);

		assertEquals("iterator not exhausted", hasMore, src.hasNext());
	}
	
	/**
	 * Assert that a list contains a certain set of values.
	 * 
	 * @param src
	 * 	The list to read values from.
	 * 
	 * @param exps
	 * 	The values to expect in the list.
	 */
	@SafeVarargs
	public static <T> void assertListEquals(List<T> src, T... exps) {
		assertEquals(exps.length, src.size());

		int i = 0;
		for (T act : src) {
			T exp = exps[i++];

			assertEquals(exp, act);
		}
	}
}