From 25c6b585f51f4d15344b5d0298adeee2cd3a9838 Mon Sep 17 00:00:00 2001 From: favoritas37 Date: Mon, 28 Aug 2017 02:25:13 +0300 Subject: [PATCH] added more tests for zxing/common/BitArray --- src/zxing/zxing/common/BitArray.cpp | 9 + src/zxing/zxing/common/BitArray.h | 2 +- tests/src/QZXingTests/EncodeValidator.cpp | 8 +- tests/src/QZXingTests/QZXingTests.pro | 10 +- tests/src/QZXingTests/TestCase.h | 4 + .../zxing/common/BitArrayTests.cpp | 218 ++++++++++++++++++ .../QZXingTests/zxing/common/BitArrayTests.h | 42 ++++ .../{BitArrayTests.cpp => BitVectorTests.cpp} | 24 +- .../{BitArrayTests.h => BitVectorTests.h} | 10 +- 9 files changed, 303 insertions(+), 24 deletions(-) create mode 100644 tests/src/QZXingTests/zxing/common/BitArrayTests.cpp create mode 100644 tests/src/QZXingTests/zxing/common/BitArrayTests.h rename tests/src/QZXingTests/zxing/qrcode/encoder/{BitArrayTests.cpp => BitVectorTests.cpp} (88%) rename tests/src/QZXingTests/zxing/qrcode/encoder/{BitArrayTests.h => BitVectorTests.h} (75%) diff --git a/src/zxing/zxing/common/BitArray.cpp b/src/zxing/zxing/common/BitArray.cpp index d7a7d67..55bfec3 100644 --- a/src/zxing/zxing/common/BitArray.cpp +++ b/src/zxing/zxing/common/BitArray.cpp @@ -39,6 +39,15 @@ BitArray::BitArray(): size(0), bits(1) {} BitArray::BitArray(int size_) : size(size_), bits(makeArraySize(size)) {} +//this could be wrong. TODO: check size value +BitArray::BitArray(std::vector other) + : size(other.size()), bits(other.size()) +{ + for(int i=0; i #include #include -#include namespace zxing { @@ -42,6 +41,7 @@ private: public: BitArray(); BitArray(int size); + BitArray(std::vector other); ~BitArray(); int getSize() const; int getSizeInBytes() const; diff --git a/tests/src/QZXingTests/EncodeValidator.cpp b/tests/src/QZXingTests/EncodeValidator.cpp index 302d996..6c92950 100644 --- a/tests/src/QZXingTests/EncodeValidator.cpp +++ b/tests/src/QZXingTests/EncodeValidator.cpp @@ -5,10 +5,11 @@ #include "TestCase.h" #include "zxing/qrcode/encoder/MatrixUtilTests.h" #include "zxing/qrcode/encoder/MaskUtilTests.h" -#include "zxing/qrcode/encoder/BitArrayTests.h" +#include "zxing/qrcode/encoder/BitVectorTests.h" #include "zxing/qrcode/encoder/QRCodeTests.h" #include "zxing/qrcode/encoder/EncoderTests.h" #include "zxing/common/reedsolomon/ReedSolomonEncoderTests.h" +#include "zxing/common/BitArrayTests.h" namespace zxing { namespace tests{ @@ -27,7 +28,7 @@ void EncodeValidator::execute() qrcode::tests::MaskUtilTests t1; t1.execute(); - qrcode::tests::BitArrayTests t2; + qrcode::tests::BitVectorTests t2; t2.execute(); qrcode::tests::QRCodeTests t3; @@ -36,6 +37,9 @@ void EncodeValidator::execute() ReedSolomonTests t4; t4.execute(); + BitArrayTests t6; + t6.execute(); + qrcode::tests::EncoderTests t5; t5.execute(); diff --git a/tests/src/QZXingTests/QZXingTests.pro b/tests/src/QZXingTests/QZXingTests.pro index 547204d..acf5080 100644 --- a/tests/src/QZXingTests/QZXingTests.pro +++ b/tests/src/QZXingTests/QZXingTests.pro @@ -14,10 +14,11 @@ HEADERS += \ TestCase.h \ zxing/qrcode/encoder/MatrixUtilTests.h \ zxing/qrcode/encoder/MaskUtilTests.h \ - zxing/qrcode/encoder/BitArrayTests.h \ zxing/qrcode/encoder/QRCodeTests.h \ zxing/qrcode/encoder/EncoderTests.h \ - zxing/common/reedsolomon/ReedSolomonEncoderTests.h + zxing/common/reedsolomon/ReedSolomonEncoderTests.h \ + zxing/common/BitArrayTests.h \ + zxing/qrcode/encoder/BitVectorTests.h #\backward.hpp SOURCES += main.cpp \ @@ -27,9 +28,10 @@ SOURCES += main.cpp \ TestCase.cpp \ zxing/qrcode/encoder/MatrixUtilTests.cpp \ zxing/qrcode/encoder/MaskUtilTests.cpp \ - zxing/qrcode/encoder/BitArrayTests.cpp \ zxing/qrcode/encoder/QRCodeTests.cpp \ zxing/qrcode/encoder/EncoderTests.cpp \ - zxing/common/reedsolomon/ReedSolomonEncoderTests.cpp + zxing/common/reedsolomon/ReedSolomonEncoderTests.cpp \ + zxing/common/BitArrayTests.cpp \ + zxing/qrcode/encoder/BitVectorTests.cpp include(../../../src/QZXing.pri) diff --git a/tests/src/QZXingTests/TestCase.h b/tests/src/QZXingTests/TestCase.h index 4a8d4ad..c520457 100644 --- a/tests/src/QZXingTests/TestCase.h +++ b/tests/src/QZXingTests/TestCase.h @@ -53,6 +53,10 @@ private: return QString::fromStdString(item.getName()); } + static QString itemToString(unsigned int item) { + return QString::number(item); + } + protected: template void assertEquals(T expected, T actual) { if(expected != actual) { diff --git a/tests/src/QZXingTests/zxing/common/BitArrayTests.cpp b/tests/src/QZXingTests/zxing/common/BitArrayTests.cpp new file mode 100644 index 0000000..18dcfb0 --- /dev/null +++ b/tests/src/QZXingTests/zxing/common/BitArrayTests.cpp @@ -0,0 +1,218 @@ +#include "BitArrayTests.h" + +namespace zxing{ +namespace tests{ + +BitArrayTests::BitArrayTests() +{ + +} + +void BitArrayTests::execute() +{ + testGetSet(); + testGetNextSet1(); + testGetNextSet2(); + testGetNextSet3(); + testGetNextSet4(); + //testGetNextSet5(); + testSetBulk(); + testClear(); + testGetArray(); + testIsRange(); + //reverseAlgorithmTest(); +} + +void BitArrayTests::testGetSet() +{ + BitArray array(33); + for (int i = 0; i < 33; i++) { + assertFalse(array.get(i)); + array.set(i); + assertTrue(array.get(i)); + } +} + +void BitArrayTests::testGetNextSet1() +{ + BitArray array(32); + for (int i = 0; i < array.getSize(); i++) { + assertEquals(32, array.getNextSet(i)); + } + array = BitArray(33); + for (int i = 0; i < array.getSize(); i++) { + assertEquals(33, array.getNextSet(i)); + } +} + +void BitArrayTests::testGetNextSet2() +{ + BitArray array(33); + array.set(31); + for (int i = 0; i < array.getSize(); i++) { + assertEquals(i <= 31 ? 31 : 33, array.getNextSet(i)); + } + array = BitArray(33); + array.set(32); + for (int i = 0; i < array.getSize(); i++) { + assertEquals(32, array.getNextSet(i)); + } +} + +void BitArrayTests::testGetNextSet3() +{ + BitArray array(63); + array.set(31); + array.set(32); + for (int i = 0; i < array.getSize(); i++) { + int expected; + if (i <= 31) { + expected = 31; + } else if (i == 32) { + expected = 32; + } else { + expected = 63; + } + assertEquals(expected, array.getNextSet(i)); + } +} + +void BitArrayTests::testGetNextSet4() +{ + BitArray array(63); + array.set(33); + array.set(40); + for (int i = 0; i < array.getSize(); i++) { + int expected; + if (i <= 33) { + expected = 33; + } else if (i <= 40) { + expected = 40; + } else { + expected = 63; + } + assertEquals(expected, array.getNextSet(i)); + } +} + +//void BitArrayTests::testGetNextSet5() +//{ +// Random r = new SecureRandom(new byte[] {(byte) 0xDE, (byte) 0xAD, (byte) 0xBE, (byte) 0xEF}); +// for (int i = 0; i < 10; i++) { +// BitArray array = new BitArray(1 + r.nextInt(100)); +// int numSet = r.nextInt(20); +// for (int j = 0; j < numSet; j++) { +// array.set(r.nextInt(array.getSize())); +// } +// int numQueries = r.nextInt(20); +// for (int j = 0; j < numQueries; j++) { +// int query = r.nextInt(array.getSize()); +// int expected = query; +// while (expected < array.getSize() && !array.get(expected)) { +// expected++; +// } +// int actual = array.getNextSet(query); +// if (actual != expected) { +// array.getNextSet(query); +// } +// assertEquals(expected, actual); +// } +// } +//} + +void BitArrayTests::testSetBulk() +{ + BitArray array(64); + array.setBulk(32, 0xFFFF0000); + for (int i = 0; i < 48; i++) { + assertFalse(array.get(i)); + } + for (int i = 48; i < 64; i++) { + assertTrue(array.get(i)); + } +} + +void BitArrayTests::testClear() +{ + BitArray array(32); + for (int i = 0; i < 32; i++) { + array.set(i); + } + array.clear(); + for (int i = 0; i < 32; i++) { + assertFalse(array.get(i)); + } +} + +void BitArrayTests::testGetArray() +{ + BitArray array(64); + array.set(0); + array.set(63); + std::vector& ints = array.getBitArray(); + assertEquals(1, ints[0]); + assertEquals(INT_MIN, ints[1]); +} + +void BitArrayTests::testIsRange() +{ + BitArray array(64); + assertTrue(array.isRange(0, 64, false)); + assertFalse(array.isRange(0, 64, true)); + array.set(32); + assertTrue(array.isRange(32, 33, true)); + array.set(31); + assertTrue(array.isRange(31, 33, true)); + array.set(34); + assertFalse(array.isRange(31, 35, true)); + for (int i = 0; i < 31; i++) { + array.set(i); + } + assertTrue(array.isRange(0, 33, true)); + for (int i = 33; i < 64; i++) { + array.set(i); + } + assertTrue(array.isRange(0, 64, true)); + assertFalse(array.isRange(0, 64, false)); +} + +void BitArrayTests::reverseAlgorithmTest() +{ + std::vector oldBits = {128, 256, 512, 6453324, 50934953}; + for (int size = 1; size < 160; size++) { + std::vector newBitsOriginal = reverseOriginal(oldBits, size); + BitArray newBitArray(oldBits); + newBitArray.reverse(); + std::vector newBitsNew = newBitArray.getBitArray(); + assertTrue(arraysAreEqual(newBitsOriginal, newBitsNew, size / 32 + 1)); + } +} + +std::vector BitArrayTests::reverseOriginal(std::vector &oldBits, int size) +{ + std::vector newBits(oldBits.size()); + for (int i = 0; i < size; i++) { + if (bitSet(oldBits, size - i - 1)) { + newBits[i / 32] |= 1 << (i & 0x1F); + } + } + return newBits; +} + +bool BitArrayTests::bitSet(std::vector &bits, int i) +{ + return (bits[i / 32] & (1 << (i & 0x1F))) != 0; +} + +bool BitArrayTests::arraysAreEqual(std::vector &left, std::vector &right, int size) +{ + for (int i = 0; i < size; i++) { + if (left[i] != right[i]) { + return false; + } + } + return true; +} + +} +} diff --git a/tests/src/QZXingTests/zxing/common/BitArrayTests.h b/tests/src/QZXingTests/zxing/common/BitArrayTests.h new file mode 100644 index 0000000..8266ffe --- /dev/null +++ b/tests/src/QZXingTests/zxing/common/BitArrayTests.h @@ -0,0 +1,42 @@ +#ifndef BITARRAYTESTS_H +#define BITARRAYTESTS_H + +#include "TestCase.h" +#include +#include +#include + +namespace zxing{ +namespace tests{ + +class BitArrayTests : public TestCase +{ +public: + BitArrayTests(); + + void execute(); + +private: + + void testGetSet(); + void testGetNextSet1(); + void testGetNextSet2(); + void testGetNextSet3(); + void testGetNextSet4(); + //void testGetNextSet5(); + void testSetBulk(); + void testClear(); + void testGetArray(); + void testIsRange(); + void reverseAlgorithmTest(); + +private: + static std::vector reverseOriginal(std::vector &oldBits, int size); + static bool bitSet(std::vector &bits, int i); + static bool arraysAreEqual(std::vector &left, std::vector &right, int size); +}; + +} +} + +#endif // BITARRAYTESTS_H diff --git a/tests/src/QZXingTests/zxing/qrcode/encoder/BitArrayTests.cpp b/tests/src/QZXingTests/zxing/qrcode/encoder/BitVectorTests.cpp similarity index 88% rename from tests/src/QZXingTests/zxing/qrcode/encoder/BitArrayTests.cpp rename to tests/src/QZXingTests/zxing/qrcode/encoder/BitVectorTests.cpp index bed838a..be116c2 100644 --- a/tests/src/QZXingTests/zxing/qrcode/encoder/BitArrayTests.cpp +++ b/tests/src/QZXingTests/zxing/qrcode/encoder/BitVectorTests.cpp @@ -1,22 +1,22 @@ -#include "BitArrayTests.h" +#include "BitVectorTests.h" namespace zxing{ namespace qrcode{ namespace tests{ -BitArrayTests::BitArrayTests() +BitVectorTests::BitVectorTests() { } -void BitArrayTests::execute() +void BitVectorTests::execute() { testAppendBit(); testAppendBits(); testNumBytes(); } -unsigned long zxing::qrcode::tests::BitArrayTests::getUnsignedInt(BitArray &v, int index) +unsigned long zxing::qrcode::tests::BitVectorTests::getUnsignedInt(BitArray &v, int index) { long result = 0L; for (int i = 0, offset = index << 3; i < 32; i++) { @@ -27,7 +27,7 @@ unsigned long zxing::qrcode::tests::BitArrayTests::getUnsignedInt(BitArray &v, i return result; } -void BitArrayTests::testAppendBit() +void BitVectorTests::testAppendBit() { BitArray v; assertEquals(0, v.getSizeInBytes()); @@ -73,7 +73,7 @@ void BitArrayTests::testAppendBit() assertEquals(0xaa800000ul, getUnsignedInt(v, 0)); } -void BitArrayTests::testAppendBits() +void BitVectorTests::testAppendBits() { BitArray v; v.appendBits(0x1, 1); @@ -89,7 +89,7 @@ void BitArrayTests::testAppendBits() assertEquals(0xff700000ul, getUnsignedInt(v, 0)); } -void BitArrayTests::testNumBytes() +void BitVectorTests::testNumBytes() { BitArray v; assertEquals(0, v.getSizeInBytes()); @@ -105,7 +105,7 @@ void BitArrayTests::testNumBytes() assertEquals(3, v.getSizeInBytes()); } -void BitArrayTests::testAppendBitVector() +void BitVectorTests::testAppendBitVector() { BitArray v1; v1.appendBits(0xbe, 8); @@ -116,7 +116,7 @@ void BitArrayTests::testAppendBitVector() assertEquals(std::string(" X.XXXXX. XXX.XXXX"), v1.toString()); } -void BitArrayTests::testXOR() +void BitVectorTests::testXOR() { BitArray v1; v1.appendBits(0x5555aaaa, 32); @@ -126,7 +126,7 @@ void BitArrayTests::testXOR() assertEquals(0xfffffffful, getUnsignedInt(v1, 0)); } -void BitArrayTests::testXOR2() +void BitVectorTests::testXOR2() { BitArray v1; v1.appendBits(0x2a, 7); // 010 1010 @@ -136,7 +136,7 @@ void BitArrayTests::testXOR2() assertEquals(0xfe000000ul, getUnsignedInt(v1, 0)); // 1111 1110 } -void BitArrayTests::testAt() +void BitVectorTests::testAt() { BitArray v; v.appendBits(0xdead, 16); // 1101 1110 1010 1101 @@ -161,7 +161,7 @@ void BitArrayTests::testAt() assertTrue(v.get(15)); } -void BitArrayTests::testToString() +void BitVectorTests::testToString() { BitArray v; v.appendBits(0xdead, 16); // 1101 1110 1010 1101 diff --git a/tests/src/QZXingTests/zxing/qrcode/encoder/BitArrayTests.h b/tests/src/QZXingTests/zxing/qrcode/encoder/BitVectorTests.h similarity index 75% rename from tests/src/QZXingTests/zxing/qrcode/encoder/BitArrayTests.h rename to tests/src/QZXingTests/zxing/qrcode/encoder/BitVectorTests.h index 104bd7c..40b281c 100644 --- a/tests/src/QZXingTests/zxing/qrcode/encoder/BitArrayTests.h +++ b/tests/src/QZXingTests/zxing/qrcode/encoder/BitVectorTests.h @@ -1,5 +1,5 @@ -#ifndef BITARRAYTESTS_H -#define BITARRAYTESTS_H +#ifndef BITVECTORTESTS_H +#define BITVECTORTESTS_H #include "TestCase.h" #include "zxing/common/BitArray.h" @@ -8,10 +8,10 @@ namespace zxing{ namespace qrcode{ namespace tests{ -class BitArrayTests : public TestCase +class BitVectorTests : public TestCase { public: - BitArrayTests(); + BitVectorTests(); void execute(); @@ -32,4 +32,4 @@ private: } } -#endif // BITARRAYTESTS_H +#endif // BITVECTORTESTS_H