Esempio n. 1
0
    static void test_bitpacker()
    {
        Log("test_bitpacker");
        const int BufferSize = 256;
        var       buffer     = new uint[BufferSize];
        var       writer     = new Network.BitWriter();

        writer.Start(buffer);
        IsTrue(writer.GetTotalBytes() == BufferSize);
        IsTrue(writer.GetBitsWritten() == 0);
        IsTrue(writer.GetBytesWritten() == 0);
        IsTrue(writer.GetBitsAvailable() == BufferSize * 8);

        writer.Bits(0, 1);
        writer.Bits(1, 1);
        writer.Bits(10, 8);
        writer.Bits(255, 8);
        writer.Bits(1000, 10);
        writer.Bits(50000, 16);
        writer.Bits(9999999, 32);
        writer.Finish();
        const int bitsWritten = 1 + 1 + 8 + 8 + 10 + 16 + 32;

        IsTrue(writer.GetBytesWritten() == 10);
        IsTrue(writer.GetTotalBytes() == BufferSize);
        IsTrue(writer.GetBitsWritten() == bitsWritten);
        IsTrue(writer.GetBitsAvailable() == BufferSize * 8 - bitsWritten);

        int       bytesWritten         = writer.GetBytesWritten();
        const int ExpectedBytesWritten = 10;

        IsTrue(bytesWritten == ExpectedBytesWritten);

        var readBuffer = writer.GetData();

        IsTrue(readBuffer.Length == ExpectedBytesWritten);

        var reader = new BitReader();

        reader.Start(readBuffer);
        IsTrue(reader.GetBitsRead() == 0);
        IsTrue(reader.GetBitsRemaining() == bytesWritten * 8);

        var a = reader.Bits(1);
        var b = reader.Bits(1);
        var c = reader.Bits(8);
        var d = reader.Bits(8);
        var e = reader.Bits(10);
        var f = reader.Bits(16);
        var g = reader.Bits(32);

        reader.Finish();
        IsTrue(a == 0);
        IsTrue(b == 1);
        IsTrue(c == 10);
        IsTrue(d == 255);
        IsTrue(e == 1000);
        IsTrue(f == 50000);
        IsTrue(g == 9999999);
        IsTrue(reader.GetBitsRead() == bitsWritten);
        IsTrue(reader.GetBitsRemaining() == bytesWritten * 8 - bitsWritten);
    }