Add boolean_vector classes
[libstick.git] / tests / booleanvector.h
1 #ifndef booleanvector_h_MeiTephaijaequuy
2 #define booleanvector_h_MeiTephaijaequuy
3
4 #include <cpptest.h>
5 #include <cpptest-suite.h>
6
7 #include <libstick-0.1/booleanvector.h>
8
9 using namespace libstick;
10
11
12 class boolean_vector_TestSuite: public Test::Suite {
13 public:
14 boolean_vector_TestSuite() {
15 TEST_ADD(boolean_vector_TestSuite::test<sorted_boolean_vector<int> >);
16 TEST_ADD(boolean_vector_TestSuite::test<heapsorted_boolean_vector<int> >);
17
18 //TEST_ADD(boolean_vector_TestSuite::test_performance<sorted_boolean_vector<int> >);
19 //TEST_ADD(boolean_vector_TestSuite::test_performance<heapsorted_boolean_vector<int> >);
20 }
21
22 protected:
23 virtual void setup() {
24 }
25
26 virtual void tear_down() {
27 }
28
29 private:
30 template<typename T>
31 void test() {
32 const int size = 1000;
33
34 T even;
35 for (int i=1; 2*i <= size; ++i)
36 even.set(2*i, true);
37
38 // We added size/2 ones, all of them at even indices.
39 TEST_ASSERT(even.get_ones().size() == size/2);
40 for (typename T::indexarray::const_iterator it = even.get_ones().begin();
41 it != even.get_ones().end(); ++it)
42 TEST_ASSERT(*it % 2 == 0);
43 for (int i=1; i <= size; ++i)
44 TEST_ASSERT(even.get(i) == (i % 2 == 0));
45
46 // A complete vector
47 T all;
48 for (int i=1; i <= size; ++i)
49 all.set(i, true);
50
51 // A vector with all odd entries
52 T odd = even;
53 odd.add(all);
54
55 // Again size/2 entries in odd.
56 TEST_ASSERT(odd.get_ones().size() == size/2);
57 // Check whether we really have odd indices only
58 for (typename T::indexarray::const_iterator it = odd.get_ones().begin();
59 it != odd.get_ones().end(); ++it)
60 TEST_ASSERT(*it % 2 == 1);
61 for (int i=1; i <= size; ++i)
62 TEST_ASSERT(odd.get(i) == (i % 2 == 1));
63
64
65 all.pop_back();
66 even.pop_back();
67 T all2 = odd;
68 all2.add(even);
69 TEST_ASSERT(all2 == all);
70 }
71
72 template<typename T>
73 void test_performance() {
74 T sum;
75
76 const int size = 1000;
77 const int cnt = 4;
78
79 T summand;
80 for (int a=0; a < cnt; ++a) {
81 summand.clear();
82 for (int i=0; i < size; ++i)
83 summand.set(a*(size/2) + i, true);
84 sum.add(summand);
85 }
86
87 //for (int i=0; i < size/2; ++i) {
88 //TEST_ASSERT(sum.get(i) == true);
89 //TEST_ASSERT(sum.get(i + size/2) == false);
90 //TEST_ASSERT(sum.get(i + (cnt-1)*(size/2)) == false);
91 //TEST_ASSERT(sum.get(i + (cnt)*(size/2)) == true);
92 //}
93
94 //std::cout << std::endl << sum << std::endl;
95 }
96 };
97
98
99 #endif