Example #1
0
        public void TestReadValueByByteIndex()
        {
            BReader br = new BReader(new byte[] { 0x12, 0x34, 0x56, 0x78 });

            br.ReadValueByByteIndex <long>(0, 4).Should().Be(0x12345678);
            br.ReadValueByByteIndex <long>(1, 3).Should().Be(0x345678);
            br.Invoking(_ => _.ReadValueByByteIndex <short>(0, 4)).Should().Throw <ArgumentOutOfRangeException>();
            br.Invoking(_ => _.ReadValueByByteIndex <string>(0, 4)).Should().Throw <NotSupportedException>();
            br.Invoking(_ => _.ReadValueByByteIndex <bool>(0, 4)).Should().Throw <NotSupportedException>();
            br.Invoking(_ => _.ReadValueByByteIndex <Foo>(0, 4)).Should().Throw <NotSupportedException>();
            new BReader(new byte[] { 0x12, 0x34, 0x56, 0x78 }).ReadValueByByteIndex <long>(0, 4, Endian.SmallEndian).Should().Be(0x78563412);
        }
Example #2
0
        public void TestReadValueByBitIndex()
        {
            BReader br = new BReader(new byte[] { 0x12, 0x34 });

            br.ReadValueByBitIndex <int>(0, 16).Should().Be(0x3412);
            br.ReadValueByBitIndex <long>(4, 8).Should().Be(0x41);
            br.ReadValueByBitIndex <bool>(13, 1).Should().Be(true);
            br.ReadValueByBitIndex <byte>(9, 5).Should().Be(0x1A);
            br.ReadValueByBitIndex <byte>(1, 1, 5).Should().Be(0x1A);
            br.Invoking(_ => _.ReadValueByBitIndex <byte>(0, 9)).Should().Throw <ArgumentOutOfRangeException>();
            br.Invoking(_ => _.ReadValueByBitIndex <string>(0, 4)).Should().Throw <NotSupportedException>();
            br.Invoking(_ => _.ReadValueByBitIndex <Foo>(0, 4)).Should().Throw <NotSupportedException>();
            br = new BReader(new byte[] { 0x12, 0x34 });
            br.ReadValueByBitIndex <int>(0, 16, Endian.BigEndian).Should().Be(0x482C);
            br.ReadValueByBitIndex <byte>(9, 6, Endian.BigEndian).Should().Be(0x16);
        }
Example #3
0
        public void TestGetSingleByte()
        {
            BReader br = new BReader(new byte[] { 0x11, 0x22, 0x33, 0x44 });

            br[1].Should().Be(0x22);
            br[2].Should().Be(0x33);
            br.Invoking(_ => _[4]).Should().Throw <ArgumentOutOfRangeException>();
        }
Example #4
0
        public void TestGetSingleBit()
        {
            BReader br = new BReader(new byte[] { 0x20, 0x22, 0x33, 0x44 });

            0.Should().Be(br[0, 4]);
            1.Should().Be(br[0, 5]);
            1.Should().Be(br[3, 2]);
            br.Invoking(_ => _[0, 32]).Should().Throw <ArgumentOutOfRangeException>();
        }
Example #5
0
        public void TestReadStringByByteIndex()
        {
            BReader br = new BReader(Encoding.Default.GetBytes("Hello world!"));

            br.ReadStringByByteIndex(0, 5).Should().Be("Hello");
            br.ReadStringByByteIndex(6, 5).Should().Be("world");

            br.Invoking(_ => _.ReadStringByByteIndex(8, 100)).Should().Throw <ArgumentOutOfRangeException>();
            new BReader(Encoding.Default.GetBytes("Hello world!")).ReadStringByByteIndex(6, 5, Endian.SmallEndian).Should().Be("dlrow");
        }
Example #6
0
        public void TestReadStringByBitIndex()
        {
            BReader br = new BReader(Encoding.Default.GetBytes("Hello world!"));

            br.ReadStringByBitIndex(0, 96).Should().Be("Hello world!");
            br.ReadStringByBitIndex(48, 48).Should().Be("world!");
            br.Invoking(_ => _.ReadStringByBitIndex(0, 42)).Should().Throw <FormatException>();
            byte[] bixetBytes = Encoding.Default.GetBytes("bixet");
            br = new BReader(bixetBytes);
            br.ReadStringByBitIndex(0, 40).Should().Be("bixet");
            byte[] bixetComplexBytes = new byte[] { 0x7F, 0b10110001, 0b00110100, 0b10111100, 0b00110010, 0b10111010 };
            br = new BReader(bixetComplexBytes);
            br.ReadStringByBitIndex(7, 40).Should().Be("bixet");
        }