3030import static org .testng .Assert .*;
3131
3232public class CountMinSketchTest {
33+
3334 @ Test
3435 public void createNewCountMinSketchTest () throws Exception {
3536 assertThrows (SketchesArgumentException .class , () -> new CountMinSketch ((byte ) 5 , 1 , 123 ));
36- assertThrows ( SketchesArgumentException . class , () -> new CountMinSketch (( byte ) 4 , 268435456 , 123 ));
37-
37+ //2^28 buckets and 4 hashes -> a long[] of 8GB! Set JVM -Xmx > 8g
38+ assertThrows ( SketchesArgumentException . class , () -> new CountMinSketch (( byte ) 4 , ( 1 << 28 ), 123 ));
3839 final byte numHashes = 3 ;
3940 final int numBuckets = 5 ;
4041 final long seed = 1234567 ;
41- CountMinSketch c = new CountMinSketch (numHashes , numBuckets , seed );
42+ final CountMinSketch c = new CountMinSketch (numHashes , numBuckets , seed );
4243
4344 assertEquals (c .getNumHashes_ (), numHashes );
4445 assertEquals (c .getNumBuckets_ (), numBuckets );
@@ -77,7 +78,7 @@ public void countMinSketchOneUpdateTest() {
7778 final int numBuckets = 5 ;
7879 final long seed = 1234567 ;
7980 long insertedWeights = 0 ;
80- CountMinSketch c = new CountMinSketch (numHashes , numBuckets , seed );
81+ final CountMinSketch c = new CountMinSketch (numHashes , numBuckets , seed );
8182 final String x = "x" ;
8283
8384 assertTrue (c .isEmpty ());
@@ -103,7 +104,7 @@ public void countMinSketchOneUpdateTest() {
103104
104105 @ Test
105106 public void frequencyCancellationTest () {
106- CountMinSketch c = new CountMinSketch ((byte ) 1 , 5 , 123456 );
107+ final CountMinSketch c = new CountMinSketch ((byte ) 1 , 5 , 123456 );
107108 c .update ("x" , 1 );
108109 c .update ("y" , -1 );
109110 assertEquals (c .getTotalWeight_ (), 2 );
@@ -114,8 +115,8 @@ public void frequencyCancellationTest() {
114115 @ Test
115116 public void frequencyEstimates () {
116117 final int numItems = 10 ;
117- long [] data = new long [numItems ];
118- long [] frequencies = new long [numItems ];
118+ final long [] data = new long [numItems ];
119+ final long [] frequencies = new long [numItems ];
119120
120121 for (int i = 0 ; i < numItems ; i ++) {
121122 data [i ] = i ;
@@ -127,7 +128,7 @@ public void frequencyEstimates() {
127128 final int numBuckets = CountMinSketch .suggestNumBuckets (relativeError );
128129 final byte numHashes = CountMinSketch .suggestNumHashes (confidence );
129130
130- CountMinSketch c = new CountMinSketch (numHashes , numBuckets , 1234567 );
131+ final CountMinSketch c = new CountMinSketch (numHashes , numBuckets , 1234567 );
131132 for (int i = 0 ; i < numItems ; i ++) {
132133 final long value = data [i ];
133134 final long freq = frequencies [i ];
@@ -150,15 +151,15 @@ public void mergeFailTest() {
150151 final long seed = 1234567 ;
151152 final int numBuckets = CountMinSketch .suggestNumBuckets (relativeError );
152153 final byte numHashes = CountMinSketch .suggestNumHashes (confidence );
153- CountMinSketch s = new CountMinSketch (numHashes , numBuckets , seed );
154+ final CountMinSketch s = new CountMinSketch (numHashes , numBuckets , seed );
154155
155156 assertThrows ("Cannot merge a sketch with itself." , SketchesException .class , () -> s .merge (s ));
156157
157- CountMinSketch s1 = new CountMinSketch ((byte ) (numHashes + 1 ), numBuckets , seed );
158- CountMinSketch s2 = new CountMinSketch (numHashes , numBuckets + 1 , seed );
159- CountMinSketch s3 = new CountMinSketch (numHashes , numBuckets , seed + 1 );
158+ final CountMinSketch s1 = new CountMinSketch ((byte ) (numHashes + 1 ), numBuckets , seed );
159+ final CountMinSketch s2 = new CountMinSketch (numHashes , numBuckets + 1 , seed );
160+ final CountMinSketch s3 = new CountMinSketch (numHashes , numBuckets , seed + 1 );
160161
161- CountMinSketch [] sketches = {s1 , s2 , s3 };
162+ final CountMinSketch [] sketches = {s1 , s2 , s3 };
162163 for (final CountMinSketch sk : sketches ) {
163164 assertThrows ("Incompatible sketch configuration." , SketchesException .class , () -> s .merge (sk ));
164165 }
@@ -171,12 +172,12 @@ public void mergeTest() {
171172 final long seed = 123456 ;
172173 final int numBuckets = CountMinSketch .suggestNumBuckets (relativeError );
173174 final byte numHashes = CountMinSketch .suggestNumHashes (confidence );
174- CountMinSketch c = new CountMinSketch (numHashes , numBuckets , seed );
175+ final CountMinSketch c = new CountMinSketch (numHashes , numBuckets , seed );
175176
176177 final byte sHashes = c .getNumHashes_ ();
177178 final int sBuckets = c .getNumBuckets_ ();
178179 final long sSeed = c .getSeed_ ();
179- CountMinSketch s = new CountMinSketch (sHashes , sBuckets , sSeed );
180+ final CountMinSketch s = new CountMinSketch (sHashes , sBuckets , sSeed );
180181
181182 c .merge (s );
182183 assertEquals (c .getTotalWeight_ (), 0 );
@@ -201,15 +202,15 @@ public void serializeDeserializeEmptyTest() {
201202 final byte numHashes = 3 ;
202203 final int numBuckets = 32 ;
203204 final long seed = 123456 ;
204- CountMinSketch c = new CountMinSketch (numHashes , numBuckets , seed );
205+ final CountMinSketch c = new CountMinSketch (numHashes , numBuckets , seed );
205206
206- ByteArrayOutputStream buf = new ByteArrayOutputStream ();
207+ final ByteArrayOutputStream buf = new ByteArrayOutputStream ();
207208 c .serialize (buf );
208209
209- byte [] b = buf .toByteArray ();
210+ final byte [] b = buf .toByteArray ();
210211 assertThrows (SketchesArgumentException .class , () -> CountMinSketch .deserialize (b , seed - 1 ));
211212
212- CountMinSketch d = CountMinSketch .deserialize (b , seed );
213+ final CountMinSketch d = CountMinSketch .deserialize (b , seed );
213214 assertEquals (d .getNumHashes_ (), c .getNumHashes_ ());
214215 assertEquals (d .getNumBuckets_ (), c .getNumBuckets_ ());
215216 assertEquals (d .getSeed_ (), c .getSeed_ ());
@@ -223,16 +224,16 @@ public void serializeDeserializeTest() {
223224 final byte numHashes = 5 ;
224225 final int numBuckets = 64 ;
225226 final long seed = 1234456 ;
226- CountMinSketch c = new CountMinSketch (numHashes , numBuckets , seed );
227+ final CountMinSketch c = new CountMinSketch (numHashes , numBuckets , seed );
227228 for (long i = 0 ; i < 10 ; i ++) {
228229 c .update (i , 10 *i *i );
229230 }
230231
231- ByteArrayOutputStream buf = new ByteArrayOutputStream ();
232+ final ByteArrayOutputStream buf = new ByteArrayOutputStream ();
232233 c .serialize (buf );
233234
234235 assertThrows (SketchesArgumentException .class , () -> CountMinSketch .deserialize (buf .toByteArray (), seed - 1 ));
235- CountMinSketch d = CountMinSketch .deserialize (buf .toByteArray (), seed );
236+ final CountMinSketch d = CountMinSketch .deserialize (buf .toByteArray (), seed );
236237
237238 assertEquals (d .getNumHashes_ (), c .getNumHashes_ ());
238239 assertEquals (d .getNumBuckets_ (), c .getNumBuckets_ ());
0 commit comments