1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
|
/*
* Copyright (c) 2023, Tim Schumacher <timschumi@gmx.de>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/BitStream.h>
#include <AK/MemoryStream.h>
#include <LibTest/TestCase.h>
// Note: This does not do any checks on the internal representation, it just ensures that the behavior of the input and output streams match.
TEST_CASE(little_endian_bit_stream_input_output_match)
{
auto memory_stream = make<AllocatingMemoryStream>();
// Note: The bit stream only ever reads from/writes to the underlying stream in one byte chunks,
// so testing with sizes that will not trigger a write will yield unexpected results.
auto bit_write_stream = MUST(LittleEndianOutputBitStream::construct(MaybeOwned<AK::Stream>(*memory_stream)));
auto bit_read_stream = MUST(LittleEndianInputBitStream::construct(MaybeOwned<AK::Stream>(*memory_stream)));
// Test two mirrored chunks of a fully mirrored pattern to check that we are not dropping bits.
{
MUST(bit_write_stream->write_bits(0b1111u, 4));
MUST(bit_write_stream->write_bits(0b1111u, 4));
auto result = MUST(bit_read_stream->read_bits(4));
EXPECT_EQ(0b1111u, result);
result = MUST(bit_read_stream->read_bits(4));
EXPECT_EQ(0b1111u, result);
}
{
MUST(bit_write_stream->write_bits(0b0000u, 4));
MUST(bit_write_stream->write_bits(0b0000u, 4));
auto result = MUST(bit_read_stream->read_bits(4));
EXPECT_EQ(0b0000u, result);
result = MUST(bit_read_stream->read_bits(4));
EXPECT_EQ(0b0000u, result);
}
// Test two mirrored chunks of a non-mirrored pattern to check that we are writing bits within a pattern in the correct order.
{
MUST(bit_write_stream->write_bits(0b1000u, 4));
MUST(bit_write_stream->write_bits(0b1000u, 4));
auto result = MUST(bit_read_stream->read_bits(4));
EXPECT_EQ(0b1000u, result);
result = MUST(bit_read_stream->read_bits(4));
EXPECT_EQ(0b1000u, result);
}
// Test two different chunks to check that we are not confusing their order.
{
MUST(bit_write_stream->write_bits(0b1000u, 4));
MUST(bit_write_stream->write_bits(0b0100u, 4));
auto result = MUST(bit_read_stream->read_bits(4));
EXPECT_EQ(0b1000u, result);
result = MUST(bit_read_stream->read_bits(4));
EXPECT_EQ(0b0100u, result);
}
// Test a pattern that spans multiple bytes.
{
MUST(bit_write_stream->write_bits(0b1101001000100001u, 16));
auto result = MUST(bit_read_stream->read_bits(16));
EXPECT_EQ(0b1101001000100001u, result);
}
}
// Note: This does not do any checks on the internal representation, it just ensures that the behavior of the input and output streams match.
TEST_CASE(big_endian_bit_stream_input_output_match)
{
auto memory_stream = make<AllocatingMemoryStream>();
// Note: The bit stream only ever reads from/writes to the underlying stream in one byte chunks,
// so testing with sizes that will not trigger a write will yield unexpected results.
auto bit_write_stream = MUST(BigEndianOutputBitStream::construct(MaybeOwned<AK::Stream>(*memory_stream)));
auto bit_read_stream = MUST(BigEndianInputBitStream::construct(MaybeOwned<AK::Stream>(*memory_stream)));
// Test two mirrored chunks of a fully mirrored pattern to check that we are not dropping bits.
{
MUST(bit_write_stream->write_bits(0b1111u, 4));
MUST(bit_write_stream->write_bits(0b1111u, 4));
auto result = MUST(bit_read_stream->read_bits(4));
EXPECT_EQ(0b1111u, result);
result = MUST(bit_read_stream->read_bits(4));
EXPECT_EQ(0b1111u, result);
}
{
MUST(bit_write_stream->write_bits(0b0000u, 4));
MUST(bit_write_stream->write_bits(0b0000u, 4));
auto result = MUST(bit_read_stream->read_bits(4));
EXPECT_EQ(0b0000u, result);
result = MUST(bit_read_stream->read_bits(4));
EXPECT_EQ(0b0000u, result);
}
// Test two mirrored chunks of a non-mirrored pattern to check that we are writing bits within a pattern in the correct order.
{
MUST(bit_write_stream->write_bits(0b1000u, 4));
MUST(bit_write_stream->write_bits(0b1000u, 4));
auto result = MUST(bit_read_stream->read_bits(4));
EXPECT_EQ(0b1000u, result);
result = MUST(bit_read_stream->read_bits(4));
EXPECT_EQ(0b1000u, result);
}
// Test two different chunks to check that we are not confusing their order.
{
MUST(bit_write_stream->write_bits(0b1000u, 4));
MUST(bit_write_stream->write_bits(0b0100u, 4));
auto result = MUST(bit_read_stream->read_bits(4));
EXPECT_EQ(0b1000u, result);
result = MUST(bit_read_stream->read_bits(4));
EXPECT_EQ(0b0100u, result);
}
// Test a pattern that spans multiple bytes.
{
MUST(bit_write_stream->write_bits(0b1101001000100001u, 16));
auto result = MUST(bit_read_stream->read_bits(16));
EXPECT_EQ(0b1101001000100001u, result);
}
}
|