• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2018 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "src/base/bit_stream.h"
16 
17 #include <gtest/gtest.h>
18 
19 namespace astc_codec {
20 namespace base {
21 
22 namespace {
23   static constexpr uint64_t kAllBits = 0xFFFFFFFFFFFFFFFF;
24   static constexpr uint64_t k40Bits = 0x000000FFFFFFFFFF;
25 }
26 
TEST(BitStream,Decode)27 TEST(BitStream, Decode) {
28   {
29     BitStream<uint64_t> stream(0, 1);
30 
31     uint64_t bits = kAllBits;
32     EXPECT_TRUE(stream.GetBits(1, &bits));
33     EXPECT_EQ(bits, 0);
34     EXPECT_FALSE(stream.GetBits(1, &bits));
35   }
36 
37   {
38     BitStream<uint64_t> stream(0b1010101010101010, 32);
39     EXPECT_EQ(stream.Bits(), 32);
40 
41     uint64_t bits = 0;
42     EXPECT_TRUE(stream.GetBits(1, &bits));
43     EXPECT_EQ(bits, 0);
44 
45     EXPECT_TRUE(stream.GetBits(3, &bits));
46     EXPECT_EQ(bits, 0b101);
47 
48     EXPECT_TRUE(stream.GetBits(8, &bits));
49     EXPECT_EQ(bits, 0b10101010);
50 
51     EXPECT_EQ(stream.Bits(), 20);
52 
53     EXPECT_TRUE(stream.GetBits(20, &bits));
54     EXPECT_EQ(bits, 0b1010);
55     EXPECT_EQ(stream.Bits(), 0);
56   }
57 
58   {
59     BitStream<uint64_t> stream(kAllBits, 64);
60     EXPECT_EQ(stream.Bits(), 64);
61 
62     uint64_t bits = 0;
63     EXPECT_TRUE(stream.GetBits(64, &bits));
64     EXPECT_EQ(bits, kAllBits);
65     EXPECT_EQ(stream.Bits(), 0);
66   }
67 
68   {
69     BitStream<uint64_t> stream(kAllBits, 64);
70     EXPECT_EQ(stream.Bits(), 64);
71 
72     uint64_t bits = 0;
73     EXPECT_TRUE(stream.GetBits(40, &bits));
74     EXPECT_EQ(bits, k40Bits);
75     EXPECT_EQ(stream.Bits(), 24);
76   }
77 
78   {
79     BitStream<uint64_t> stream(kAllBits, 32);
80 
81     uint64_t bits = 0;
82     EXPECT_TRUE(stream.GetBits(0, &bits));
83     EXPECT_EQ(bits, 0);
84     EXPECT_TRUE(stream.GetBits(32, &bits));
85     EXPECT_EQ(bits, k40Bits & 0xFFFFFFFF);
86     EXPECT_TRUE(stream.GetBits(0, &bits));
87     EXPECT_EQ(bits, 0);
88     EXPECT_EQ(stream.Bits(), 0);
89   }
90 }
91 
TEST(BitStream,Encode)92 TEST(BitStream, Encode) {
93   {
94     BitStream<uint64_t> stream;
95 
96     stream.PutBits(0, 1);
97     stream.PutBits(0b11, 2);
98     EXPECT_EQ(stream.Bits(), 3);
99 
100     uint64_t bits = 0;
101     EXPECT_TRUE(stream.GetBits(3, &bits));
102     EXPECT_EQ(bits, 0b110);
103   }
104 
105   {
106     BitStream<uint64_t> stream;
107 
108     uint64_t bits = 0;
109     stream.PutBits(kAllBits, 64);
110     EXPECT_EQ(stream.Bits(), 64);
111 
112     EXPECT_TRUE(stream.GetBits(64, &bits));
113     EXPECT_EQ(bits, kAllBits);
114     EXPECT_EQ(stream.Bits(), 0);
115   }
116 
117   {
118     BitStream<uint64_t> stream;
119     stream.PutBits(kAllBits, 40);
120 
121     uint64_t bits = 0;
122     EXPECT_TRUE(stream.GetBits(40, &bits));
123     EXPECT_EQ(bits, k40Bits);
124     EXPECT_EQ(stream.Bits(), 0);
125   }
126 
127   {
128     BitStream<uint64_t> stream;
129     stream.PutBits(0, 0);
130     stream.PutBits(kAllBits, 32);
131     stream.PutBits(0, 0);
132 
133     uint64_t bits = 0;
134     EXPECT_TRUE(stream.GetBits(32, &bits));
135     EXPECT_EQ(bits, k40Bits & 0xFFFFFFFF);
136     EXPECT_EQ(stream.Bits(), 0);
137   }
138 }
139 
140 }  // namespace base
141 }  // namespace astc_codec
142