changeset 47:b23f04d4a276

Test a custom BitArray.
author Tom Fredrik Blenning Klaussen <bfg@blenning.no>
date Mon, 10 Sep 2012 23:59:05 +0200
parents 877327e9061a
children ef429402e03b
files BitArray.cpp BitArray.hpp TestBitArray.cpp
diffstat 3 files changed, 126 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/BitArray.cpp	Mon Sep 10 23:59:05 2012 +0200
@@ -0,0 +1,2 @@
+#include "BitArray.hpp"
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/BitArray.hpp	Mon Sep 10 23:59:05 2012 +0200
@@ -0,0 +1,72 @@
+#ifndef BITARRAY_HPP
+#define BITARRAY_HPP
+
+
+#if 0
+#include <QtCore/QBitArray>
+typedef QBitArray BitArray;
+#else
+#define HIGH(B) ((B) >> 3)
+#define LOW(B) ((B) & 0x07)
+
+#define MASK(X) (0x1 << X)
+#define SELECTMASK(X) (uchar(MASK(X)))
+#define DESELECTMASK(X) (uchar(~MASK(X)))
+
+#define NUMCHARS(X) HIGH(X + 7)
+
+#include <cassert>
+
+class QBitArray;
+class BitArray
+{
+  typedef unsigned char uchar;
+  typedef unsigned int uint;
+  uint size_;
+  unsigned char* bits;
+public:
+  bool testBit(uint i) const
+  {
+    assert(i < size_);
+    return bits[HIGH(i)] & MASK(LOW(i));
+  }
+
+  void setBit(uint i, bool val)
+  {
+    assert(i < size_);
+    if (val) {
+      bits[HIGH(i)] |= SELECTMASK(LOW(i));
+    }
+    else {
+      bits[HIGH(i)] &= DESELECTMASK(LOW(i));
+    }
+  }
+
+  BitArray(uint size)
+  {
+    size_ = size;
+    bits = new uchar[NUMCHARS(size)];
+  }
+
+  BitArray(uint size, bool val) : size_(size), bits(new uchar[NUMCHARS(size)])
+  {
+    uint n = NUMCHARS(size);
+    for (uint i = 0; i < n; ++i) {
+      bits[i] = (val) ? 0xff : 0;
+    }
+  }
+
+  BitArray() : size_(0), bits(0)
+  {
+  }
+
+  uint size() const
+  {
+    return size_;
+  }  
+};
+#endif
+
+#endif //BITARRAY_HPP
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TestBitArray.cpp	Mon Sep 10 23:59:05 2012 +0200
@@ -0,0 +1,52 @@
+#include "BitArray.hpp"
+#include "TestFramework.hpp"
+
+#include <QtCore/QDebug>
+
+BOOST_AUTO_TEST_CASE( TestBasic )
+{
+  {
+    BitArray tbits(16, true);
+    BitArray fbits(16, false);
+    for (uint i = 0; i < tbits.size(); ++i) {
+      BOOST_REQUIRE_EQUAL(tbits.testBit(i), true);
+      BOOST_REQUIRE_EQUAL(fbits.testBit(i), false);
+    }
+  }
+
+  {
+    BitArray tbits(9, true);
+    BitArray fbits(9, false);
+    for (uint i = 0; i < tbits.size(); ++i) {
+      BOOST_REQUIRE_EQUAL(tbits.testBit(i), true);
+      BOOST_REQUIRE_EQUAL(fbits.testBit(i), false);
+    }
+  }
+
+  {
+    BitArray tbits(13, true);
+    BitArray fbits(13, false);
+    for (uint i = 0; i < tbits.size(); ++i) {
+      BOOST_REQUIRE_EQUAL(tbits.testBit(i), true);
+      BOOST_REQUIRE_EQUAL(fbits.testBit(i), false);
+    }
+  }
+
+}
+
+
+BOOST_AUTO_TEST_CASE( TestSetBit )
+{
+
+  for (uint i = 0; i < 13; ++i) {
+    BitArray tbits(13, true);
+    BitArray fbits(13, false);
+    tbits.setBit(i, false);
+    fbits.setBit(i, true);
+    for (uint j = 0; j < tbits.size(); ++j) {
+      BOOST_REQUIRE_EQUAL(tbits.testBit(j), i != j);
+      BOOST_REQUIRE_EQUAL(fbits.testBit(j), i == j);
+    }
+  }
+
+}