public void Test_Logical_Binary_Operations() { var a = SuperSlowUncompressedBitmap.FromBitString("0101").ToBitmap(); var b = SuperSlowUncompressedBitmap.FromBitString("0011").ToBitmap(); var and = a.And(b); Assert.That(and, Is.Not.Null); Assert.That(new SuperSlowUncompressedBitmap(and).ToBitString(), Is.EqualTo("0001000000000000000000000000000"), "a AND b"); var or = a.Or(b); Assert.That(or, Is.Not.Null); Assert.That(new SuperSlowUncompressedBitmap(or).ToBitString(), Is.EqualTo("0111000000000000000000000000000"), "a OR b"); var xor = a.Xor(b); Assert.That(xor, Is.Not.Null); Assert.That(new SuperSlowUncompressedBitmap(xor).ToBitString(), Is.EqualTo("0110000000000000000000000000000"), "a XOR b"); var andNot = a.AndNot(b); Assert.That(andNot, Is.Not.Null); Assert.That(new SuperSlowUncompressedBitmap(andNot).ToBitString(), Is.EqualTo("0100000000000000000000000000000"), "a AND NOT b"); var orNot = a.OrNot(b); Assert.That(orNot, Is.Not.Null); Assert.That(new SuperSlowUncompressedBitmap(orNot).ToBitString(), Is.EqualTo("1101111111111111111111111111111"), "a OR NOT b"); var xorNot = a.XorNot(b); Assert.That(xorNot, Is.Not.Null); Assert.That(new SuperSlowUncompressedBitmap(xorNot).ToBitString(), Is.EqualTo("1001111111111111111111111111111")); }
private static bool SetBitAndVerify(CompressedBitmapBuilder builder, SuperSlowUncompressedBitmap witness, int offset) { Console.WriteLine(); Console.WriteLine("Set({0}):", offset); bool actual = builder.Set(offset); bool expected = witness.Set(offset); Assert.That(actual, Is.EqualTo(expected), "Set({0})", offset); Verify(builder, witness); return(actual); }
private static bool ClearBitAndVerify(CompressedBitmapBuilder builder, SuperSlowUncompressedBitmap witness, int offset) { Log(); Log("Clear({0}):", offset); bool actual = builder.Clear(offset); bool expected = witness.Clear(offset); Assert.That(actual, Is.EqualTo(expected), "Clear({0})", offset); Verify(builder, witness); return(actual); }
public void Test_CompressedBitmapBuilder_Random_Sets_And_Clears() { // randomly alternate between setting and clearing random bits int K = 20; int S = 100; int C = 100; int N = 5 * 1000; var bmp = CompressedBitmap.Empty; var witness = new SuperSlowUncompressedBitmap(); var rnd = new Random(12345678); for (int k = 0; k < K; k++) { Console.WriteLine("### Generation " + k); // convert to builder var builder = bmp.ToBuilder(); Verify(builder, witness); // set S bits for (int i = 0; i < S; i++) { int p = rnd.Next(N); builder.Set(p); witness.Set(p); //SetBitAndVerify(builder, witness, p); } // clear C bits for (int i = 0; i < C; i++) { int p = rnd.Next(N); //ClearBitAndVerify(builder, witness, p); builder.Clear(p); witness.Clear(p); } // pack back to bitmap bmp = builder.ToBitmap(); Console.WriteLine(); Console.WriteLine("> Result of gen #{0}: {1}", k, bmp.Dump()); Console.WriteLine("> " + bmp.ToSlice().ToHexaString()); Console.WriteLine(); } }
public void Test_CompressedBitmapBuilder_Clear_Bits() { var builder = CompressedBitmap.Empty.ToBuilder(); Assert.That(builder, Is.Not.Null.And.Count.EqualTo(0)); var witness = new SuperSlowUncompressedBitmap(); // clearing anything in the empty bitmap is a no-op Assert.That(ClearBitAndVerify(builder, witness, 0), Is.False); Assert.That(ClearBitAndVerify(builder, witness, 42), Is.False); Assert.That(ClearBitAndVerify(builder, witness, int.MaxValue), Is.False); Assert.That(SetBitAndVerify(builder, witness, 42), Is.True); Assert.That(ClearBitAndVerify(builder, witness, 42), Is.True, "Clear just after set"); Assert.That(ClearBitAndVerify(builder, witness, 42), Is.False, "Clear just after clear"); }
public void Test_CompressedBitmapBuilder_Random_Sets() { // randomly set K bits in a set of N possible bits (with possible overlap) // => this test random insertions that need to modifiy the inside of a bitmap var builder = CompressedBitmap.Empty.ToBuilder(); var witness = new SuperSlowUncompressedBitmap(); int N = 5 * 1000; int K = 100; var rnd = new Random(12345678); for (int i = 0; i < K; i++) { SetBitAndVerify(builder, witness, rnd.Next(N)); } }
public void Test_CompressedBitmapBuilder_Set_Bits() { // start with an empty bitmap var builder = CompressedBitmap.Empty.ToBuilder(); Assert.That(builder, Is.Not.Null.And.Count.EqualTo(0)); var witness = new SuperSlowUncompressedBitmap(); Assert.That(SetBitAndVerify(builder, witness, 0), Is.True); Assert.That(SetBitAndVerify(builder, witness, 17), Is.True); Assert.That(SetBitAndVerify(builder, witness, 17), Is.False); Assert.That(SetBitAndVerify(builder, witness, 31), Is.True); Assert.That(SetBitAndVerify(builder, witness, 1234), Is.True); Assert.That(SetBitAndVerify(builder, witness, 777), Is.True); Assert.That(SetBitAndVerify(builder, witness, 62), Is.True); Assert.That(SetBitAndVerify(builder, witness, 774), Is.True); Assert.That(SetBitAndVerify(builder, witness, 124), Is.True); Assert.That(SetBitAndVerify(builder, witness, 93), Is.True); }
private static void Verify(CompressedBitmapBuilder builder, SuperSlowUncompressedBitmap witness) { var bmpBuilder = builder.ToBitmap(); var bmpWitness = witness.ToBitmap(); Console.WriteLine("> B: {0,12} ({1,3}) {2}", bmpBuilder.Bounds, bmpBuilder.CountBits(), bmpBuilder.ToSlice().ToHexaString()); Console.WriteLine("> W: {0,12} ({1,3}) {2}", bmpWitness.Bounds, bmpWitness.CountBits(), bmpWitness.ToSlice().ToHexaString()); var rawBuilder = builder.ToBooleanArray(); var rawWitness = witness.ToBooleanArray(); Console.WriteLine("> B: " + bmpBuilder.Dump()); Console.WriteLine("> W: " + bmpWitness.Dump()); var a = SuperSlowUncompressedBitmap.Dump(rawBuilder).ToString().Split('\n'); var b = SuperSlowUncompressedBitmap.Dump(rawWitness).ToString().Split('\n'); Console.WriteLine(String.Join("\n", a.Zip(b, (x, y) => (x == y ? "= " : "##") + x + "\n " + y))); Assert.That(rawBuilder, Is.EqualTo(rawWitness), "Uncompressed bitmap does not match"); }
public void Test_CompressedBitmapBuilder_Linear_Sets() { // for each bit, from 0 to N-1, set it with proability P // => this test linear insertion, that always need to patch or append at the end of the bitmap var builder = CompressedBitmap.Empty.ToBuilder(); var witness = new SuperSlowUncompressedBitmap(); int N = 5 * 1000; int P = 100; var rnd = new Random(12345678); for (int i = 0; i < N; i++) { if (rnd.Next(P) == 42) { SetBitAndVerify(builder, witness, rnd.Next(N)); } } }