Exemplo n.º 1
0
    static void TestBitStream()
    {
        BitStreamWriter writer = new BitStreamWriter(2);
        BitStreamReader reader = new BitStreamReader();

        // Resize, basic int
        writer.WriteInt32(0x12345678);
        writer.WriteInt8(39);
        writer.WriteInt(2, 2);
        writer.WriteInt(87, 7);
        writer.WriteInt(33, 6);
        reader.SetBytes(writer.DumpBytes());
        Assert.IsTrue(reader.ReadInt32() == 0x12345678);
        Assert.IsTrue(reader.ReadInt8() == 39);
        Assert.IsTrue(reader.ReadInt(2) == 2);
        Assert.IsTrue(reader.ReadInt(7) == 87);
        Assert.IsTrue(reader.ReadInt(6) == 33);

        // Concatnat bits
        // 1, 0000010 11000011, 1
        // should be 00000101 10000110 00000011
        writer.WriteBool(true);
        writer.WriteInt16(707);
        writer.WriteBool(true);
        var data = writer.DumpBytes();

        Assert.IsTrue(data.Length == 3);
        Assert.IsTrue(data[0] == 0x05);
        Assert.IsTrue(data[1] == 0x86);
        Assert.IsTrue(data[2] == 0x03);
        reader.SetBytes(data);
        Assert.IsTrue(reader.ReadBool());
        Assert.IsTrue(reader.ReadInt16() == 707);
        Assert.IsTrue(reader.ReadBool());

        // float
        writer.WriteBool(false);
        writer.WriteFloat(12345.012345f);
        writer.WriteBool(true);
        reader.SetBytes(writer.DumpBytes());
        Assert.IsFalse(reader.ReadBool());
        Assert.IsTrue(reader.ReadFloat() == 12345.012345f);
        Assert.IsTrue(reader.ReadBool());

        // vector3, quaternion
        Vector3    vec = new Vector3(-0.51231f, 0.113123f, 1.1231123f);
        Quaternion q   = new Quaternion(-0.123f, 0.345f, 0.678f, -0.23f);

        q.Normalize();
        writer.WriteQuaternionRot(q);
        writer.WriteBool(false);
        writer.WriteVector3(Vector3.one);
        writer.WriteVector3(vec);
        reader.SetBytes(writer.DumpBytes());
        Assert.IsTrue(IsApprox(reader.ReadQuaternionRot(), q));
        Assert.IsFalse(reader.ReadBool());
        Assert.IsTrue(reader.ReadVector3() == Vector3.one);
        Assert.IsTrue(reader.ReadVector3() == vec);
    }