-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbitvector_test.cpp
More file actions
97 lines (81 loc) · 2.16 KB
/
bitvector_test.cpp
File metadata and controls
97 lines (81 loc) · 2.16 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 "bitvector.hpp"
#include <gtest/gtest.h>
TEST(BitvectorTest, PushBackBasic) {
bowen::BitVector<> bv;
EXPECT_TRUE(bv.empty());
bv.push_back(false);
bv.push_back(true);
bv.push_back(true);
ASSERT_EQ(bv.size(), 3u);
EXPECT_FALSE(bv[0]);
EXPECT_TRUE(bv[1]);
EXPECT_TRUE(bv[2]);
}
TEST(BitvectorTest, IncrementUntilZero) {
const size_t N = 20;
bowen::BitVector<> bv(N);
// Set bits 5 through 7 to 1
for (size_t i = 5; i <= 7; ++i) {
bv.set_bit(i, true);
}
size_t pos = 5;
bv.incrementUntilZero(pos);
EXPECT_EQ(pos, 8u);
}
TEST(BitvectorTest, ConstructWithValue) {
bowen::BitVector<> bv(10, true);
ASSERT_EQ(bv.size(), 10u);
for (size_t i = 0; i < bv.size(); ++i) {
EXPECT_TRUE(bv[i]);
}
}
#ifndef BITVECTOR_NO_BOUND_CHECK
TEST(BitvectorTest, OutOfRangeThrows) {
bowen::BitVector<> bv(5);
EXPECT_THROW(bv[5], std::out_of_range);
EXPECT_THROW(bv.set_bit(5, true), std::out_of_range);
}
#endif
TEST(BitvectorTest, CopyAndAssignment) {
bowen::BitVector<> bv1;
bv1.push_back(false);
bv1.push_back(true);
bowen::BitVector<> bv2(bv1);
ASSERT_EQ(bv2.size(), bv1.size());
EXPECT_FALSE(bv2[0]);
EXPECT_TRUE(bv2[1]);
bv1.set_bit(0, true);
EXPECT_TRUE(bv1[0]);
EXPECT_FALSE(bv2[0]);
bowen::BitVector<> bv3;
bv3 = bv1;
ASSERT_EQ(bv3.size(), bv1.size());
EXPECT_TRUE(bv3[0]);
EXPECT_TRUE(bv3[1]);
}
TEST(BitvectorTest, AssignResizesAndSets) {
bowen::BitVector<> bv;
bv.assign(12, true);
ASSERT_EQ(bv.size(), 12u);
for (size_t i = 0; i < bv.size(); ++i)
EXPECT_TRUE(bv[i]);
bv.assign(4, false);
ASSERT_EQ(bv.size(), 4u);
for (size_t i = 0; i < bv.size(); ++i)
EXPECT_FALSE(bv[i]);
}
TEST(BitvectorTest, IteratorTraversal) {
bowen::BitVector<> bv;
bv.push_back(true);
bv.push_back(false);
bv.push_back(true);
std::vector<bool> values;
auto it = bv.begin();
for (size_t i = 0; i < bv.size(); ++i, ++it) {
values.push_back(*it);
}
ASSERT_EQ(values.size(), 3u);
EXPECT_TRUE(values[0]);
EXPECT_FALSE(values[1]);
EXPECT_TRUE(values[2]);
}