-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIntegerAggregatorTest.java
188 lines (161 loc) · 4.41 KB
/
IntegerAggregatorTest.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package simpledb;
import static org.junit.Assert.assertEquals;
import java.util.NoSuchElementException;
import junit.framework.JUnit4TestAdapter;
import org.junit.Before;
import org.junit.Test;
import simpledb.systemtest.SimpleDbTestBase;
public class IntegerAggregatorTest extends SimpleDbTestBase {
int width1 = 2;
OpIterator scan1;
int[][] sum = null;
int[][] min = null;
int[][] max = null;
int[][] avg = null;
/**
* Initialize each unit test
*/
@Before public void createTupleList() throws Exception {
this.scan1 = TestUtil.createTupleList(width1,
new int[] { 1, 2,
1, 4,
1, 6,
3, 2,
3, 4,
3, 6,
5, 7 });
// verify how the results progress after a few merges
this.sum = new int[][] {
{ 1, 2 },
{ 1, 6 },
{ 1, 12 },
{ 1, 12, 3, 2 }
};
this.min = new int[][] {
{ 1, 2 },
{ 1, 2 },
{ 1, 2 },
{ 1, 2, 3, 2 }
};
this.max = new int[][] {
{ 1, 2 },
{ 1, 4 },
{ 1, 6 },
{ 1, 6, 3, 2 }
};
this.avg = new int[][] {
{ 1, 2 },
{ 1, 3 },
{ 1, 4 },
{ 1, 4, 3, 2 }
};
}
/**
* Test IntegerAggregator.mergeTupleIntoGroup() and iterator() over a sum
*/
@Test public void mergeSum() throws Exception {
scan1.open();
IntegerAggregator agg = new IntegerAggregator(0, Type.INT_TYPE, 1, Aggregator.Op.SUM);
for (int[] step : sum) {
agg.mergeTupleIntoGroup(scan1.next());
OpIterator it = agg.iterator();
it.open();
TestUtil.matchAllTuples(TestUtil.createTupleList(width1, step), it);
}
}
/**
* Test IntegerAggregator.mergeTupleIntoGroup() and iterator() over a min
*/
@Test public void mergeMin() throws Exception {
scan1.open();
IntegerAggregator agg = new IntegerAggregator(0,Type.INT_TYPE, 1, Aggregator.Op.MIN);
OpIterator it;
for (int[] step : min) {
agg.mergeTupleIntoGroup(scan1.next());
it = agg.iterator();
it.open();
TestUtil.matchAllTuples(TestUtil.createTupleList(width1, step), it);
}
}
/**
* Test IntegerAggregator.mergeTupleIntoGroup() and iterator() over a max
*/
@Test public void mergeMax() throws Exception {
scan1.open();
IntegerAggregator agg = new IntegerAggregator(0, Type.INT_TYPE, 1, Aggregator.Op.MAX);
OpIterator it;
for (int[] step : max) {
agg.mergeTupleIntoGroup(scan1.next());
it = agg.iterator();
it.open();
TestUtil.matchAllTuples(TestUtil.createTupleList(width1, step), it);
}
}
/**
* Test IntegerAggregator.mergeTupleIntoGroup() and iterator() over an avg
*/
@Test public void mergeAvg() throws Exception {
scan1.open();
IntegerAggregator agg = new IntegerAggregator(0, Type.INT_TYPE, 1, Aggregator.Op.AVG);
OpIterator it;
for (int[] step : avg) {
agg.mergeTupleIntoGroup(scan1.next());
it = agg.iterator();
it.open();
TestUtil.matchAllTuples(TestUtil.createTupleList(width1, step), it);
}
}
/**
* Test IntegerAggregator.iterator() for OpIterator behaviour
*/
@Test public void testIterator() throws Exception {
// first, populate the aggregator via sum over scan1
scan1.open();
IntegerAggregator agg = new IntegerAggregator(0, Type.INT_TYPE, 1, Aggregator.Op.SUM);
try {
while (true)
agg.mergeTupleIntoGroup(scan1.next());
} catch (NoSuchElementException e) {
// explicitly ignored
}
OpIterator 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("IntegerAggregator iterator yielded tuple after close");
} catch (Exception e) {
// explicitly ignored
}
}
/**
* JUnit suite target
*/
public static junit.framework.Test suite() {
return new JUnit4TestAdapter(IntegerAggregatorTest.class);
}
}