# HG changeset patch # User Tom Fredrik Blenning Klaussen # Date 1347314345 -7200 # Node ID b23f04d4a27685001969f3bdca6498970558696c # Parent 877327e9061aef61fb9094d8e30facd4631446a1 Test a custom BitArray. diff -r 877327e9061a -r b23f04d4a276 BitArray.cpp --- /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" + diff -r 877327e9061a -r b23f04d4a276 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 +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 + +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 + + diff -r 877327e9061a -r b23f04d4a276 TestBitArray.cpp --- /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 + +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); + } + } + +}