testBit.cc
Go to the documentation of this file.
1 
11 #include <utl/Test.h>
12 #include <tst/Verify.h>
13 #include <utl/Bit.h>
14 #include <cppunit/extensions/HelperMacros.h>
15 #include <cstdio>
16 #include <iostream>
17 #include <bitset>
18 
19 using namespace tst;
20 using namespace utl;
21 using namespace std;
22 
23 
27 class TestBit : public CppUnit::TestFixture {
28 
29  CPPUNIT_TEST_SUITE(TestBit);
30  CPPUNIT_TEST(TestGet);
31  CPPUNIT_TEST(TestSet);
32  CPPUNIT_TEST(TestMask);
33  CPPUNIT_TEST_SUITE_END();
34 
35 public:
36  void setUp() { }
37 
38  void tearDown() { }
39 
40  void
42  {
43  const int size = sizeof(int)*8;
44  const int bc2 = 12345;
45  int b2 = bc2;
46  bitset<size> b1(bc2);
47 
48  ostringstream out1;
49  ostringstream out2;
50  ostringstream out3;
51  for (int i = 0; i < size; ++i) {
52  out1 << (b1[i] ? '^' : '.');
53  out2 << (AsBitArray(bc2)[i] ? '^' : '.');
54  out3 << (AsBitArray(b2)[i] ? '^' : '.');
55  }
56 
57  CPPUNIT_ASSERT(Verify<Equal>(out1.str(), out2.str()));
58  CPPUNIT_ASSERT(Verify<Equal>(out1.str(), out3.str()));
59  }
60 
61  void
63  {
64  const int size = sizeof(int)*8;
65  const int number = 12345;
66  bitset<size> b1(number);
67  int b2 = 11111;
68 
69  for (int i = 0; i < size; ++i)
70  AsBitArray(b2)[i] = b1[i];
71 
72  CPPUNIT_ASSERT(Verify<Equal>(b2, number));
73  }
74 
75  void
77  {
78  const int n = (1 << 18) | (1 << 5);
79  int m = 0;
80 
81  AsBitArray(m)[18] = true;
82  AsBitArray(m)[5] = true;
83  CPPUNIT_ASSERT(Verify<Equal>(n, m));
84 
85  for (unsigned int i = 0; i < 8*sizeof(int); ++i)
86  AsBitArray(m)[i] = false;
87  CPPUNIT_ASSERT(Verify<Equal>(m, 0));
88 
89  m = 1;
90  AsBitArray(m).Mask(n, true);
91  CPPUNIT_ASSERT(Verify<Equal>(m, n+1));
92 
93  AsBitArray(m).Mask(n, false);
94  CPPUNIT_ASSERT(Verify<Equal>(m, 1));
95  }
96 
97 };
98 
99 
Bit::Array< T > AsBitArray(T &target)
Definition: Bit.h:77
CPPUNIT_TEST_SUITE_REGISTRATION(testAiresShowerFile)
void TestSet()
Definition: testBit.cc:62
void setUp()
Definition: testBit.cc:36
void TestGet()
Definition: testBit.cc:41
void TestMask()
Definition: testBit.cc:76
constexpr double m
Definition: AugerUnits.h:121
void tearDown()
Definition: testBit.cc:38

, generated on Tue Sep 26 2023.