Repository navigation
Expand file tree
/
Copy pathBitStreamWriter_test.cpp
More file actions
97 lines (74 loc) · 2.33 KB
/
Copy pathBitStreamWriter_test.cpp
File metadata and controls
97 lines (74 loc) · 2.33 KB
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
#include <gtest/gtest.h>
#include <cstdint>
#include <sstream>
#include <string>
#include <vector>
#include "BitStreamWriter.h"
namespace {
static void pushByteLsbFirst(BitStreamWriter<std::ostringstream>& writer, uint8_t value) {
for (uint32_t bit = 0; bit < 8; ++bit) {
writer.pushbit((value >> bit) & 1U);
}
}
static std::vector<uint8_t> toBytes(const std::string& data) {
return std::vector<uint8_t>(data.begin(), data.end());
}
} // namespace
TEST(BitStreamWriter, FlushOnEmptyProducesNoOutput) {
std::ostringstream out;
BitStreamWriter<std::ostringstream> writer(out);
writer.flush();
EXPECT_TRUE(out.str().empty());
}
TEST(BitStreamWriter, WritesSingleByteLsbFirst) {
std::ostringstream out;
BitStreamWriter<std::ostringstream> writer(out);
pushByteLsbFirst(writer, 0xA5);
writer.flush();
auto bytes = toBytes(out.str());
ASSERT_EQ(bytes.size(), 1U);
EXPECT_EQ(bytes[0], 0xA5);
}
TEST(BitStreamWriter, WritesMultipleBytesInOrder) {
std::ostringstream out;
BitStreamWriter<std::ostringstream> writer(out);
pushByteLsbFirst(writer, 0xAB);
pushByteLsbFirst(writer, 0xCD);
writer.flush();
auto bytes = toBytes(out.str());
ASSERT_EQ(bytes.size(), 2U);
EXPECT_EQ(bytes[0], 0xAB);
EXPECT_EQ(bytes[1], 0xCD);
}
TEST(BitStreamWriter, CommitsAt64BitBoundary) {
std::ostringstream out;
BitStreamWriter<std::ostringstream> writer(out);
const std::vector<uint8_t> expected = {0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE};
for (uint8_t value : expected) {
pushByteLsbFirst(writer, value);
}
auto bytes = toBytes(out.str());
ASSERT_EQ(bytes.size(), expected.size());
EXPECT_EQ(bytes, expected);
}
TEST(BitStreamWriter, DestructorFlushesPendingFullBytes) {
std::ostringstream out;
{
BitStreamWriter<std::ostringstream> writer(out);
pushByteLsbFirst(writer, 0x5A);
}
auto bytes = toBytes(out.str());
ASSERT_EQ(bytes.size(), 1U);
EXPECT_EQ(bytes[0], 0x5A);
}
TEST(BitStreamWriter, FlushPadsPartialByteWithZeros) {
std::ostringstream out;
BitStreamWriter<std::ostringstream> writer(out);
writer.pushbit(1U);
writer.pushbit(0U);
writer.pushbit(1U);
writer.flush();
auto bytes = toBytes(out.str());
ASSERT_EQ(bytes.size(), 1U);
EXPECT_EQ(bytes[0], 0x05);
}