15 const char buffer[] = {0x01, 0x23, 0x45, 0x67, (char)0x89, (
char)0xAB, (char)0xCD, (
char)0xEF};
17 CHECK(bitstream.bufferSizeInBytes() ==
sizeof(buffer));
18 CHECK(bitstream.sizeInBits() ==
sizeof(buffer) * CHAR_BIT);
19 SUBCASE(
"Unsigned integer reads")
22 CHECK_EQ(bitstream.readUnsigned( 0) , ( 0 ));
23 CHECK_EQ(bitstream.readUnsigned( 8) , ( 0x01 ));
24 CHECK_EQ(bitstream.readUnsigned(16) , ( 0x4523 ));
25 CHECK_EQ(bitstream.readUnsigned( 3) , ( 0x67 & 0b111 ));
26 CHECK_EQ(bitstream.readUnsigned(13) , ( 0x8967 >> 3 ));
27 CHECK_EQ(bitstream.readUnsigned(13) , ( 0xCDAB & 0x1FFF));
28 CHECK_EQ(bitstream.readUnsigned( 2) , ((0xCDAB >> 13) & 0b11 ));
29 CHECK_EQ(bitstream.readUnsigned( 9) , ( 0xEFCDAB >> 15 ));
32 SUBCASE(
"Unsigned integer reads with smaller types")
35 CHECK(bitstream.readUnsigned< uint8_t>( 0) == uint8_t( 0));
36 CHECK(bitstream.readUnsigned< uint8_t>( 8) == uint8_t( 0x01));
37 CHECK(bitstream.readUnsigned< uint8_t>( 8) == uint8_t( 0x23));
38 CHECK(bitstream.readUnsigned< uint8_t>( 8) == uint8_t( 0x45));
39 CHECK(bitstream.readUnsigned<uint16_t>(16) == uint16_t(0x8967));
42 SUBCASE(
"Unsigned integer 8-bit or less reads")
45 CHECK_EQ(bitstream.readUnsigned8OrLess(0), ( 0 ));
46 CHECK_EQ(bitstream.readUnsigned8OrLess(8), ( 0x01 ));
47 CHECK_EQ(bitstream.readUnsigned(16), ( 0x4523 ));
48 CHECK_EQ(bitstream.readUnsigned8OrLess(3), ( 0x67 & 0b111 ));
49 CHECK_EQ(bitstream.readUnsigned(13), ( 0x8967 >> 3 ));
50 CHECK_EQ(bitstream.readUnsigned(13), ( 0xCDAB & 0x1FFF ));
51 CHECK_EQ(bitstream.readUnsigned8OrLess(2), ((0xCDAB >> 13) & 0b11 ));
52 CHECK_EQ(bitstream.readUnsigned8OrLess(5), ((0xEFCDAB >> 15) & 0b11111));
53 CHECK_EQ(bitstream.readUnsigned8OrLess(4), ((0xEFCDAB >> 20) & 0b1111 ));
56 SUBCASE(
"Signed integer reads")
59 CHECK_EQ(bitstream.readSigned< 0>() , ( 0 ));
60 CHECK_EQ(bitstream.readSigned< 8>() , ( 0x01 ));
61 CHECK_EQ(bitstream.readSigned<16>() , ( 0x4523 ));
62 CHECK_EQ(bitstream.readSigned< 3>() , ( 0b111 | 0xFFFFFFF8));
63 CHECK_EQ(bitstream.readSigned<13>() , ( 0x8967 >> 3 | 0xFFFFE000));
64 CHECK_EQ(bitstream.readSigned<13>() , ( 0xCDAB & 0x1FFF ));
65 CHECK_EQ(bitstream.readSigned< 2>() , ( 0b10 | 0xFFFFFFFC));
66 CHECK_EQ(bitstream.readSigned< 9>() , ( 0x1DF | 0xFFFFFE00));
70 SUBCASE(
"0 and 1 bits reads")
72 CHECK(bitstream.readBit());
73 CHECK_FALSE(bitstream.readBit());
74 CHECK(bitstream.tell() == 2);
75 CHECK(bitstream.read0Bits() == 0);
76 CHECK(bitstream.tell() == 2);
79 CHECK(bitstream.readSigned<0>() == 0);
80 CHECK(bitstream.readSigned<1>() == -1);
82 CHECK(bitstream.tell() == 2 + 6 + 1);
84 SUBCASE(
"Aligning to byte")
87 bitstream.setPosition(9);
88 bitstream.alignToByte();
89 CHECK(bitstream.tell() == 16);
91 bitstream.alignToByte();
92 CHECK(bitstream.tell() == 16);
95 SUBCASE(
"Subview of same size")
97 BitStreamView subView = bitstream.createSubView(bitstream.sizeInBits());
98 CHECK(subView.
sizeInBits() == bitstream.sizeInBits());
99 CHECK(subView.
tell() == bitstream.tell());
101 SUBCASE(
"Subview of same size and offset in first byte")
104 BitStreamView subView = bitstream.createSubView(bitstream.sizeInBits() - 5);
105 CHECK(subView.bufferSizeInBytes() == bitstream.bufferSizeInBytes());
106 CHECK(subView.
sizeInBits() == bitstream.sizeInBits() - 5);
107 CHECK(subView.
tell() == 0);
109 SUBCASE(
"Subview of same size and offset in 2nd byte")
111 const size_t offset = CHAR_BIT + 2;
113 BitStreamView subView = bitstream.createSubView(bitstream.sizeInBits() - offset);
114 CHECK(subView.bufferSizeInBytes() ==
115 bitstream.bufferSizeInBytes() - 1);
116 CHECK(subView.
tell() == 0);
118 SUBCASE(
"Subview of size 0")
120 bitstream.
setPosition(rand() % bitstream.sizeInBits());
122 CHECK(subView.bufferSizeInBytes() == 0);
123 CHECK(subView.
tell() == 0);
125 CHECK(bitstream.good());
void setPosition(size_t newPosition)
Set the current position, in bits.
size_t tell() const
Returns the current position in the stream in bits.
size_t sizeInBits() const
Returns the total size of the current stream buffer in bytes.
TEST_CASE("BitStreamView read.")
Test the Bitstream usage in read-only.
Provides access to a variable bitsize values stream.