/
NonBlockingSetIntTest.java
231 lines (200 loc) · 6.8 KB
/
NonBlockingSetIntTest.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package org.cliffc.high_scale_lib;
import junit.framework.TestCase;
import java.io.*;
import java.util.Iterator;
import static org.hamcrest.CoreMatchers.anyOf;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
public class NonBlockingSetIntTest extends TestCase {
public void testSetOperations() {
// add values to the following bitsets
// 'a' will get all values divisible by 63
// 'b' will get the rest
// this ensures that one NBSI will have the largest bit array, while the other
// will have the largest recursive bitset used to store values for every 64th bit
// this exercises that the constructor building a blank NBSI to hold bitwise operations
// on two others is sized appropriately initially
NonBlockingSetInt a = new NonBlockingSetInt();
NonBlockingSetInt b = new NonBlockingSetInt();
int max = 10000;
for(int i = 0; i < max; i++) {
NonBlockingSetInt t = (i&63) == 63 ? a : b;
t.add(i);
assertTrue(t.contains(i));
}
// c should contain the empty set since a and b are disjoint
NonBlockingSetInt c = a.intersect(b);
NonBlockingSetInt d = b.intersect(a);
for(int i = 0; i < max; i++) {
assertFalse(c.contains(i));
assertFalse(d.contains(i));
}
c = a.union(b);
d = b.union(a);
for(int i = 0; i < max; i++) {
assertTrue(c.contains(i));
assertTrue(d.contains(i));
}
// just make sure the bitset is usable after building out an instance with an ample internal buffer
for(int i = 0; i < max; i++) {
c.add(i);
assertTrue(c.contains(i));
d.add(i);
assertTrue(d.contains(i));
}
c.intersect(new NonBlockingSetInt());
new NonBlockingSetInt().intersect(c);
}
private NonBlockingSetInt _nbsi;
protected void setUp () { _nbsi = new NonBlockingSetInt(); }
protected void tearDown() { _nbsi = null; }
// Test some basic stuff; add a few keys, remove a few keys
public void testBasic() {
assertTrue ( _nbsi.isEmpty() );
assertTrue ( _nbsi.add(1) );
checkSizes (1);
assertTrue ( _nbsi.add(2) );
checkSizes (2);
assertFalse( _nbsi.add(1) );
assertFalse( _nbsi.add(2) );
checkSizes (2);
assertThat ( _nbsi.remove(1), is(true ) );
checkSizes (1);
assertThat ( _nbsi.remove(1), is(false) );
assertTrue ( _nbsi.remove(2) );
checkSizes (0);
assertFalse( _nbsi.remove(2) );
assertFalse( _nbsi.remove(3) );
assertTrue ( _nbsi.isEmpty() );
assertTrue ( _nbsi.add(63) );
checkSizes (1);
assertTrue ( _nbsi.remove(63) );
assertFalse( _nbsi.remove(63) );
assertTrue ( _nbsi.isEmpty() );
assertTrue ( _nbsi.add(10000) );
checkSizes (1);
assertTrue ( _nbsi.add(20000) );
checkSizes (2);
assertFalse( _nbsi.add(10000) );
assertFalse( _nbsi.add(20000) );
checkSizes (2);
assertThat ( _nbsi.remove(10000), is(true ) );
checkSizes (1);
assertThat ( _nbsi.remove(10000), is(false) );
assertTrue ( _nbsi.remove(20000) );
checkSizes (0);
assertFalse( _nbsi.remove(20000) );
}
// Check all iterators for correct size counts
private void checkSizes(int expectedSize) {
assertEquals( "size()", _nbsi.size(), expectedSize );
Iterator it = _nbsi.iterator();
int result = 0;
while (it.hasNext()) {
result++;
it.next();
}
assertEquals( "iterator missed", expectedSize, result );
}
public void testIteration() {
assertTrue ( _nbsi.isEmpty() );
assertTrue ( _nbsi.add(1) );
assertTrue ( _nbsi.add(2) );
StringBuffer buf = new StringBuffer();
for( Iterator<Integer> i = _nbsi.iterator(); i.hasNext(); ) {
Integer val = i.next();
buf.append(val);
}
assertThat("found all vals",buf.toString(),anyOf(is("12"),is("21")));
assertThat("toString works",_nbsi.toString(), anyOf(is("[1, 2]"),is("[2, 1]")));
}
public void testIterationBig() {
for( int i=0; i<100; i++ )
_nbsi.add(i);
assertThat( _nbsi.size(), is(100) );
int sz =0;
int sum = 0;
for( Integer x : _nbsi ) {
sz++;
sum += x;
assertTrue(x>=0 && x<=99);
}
assertThat("Found 100 ints",sz,is(100));
assertThat("Found all integers in list",sum,is(100*99/2));
assertThat( "can remove 3", _nbsi.remove(3), is(true) );
assertThat( "can remove 4", _nbsi.remove(4), is(true) );
sz =0;
sum = 0;
for( Integer x : _nbsi ) {
sz++;
sum += x;
assertTrue(x>=0 && x<=99);
}
assertThat("Found 98 ints",sz,is(98));
assertThat("Found all integers in list",sum,is(100*99/2 - (3+4)));
}
public void testSerial() {
assertTrue ( _nbsi.isEmpty() );
assertTrue ( _nbsi.add(1) );
assertTrue ( _nbsi.add(2) );
// Serialize it out
try {
FileOutputStream fos = new FileOutputStream("NBSI_test.txt");
ObjectOutputStream out = new ObjectOutputStream(fos);
out.writeObject(_nbsi);
out.close();
} catch(IOException ex) {
ex.printStackTrace();
}
// Read it back
try {
File f = new File("NBSI_test.txt");
FileInputStream fis = new FileInputStream(f);
ObjectInputStream in = new ObjectInputStream(fis);
NonBlockingSetInt nbsi = (NonBlockingSetInt)in.readObject();
in.close();
assertEquals(_nbsi.toString(),nbsi.toString());
if( !f.delete() ) throw new IOException("delete failed");
} catch(IOException ex) {
ex.printStackTrace();
} catch(ClassNotFoundException ex) {
ex.printStackTrace();
}
}
// Do some simple concurrent testing
public void testConcurrentSimple() throws InterruptedException {
final NonBlockingSetInt nbsi = new NonBlockingSetInt();
// In 2 threads, add & remove even & odd elements concurrently
Thread t1 = new Thread() { public void run() { work_helper(nbsi,"T1",1); } };
t1.start();
work_helper(nbsi,"T0",1);
t1.join();
// In the end, all members should be removed
StringBuffer buf = new StringBuffer();
buf.append("Should be emptyset but has these elements: {");
boolean found = false;
for( Integer x : nbsi ) {
buf.append(" ").append(x);
found = true;
}
if( found ) System.out.println(buf);
assertThat( "concurrent size=0", nbsi.size(), is(0) );
for( Integer x : nbsi ) {
assertTrue("No elements so never get here",false);
}
}
void work_helper(NonBlockingSetInt nbsi, String thrd, int d) {
final int ITERS = 100000;
for( int j=0; j<10; j++ ) {
long start = System.nanoTime();
for( int i=d; i<ITERS; i+=2 )
nbsi.add(i);
for( int i=d; i<ITERS; i+=2 )
nbsi.remove(i);
double delta_nanos = System.nanoTime()-start;
double delta_secs = delta_nanos/1000000000.0;
double ops = ITERS*2;
//System.out.println("Thrd"+thrd+" "+(ops/delta_secs)+" ops/sec size="+nbsi.size());
}
}
}