From 913f87b1ce2798c8e57ff483eab978831cde94fd Mon Sep 17 00:00:00 2001 From: "smparkes@smparkes.net" Date: Mon, 1 Apr 2013 06:00:11 +0000 Subject: [PATCH] 1d complete; tests pass; need to valgrind git-svn-id: https://zxing.googlecode.com/svn/trunk@2606 59b500cc-1b3d-0410-9834-0bbf25fbcc57 --- cpp/.gdbinit | 2 + cpp/.valgrind.supp | 25 +++ cpp/core/src/zxing/common/BitArray.cpp | 166 +++++++++--------- cpp/core/src/zxing/common/BitArray.h | 22 +-- cpp/core/src/zxing/common/BitMatrix.cpp | 117 ++++++------ cpp/core/src/zxing/common/BitMatrix.h | 26 +-- .../zxing/common/IllegalArgumentException.cpp | 12 +- .../zxing/common/IllegalArgumentException.h | 1 + .../datamatrix/decoder/BitMatrixParser.cpp | 6 +- .../zxing/qrcode/decoder/BitMatrixParser.cpp | 11 +- cpp/core/tests/src/common/BitArrayTest.cpp | 50 +++--- cpp/core/tests/src/common/BitMatrixTest.cpp | 18 +- cpp/core/tests/src/common/BitMatrixTest.h | 4 +- .../src/common/BlackPointEstimatorTest.cpp | 49 ------ .../src/common/BlackPointEstimatorTest.h | 45 ----- .../common/reedsolomon/ReedSolomonTest.cpp | 1 + cpp/core/tests/src/qrcode/VersionTest.cpp | 2 +- 17 files changed, 237 insertions(+), 320 deletions(-) delete mode 100644 cpp/core/tests/src/common/BlackPointEstimatorTest.cpp delete mode 100644 cpp/core/tests/src/common/BlackPointEstimatorTest.h diff --git a/cpp/.gdbinit b/cpp/.gdbinit index adb6d4184..8db67b724 100644 --- a/cpp/.gdbinit +++ b/cpp/.gdbinit @@ -1 +1,3 @@ set history save on +b __cxa_throw +r diff --git a/cpp/.valgrind.supp b/cpp/.valgrind.supp index 4b343b323..7884f7f5d 100644 --- a/cpp/.valgrind.supp +++ b/cpp/.valgrind.supp @@ -9,4 +9,29 @@ dlsym Memcheck:Cond fun:_ZN4dyld9mkstringfEPKcz +} + +{ + osx + Memcheck:Leak + fun:malloc_zone_malloc + fun:recursive_mutex_init + fun:_objc_init + fun:libSystem_initializer +} + +{ + throws + Memcheck:Leak + fun:calloc + fun:__cxa_get_globals + fun:__cxa_throw +} + +{ + osx + Memcheck:Leak + fun:malloc_zone_calloc + ... + fun:map_images } \ No newline at end of file diff --git a/cpp/core/src/zxing/common/BitArray.cpp b/cpp/core/src/zxing/common/BitArray.cpp index 44ba7065b..8b714728b 100644 --- a/cpp/core/src/zxing/common/BitArray.cpp +++ b/cpp/core/src/zxing/common/BitArray.cpp @@ -20,75 +20,74 @@ using std::vector; using zxing::BitArray; -int BitArray::wordsForBits(int bits) { - int arraySize = (bits + bitsPerWord_ - 1) >> logBits_; - return arraySize; +int BitArray::makeArraySize(int size) { + return (size + bitsPerWord-1) >> logBits; } -BitArray::BitArray(int size) : - size_(size), bits_(wordsForBits(size), 0) { -} +BitArray::BitArray(int size_) + : size(size_), bits(makeArraySize(size)) {} BitArray::~BitArray() { } int BitArray::getSize() const { - return size_; + return size; } void BitArray::setBulk(int i, int newBits) { - bits_[i >> logBits_] = newBits; + bits[i >> logBits] = newBits; } -void BitArray::setRange(int start, int end) { +/* + void BitArray::setRange(int start, int end) { if (end < start) { - throw IllegalArgumentException("invalid call to BitArray::setRange"); + throw IllegalArgumentException("invalid call to BitArray::setRange"); } if (end == start) { - return; + return; } end--; // will be easier to treat this as the last actually set bit -- inclusive int firstInt = start >> 5; int lastInt = end >> 5; for (int i = firstInt; i <= lastInt; i++) { - int firstBit = i > firstInt ? 0 : start & 0x1F; - int lastBit = i < lastInt ? 31 : end & 0x1F; - int mask; - if (firstBit == 0 && lastBit == 31) { - mask = -1; - } else { - mask = 0; - for (int j = firstBit; j <= lastBit; j++) { - mask |= 1 << j; - } - } - bits_[i] |= mask; + int firstBit = i > firstInt ? 0 : start & 0x1F; + int lastBit = i < lastInt ? 31 : end & 0x1F; + int mask; + if (firstBit == 0 && lastBit == 31) { + mask = -1; + } else { + mask = 0; + for (int j = firstBit; j <= lastBit; j++) { + mask |= 1 << j; } -} + } + bits_[i] |= mask; + } + } +*/ void BitArray::clear() { - int max = bits_.size(); + int max = bits.size(); for (int i = 0; i < max; i++) { - bits_[i] = 0; + bits[i] = 0; } } bool BitArray::isRange(int start, int end, bool value) { if (end < start) { - throw IllegalArgumentException("end must be after start"); + throw IllegalArgumentException(); } if (end == start) { - return true; + return true; // empty range matches } - // treat the 'end' as inclusive, rather than exclusive - end--; - int firstWord = start >> logBits_; - int lastWord = end >> logBits_; - for (int i = firstWord; i <= lastWord; i++) { - int firstBit = i > firstWord ? 0 : start & bitsMask_; - int lastBit = i < lastWord ? bitsPerWord_ - 1: end & bitsMask_; + end--; // will be easier to treat this as the last actually set bit -- inclusive + int firstInt = start >> logBits; + int lastInt = end >> logBits; + for (int i = firstInt; i <= lastInt; i++) { + int firstBit = i > firstInt ? 0 : start & bitsMask; + int lastBit = i < lastInt ? (bitsPerWord-1) : end & bitsMask; int mask; - if (firstBit == 0 && lastBit == bitsPerWord_ - 1) { + if (firstBit == 0 && lastBit == (bitsPerWord-1)) { mask = -1; } else { mask = 0; @@ -96,88 +95,87 @@ bool BitArray::isRange(int start, int end, bool value) { mask |= 1 << j; } } - if (value) { - if ((bits_[i] & mask) != mask) { - return false; - } - } else { - if ((bits_[i] & mask) != 0) { - return false; - } + + // Return false if we're looking for 1s and the masked bits[i] isn't all 1s (that is, + // equals the mask, or we're looking for 0s and the masked portion is not all 0s + if ((bits[i] & mask) != (value ? mask : 0)) { + return false; } } return true; } vector& BitArray::getBitArray() { - return bits_; + return bits; } void BitArray::reverse() { - // std::cerr << "reverse" << std::endl; - std::vector newBits(bits_.size(), 0); - for (int i = 0; i < size_; i++) { - if (get(size_ - i - 1)) { - newBits[i >> logBits_] |= 1<< (i & bitsMask_); + vector newBits(bits.size()); + int size = this->size; + for (int i = 0; i < size; i++) { + if (get(size - i - 1)) { + newBits[i >> logBits] |= 1 << (i & bitsMask); } } - bits_ = newBits; + bits = newBits; } -BitArray::Reverse::Reverse(Ref array_) : array(array_) { +/* + BitArray::Reverse::Reverse(Ref array_) : array(array_) { array->reverse(); -} - -BitArray::Reverse::~Reverse() { - array->reverse(); -} - -namespace { - int numberOfTrailingZeros(int i) { - // HD, Figure 5-14 - int y; - if (i == 0) return 32; - int n = 31; - y = i <<16; if (y != 0) { n = n -16; i = y; } - y = i << 8; if (y != 0) { n = n - 8; i = y; } - y = i << 4; if (y != 0) { n = n - 4; i = y; } - y = i << 2; if (y != 0) { n = n - 2; i = y; } - return n - (((unsigned int)(i << 1)) >> 31); } -} -int BitArray::getNextSet(int from) { + BitArray::Reverse::~Reverse() { + array->reverse(); + } + + namespace { + int numberOfTrailingZeros(int i) { + // HD, Figure 5-14 + int y; + if (i == 0) return 32; + int n = 31; + y = i <<16; if (y != 0) { n = n -16; i = y; } + y = i << 8; if (y != 0) { n = n - 8; i = y; } + y = i << 4; if (y != 0) { n = n - 4; i = y; } + y = i << 2; if (y != 0) { n = n - 2; i = y; } + return n - (((unsigned int)(i << 1)) >> 31); + } + } + + int BitArray::getNextSet(int from) { if (from >= size_) { - return size_; + return size_; } int bitsOffset = from >> 5; int currentBits = bits_[bitsOffset]; // mask off lesser bits first currentBits &= ~((1 << (from & 0x1F)) - 1); while (currentBits == 0) { - if (++bitsOffset == (int)bits_.size()) { - return size_; - } - currentBits = bits_[bitsOffset]; + if (++bitsOffset == (int)bits_.size()) { + return size_; + } + currentBits = bits_[bitsOffset]; } int result = (bitsOffset << 5) + numberOfTrailingZeros(currentBits); return result > size_ ? size_ : result; -} + } -int BitArray::getNextUnset(int from) { + int BitArray::getNextUnset(int from) { if (from >= size_) { - return size_; + return size_; } int bitsOffset = from >> 5; int currentBits = ~bits_[bitsOffset]; // mask off lesser bits first currentBits &= ~((1 << (from & 0x1F)) - 1); while (currentBits == 0) { - if (++bitsOffset == (int)bits_.size()) { - return size_; - } - currentBits = ~bits_[bitsOffset]; + if (++bitsOffset == (int)bits_.size()) { + return size_; + } + currentBits = ~bits_[bitsOffset]; } int result = (bitsOffset << 5) + numberOfTrailingZeros(currentBits); return result > size_ ? size_ : result; -} + } +*/ diff --git a/cpp/core/src/zxing/common/BitArray.h b/cpp/core/src/zxing/common/BitArray.h index 36acc7a93..2832647a6 100644 --- a/cpp/core/src/zxing/common/BitArray.h +++ b/cpp/core/src/zxing/common/BitArray.h @@ -38,15 +38,14 @@ namespace zxing { (-1)))))) class zxing::BitArray : public Counted { +public: + static const int bitsPerWord = std::numeric_limits::digits; + private: - int size_; - std::vector bits_; - static const int bitsPerWord_ = - std::numeric_limits::digits; - static const int logBits_ = ZX_LOG_DIGITS(bitsPerWord_); - static const int bitsMask_ = (1 << logBits_) - 1; - static int wordsForBits(int bits); - explicit BitArray(); + int size; + std::vector bits; + static const int logBits = ZX_LOG_DIGITS(bitsPerWord); + static const int bitsMask = (1 << logBits) - 1; public: BitArray(int size); @@ -54,11 +53,11 @@ public: int getSize() const; bool get(int i) const { - return (bits_[i >> logBits_] & (1 << (i & bitsMask_))) != 0; + return (bits[i >> logBits] & (1 << (i & bitsMask))) != 0; } void set(int i) { - bits_[i >> logBits_] |= 1 << (i & bitsMask_); + bits[i >> logBits] |= 1 << (i & bitsMask); } int getNextSet(int from); @@ -72,6 +71,9 @@ public: void reverse(); class Reverse; + +private: + static int makeArraySize(int size); }; class zxing::BitArray::Reverse { diff --git a/cpp/core/src/zxing/common/BitMatrix.cpp b/cpp/core/src/zxing/common/BitMatrix.cpp index 581e0c542..2360d9d43 100644 --- a/cpp/core/src/zxing/common/BitMatrix.cpp +++ b/cpp/core/src/zxing/common/BitMatrix.cpp @@ -30,43 +30,58 @@ using zxing::BitArray; using zxing::ArrayRef; using zxing::Ref; -namespace { - int wordsForSize(int width, - int height, - int bitsPerWord, - int logBits) { - int bits = width * height; - int arraySize = (bits + bitsPerWord - 1) >> logBits; - return arraySize; +void BitMatrix::init(int width, int height) { + if (width < 1 || height < 1) { + throw IllegalArgumentException("Both dimensions must be greater than 0"); } + this->width = width; + this->height = height; + this->rowSize = (width + bitsPerWord - 1) >> logBits; + bits = ArrayRef(rowSize * height); } -BitMatrix::BitMatrix(int dimension) : - width_(dimension), height_(dimension), words_(0) { - words_ = wordsForSize(width_, height_, bitsPerWord, logBits); - bits_ = ArrayRef(words_); - clear(); +BitMatrix::BitMatrix(int dimension) { + init(dimension, dimension); } -BitMatrix::BitMatrix(int width, int height) : - width_(width), height_(height), words_(0) { - words_ = wordsForSize(width_, height_, bitsPerWord, logBits); - bits_ = ArrayRef(words_); - clear(); +BitMatrix::BitMatrix(int width, int height) { + init(width, height); } BitMatrix::~BitMatrix() {} - void BitMatrix::flip(int x, int y) { - int offset = x + width_ * y; - bits_[offset >> logBits] ^= 1 << (offset & bitsMask); + int offset = y * rowSize + (x >> logBits); + bits[offset] ^= 1 << (x & bitsMask); } +/* void BitMatrix::clear() { - std::fill(&bits_[0], &bits_[words_], 0); + std::fill(&bits[0], &bits[rowSize], 0); +} +*/ + +void BitMatrix::setRegion(int left, int top, int width, int height) { + if (top < 0 || left < 0) { + throw IllegalArgumentException("Left and top must be nonnegative"); + } + if (height < 1 || width < 1) { + throw IllegalArgumentException("Height and width must be at least 1"); + } + int right = left + width; + int bottom = top + height; + if (bottom > this->height || right > this->width) { + throw new IllegalArgumentException("The region must fit inside the matrix"); + } + for (int y = top; y < bottom; y++) { + int offset = y * rowSize; + for (int x = left; x < right; x++) { + bits[offset + (x >> logBits)] |= 1 << (x & bitsMask); + } + } } +/* void BitMatrix::setRegion(int left, int top, int width, int height) { if ((long)top < 0 || (long)left < 0) { throw IllegalArgumentException("topI and leftJ must be nonnegative"); @@ -76,71 +91,43 @@ void BitMatrix::setRegion(int left, int top, int width, int height) { } int right = left + width; int bottom = top + height; - if (right > width_ || bottom > height_) { + if (right > this->width || bottom > this->height) { throw IllegalArgumentException("top + height and left + width must be <= matrix dimension"); } for (int y = top; y < bottom; y++) { - int yOffset = width_ * y; + int offset = y * rowSize; for (int x = left; x < right; x++) { int offset = x + yOffset; - bits_[offset >> logBits] |= 1 << (offset & bitsMask); + bits[offset + (x >> 5)] |= 1 << (offset & bitsMask); } } } +*/ Ref BitMatrix::getRow(int y, Ref row) { - if (row.empty() || row->getSize() < width_) { - row = new BitArray(width_); - } else { - row->clear(); + if (row.empty() || row->getSize() < width) { + row = new BitArray(width); } - int start = y * width_; - int end = start + width_ - 1; // end is inclusive - int firstWord = start >> logBits; - int lastWord = end >> logBits; - int bitOffset = start & bitsMask; - for (int i = firstWord; i <= lastWord; i++) { - int firstBit = i > firstWord ? 0 : start & bitsMask; - int lastBit = i < lastWord ? bitsPerWord - 1 : end & bitsMask; - int mask; - if (firstBit == 0 && lastBit == logBits) { - mask = std::numeric_limits::max(); - } else { - mask = 0; - for (int j = firstBit; j <= lastBit; j++) { - mask |= 1 << j; - } - } - row->setBulk((i - firstWord) << logBits, (bits_[i] & mask) >> bitOffset); - if (firstBit == 0 && bitOffset != 0) { - int prevBulk = row->getBitArray()[i - firstWord - 1]; - prevBulk |= (bits_[i] & mask) << (bitsPerWord - bitOffset); - row->setBulk((i - firstWord - 1) << logBits, prevBulk); - } + int offset = y * rowSize; + for (int x = 0; x < rowSize; x++) { + row->setBulk(x << logBits, bits[offset + x]); } return row; } int BitMatrix::getWidth() const { - return width_; + return width; } int BitMatrix::getHeight() const { - return height_; -} - -int BitMatrix::getDimension() const { - return width_; -} - -ArrayRef BitMatrix::getBits() const { - return bits_; + return height; } +/* namespace zxing { ostream& operator<<(ostream &out, const BitMatrix &bm) { - for (int y = 0; y < bm.height_; y++) { - for (int x = 0; x < bm.width_; x++) { + for (int y = 0; y < bm.height; y++) { + for (int x = 0; x < bm.width; x++) { out << (bm.get(x, y) ? "X " : " "); } out << "\n"; @@ -154,3 +141,5 @@ const char* BitMatrix::description() { out << *this; return out.str().c_str(); } + +*/ diff --git a/cpp/core/src/zxing/common/BitMatrix.h b/cpp/core/src/zxing/common/BitMatrix.h index 18e7a6eb5..dfbb2e06f 100644 --- a/cpp/core/src/zxing/common/BitMatrix.h +++ b/cpp/core/src/zxing/common/BitMatrix.h @@ -31,11 +31,14 @@ namespace zxing { } class zxing::BitMatrix : public Counted { +public: + static const int bitsPerWord = std::numeric_limits::digits; + private: - int width_; - int height_; - int words_; - ArrayRef bits_; + int width; + int height; + int rowSize; + ArrayRef bits; #define ZX_LOG_DIGITS(digits) \ ((digits == 8) ? 3 : \ @@ -45,8 +48,6 @@ private: ((digits == 128) ? 7 : \ (-1)))))) - static const int bitsPerWord = - std::numeric_limits::digits; static const int logBits = ZX_LOG_DIGITS(bitsPerWord); static const int bitsMask = (1 << logBits) - 1; @@ -57,13 +58,13 @@ public: ~BitMatrix(); bool get(int x, int y) const { - int offset = x + width_ * y; - return ((bits_[offset >> logBits] >> (offset & bitsMask)) & 0x01) != 0; + int offset = y * rowSize + (x >> logBits); + return ((((unsigned)bits[offset]) >> (x & bitsMask)) & 1) != 0; } void set(int x, int y) { - int offset = x + width_ * y; - bits_[offset >> logBits] |= 1 << (offset & bitsMask); + int offset = y * rowSize + (x >> logBits); + bits[offset] |= 1 << (x & bitsMask); } void flip(int x, int y); @@ -71,16 +72,17 @@ public: void setRegion(int left, int top, int width, int height); Ref getRow(int y, Ref row); - int getDimension() const; int getWidth() const; int getHeight() const; - ArrayRef getBits() const; + // ArrayRef getBits() const; friend std::ostream& operator<<(std::ostream &out, const BitMatrix &bm); const char *description(); private: + inline void init(int, int); + BitMatrix(const BitMatrix&); BitMatrix& operator =(const BitMatrix&); }; diff --git a/cpp/core/src/zxing/common/IllegalArgumentException.cpp b/cpp/core/src/zxing/common/IllegalArgumentException.cpp index 49068ca5d..45c1fa1c4 100644 --- a/cpp/core/src/zxing/common/IllegalArgumentException.cpp +++ b/cpp/core/src/zxing/common/IllegalArgumentException.cpp @@ -20,12 +20,8 @@ #include -namespace zxing { +using zxing::IllegalArgumentException; -IllegalArgumentException::IllegalArgumentException(const char *msg) : - Exception(msg) { -} -IllegalArgumentException::~IllegalArgumentException() throw() { - -} -} +IllegalArgumentException::IllegalArgumentException() : Exception() {} +IllegalArgumentException::IllegalArgumentException(const char *msg) : Exception(msg) {} +IllegalArgumentException::~IllegalArgumentException() throw() {} diff --git a/cpp/core/src/zxing/common/IllegalArgumentException.h b/cpp/core/src/zxing/common/IllegalArgumentException.h index 9d536c384..4aa3ce973 100644 --- a/cpp/core/src/zxing/common/IllegalArgumentException.h +++ b/cpp/core/src/zxing/common/IllegalArgumentException.h @@ -25,6 +25,7 @@ namespace zxing { class IllegalArgumentException : public zxing::Exception { public: + IllegalArgumentException(); IllegalArgumentException(const char *msg); ~IllegalArgumentException() throw(); }; diff --git a/cpp/core/src/zxing/datamatrix/decoder/BitMatrixParser.cpp b/cpp/core/src/zxing/datamatrix/decoder/BitMatrixParser.cpp index ef31f6fbc..94634680a 100644 --- a/cpp/core/src/zxing/datamatrix/decoder/BitMatrixParser.cpp +++ b/cpp/core/src/zxing/datamatrix/decoder/BitMatrixParser.cpp @@ -33,7 +33,7 @@ int BitMatrixParser::copyBit(size_t x, size_t y, int versionBits) { BitMatrixParser::BitMatrixParser(Ref bitMatrix) : bitMatrix_(NULL), parsedVersion_(NULL), readBitMatrix_(NULL) { - size_t dimension = bitMatrix->getDimension(); + size_t dimension = bitMatrix->getHeight(); if (dimension < 8 || dimension > 144 || (dimension & 0x01) != 0) throw ReaderException("Dimension must be even, > 8 < 144"); @@ -47,8 +47,8 @@ Ref BitMatrixParser::readVersion(Ref bitMatrix) { return parsedVersion_; } - int numRows = bitMatrix->getHeight();//getDimension(); - int numColumns = bitMatrix->getWidth();//numRows; + int numRows = bitMatrix->getHeight(); + int numColumns = bitMatrix->getWidth(); Ref version = parsedVersion_->getVersionForDimensions(numRows, numColumns); if (version != 0) { diff --git a/cpp/core/src/zxing/qrcode/decoder/BitMatrixParser.cpp b/cpp/core/src/zxing/qrcode/decoder/BitMatrixParser.cpp index ea80bd58e..467d8fcbb 100644 --- a/cpp/core/src/zxing/qrcode/decoder/BitMatrixParser.cpp +++ b/cpp/core/src/zxing/qrcode/decoder/BitMatrixParser.cpp @@ -31,7 +31,7 @@ int BitMatrixParser::copyBit(size_t x, size_t y, int versionBits) { BitMatrixParser::BitMatrixParser(Ref bitMatrix) : bitMatrix_(bitMatrix), parsedVersion_(0), parsedFormatInfo_() { - size_t dimension = bitMatrix->getDimension(); + size_t dimension = bitMatrix->getHeight(); if ((dimension < 21) || (dimension & 0x03) != 1) { throw ReaderException("Dimension must be 1 mod 4 and >= 21"); } @@ -57,7 +57,7 @@ Ref BitMatrixParser::readFormatInformation() { } // Read the top-right/bottom-left pattern - int dimension = bitMatrix_->getDimension(); + int dimension = bitMatrix_->getHeight(); int formatInfoBits2 = 0; int jMin = dimension - 7; for (int j = dimension - 1; j >= jMin; j--) { @@ -79,7 +79,7 @@ Version *BitMatrixParser::readVersion() { return parsedVersion_; } - int dimension = bitMatrix_->getDimension(); + int dimension = bitMatrix_->getHeight(); int provisionalVersion = (dimension - 17) >> 2; if (provisionalVersion <= 6) { @@ -121,14 +121,11 @@ ArrayRef BitMatrixParser::readCodewords() { Version *version = readVersion(); - // cerr << *bitMatrix_ << endl; - // cerr << bitMatrix_->getDimension() << endl; - // Get the data mask for the format used in this QR Code. This will exclude // some bits from reading as we wind through the bit matrix. DataMask &dataMask = DataMask::forReference((int)formatInfo->getDataMask()); // cout << (int)formatInfo->getDataMask() << endl; - int dimension = bitMatrix_->getDimension(); + int dimension = bitMatrix_->getHeight(); dataMask.unmaskBitMatrix(*bitMatrix_, dimension); diff --git a/cpp/core/tests/src/common/BitArrayTest.cpp b/cpp/core/tests/src/common/BitArrayTest.cpp index 560d348ea..98ba4b6ef 100644 --- a/cpp/core/tests/src/common/BitArrayTest.cpp +++ b/cpp/core/tests/src/common/BitArrayTest.cpp @@ -29,9 +29,9 @@ namespace zxing { CPPUNIT_TEST_SUITE_REGISTRATION(BitArrayTest); void BitArrayTest::testGetSet() { - size_t bits = numeric_limits::digits + 1; + const int bits = BitArray::bitsPerWord + 1; BitArray array(bits); - for(size_t i = 0; i < bits; i++) { + for(int i = 0; i < bits; i++) { CPPUNIT_ASSERT_EQUAL(false, array.get(i)); array.set(i); CPPUNIT_ASSERT_EQUAL(true, array.get(i)); @@ -50,19 +50,19 @@ void BitArrayTest::testSetBulk() { } void BitArrayTest::testClear() { - size_t bits = numeric_limits::digits; + const int bits = BitArray::bitsPerWord; BitArray array(bits); - for(size_t i = 0; i < bits; i++) { + for(int i = 0; i < bits; i++) { array.set(i); } array.clear(); - for(size_t i = 0; i < bits; i++) { + for(int i = 0; i < bits; i++) { CPPUNIT_ASSERT_EQUAL(false, array.get(i)); } } void BitArrayTest::testGetArray() { - size_t bits = numeric_limits::digits; + const int bits = BitArray::bitsPerWord; BitArray array(2 * bits); array.set(0); array.set(2 * bits - 1); @@ -72,8 +72,8 @@ void BitArrayTest::testGetArray() { } void BitArrayTest::testIsRange() { - size_t bits = numeric_limits::digits; - size_t bits2 = 2 * bits; + const int bits = BitArray::bitsPerWord; + int bits2 = 2 * bits; BitArray array(bits2); CPPUNIT_ASSERT_EQUAL(true, array.isRange(0, bits2, false)); CPPUNIT_ASSERT_EQUAL(false, array.isRange(0, bits2, true)); @@ -83,7 +83,7 @@ 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(int i = 0; i < bits - 1; i++) { array.set(i); } CPPUNIT_ASSERT_EQUAL(true, array.isRange(0, bits + 1, true)); @@ -108,39 +108,39 @@ void BitArrayTest::fillRandom(BitArray& test, BitArray& reference) { void BitArrayTest::testReverseHalves() { // one word test, split in half { - size_t bits = numeric_limits::digits; + const int bits = BitArray::bitsPerWord; BitArray test(bits); test.clear(); - for(size_t i = 0; i < bits / 2; ++i) { + for(int i = 0; i < bits / 2; ++i) { test.set(i); } test.reverse(); - for(size_t i = 0; i < bits / 2; ++i) { + for(int 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::digits * 2; + const int bits2 = BitArray::bitsPerWord * 2; BitArray test2(bits2); test2.clear(); - for(size_t i = 0; i < bits2 / 2; ++i) { + for(int i = 0; i < bits2 / 2; ++i) { test2.set(i); } test2.reverse(); - for(size_t i = 0; i < bits2 / 2; ++i) { + for(int 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::digits * 8; + const int bits = BitArray::bitsPerWord * 8; BitArray test(bits); BitArray reference(bits); @@ -150,13 +150,13 @@ void BitArrayTest::testReverseEven() { fillRandom(test, reference); test.reverse(); - for(size_t i = 0; i < bits; ++i) { + for(int i = 0; i < bits; ++i) { CPPUNIT_ASSERT_EQUAL(test.get(i), reference.get(bits - 1 - i)); } } void BitArrayTest::testReverseOdd() { - size_t bits = numeric_limits::digits * 6 + 11; + const int bits = BitArray::bitsPerWord * 6 + 11; BitArray test(bits); BitArray reference(bits); @@ -166,14 +166,14 @@ void BitArrayTest::testReverseOdd() { fillRandom(test, reference); test.reverse(); - for(size_t i = 0; i < bits; ++i) { + for(int 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::digits * 10; + int bits; + const int bitsHigh = BitArray::bitsPerWord * 10; for(bits = 1; bits < bitsHigh; ++bits) { BitArray test(bits); @@ -185,14 +185,14 @@ void BitArrayTest::testReverseSweep() { fillRandom(test, reference); test.reverse(); - for(size_t i = 0; i < bits; ++i) { + for(int i = 0; i < bits; ++i) { CPPUNIT_ASSERT_EQUAL(test.get(i), reference.get(bits - 1 - i)); } } } void BitArrayTest::testReverseReverse() { - size_t bits = numeric_limits::digits * 4 + 17; + const int bits = BitArray::bitsPerWord * 4 + 17; BitArray test(bits); BitArray reference(bits); @@ -203,12 +203,12 @@ void BitArrayTest::testReverseReverse() { // flip it once and test test.reverse(); - for(size_t i = 0; i < bits; ++i) { + for(int 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) { + for(int i = 0; i < bits; ++i) { CPPUNIT_ASSERT_EQUAL(test.get(i), reference.get(i)); } } diff --git a/cpp/core/tests/src/common/BitMatrixTest.cpp b/cpp/core/tests/src/common/BitMatrixTest.cpp index 76f77108f..26625b5c3 100644 --- a/cpp/core/tests/src/common/BitMatrixTest.cpp +++ b/cpp/core/tests/src/common/BitMatrixTest.cpp @@ -32,9 +32,9 @@ BitMatrixTest::BitMatrixTest() { } void BitMatrixTest::testGetSet() { - int bits = numeric_limits::digits; + const int bits = BitMatrix::bitsPerWord; BitMatrix matrix(bits + 1); - CPPUNIT_ASSERT_EQUAL(bits + 1, matrix.getDimension()); + CPPUNIT_ASSERT_EQUAL(bits + 1, matrix.getHeight()); for (int i = 0; i < bits + 1; i++) { for (int j = 0; j < bits + 1; j++) { if (i * j % 3 == 0) { @@ -60,13 +60,10 @@ void BitMatrixTest::testSetRegion() { } } -void BitMatrixTest::testGetBits() { - BitMatrix matrix(6); - matrix.set(0, 0); - matrix.set(5, 5); - ArrayRef bits = matrix.getBits(); - CPPUNIT_ASSERT_EQUAL(1, bits[0]); - CPPUNIT_ASSERT_EQUAL(8, bits[1]); +void BitMatrixTest::testGetRow0() { + const int width = 2; + const int height = 2; + runBitMatrixGetRowTest(width, height); } void BitMatrixTest::testGetRow1() { @@ -91,7 +88,8 @@ void BitMatrixTest::runBitMatrixGetRowTest(int width, int height) { BitMatrix mat(width, height); for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { - if ((rand() & 0x01) != 0) { + bool v = ((rand() & 0x01) != 0); + if (v) { mat.set(x, y); } } diff --git a/cpp/core/tests/src/common/BitMatrixTest.h b/cpp/core/tests/src/common/BitMatrixTest.h index c5841d959..61641ccf4 100644 --- a/cpp/core/tests/src/common/BitMatrixTest.h +++ b/cpp/core/tests/src/common/BitMatrixTest.h @@ -29,7 +29,7 @@ class BitMatrixTest : public CPPUNIT_NS::TestFixture { CPPUNIT_TEST_SUITE(BitMatrixTest); CPPUNIT_TEST(testGetSet); CPPUNIT_TEST(testSetRegion); - CPPUNIT_TEST(testGetBits); + CPPUNIT_TEST(testGetRow0); CPPUNIT_TEST(testGetRow1); CPPUNIT_TEST(testGetRow2); CPPUNIT_TEST(testGetRow3); @@ -41,7 +41,7 @@ public: protected: void testGetSet(); void testSetRegion(); - void testGetBits(); + void testGetRow0(); void testGetRow1(); void testGetRow2(); void testGetRow3(); diff --git a/cpp/core/tests/src/common/BlackPointEstimatorTest.cpp b/cpp/core/tests/src/common/BlackPointEstimatorTest.cpp deleted file mode 100644 index 75c51accc..000000000 --- a/cpp/core/tests/src/common/BlackPointEstimatorTest.cpp +++ /dev/null @@ -1,49 +0,0 @@ -/* - * BlackPointEstimatorTest.cpp - * zxing - * - * Created by Christian Brunschen on 12/05/2008. - * Copyright 2008 Google UK. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "BlackPointEstimatorTest.h" -#include -#include - -namespace zxing { -using namespace std; -CPPUNIT_TEST_SUITE_REGISTRATION(BlackPointEstimatorTest); - -void BlackPointEstimatorTest::testBasic() { - int histogramRaw[] = { 0, 0, 11, 43, 37, 18, 3, 1, 0, 0, 13, 36, 24, 0, 11, 2 }; - vector histogram(histogramRaw, histogramRaw+16); - ArrayRef array (new Array(histogram)); - int point = GlobalHistogramBinarizer::estimateBlackPoint(array); - CPPUNIT_ASSERT_EQUAL((int)64, point); -} - -void BlackPointEstimatorTest::testTooLittleRange() { - try { - int histogramRaw[] = { 0, 0, 0, 0, 0, 0, 1, 43, 48, 18, 3, 1, 0, 0, 0, 0 }; - vector histogram(histogramRaw, histogramRaw+16); - ArrayRef array (new Array(histogram)); - GlobalHistogramBinarizer::estimateBlackPoint(array); - CPPUNIT_FAIL("Should have thrown an exception"); - - } catch (IllegalArgumentException ie) { - // good - } -} -} diff --git a/cpp/core/tests/src/common/BlackPointEstimatorTest.h b/cpp/core/tests/src/common/BlackPointEstimatorTest.h deleted file mode 100644 index 8e7556610..000000000 --- a/cpp/core/tests/src/common/BlackPointEstimatorTest.h +++ /dev/null @@ -1,45 +0,0 @@ -#ifndef __BLACK_POINT_ESTIMATOR_TEST_H__ -#define __BLACK_POINT_ESTIMATOR_TEST_H__ - -/* - * BlackPointEstimatorTest.h - * zxing - * - * Copyright 2010 ZXing authors All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include -#include -#include - -namespace zxing { -class BlackPointEstimatorTest : public CPPUNIT_NS::TestFixture { - CPPUNIT_TEST_SUITE(BlackPointEstimatorTest); - CPPUNIT_TEST(testBasic); - CPPUNIT_TEST(testTooLittleRange); - CPPUNIT_TEST_SUITE_END(); - -public: - -protected: - void testBasic(); - void testTooLittleRange(); - -private: -}; -} - - -#endif // __BLACK_POINT_ESTIMATOR_TEST_H__ diff --git a/cpp/core/tests/src/common/reedsolomon/ReedSolomonTest.cpp b/cpp/core/tests/src/common/reedsolomon/ReedSolomonTest.cpp index 2a5c053df..bd0dcfb24 100644 --- a/cpp/core/tests/src/common/reedsolomon/ReedSolomonTest.cpp +++ b/cpp/core/tests/src/common/reedsolomon/ReedSolomonTest.cpp @@ -58,6 +58,7 @@ void ReedSolomonTest::setUp() { } void ReedSolomonTest::tearDown() { + delete qrRSDecoder_; } void ReedSolomonTest::testNoError() { diff --git a/cpp/core/tests/src/qrcode/VersionTest.cpp b/cpp/core/tests/src/qrcode/VersionTest.cpp index 62a7cd73f..3fcc11ed0 100644 --- a/cpp/core/tests/src/qrcode/VersionTest.cpp +++ b/cpp/core/tests/src/qrcode/VersionTest.cpp @@ -48,7 +48,7 @@ static void checkVersion(Version *version, int number, int dimension) { CPPUNIT_TEST_SUITE_REGISTRATION(VersionTest); void VersionTest::testVersionForNumber() { - for (size_t i = 1; i <= Version::VERSIONS.size(); i++) { + for (int i = 1; i <= (int)Version::VERSIONS.size(); i++) { Version *v = Version::VERSIONS[i-1]; CPPUNIT_ASSERT_EQUAL((int)i, v->getVersionNumber()); }