public void TestFoo() { var rnd = new Random(); Func <Slice, Slice> compress = (input) => { Console.WriteLine("IN [{0}] => {1}", input.Count, input); var writer = new CompressedBitmapWriter(); int r = WordAlignHybridEncoder.CompressTo(input, writer); Slice compressed = writer.GetBuffer(); Console.WriteLine("OUT [{0}] => {1} [r={2}]", compressed.Count, compressed, r); var sb = new StringBuilder(); Console.WriteLine(WordAlignHybridEncoder.DumpCompressed(compressed).ToString()); Console.WriteLine(); return(compressed); }; compress(Slice.FromString("This is a test of the emergency broadcast system")); // all zeroes (multiple of 31 bits) compress(Slice.Repeat(0, 62)); // all zeroes (with padding) compress(Slice.Repeat(0, 42)); // all ones (multiple of 31 bits) compress(Slice.Repeat(255, 62)); // all ones (with padding) compress(Slice.Repeat(255, 42)); // random stuff (multiple of 31 bits) compress(Slice.Random(rnd, 42)); // random stuff (with padding) compress(Slice.Random(rnd, 42)); // mostly zeroes Action <byte[], int> setBit = (b, p) => { b[p >> 3] |= (byte)(1 << (p & 7)); }; Func <int, byte[]> mostlyZeroes = (count) => { var buf = new byte[1024]; for (int i = 0; i < count; i++) { setBit(buf, rnd.Next(buf.Length * 8)); } Console.WriteLine("Mostly zeroes: " + count); return(buf); }; compress(Slice.Create(mostlyZeroes(1))); compress(Slice.Create(mostlyZeroes(10))); compress(Slice.Create(mostlyZeroes(42))); compress(Slice.Create(mostlyZeroes(100))); // mostly ones Action <byte[], int> clearBit = (b, p) => { b[p >> 3] &= (byte)~(1 << (p & 7)); }; Func <int, byte[]> mostlyOnes = (count) => { var buf = new byte[1024]; for (int i = 0; i < buf.Length; i++) { buf[i] = 0xFF; } for (int i = 0; i < 10; i++) { clearBit(buf, rnd.Next(buf.Length * 8)); } Console.WriteLine("Mostly ones: " + count); return(buf); }; compress(Slice.Create(mostlyOnes(1))); compress(Slice.Create(mostlyOnes(10))); compress(Slice.Create(mostlyOnes(42))); compress(Slice.Create(mostlyOnes(100))); // progressive Func <byte[], int, bool> testBit = (b, p) => (b[p >> 3] & (1 << (p & 7))) != 0; const int VALUES = 8192; var buffer = new byte[VALUES / 8]; var output = new CompressedBitmapWriter(); WordAlignHybridEncoder.CompressTo(Slice.Create(buffer), output); Console.WriteLine("{0}\t{1}\t1024", 0, output.Length); for (int i = 0; i < VALUES / 8; i++) { int p; do { p = rnd.Next(VALUES); }while (testBit(buffer, p)); setBit(buffer, p); output.Reset(); WordAlignHybridEncoder.CompressTo(Slice.Create(buffer), output); Console.WriteLine("{0}\t{1}\t1024", 1.0d * (i + 1) / VALUES, output.Length); } }
public void TestFoo() { var rnd = new Random(); void Compress(Slice input) { Log($"IN [{input.Count}] => {input}"); var writer = new CompressedBitmapWriter(); int r = WordAlignHybridEncoder.CompressTo(input, writer); var compressed = writer.GetBuffer(); Log($"OUT [{compressed.Count}] => {compressed} [r={r}]"); var sb = new StringBuilder(); Log(WordAlignHybridEncoder.DumpCompressed(compressed).ToString()); Log(); } Compress(Slice.FromString("This is a test of the emergency broadcast system")); // all zeroes (multiple of 31 bits) Compress(Slice.Repeat(0, 62)); // all zeroes (with padding) Compress(Slice.Repeat(0, 42)); // all ones (multiple of 31 bits) Compress(Slice.Repeat(255, 62)); // all ones (with padding) Compress(Slice.Repeat(255, 42)); // random stuff (multiple of 31 bits) Compress(Slice.Random(rnd, 42)); // random stuff (with padding) Compress(Slice.Random(rnd, 42)); // mostly zeroes void SetBit(byte[] b, int p) { b[p >> 3] |= (byte)(1 << (p & 7)); } byte[] MostlyZeroes(int count) { var buf = new byte[1024]; for (int i = 0; i < count; i++) { SetBit(buf, rnd.Next(buf.Length * 8)); } Log("Mostly zeroes: " + count); return(buf); } Compress(MostlyZeroes(1).AsSlice()); Compress(MostlyZeroes(10).AsSlice()); Compress(MostlyZeroes(42).AsSlice()); Compress(MostlyZeroes(100).AsSlice()); // mostly ones void ClearBit(byte[] b, int p) { b[p >> 3] &= (byte)~(1 << (p & 7)); } byte[] MostlyOnes(int count) { var buf = new byte[1024]; for (int i = 0; i < buf.Length; i++) { buf[i] = 0xFF; } for (int i = 0; i < 10; i++) { ClearBit(buf, rnd.Next(buf.Length * 8)); } Log("Mostly ones: " + count); return(buf); } Compress(MostlyOnes(1).AsSlice()); Compress(MostlyOnes(10).AsSlice()); Compress(MostlyOnes(42).AsSlice()); Compress(MostlyOnes(100).AsSlice()); // progressive bool TestBit(byte[] b, int p) => (b[p >> 3] & (1 << (p & 7))) != 0; const int VALUES = 8192; var buffer = new byte[VALUES / 8]; var output = new CompressedBitmapWriter(); WordAlignHybridEncoder.CompressTo(buffer.AsSlice(), output); Log($"{0}\t{output.Length}\t1024"); for (int i = 0; i < VALUES / 8; i++) { int p; do { p = rnd.Next(VALUES); }while (TestBit(buffer, p)); SetBit(buffer, p); output.Reset(); WordAlignHybridEncoder.CompressTo(buffer.AsSlice(), output); Log($"{1.0d * (i + 1) / VALUES}\t{output.Length}\t1024"); } }