1 // Copyright 2017 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "net/tools/huffman_trie/bit_writer.h"
6 #include "testing/gmock/include/gmock/gmock.h"
7 #include "testing/gtest/include/gtest/gtest.h"
8
9 namespace net::huffman_trie {
10
11 namespace {
12
13 // Test that single bits are written to the buffer correctly.
TEST(BitWriterTest,WriteBit)14 TEST(BitWriterTest, WriteBit) {
15 BitWriter writer;
16
17 EXPECT_EQ(0U, writer.position());
18 EXPECT_EQ(0U, writer.bytes().size());
19
20 writer.WriteBit(0);
21
22 EXPECT_EQ(1U, writer.position());
23
24 writer.WriteBit(1);
25 writer.WriteBit(0);
26 writer.WriteBit(1);
27 writer.WriteBit(0);
28 writer.WriteBit(1);
29 writer.WriteBit(0);
30 writer.WriteBit(1);
31
32 EXPECT_EQ(8U, writer.position());
33
34 writer.WriteBit(0);
35
36 EXPECT_EQ(9U, writer.position());
37
38 writer.WriteBit(1);
39 writer.WriteBit(0);
40
41 EXPECT_EQ(11U, writer.position());
42
43 // Flush should pad the current byte with zero's until it's full.
44 writer.Flush();
45
46 // The writer should have 2 bytes now even though we only wrote 11 bits.
47 EXPECT_EQ(16U, writer.position());
48
49 // 0 + 1 + 0 + 1 + 0 + 1 + 0 + 1 + 0 + 1 + 0 + 00000 (padding) = 0x5540.
50 EXPECT_THAT(writer.bytes(), testing::ElementsAre(0x55, 0x40));
51 }
52
53 // Test that when multiple bits are written to the buffer, they are appended
54 // correctly.
TEST(BitWriterTest,WriteBits)55 TEST(BitWriterTest, WriteBits) {
56 BitWriter writer;
57
58 // 0xAA is 10101010 in binary. WritBits will write the n least significant
59 // bits where n is given as the second parameter.
60 writer.WriteBits(0xAA, 1);
61 EXPECT_EQ(1U, writer.position());
62 writer.WriteBits(0xAA, 2);
63 EXPECT_EQ(3U, writer.position());
64 writer.WriteBits(0xAA, 3);
65 EXPECT_EQ(6U, writer.position());
66 writer.WriteBits(0xAA, 2);
67 EXPECT_EQ(8U, writer.position());
68 writer.WriteBits(0xAA, 2);
69 EXPECT_EQ(10U, writer.position());
70
71 // Flush should pad the current byte with zero's until it's full.
72 writer.Flush();
73
74 // The writer should have 2 bytes now even though we only wrote 10 bits.
75 EXPECT_EQ(16U, writer.position());
76
77 // 0 + 10 + 010 + 10 + 10 + 000000 (padding) = 0x4A80
78 EXPECT_THAT(writer.bytes(), testing::ElementsAre(0x4A, 0x80));
79 }
80
81 // Test that buffering works correct when the methods are mixed.
TEST(BitWriterTest,WriteBoth)82 TEST(BitWriterTest, WriteBoth) {
83 BitWriter writer;
84
85 // 0xAA is 10101010 in binary. WritBits will write the n least significant
86 // bits where n is given as the second parameter.
87 writer.WriteBits(0xAA, 1);
88 EXPECT_EQ(1U, writer.position());
89 writer.WriteBit(1);
90 writer.WriteBits(0xAA, 2);
91 EXPECT_EQ(4U, writer.position());
92 writer.WriteBits(0xAA, 3);
93 EXPECT_EQ(7U, writer.position());
94 writer.WriteBit(1);
95 EXPECT_EQ(8U, writer.position());
96
97 writer.WriteBits(0xAA, 2);
98 writer.WriteBit(0);
99 EXPECT_EQ(11U, writer.position());
100
101 // Flush should pad the current byte with zero's until it's full.
102 writer.Flush();
103
104 // The writer should have 2 bytes now even though we only wrote 10 bits.
105 EXPECT_EQ(16U, writer.position());
106
107 // 0 + 1 + 10 + 010 + 1 + 10 + 0 + 00000 (padding) = 0x6580
108 EXPECT_THAT(writer.bytes(), testing::ElementsAre(0x65, 0x80));
109 }
110
111 } // namespace
112
113 } // namespace net::huffman_trie
114