added unit tests for C++ BitArray::reverse(). Closes issue 1104

From evansepdx. Thanks!

git-svn-id: https://zxing.googlecode.com/svn/trunk@2086 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
smparkes@smparkes.net 2011-12-13 20:58:25 +00:00
parent 703cc2e8bf
commit d7a9a8b96a
2 changed files with 140 additions and 9 deletions

View file

@ -20,6 +20,7 @@
#include "BitArrayTest.h"
#include <limits>
#include <cstdlib>
using namespace std;
@ -30,7 +31,7 @@ CPPUNIT_TEST_SUITE_REGISTRATION(BitArrayTest);
void BitArrayTest::testGetSet() {
size_t bits = numeric_limits<unsigned int>::digits + 1;
BitArray array(bits);
for (size_t i = 0; i < bits; i++) {
for(size_t i = 0; i < bits; i++) {
CPPUNIT_ASSERT_EQUAL(false, array.get(i));
array.set(i);
CPPUNIT_ASSERT_EQUAL(true, array.get(i));
@ -40,10 +41,10 @@ void BitArrayTest::testGetSet() {
void BitArrayTest::testSetBulk() {
BitArray array(64);
array.setBulk(32, 0xFFFF0000);
for (int i = 0; i < 48; i++) {
for(int i = 0; i < 48; i++) {
CPPUNIT_ASSERT_EQUAL(false, array.get(i));
}
for (int i = 48; i < 64; i++) {
for(int i = 48; i < 64; i++) {
CPPUNIT_ASSERT_EQUAL(true, array.get(i));
}
}
@ -51,20 +52,20 @@ void BitArrayTest::testSetBulk() {
void BitArrayTest::testClear() {
size_t bits = numeric_limits<unsigned int>::digits;
BitArray array(bits);
for (size_t i = 0; i < bits; i++) {
for(size_t i = 0; i < bits; i++) {
array.set(i);
}
array.clear();
for (size_t i = 0; i < bits; i++) {
for(size_t i = 0; i < bits; i++) {
CPPUNIT_ASSERT_EQUAL(false, array.get(i));
}
}
void BitArrayTest::testGetArray() {
size_t bits = numeric_limits<unsigned int>::digits;
BitArray array(2*bits);
BitArray array(2 * bits);
array.set(0);
array.set(2*bits - 1);
array.set(2 * bits - 1);
vector<unsigned> words(array.getBitArray());
CPPUNIT_ASSERT_EQUAL(1u, words[0]);
CPPUNIT_ASSERT_EQUAL((1u << (bits - 1)), words[1]);
@ -82,15 +83,134 @@ void BitArrayTest::testIsRange() {
CPPUNIT_ASSERT_EQUAL(true, array.isRange(bits - 1, bits + 1, true));
array.set(bits + 2);
CPPUNIT_ASSERT_EQUAL(false, array.isRange(bits - 1, bits + 3, true));
for (size_t i = 0; i < bits - 1; i++) {
for(size_t i = 0; i < bits - 1; i++) {
array.set(i);
}
CPPUNIT_ASSERT_EQUAL(true, array.isRange(0, bits + 1, true));
for (int i = 33; i < 64; i++) {
for(int i = 33; i < 64; i++) {
array.set(i);
}
CPPUNIT_ASSERT_EQUAL(true, array.isRange(0, 64, true));
CPPUNIT_ASSERT_EQUAL(false, array.isRange(0, 64, false));
}
// fills the two arrays with identical random bits
void BitArrayTest::fillRandom(BitArray& test, BitArray& reference) {
srandom(0xDEADBEEFL + test.getSize());
for(size_t i = 0; i < test.getSize(); ++i) {
if(random() & 0x1) {
test.set(i);
reference.set(i);
}
}
}
void BitArrayTest::testReverseHalves() {
// one word test, split in half
{
size_t bits = numeric_limits<unsigned int>::digits;
BitArray test(bits);
test.clear();
for(size_t i = 0; i < bits / 2; ++i) {
test.set(i);
}
test.reverse();
for(size_t i = 0; i < bits / 2; ++i) {
CPPUNIT_ASSERT_EQUAL(test.get(i), !test.get(bits - 1 - i));
}
}
// two word test
{
size_t bits2 = numeric_limits<unsigned int>::digits * 2;
BitArray test2(bits2);
test2.clear();
for(size_t i = 0; i < bits2 / 2; ++i) {
test2.set(i);
}
test2.reverse();
for(size_t i = 0; i < bits2 / 2; ++i) {
CPPUNIT_ASSERT_EQUAL(test2.get(i), !test2.get(bits2 - 1 - i));
}
}
}
void BitArrayTest::testReverseEven() {
size_t bits = numeric_limits<unsigned int>::digits * 8;
BitArray test(bits);
BitArray reference(bits);
test.clear();
reference.clear();
fillRandom(test, reference);
test.reverse();
for(size_t i = 0; i < bits; ++i) {
CPPUNIT_ASSERT_EQUAL(test.get(i), reference.get(bits - 1 - i));
}
}
void BitArrayTest::testReverseOdd() {
size_t bits = numeric_limits<unsigned int>::digits * 6 + 11;
BitArray test(bits);
BitArray reference(bits);
test.clear();
reference.clear();
fillRandom(test, reference);
test.reverse();
for(size_t i = 0; i < bits; ++i) {
CPPUNIT_ASSERT_EQUAL(test.get(i), reference.get(bits - 1 - i));
}
}
void BitArrayTest::testReverseSweep() {
size_t bits;
size_t bitsHigh = numeric_limits<unsigned int>::digits * 10;
for(bits = 1; bits < bitsHigh; ++bits) {
BitArray test(bits);
BitArray reference(bits);
test.clear();
reference.clear();
fillRandom(test, reference);
test.reverse();
for(size_t i = 0; i < bits; ++i) {
CPPUNIT_ASSERT_EQUAL(test.get(i), reference.get(bits - 1 - i));
}
}
}
void BitArrayTest::testReverseReverse() {
size_t bits = numeric_limits<unsigned int>::digits * 4 + 17;
BitArray test(bits);
BitArray reference(bits);
test.clear();
reference.clear();
fillRandom(test, reference);
// flip it once and test
test.reverse();
for(size_t i = 0; i < bits; ++i) {
CPPUNIT_ASSERT_EQUAL(test.get(i), reference.get(bits - 1 - i));
}
// flip it back and test
test.reverse();
for(size_t i = 0; i < bits; ++i) {
CPPUNIT_ASSERT_EQUAL(test.get(i), reference.get(i));
}
}
}

View file

@ -32,6 +32,11 @@ class BitArrayTest : public CPPUNIT_NS::TestFixture {
CPPUNIT_TEST(testClear);
CPPUNIT_TEST(testGetArray);
CPPUNIT_TEST(testIsRange);
CPPUNIT_TEST(testReverseHalves);
CPPUNIT_TEST(testReverseEven);
CPPUNIT_TEST(testReverseOdd);
CPPUNIT_TEST(testReverseSweep);
CPPUNIT_TEST(testReverseReverse);
CPPUNIT_TEST_SUITE_END();
public:
@ -42,8 +47,14 @@ protected:
void testClear();
void testGetArray();
void testIsRange();
void testReverseHalves();
void testReverseEven();
void testReverseOdd();
void testReverseSweep();
void testReverseReverse();
private:
static void fillRandom(BitArray& test, BitArray& reference);
};
}