Beispiel #1
0
        public void BufferPutBufferTestBounds()
        {
            for (int offset1 = 0; offset1 < 16; offset1++)
            {
                for (int offset2 = 0; offset2 < 16; offset2++)
                {
                    for (int length = 2; length < 14; length++)
                    {
                        byte[] data = new byte[40];
                        for (int i = 0; i < data.Length; i++)
                        {
                            data[i] = 0xff;
                        }

                        var dBitBuffer = new BitBuffer(data, offset2, length);

                        var bWrite = dBitBuffer;
                        bWrite.Put(false);
                        bWrite.Position += length - 2;
                        bWrite.Put(false);

                        var b = new BitBuffer(new byte[60], offset1);
                        b.Put(dBitBuffer);

                        var oldArray = dBitBuffer.ToArray();
                        b = b.FromStartToPosition();
                        var newArr = b.ToArray();
                        CollectionAssert.AreEqual(oldArray, newArr, "offset1=" + offset1 + ", offset2 = " + offset2 + ", length=" + length);
                        Assert.IsTrue(dBitBuffer.BufferEquals(b), "offset1=" + offset1 + ", offset2 = " + offset2 + ", length=" + length);
                    }
                }
            }
        }
Beispiel #2
0
        public void TestPutGetBitBuffer()
        {
            var b = new BitBuffer(new byte[1024]);
            var r = new Random(0);

            for (int i = 0; i < b.data.Length / 4; i++)
            {
                b.Put(r.Next());
            }

            for (int i = 0; i < 16; i++)
            {
                for (int length = 0; length < 32; length++)
                {
                    for (int offset = 0; offset < 16; offset++)
                    {
                        var c = new BitBuffer(new byte[(int)Math.Ceiling((offset + length) / 8f)], offset);
                        c.Put(b.GetBitsAt(i, length));
                        c = c.FromStartToPosition();
                        CollectionAssert.AreEqual(b.GetBitsAt(i, length).ToArray(), c.ToArray(), "i=" + i + ", length=" + length + ", offset=" + offset);
                        Assert.IsTrue(b.GetBitsAt(i, length).BufferEquals(c), "i=" + i + ", length=" + length + ", offset=" + offset);
                    }
                }
            }
        }
Beispiel #3
0
        public void Test()
        {
            // https://www.h-schmidt.net/FloatConverter/IEEE754.html

            byte[] tmp = new byte[400];

            byte[] expectedMessage = new byte[] {
                0x56, 0x47, 0x68, 0x70, 0x63, 0x79, 0x42, 0x70,
                0x63, 0x79, 0x42, 0x68, 0x49, 0x48, 0x52, 0x6c,
                0x63, 0x33, 0x51, 0x67, 0x62, 0x57, 0x56, 0x7a,
                0x63, 0x32, 0x46, 0x6e, 0x5a, 0x53, 0x45, 0x3d
            };

            for (int off = 0; off < 30; off++)
            {
                for (int len = 300; len < 330; len++)
                {
                    BitBuffer b = new BitBuffer(tmp, off, len);
                    {
                        Assert.AreEqual(off, b.absPosition);
                        Assert.AreEqual(0, b.Position);
                        Assert.AreEqual(off + len, b.absLength);
                        Assert.AreEqual(len, b.Length);
                        Assert.AreEqual(off, b.absOffset);
                    }

                    b.Put((ushort)0x4756);
                    b.Put((byte)0x11);
                    b.Put((byte)0x70);
                    b.PutAt(1 * 8, (ushort)0x6847);
                    b.Put(0x70427963);
                    b.Position += 64;
                    b.Put((ulong)0x7a56576267513363);
                    b.Position = 8 * 8;
                    b.Put(3.67351315E+24f);
                    b.Put(1.01686312E+27f);
                    b.Position += 64;
                    b.Put(1.5152749180821361E-13d);

                    Assert.IsTrue(b.FromStartToPosition().BufferEquals(new BitBuffer(expectedMessage)), "off=" + off + ", len=" + len);

                    Assert.AreEqual(off + expectedMessage.Length * 8, b.absPosition);
                    Assert.AreEqual(off, b.absOffset);
                    Assert.AreEqual(expectedMessage.Length * 8, b.Position);
                    Assert.AreEqual(len, b.Length);
                    Assert.AreEqual(off + len, b.absLength);

                    Assert.IsTrue(b.BufferEquals(b));
                }
            }
        }
Beispiel #4
0
        public void ToArrayTest()
        {
            Random r = new Random(0);

            for (int offset = 0; offset < 16; offset++)
            {
                byte[] data = new byte[40];
                r.NextBytes(data);
                var b = new BitBuffer(new byte[60], offset);
                b.Put(data);
                var newArr = b.FromStartToPosition().ToArray();
                CollectionAssert.AreEqual(data, newArr, "offset=" + offset);
            }
        }
Beispiel #5
0
        public void BufferPutBuffer()
        {
            Random r = new Random(0);

            for (int offset1 = 0; offset1 < 16; offset1++)
            {
                byte[] data = new byte[40];
                r.NextBytes(data);
                for (int offset2 = 0; offset2 < 16; offset2++)
                {
                    var dBitBuffer = new BitBuffer(data, offset2, 4 * 8);
                    var b          = new BitBuffer(new byte[60], offset1);
                    b.Put(dBitBuffer);
                    var oldArray = dBitBuffer.ToArray();
                    b = b.FromStartToPosition();
                    var newArr = b.ToArray();
                    CollectionAssert.AreEqual(oldArray, newArr, "offset1=" + offset1 + ", offset2 = " + offset2);
                    Assert.IsTrue(dBitBuffer.BufferEquals(b), "offset1=" + offset1 + ", offset2 = " + offset2);
                }
            }
        }