Ejemplo n.º 1
0
        public u16 u16(BitBufferReader <FibonacciDecoding> b)
        {
            bool lastbit = false;
            u8   fib     = 0;
            u16  result  = 0;

            while (true) // TODO: prevent loop with sane check
            {
                // TODO: optimize
                if (b.b())
                {
                    if (lastbit)
                    {
                        break;
                    }

                    result += Coder.Fibonacci.u16Lookup[fib];
                    lastbit = true;
                }
                else
                {
                    lastbit = false;
                }

                fib++;
            }

            return((u16)(result - 1));
        }
        public void BitsRead()
        {
            var writer = new BitBufferWriter <SevenBitEncoding <u32ArrayMemory> >();

            Assert.AreEqual(0, writer.BitsWritten);
            writer.b(true);
            Assert.AreEqual(1, writer.BitsWritten);
            writer.u8(123);
            Assert.AreEqual(9, writer.BitsWritten);
            writer.i16(12345);
            Assert.AreEqual(33, writer.BitsWritten);
            writer.i32(1234567890);
            Assert.AreEqual(73, writer.BitsWritten);
            var data   = writer.ToArray();
            var reader = new BitBufferReader <SevenBitDecoding <u32ArrayMemory> >();

            reader.CopyFrom(data);

            Assert.AreEqual(0, reader.BitsRead);
            reader.b();
            Assert.AreEqual(1, reader.BitsRead);
            reader.u8();
            Assert.AreEqual(9, reader.BitsRead);
            reader.i16();
            Assert.AreEqual(33, reader.BitsRead);
            reader.i32();
            Assert.AreEqual(73, reader.BitsRead);
        }
Ejemplo n.º 3
0
        public void i16ReadWrite()
        {
            var writer = new BitBufferWriter <SevenBitEncoding <u32ArrayMemory> >();

            writer.i16(i16.MinValue);
            writer.i16(i16.MinValue / 2);
            writer.i16(0);
            i16 half = i16.MaxValue / 2;

            writer.i16(half);
            writer.i16(i16.MaxValue);
            writer.Align();
            var allocated = new byte[i16.MaxValue];

            writer.ToSpan(allocated);
            var reader = new BitBufferReader <SevenBitDecoding <u32ArrayMemory> >(allocated.Length);

            reader.CopyFrom(allocated);
            Assert.AreEqual(i16.MinValue, reader.i16());
            Assert.AreEqual(i16.MinValue / 2, reader.i16());
            Assert.AreEqual(0, reader.i16());
            Assert.AreEqual(half, reader.i16Peek());
            Assert.AreEqual(half, reader.i16());
            Assert.AreEqual(i16.MaxValue, reader.i16());
        }
        public void RandomManyTimes()
        {
            var writer = new BitBufferWriter <SevenBitEncoding <u32ArrayMemory> >();
            var reader = new BitBufferReader <SevenBitDecoding <u32ArrayMemory> >();
            var random = new Random(42);

            for (var i = 0; i < i16.MaxValue; i++)
            {
                for (var j = 0; j < BitBufferLimits.MtuIeee802; j++)
                {
                    if (random.Next() % 11 == 0)
                    {
                        writer.i64(long.MaxValue);
                    }
                    if (random.Next() % 7 == 0)
                    {
                        writer.i32(int.MaxValue);
                    }
                    if (random.Next() % 5 == 0)
                    {
                        writer.i16(i16.MaxValue);
                    }
                    if (random.Next() % 3 == 0)
                    {
                        writer.b(true);
                    }
                }


                var result = writer.ToArray();
                reader.CopyFrom(result);
                writer.Reset();
            }
        }
        public void ResetSet()
        {
            var writer = new BitBufferWriter <SevenBitEncoding <u32ArrayMemory> >(8);

            writer.i32(666);
            var bitsWritten   = writer.BitsWritten;
            var bitsAvailable = writer.BitsAvailable;

            writer.Reset();
            Assert.AreEqual(0, writer.BitsWritten);

            writer.i32(666);
            Assert.AreEqual(bitsWritten, writer.BitsWritten);
            Assert.AreEqual(bitsAvailable, writer.BitsAvailable);
            writer.i32(-273);
            writer.b(true);
            writer.i64(1234567890);
            var reader = new BitBufferReader <SevenBitDecoding <u32ArrayMemory> >();

            reader.CopyFrom(writer.ToArray());
            Assert.AreEqual(666, reader.i32());
            var bitsRead = reader.BitsRead;

            reader.i32();
            reader.SetPosition(bitsRead);
            Assert.AreEqual(-273, reader.i32());
            var bitPosition = reader.BitsRead;

            reader.b();
            reader.SetPosition(bitPosition);
            Assert.True(reader.b());
        }
Ejemplo n.º 6
0
        public void i32ReadOutOfRange()
        {
            var reader = new BitBufferReader <SevenBitDecoding <u32ArrayMemory> >();

            reader.CopyFrom(new u8[666]);
            Assert.Throws <ArgumentException>(() => reader.i32(666, 123));
            Assert.Throws <ArgumentOutOfRangeException>(() => reader.i32(255));
            Assert.Throws <ArgumentOutOfRangeException>(() => reader.i32(33));
        }
Ejemplo n.º 7
0
        public void int32VerySmallReader()
        {
            var smallReader = new BitBufferReader <SevenBitDecoding <u32ArrayMemory> >(1);

            smallReader.CopyFrom(new u8[4]);
            smallReader.i32(i32.MinValue, i32.MaxValue);
            Assert.True(smallReader.BitsRead > 0);
            smallReader.i32(i32.MinValue, i32.MaxValue);
            Assert.Throws <ArgumentOutOfRangeException>(() => smallReader.i32(i32.MinValue, i32.MaxValue));
        }
Ejemplo n.º 8
0
        public void f64ReadWrite()
        {
            var writer = new BitBufferWriter <SevenBitEncoding <u32ArrayMemory> >();

            writer.f64(f64.MaxValue);
            var data   = writer.ToArray();
            var reader = new BitBufferReader <SevenBitDecoding <u32ArrayMemory> >();

            reader.CopyFrom(data);
            Assert.AreEqual(f64.MaxValue, reader.f64());
        }
Ejemplo n.º 9
0
        public void f32Bits()
        {
            var writer = new BitBufferWriter <SevenBitEncoding <u32ArrayMemory> >();

            writer.f32(1f, 0f, 1f, 1);
            var data   = writer.ToArray();
            var reader = new BitBufferReader <SevenBitDecoding <u32ArrayMemory> >();

            reader.CopyFrom(data);
            Assert.AreEqual(1, reader.f32(0f, 1f, 1));
        }
Ejemplo n.º 10
0
        public void Capacity()
        {
            var writer = new BitBufferWriter <SevenBitEncoding <u32ArrayMemory> >(100);

            Assert.AreEqual(0, writer.LengthWritten);
            var received = new byte[2000];
            var reader   = new BitBufferReader <SevenBitDecoding <u32ArrayMemory> >();

            reader.CopyFrom(received);
            Assert.AreEqual(0, writer.LengthWritten);
        }
Ejemplo n.º 11
0
        public void f32MinMaxRequired()
        {
            var writer = new BitBufferWriter <SevenBitEncoding <u32ArrayMemory> >();

            writer.f32(1234.5f, 0, 12345.6f, 0.01f);
            var data   = writer.ToArray();
            var reader = new BitBufferReader <SevenBitDecoding <u32ArrayMemory> >();

            reader.CopyFrom(data);
            Assert.AreEqual(1234.5f, reader.f32(0, 12345.6f, 0.01f));
        }
Ejemplo n.º 12
0
        public void PeekBool()
        {
            var writer = new BitBufferWriter <SevenBitEncoding <u32ArrayMemory> >();

            writer.b(true);
            var data   = writer.ToArray();
            var reader = new BitBufferReader <SevenBitDecoding <u32ArrayMemory> >();

            reader.CopyFrom(data);
            Assert.True(reader.bPeek());
            Assert.True(reader.b());
        }
Ejemplo n.º 13
0
        public void u8MaxValueWritePeek()
        {
            var writer = new BitBufferWriter <SevenBitEncoding <u32ArrayMemory> >();

            writer.u8(u8.MaxValue);
            var data   = writer.ToArray();
            var reader = new BitBufferReader <SevenBitDecoding <u32ArrayMemory> >();

            reader.CopyFrom(data);
            Assert.AreEqual(u8.MaxValue, reader.u8Peek());
            Assert.AreEqual(reader.u8Peek(), reader.u8());
        }
        public void RawToEncodedReadWrite()
        {
            var rawWriter = new BitBufferWriter <RawEncoding <u32ArrayMemory> >();

            rawWriter.i32(i32.MaxValue - 13);
            rawWriter.u32(u32.MaxValue - 666);
            var rawReader = new BitBufferReader <RawDecoding <u32ArrayMemory> >();
            var data      = rawWriter.ToArray();

            rawReader.CopyFrom(data);
            Assert.AreEqual(i32.MaxValue - 13, rawReader.i32());
            Assert.AreEqual(u32.MaxValue - 666, rawReader.u32());
        }
Ejemplo n.º 15
0
        public void f32ReadWrite()
        {
            var writer = new BitBufferWriter <SevenBitEncoding <u32ArrayMemory> >();

            writer.f32(123.456f);
            writer.Align();
            var data   = writer.ToArray();
            var reader = new BitBufferReader <SevenBitDecoding <u32ArrayMemory> >();

            reader.CopyFrom(data);
            Assert.AreEqual(123.456f, reader.f32Peek());
            Assert.AreEqual(123.456f, reader.f32());
        }
Ejemplo n.º 16
0
        public void StringWriteRead()
        {
            var writer = new BitBufferWriter <SevenBitEncoding <u32ArrayMemory> >();

            writer.c("lahoda.prо/минск");
            var allocated = new byte[ushort.MaxValue];

            writer.ToSpan(allocated);
            var reader = new BitBufferReader <SevenBitDecoding <u32ArrayMemory> >(allocated.Length);

            reader.CopyFrom(allocated);
            Assert.AreEqual("lahoda.prо/минск", reader.String());
        }
Ejemplo n.º 17
0
        public void u16Bdiff()
        {
            var writer = new BitBufferWriter <SevenBitEncoding <u32ArrayMemory> >();

            writer.u16BDiff(0, 1);
            writer.u16BDiff(1, 1);
            var data   = writer.ToArray();
            var reader = new BitBufferReader <SevenBitDecoding <u32ArrayMemory> >();

            reader.CopyFrom(data);
            Assert.AreEqual(1, reader.u16BDiff(0));
            Assert.AreEqual(1, reader.u16BDiff(1));
        }
Ejemplo n.º 18
0
        public void u16ReadWriteLimits()
        {
            var writer = new BitBufferWriter <SevenBitEncoding <u32ArrayMemory> >();

            writer.u16(2, 0, 10);
            writer.u16(1, 3);
            var data   = writer.ToArray();
            var reader = new BitBufferReader <SevenBitDecoding <u32ArrayMemory> >();

            reader.CopyFrom(data);
            Assert.AreEqual(2, reader.i16(0, 10));
            Assert.AreEqual(1, reader.u16(3));
        }
Ejemplo n.º 19
0
        public void i32BdiffLimits()
        {
            var writer = new BitBufferWriter <SevenBitEncoding <u32ArrayMemory> >();

            writer.i32BDiff(0, 1, -1, 10);
            writer.i32BDiff(1, 1, 0, 100);
            var data   = writer.ToArray();
            var reader = new BitBufferReader <SevenBitDecoding <u32ArrayMemory> >();

            reader.CopyFrom(data);
            Assert.AreEqual(1, reader.i32BDiff(0, -1, 10));
            Assert.AreEqual(1, reader.i32BDiff(1, 0, 100));
        }
Ejemplo n.º 20
0
        public void BoolWritePeek128()
        {
            var writer = new BitBufferWriter <SevenBitEncoding <u32ArrayMemory> >();

            writer.b(true);
            var data   = writer.ToArray();
            var reader = new BitBufferReader <SevenBitDecoding <u32ArrayMemory> >();

            reader.CopyFrom(data);
            for (int i = 0; i < 128; i++)
            {
                Assert.True(reader.bPeek());
            }
        }
Ejemplo n.º 21
0
        public void u8WriteRead()
        {
            var writer = new BitBufferWriter <SevenBitEncoding <u32ArrayMemory> >();

            writer.u8(u8.MaxValue);
            writer.Align();
            var allocated = new u8[ushort.MaxValue];

            writer.ToSpan(allocated);
            var reader = new BitBufferReader <SevenBitDecoding <u32ArrayMemory> >(allocated.Length);

            reader.CopyFrom(allocated);
            Assert.AreEqual(u8.MaxValue, reader.u8());
        }
Ejemplo n.º 22
0
        public void BoolReadWrite()
        {
            var writer = new BitBufferWriter <SevenBitEncoding <u32ArrayMemory> >();

            writer.b(true);
            writer.Align();
            var allocated = new byte[ushort.MaxValue];

            writer.ToSpan(allocated);
            var reader = new BitBufferReader <SevenBitDecoding <u32ArrayMemory> >(allocated.Length);

            reader.CopyFrom(allocated);
            Assert.True(reader.b());
        }
        public void u8ArrayReadLimit()
        {
            var writer = new BitBufferWriter <SevenBitEncoding <u32ArrayMemory> >();
            var input  = new byte[writer.Options.U8SpanLengthMax];

            writer.u8(input);
            var data   = writer.ToArray();
            var reader = new BitBufferReader <SevenBitDecoding <u32ArrayMemory> >();

            reader.CopyFrom(data);

            Assert.Throws <ArgumentException>(() => reader.u8(new byte[1]));
            Assert.Throws <ArgumentException>(() => reader.u8(new byte[writer.Options.U8SpanLengthMax], writer.Options.U8SpanLengthMax + 1));
        }
        public void u8ArrayMaxWriteRead()
        {
            var writer = new BitBufferWriter <SevenBitEncoding <u32ArrayMemory> >();
            var input  = new byte[writer.Options.U8SpanLengthMax];

            writer.u8(input);
            var allocated = new byte[ushort.MaxValue];

            writer.ToSpan(allocated);
            var reader = new BitBufferReader <SevenBitDecoding <u32ArrayMemory> >(allocated.Length);

            reader.CopyFrom(allocated);
            Assert.AreEqual(writer.Options.U8SpanLengthMax, reader.u8SpanLengthPeek());
        }
Ejemplo n.º 25
0
        private void u32ReadWrite <TEncoder, TDecoder>()
            where TEncoder : struct, ICompression <RawBitWriter <u32ArrayMemory> >
            where TDecoder : struct, IDecompression <BitBufferReader <TDecoder> >
        {
            var writer = new BitBufferWriter <TEncoder>();

            writer.u32(u32.MinValue);
            writer.u32(u32.MaxValue);
            var data   = writer.ToArray();
            var reader = new BitBufferReader <TDecoder>();

            reader.CopyFrom(data);
            Assert.AreEqual(u32.MinValue, reader.u32());
            Assert.AreEqual(u32.MaxValue, reader.u32());
        }
Ejemplo n.º 26
0
        public void AngleQuantization()
        {
            var buffer = new BitBufferWriter <SevenBitEncoding <u32ArrayMemory> >();
            var angle  = 35901u;

            buffer.u32(angle, 0, 36000);
            Assert.AreEqual(16, buffer.BitsWritten);
            var data   = buffer.ToArray();
            var reader = new BitBufferReader <SevenBitDecoding <u32ArrayMemory> >();

            reader.CopyFrom(data);
            var result = reader.f32(0f, 360f, 0.01f);

            Assert.AreEqual(359.01f, result, 2);
        }
Ejemplo n.º 27
0
        public void AngleHalf()
        {
            var buffer = new BitBufferWriter <SevenBitEncoding <u32ArrayMemory> >();
            var angle  = 359.1f;

            buffer.f32Half(angle);
            Assert.AreEqual(16, buffer.BitsWritten);
            var data   = buffer.ToArray();
            var reader = new BitBufferReader <SevenBitDecoding <u32ArrayMemory> >();

            reader.CopyFrom(data);
            var result = reader.f32Half();

            Assert.AreEqual(359, result);
        }
Ejemplo n.º 28
0
        public void u32ReadWriteLimits <TEncoder, TDecoder>()
            where TEncoder : struct, ICompression <RawBitWriter <u32ArrayMemory> >
            where TDecoder : struct, IDecompression <RawBitReader <u32ArrayMemory> >
        {
            var writer = new BitBufferWriter <TEncoder>();

            writer.u32(123123, 0, 13213123);
            writer.u32(123, 20);
            var data   = writer.ToArray();
            var reader = new BitBufferReader <TDecoder>();

            reader.CopyFrom(data);
            Assert.AreEqual(123123u, reader.u32(0, 13213123));
            Assert.AreEqual(123u, reader.u32(20));
        }
Ejemplo n.º 29
0
        public void i8ReadWriteLimits()
        {
            var writer = new BitBufferWriter <SevenBitEncoding <u32ArrayMemory> >();

            writer.i8(13, 0, 14);
            writer.i8(2, 4);
            var bitsWritten = writer.BitsWritten;
            var data        = writer.ToArray();
            var reader      = new BitBufferReader <SevenBitDecoding <u32ArrayMemory> >();

            reader.CopyFrom(data);
            Assert.AreEqual(13, reader.i8(0, 14));
            Assert.AreEqual(2, reader.i8(4));
            Assert.AreEqual(bitsWritten, reader.BitsRead);
        }
Ejemplo n.º 30
0
        public void i16ReadWriteLimits()
        {
            var writer = new BitBufferWriter <SevenBitEncoding <u32ArrayMemory> >();

            writer.i16(-1, -2, 2);
            writer.i16(-1, 4);
            var allocated = new byte[i16.MaxValue];

            writer.ToSpan(allocated);
            var reader = new BitBufferReader <SevenBitDecoding <u32ArrayMemory> >(allocated.Length);

            reader.CopyFrom(allocated);
            Assert.AreEqual(-1, reader.i16(-2, 2));
            Assert.AreEqual(-1, reader.i16(4));
        }