forked from HIT-SCIR/ltp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils_tinybitset_unittest.cpp
116 lines (102 loc) · 2.71 KB
/
utils_tinybitset_unittest.cpp
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
#include <iostream>
#include <gtest/gtest.h>
#include "utils/tinybitset.hpp"
using namespace std;
using namespace ltp::utility;
const int kNormalCaseNum = 8;
const int kNormalCase[8] = {0,1,3,32,34,57,89,127};
TEST(tinybitset_unittest, test_bitset_init) {
Bitset bitset;
bool res = bitset.empty();
EXPECT_EQ(true, res);
vector<int> bitones = bitset.getbitones();
EXPECT_EQ(0,bitones.size());
}
TEST(tinybitset_unittest, test_bitset_allsetones) {
Bitset bitset;
bitset.allsetones();
bool res = bitset.empty();
EXPECT_EQ(true, res);
vector<int> bitones = bitset.getbitones();
EXPECT_EQ(128,bitones.size());
for(int i=0;i<bitones.size();i++){
EXPECT_EQ(i,bitones[i]);
}
}
TEST(tinybitset_unittest, test_bitset_boundrytest) {
Bitset bitset;
bool res = bitset.set(-1);
EXPECT_EQ(0,res);
res = bitset.set(128);
EXPECT_EQ(0,res);
res = bitset.set(0);
EXPECT_EQ(1,res);
res = bitset.set(127);
EXPECT_EQ(1,res);
res = bitset.get(-1);
EXPECT_EQ(0,res);
res = bitset.get(128);
EXPECT_EQ(0,res);
res = bitset.get(0);
EXPECT_EQ(1,res);
res = bitset.get(127);
EXPECT_EQ(1,res);
}
TEST(tinybitset_unittest, test_bitset_normaltest) {
Bitset bitset;
bool res;
for(int i=0;i<kNormalCaseNum;i++){
bitset.set(kNormalCase[i]);
res = bitset.get(kNormalCase[i]);
EXPECT_EQ(1,res);
res = bitset.empty();
EXPECT_EQ(0,res);
}
vector<int> bitones = bitset.getbitones();
EXPECT_EQ(kNormalCaseNum,bitones.size());
for(int i=0;i<kNormalCaseNum;i++){
EXPECT_EQ(kNormalCase[i],bitones[i]);
}
}
TEST(tinybitset_unittest, test_bitset_mergetest) {
Bitset bitset1;
Bitset bitset2;
bitset1.merge(bitset2);
bool res = bitset1.empty();
EXPECT_EQ(1,res);
res = bitset2.empty();
EXPECT_EQ(1,res);
vector<int> bitones = bitset1.getbitones();
EXPECT_EQ(0,bitones.size());
bitones = bitset2.getbitones();
EXPECT_EQ(0,bitones.size());
bitset2.set(5);
bitset2.set(32);
bitset2.set(34);
bitset1.merge(bitset2);
res = bitset1.empty();
EXPECT_EQ(0,res);
bitones = bitset1.getbitones();
EXPECT_EQ(3,bitones.size());
EXPECT_EQ(5,bitones[0]);
EXPECT_EQ(32,bitones[1]);
EXPECT_EQ(34,bitones[2]);
}
TEST(tinybitset_unittest, get_performance_test) {
const int kNumRepeat = 1000000000;
long start_time = clock();
Bitset mask(127);
for (int i = 0; i < kNumRepeat; ++ i) {
if (mask.get(0)) {
//
}
}
long throughput_per_millisecond = (kNumRepeat / (double(clock() -start_time) / 1000));
std::cout << "#throughput ltp::utility:Bitset::get : "
<< throughput_per_millisecond << std::endl;
EXPECT_LT(5000, throughput_per_millisecond);
}
int main(int argc, char ** argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}