libtasks Documentation  1.6
test_bitset.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013-2014 ADTECH GmbH
3  * Licensed under MIT (https://github.com/adtechlabs/libtasks/blob/master/COPYING)
4  *
5  * Author: Andreas Pohl
6  */
7 
8 #include <tasks/tools/bitset.h>
9 
10 #include "test_bitset.h"
11 
12 using namespace tasks::tools;
13 
15  for (bitset::int_type i = 0; i < bs.bits(); i++) {
16  bs.toggle(i);
17  }
18  for (bitset::int_type i = 0; i < bs.bits(); i++) {
19  CPPUNIT_ASSERT_MESSAGE("bitset(" + std::to_string(bs.bits()) + ") toggle test 1: i=" + std::to_string(i),
20  bs.test(i));
21  }
22 
23  for (bitset::int_type i = 0; i < bs.bits(); i++) {
24  bs.toggle(i);
25  }
26  for (bitset::int_type i = 0; i < bs.bits(); i++) {
27  CPPUNIT_ASSERT_MESSAGE("bitset(" + std::to_string(bs.bits()) + ") toggle test 2: i=" + std::to_string(i),
28  !bs.test(i));
29  }
30 
31  for (bitset::int_type i = 0; i < bs.bits(); i++) {
32  bs.set(i);
33  }
34  for (bitset::int_type i = 0; i < bs.bits(); i++) {
35  CPPUNIT_ASSERT_MESSAGE("bitset(" + std::to_string(bs.bits()) + ") set test: i=" + std::to_string(i),
36  bs.test(i));
37  }
38 
39  for (bitset::int_type i = 0; i < bs.bits(); i++) {
40  bs.unset(i);
41  }
42  for (bitset::int_type i = 0; i < bs.bits(); i++) {
43  CPPUNIT_ASSERT_MESSAGE("bitset(" + std::to_string(bs.bits()) + ") unset test: i=" + std::to_string(i),
44  !bs.test(i));
45  }
46 }
47 
49  bitset::int_type bit;
50 
51  bitset bs1(10);
52  CPPUNIT_ASSERT(10 == bs1.buckets());
53  full_iteration(bs1);
54  bs1.set(0);
55  bs1.set(3);
56  bs1.set(6);
57  bs1.set(9);
58  CPPUNIT_ASSERT(bs1.first(bit));
59  CPPUNIT_ASSERT(0 == bit);
60  bs1.unset(0);
61  CPPUNIT_ASSERT(bs1.first(bit));
62  CPPUNIT_ASSERT(3 == bit);
63  CPPUNIT_ASSERT(bs1.next(bit));
64  CPPUNIT_ASSERT(3 == bit);
65  CPPUNIT_ASSERT(bs1.next(bit, 3));
66  CPPUNIT_ASSERT(3 == bit);
67  CPPUNIT_ASSERT(bs1.next(bit, 4));
68  CPPUNIT_ASSERT(6 == bit);
69  CPPUNIT_ASSERT(bs1.next(bit, 7));
70  CPPUNIT_ASSERT(9 == bit);
71  bs1.unset(9);
72  CPPUNIT_ASSERT(bs1.next(bit, 9));
73  CPPUNIT_ASSERT(3 == bit);
74 
75  bitset bs2(64);
76  CPPUNIT_ASSERT(64 == bs2.buckets());
77  full_iteration(bs2);
78  bs2.set(63);
79  CPPUNIT_ASSERT(bs2.next(bit));
80  CPPUNIT_ASSERT(63 == bit);
81  CPPUNIT_ASSERT(bs2.next(bit, 62));
82  CPPUNIT_ASSERT(63 == bit);
83  CPPUNIT_ASSERT(bs2.next(bit, 63));
84  CPPUNIT_ASSERT(63 == bit);
85 
86  bitset bs3(120);
87  CPPUNIT_ASSERT(120 == bs3.buckets());
88  full_iteration(bs3);
89  bs3.set(3);
90  bs3.set(63);
91  bs3.set(64);
92  bs3.set(118);
93  CPPUNIT_ASSERT(bs3.next(bit));
94  CPPUNIT_ASSERT(3 == bit);
95  CPPUNIT_ASSERT(bs3.next(bit, 4));
96  CPPUNIT_ASSERT(63 == bit);
97  CPPUNIT_ASSERT(bs3.next(bit, 64));
98  CPPUNIT_ASSERT(64 == bit);
99  CPPUNIT_ASSERT(bs3.next(bit, 65));
100  CPPUNIT_ASSERT(118 == bit);
101  CPPUNIT_ASSERT(bs3.next(bit, 119));
102  bs3.unset(3);
103  bs3.unset(63);
104  bs3.unset(64);
105  CPPUNIT_ASSERT(bs3.next(bit));
106  CPPUNIT_ASSERT(118 == bit);
107  CPPUNIT_ASSERT(bs3.first(bit));
108  CPPUNIT_ASSERT(118 == bit);
109 
110  bitset bs4(129);
111  CPPUNIT_ASSERT(129 == bs4.buckets());
112  full_iteration(bs4);
113  bs4.set(128);
114  CPPUNIT_ASSERT(bs4.next(bit));
115  CPPUNIT_ASSERT(128 == bit);
116  CPPUNIT_ASSERT(bs4.first(bit));
117  CPPUNIT_ASSERT(128 == bit);
118  bs4.unset(128);
119  bs4.set(60);
120  CPPUNIT_ASSERT(bs4.next(bit, 61));
121  CPPUNIT_ASSERT(60 == bit);
122 }
void toggle(int_type p)
Toggle a bit.
Definition: bitset.h:39
bool first(int_type &idx) const
Definition: bitset.h:75
void set(int_type p)
Set a bit.
Definition: bitset.h:45
std::vector< data_type >::size_type int_type
Definition: bitset.h:23
void unset(int_type p)
Unset a bit.
Definition: bitset.h:51
A thread safe lock free bitset.
Definition: bitset.h:20
int_type bits() const
Definition: bitset.h:33
bool test(int_type p) const
Test if a bit is set.
Definition: bitset.h:57
void full_iteration(bitset &bs)
Definition: test_bitset.cpp:14
size_t buckets() const
Definition: bitset.h:36
bool next(int_type &idx, int_type start=0) const
Definition: bitset.h:84