mirror of
https://github.com/status-im/qzxing.git
synced 2025-02-12 19:06:50 +00:00
added more tests for zxing/common/BitArray
This commit is contained in:
parent
069ded5923
commit
25c6b585f5
@ -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<int> other)
|
||||
: size(other.size()), bits(other.size())
|
||||
{
|
||||
for(int i=0; i<other.size(); i++)
|
||||
if(other[i])
|
||||
set(i);
|
||||
}
|
||||
|
||||
BitArray::~BitArray() {
|
||||
}
|
||||
|
||||
|
@ -25,7 +25,6 @@
|
||||
#include <vector>
|
||||
#include <limits>
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
namespace zxing {
|
||||
|
||||
@ -42,6 +41,7 @@ private:
|
||||
public:
|
||||
BitArray();
|
||||
BitArray(int size);
|
||||
BitArray(std::vector<int> other);
|
||||
~BitArray();
|
||||
int getSize() const;
|
||||
int getSizeInBytes() const;
|
||||
|
@ -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();
|
||||
|
||||
|
@ -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)
|
||||
|
@ -53,6 +53,10 @@ private:
|
||||
return QString::fromStdString(item.getName());
|
||||
}
|
||||
|
||||
static QString itemToString(unsigned int item) {
|
||||
return QString::number(item);
|
||||
}
|
||||
|
||||
protected:
|
||||
template<class T> void assertEquals(T expected, T actual) {
|
||||
if(expected != actual) {
|
||||
|
218
tests/src/QZXingTests/zxing/common/BitArrayTests.cpp
Normal file
218
tests/src/QZXingTests/zxing/common/BitArrayTests.cpp
Normal file
@ -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<int>& 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<int> oldBits = {128, 256, 512, 6453324, 50934953};
|
||||
for (int size = 1; size < 160; size++) {
|
||||
std::vector<int> newBitsOriginal = reverseOriginal(oldBits, size);
|
||||
BitArray newBitArray(oldBits);
|
||||
newBitArray.reverse();
|
||||
std::vector<int> newBitsNew = newBitArray.getBitArray();
|
||||
assertTrue(arraysAreEqual(newBitsOriginal, newBitsNew, size / 32 + 1));
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<int> BitArrayTests::reverseOriginal(std::vector<int> &oldBits, int size)
|
||||
{
|
||||
std::vector<int> 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<int> &bits, int i)
|
||||
{
|
||||
return (bits[i / 32] & (1 << (i & 0x1F))) != 0;
|
||||
}
|
||||
|
||||
bool BitArrayTests::arraysAreEqual(std::vector<int> &left, std::vector<int> &right, int size)
|
||||
{
|
||||
for (int i = 0; i < size; i++) {
|
||||
if (left[i] != right[i]) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
42
tests/src/QZXingTests/zxing/common/BitArrayTests.h
Normal file
42
tests/src/QZXingTests/zxing/common/BitArrayTests.h
Normal file
@ -0,0 +1,42 @@
|
||||
#ifndef BITARRAYTESTS_H
|
||||
#define BITARRAYTESTS_H
|
||||
|
||||
#include "TestCase.h"
|
||||
#include <zxing/common/BitArray.h>
|
||||
#include <zxing/common/Types.h>
|
||||
#include <vector>
|
||||
|
||||
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<int> reverseOriginal(std::vector<int> &oldBits, int size);
|
||||
static bool bitSet(std::vector<int> &bits, int i);
|
||||
static bool arraysAreEqual(std::vector<int> &left, std::vector<int> &right, int size);
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#endif // BITARRAYTESTS_H
|
@ -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
|
@ -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
|
Loading…
x
Reference in New Issue
Block a user