Permalink
Browse files

Deprecate TinyBitSet (#97)

* Deprecated TinyBitSet.

* Updated TestTinyBitSet to communicate shortcomings.
1 parent d6fe1d8 commit 5b380a94883ae4a4762c4657b6bd043708d0a2d1 @ham1 ham1 committed with raphw Jan 20, 2017
@@ -15,6 +15,7 @@
*/
package net.sf.cglib.core;
+@Deprecated
public class TinyBitSet {
private static int[] T = new int[256];
private int value = 0;
@@ -29,7 +30,7 @@ private static int gcount(int x) {
}
static {
- for(int j = 0; j < 256; j++) {
+ for (int j = 0; j < 256; j++) {
T[j] = gcount(j);
}
}
@@ -49,11 +50,16 @@ private static int log2(int i) {
}
return j;
}
-
+
public int length() {
return log2(topbit(value));
}
+ /**
+ * If bit 31 is set then this method results in an infinite loop.
+ *
+ * @return the number of bits set to <code>true</code> in this TinyBitSet.
+ */
public int cardinality() {
int w = value;
int c = 0;
@@ -15,52 +15,79 @@
*/
package net.sf.cglib.core;
-import net.sf.cglib.CodeGenTestCase;
-import junit.framework.*;
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
public class TestTinyBitSet extends TestCase {
+
+ private TinyBitSet b = new TinyBitSet();
+
public void testGetSetClear() {
- TinyBitSet b = new TinyBitSet();
- assertTrue(!b.get(5));
- b.set(5);
- assertTrue(b.get(5));
- b.clear(5);
- assertTrue(!b.get(5));
+ // get, set, clear, set bits 0 - 31
+ for (int index = 0; index < 32; index++) {
+ assertFalse(b.get(index));
+ b.set(index);
+ assertTrue(b.get(index));
+ b.clear(index);
+ assertFalse(b.get(index));
+ b.set(index);
+ }
+ // after setting bits 0-31, get bits 32 and above reports true;
+ assertTrue(b.get(32));
+ assertTrue(b.get(255));
+ assertTrue(b.get(256));
+ assertTrue(b.get(1000000));
}
-
+
+ public void testGetSetClear2() {
+ for (int index = 2; index > 0; index *= 2) {
+ b.set(index);
+ assertTrue(b.get(index));
+ b.clear(index);
+ assertFalse(b.get(index));
+ b.set(index);
+ }
+ }
+
public void testLength() {
- TinyBitSet b = new TinyBitSet();
+ assertEquals(0, b.length());
b.set(10);
- assertTrue(b.length() == 11);
+ assertEquals(11, b.length());
b.set(15);
- assertTrue(b.length() == 16);
+ assertEquals(16, b.length());
b.set(14);
- assertTrue(b.length() == 16);
+ assertEquals(16, b.length());
}
public void testCardinality() {
- TinyBitSet b = new TinyBitSet();
- assertTrue(b.cardinality() == 0);
+ assertEquals(0, b.cardinality());
b.set(1);
- assertTrue(b.cardinality() == 1);
+ assertEquals(1, b.cardinality());
b.set(4);
- assertTrue(b.cardinality() == 2);
+ assertEquals(2, b.cardinality());
b.set(10);
- assertTrue(b.cardinality() == 3);
+ assertEquals(3, b.cardinality());
b.set(10);
- assertTrue(b.cardinality() == 3);
+ assertEquals(3, b.cardinality());
b.clear(10);
- assertTrue(b.cardinality() == 2);
+ assertEquals(2, b.cardinality());
+ }
+
+ /** Causes infinite loop */
+ public void set31InfiniteLoopCardinality() {
+ b.set(31);
+ assertEquals(1, b.cardinality()); // infinite loop...
}
public TestTinyBitSet(String testName) {
super(testName);
}
-
+
public static void main(String[] args) {
junit.textui.TestRunner.run(suite());
}
-
+
public static Test suite() {
return new TestSuite(TestTinyBitSet.class);
}

0 comments on commit 5b380a9

Please sign in to comment.