-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_bitio.cc
160 lines (134 loc) · 2.99 KB
/
test_bitio.cc
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// Eriksen Liu
/*
* Unit tests for class BitIO
*/
#include "bitio.hh"
#include <cassert>
#include <sstream>
#include <iostream>
//////////////////////////////////////////////////////////////////////////////
void
test_1_bit()
{
std::stringstream bits;
{
BitOutput bito(bits);
bito.output_bit(1);
}
BitInput biti(bits);
assert(biti.input_bit() == true);
assert(biti.input_bit() == false); // Should just be a trailing zero
}
//////////////////////////////////////////////////////////////////////////////
void
test_4_bits()
{
std::stringstream bits;
{
BitOutput bito(bits);
bito.output_bit(0);
bito.output_bit(1);
bito.output_bit(0);
bito.output_bit(1);
}
BitInput biti(bits);
assert(biti.input_bit() == false);
assert(biti.input_bit() == true);
assert(biti.input_bit() == false);
assert(biti.input_bit() == true);
assert(biti.input_bit() == false); // Should just be a trailing zero
}
//////////////////////////////////////////////////////////////////////////////
void
test_8_bits()
{
std::stringstream bits0, bits1;
{
BitOutput bito0(bits0);
BitOutput bito1(bits1);
for (int i = 0; i < 8; ++i) {
bito0.output_bit(0);
}
for (int i = 0; i < 8; ++i) {
bito1.output_bit(1);
}
}
BitInput biti0(bits0);
BitInput biti1(bits1);
for (int i = 0; i < 8; ++i) {
assert(!biti0.input_bit());
}
biti0.input_bit();
assert(bits0.eof()); // Should be no trailing zeros this time!
for (int i = 0; i < 8; ++i) {
assert(biti1.input_bit());
}
}
//////////////////////////////////////////////////////////////////////////////
void
test_9_bits()
{
std::stringstream bits0, bits1;
{
BitOutput bito1(bits1);
for (int i = 0; i < 9; ++i) {
bito1.output_bit(1);
}
}
BitInput biti1(bits1);
for (int i = 0; i < 9; ++i) {
assert(biti1.input_bit());
}
assert(!biti1.input_bit());
}
//////////////////////////////////////////////////////////////////////////////
void
test_16_bits()
{
std::stringstream bits;
{
BitOutput bito(bits);
for (int i = 0; i < 32; ++i) {
bito.output_bit(i % 2);
}
}
BitInput biti(bits);
for (int i = 0; i < 32; ++i) {
assert(biti.input_bit() == i % 2);
}
biti.input_bit();
assert(bits.eof()); // Should be no trailing zeros this time!
}
//////////////////////////////////////////////////////////////////////////////
void
test_100_bits()
{
std::stringstream bits;
{
BitOutput bito(bits);
for (int i = 0; i < 100; ++i) {
bito.output_bit(!(i % 2));
}
}
BitInput biti(bits);
for (int i = 0; i < 100; ++i) {
assert(biti.input_bit() != i % 2);
}
assert(!biti.input_bit());
assert(!biti.input_bit());
assert(!biti.input_bit());
assert(!biti.input_bit());
biti.input_bit();
assert(bits.eof());
}
//////////////////////////////////////////////////////////////////////////////
int
main() {
test_1_bit();
test_4_bits();
test_8_bits();
test_9_bits();
test_16_bits();
test_100_bits();
return 0;
}