-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathStringAggregatorTest.java
115 lines (98 loc) · 2.64 KB
/
StringAggregatorTest.java
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
package simpledb;
import java.util.*;
import org.junit.Before;
import org.junit.Test;
import simpledb.systemtest.SimpleDbTestBase;
import static org.junit.Assert.assertEquals;
import junit.framework.JUnit4TestAdapter;
public class StringAggregatorTest extends SimpleDbTestBase {
int width1 = 2;
DbIterator scan1;
int[][] count = null;
/**
* Initialize each unit test
*/
@Before public void createTupleList() throws Exception {
this.scan1 = TestUtil.createTupleList(width1,
new Object[] { 1, "a",
1, "b",
1, "c",
3, "d",
3, "e",
3, "f",
5, "g" });
// verify how the results progress after a few merges
this.count = new int[][] {
{ 1, 1 },
{ 1, 2 },
{ 1, 3 },
{ 1, 3, 3, 1 }
};
}
/**
* Test String.mergeTupleIntoGroup() and iterator() over a COUNT
*/
@Test public void mergeCount() throws Exception {
scan1.open();
StringAggregator agg = new StringAggregator(0, Type.INT_TYPE, 1, Aggregator.Op.COUNT);
for (int[] step : count) {
agg.mergeTupleIntoGroup(scan1.next());
DbIterator it = agg.iterator();
it.open();
TestUtil.matchAllTuples(TestUtil.createTupleList(width1, step), it);
}
}
/**
* Test StringAggregator.iterator() for DbIterator behaviour
*/
@Test public void testIterator() throws Exception {
// first, populate the aggregator via sum over scan1
scan1.open();
StringAggregator agg = new StringAggregator(0, Type.INT_TYPE, 1, Aggregator.Op.COUNT);
try {
while (true)
agg.mergeTupleIntoGroup(scan1.next());
} catch (NoSuchElementException e) {
// explicitly ignored
}
DbIterator it = agg.iterator();
it.open();
// verify it has three elements
int count = 0;
try {
while (true) {
it.next();
count++;
}
} catch (NoSuchElementException e) {
// explicitly ignored
}
assertEquals(3, count);
// rewind and try again
it.rewind();
count = 0;
try {
while (true) {
it.next();
count++;
}
} catch (NoSuchElementException e) {
// explicitly ignored
}
assertEquals(3, count);
// close it and check that we don't get anything
it.close();
try {
it.next();
throw new Exception("StringAggreator iterator yielded tuple after close");
} catch (Exception e) {
// explicitly ignored
}
}
/**
* JUnit suite target
*/
public static junit.framework.Test suite() {
return new JUnit4TestAdapter(StringAggregatorTest.class);
}
}