diff --git a/src/zxing/zxing/qrcode/decoder/Mode.h b/src/zxing/zxing/qrcode/decoder/Mode.h index ae8d67a..e6c9dc7 100644 --- a/src/zxing/zxing/qrcode/decoder/Mode.h +++ b/src/zxing/zxing/qrcode/decoder/Mode.h @@ -59,8 +59,13 @@ public: int getBits() const { return bits_; } bool operator==(const Mode& other); + bool operator!=(const Mode& other); std::string getName() const { return name_; } + + std::string toString() const { + return getName(); + } }; } } diff --git a/src/zxing/zxing/qrcode/decoder/QRMode.cpp b/src/zxing/zxing/qrcode/decoder/QRMode.cpp index 994d0d3..29bd5a1 100644 --- a/src/zxing/zxing/qrcode/decoder/QRMode.cpp +++ b/src/zxing/zxing/qrcode/decoder/QRMode.cpp @@ -114,5 +114,10 @@ bool Mode::operator==(const Mode& other) && characterCountBitsForVersions10To26_ == other.characterCountBitsForVersions10To26_ && characterCountBitsForVersions27AndHigher_ == other.characterCountBitsForVersions27AndHigher_ && name_ == other.name_ - && bits_ == other.bits_ ); + && bits_ == other.bits_ ); +} + +bool Mode::operator!=(const zxing::qrcode::Mode &other) +{ + return !(operator==(other)); } diff --git a/tests/src/QZXingTests/EncodeValidator.cpp b/tests/src/QZXingTests/EncodeValidator.cpp index 9b79c76..2d2de51 100644 --- a/tests/src/QZXingTests/EncodeValidator.cpp +++ b/tests/src/QZXingTests/EncodeValidator.cpp @@ -7,6 +7,7 @@ #include "zxing/qrcode/encoder/MaskUtilTests.h" #include "zxing/qrcode/encoder/BitArrayTests.h" #include "zxing/qrcode/encoder/QRCodeTests.h" +#include "zxing/qrcode/encoder/EncoderTests.h" namespace zxing { namespace qrcode { @@ -31,6 +32,9 @@ void EncodeValidator::execute() QRCodeTests t3; t3.execute(); + + EncoderTests t4; + t4.execute(); } catch(zxing::Exception &e) { diff --git a/tests/src/QZXingTests/QZXingTests.pro b/tests/src/QZXingTests/QZXingTests.pro index 58d1274..aeb3c62 100644 --- a/tests/src/QZXingTests/QZXingTests.pro +++ b/tests/src/QZXingTests/QZXingTests.pro @@ -17,7 +17,8 @@ SOURCES += main.cpp \ zxing/qrcode/encoder/MatrixUtilTests.cpp \ zxing/qrcode/encoder/MaskUtilTests.cpp \ zxing/qrcode/encoder/BitArrayTests.cpp \ - zxing/qrcode/encoder/QRCodeTests.cpp + zxing/qrcode/encoder/QRCodeTests.cpp \ + zxing/qrcode/encoder/EncoderTests.cpp HEADERS += \ DecodeValidator.h \ @@ -27,6 +28,7 @@ HEADERS += \ TestCase.h \ zxing/qrcode/encoder/MaskUtilTests.h \ zxing/qrcode/encoder/BitArrayTests.h \ - zxing/qrcode/encoder/QRCodeTests.h + zxing/qrcode/encoder/QRCodeTests.h \ + zxing/qrcode/encoder/EncoderTests.h include(../../../src/QZXing.pri) diff --git a/tests/src/QZXingTests/TestCase.h b/tests/src/QZXingTests/TestCase.h index 2f5d13c..aea5acf 100644 --- a/tests/src/QZXingTests/TestCase.h +++ b/tests/src/QZXingTests/TestCase.h @@ -4,6 +4,7 @@ #include #include #include +#include namespace zxing{ @@ -26,6 +27,10 @@ private: return QString::number(item); } + static QString itemToString(qrcode::Mode& item) { + return QString::fromStdString(item.toString()); + } + protected: template void assertEquals(T expected, T actual) { if(expected != actual) { @@ -36,6 +41,10 @@ protected: } } + void assertSame(qrcode::Mode &expected, qrcode::Mode &actual){ + assertEquals(expected, actual); + } + void assertSame(void *expected, void *actual) { assertEquals((int)expected, (int)actual); } diff --git a/tests/src/QZXingTests/zxing/qrcode/encoder/BitArrayTests.h b/tests/src/QZXingTests/zxing/qrcode/encoder/BitArrayTests.h index 6eb16d5..104bd7c 100644 --- a/tests/src/QZXingTests/zxing/qrcode/encoder/BitArrayTests.h +++ b/tests/src/QZXingTests/zxing/qrcode/encoder/BitArrayTests.h @@ -10,6 +10,11 @@ namespace tests{ class BitArrayTests : public TestCase { +public: + BitArrayTests(); + + void execute(); + private: static long unsigned int getUnsignedInt(BitArray &v, int index); @@ -21,10 +26,6 @@ private: void testXOR2(); void testAt(); void testToString(); -public: - BitArrayTests(); - - void execute(); }; } diff --git a/tests/src/QZXingTests/zxing/qrcode/encoder/EncoderTests.cpp b/tests/src/QZXingTests/zxing/qrcode/encoder/EncoderTests.cpp new file mode 100644 index 0000000..c28bd1e --- /dev/null +++ b/tests/src/QZXingTests/zxing/qrcode/encoder/EncoderTests.cpp @@ -0,0 +1,89 @@ +#include "EncoderTests.h" +#include + +namespace zxing{ +namespace qrcode{ +namespace tests{ + +EncoderTests::EncoderTests() +{ + +} + +void EncoderTests::execute() +{ + testGetAlphanumericCode(); + testChooseMode(); +} + +void EncoderTests::testGetAlphanumericCode() +{ + // The first ten code points are numbers. + for (int i = 0; i < 10; ++i) { + assertEquals(i, Encoder::getAlphanumericCode('0' + i)); + } + + // The next 26 code points are capital alphabet letters. + for (int i = 10; i < 36; ++i) { + assertEquals(i, Encoder::getAlphanumericCode('A' + i - 10)); + } + + // Others are symbol letters + assertEquals(36, Encoder::getAlphanumericCode(' ')); + assertEquals(37, Encoder::getAlphanumericCode('$')); + assertEquals(38, Encoder::getAlphanumericCode('%')); + assertEquals(39, Encoder::getAlphanumericCode('*')); + assertEquals(40, Encoder::getAlphanumericCode('+')); + assertEquals(41, Encoder::getAlphanumericCode('-')); + assertEquals(42, Encoder::getAlphanumericCode('.')); + assertEquals(43, Encoder::getAlphanumericCode('/')); + assertEquals(44, Encoder::getAlphanumericCode(':')); + + // Should return -1 for other letters; + assertEquals(-1, Encoder::getAlphanumericCode('a')); + assertEquals(-1, Encoder::getAlphanumericCode('#')); + assertEquals(-1, Encoder::getAlphanumericCode('\0')); +} + +void EncoderTests::testChooseMode() +{ + // Numeric mode. + Mode mode_(Encoder::chooseMode("0")); + assertSame(Mode::NUMERIC, mode_); + mode_ = Encoder::chooseMode("0123456789"); + assertSame(Mode::NUMERIC, mode_); + // Alphanumeric mode. + mode_ = Encoder::chooseMode("A"); + assertSame(Mode::ALPHANUMERIC, mode_); + mode_ = Encoder::chooseMode("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:"); + assertSame(Mode::ALPHANUMERIC, mode_); + // 8-bit byte mode. + mode_ = Encoder::chooseMode("a"); + assertSame(Mode::BYTE, mode_); + mode_ = Encoder::chooseMode("#"); + assertSame(Mode::BYTE, mode_); + mode_ = Encoder::chooseMode(""); + assertSame(Mode::BYTE, mode_); + // Kanji mode. We used to use MODE_KANJI for these, but we stopped + // doing that as we cannot distinguish Shift_JIS from other encodings + // from data bytes alone. See also comments in qrcode_encoder.h. + + // AIUE in Hiragana in Shift_JIS +// mode_ = Encoder::chooseMode(shiftJISString(new byte[]{0x8, 0xa, 0x8, 0xa, 0x8, 0xa, 0x8, (byte) 0xa6})); +// assertSame(Mode::BYTE,mode_); + +// // Nihon in Kanji in Shift_JIS. +// mode_ = Encoder::chooseMode(shiftJISString(new byte[]{0x9, 0xf, 0x9, 0x7b})); +// assertSame(Mode::BYTE, mode_); + +// // Sou-Utsu-Byou in Kanji in Shift_JIS. +// mode_ = Encoder::chooseMode(shiftJISString(new byte[]{0xe, 0x4, 0x9, 0x5, 0x9, 0x61})); +// assertSame(Mode::BYTE, mode_); +} + + +} +} +} + + diff --git a/tests/src/QZXingTests/zxing/qrcode/encoder/EncoderTests.h b/tests/src/QZXingTests/zxing/qrcode/encoder/EncoderTests.h new file mode 100644 index 0000000..654b648 --- /dev/null +++ b/tests/src/QZXingTests/zxing/qrcode/encoder/EncoderTests.h @@ -0,0 +1,28 @@ +#ifndef ENCODERTESTS_H +#define ENCODERTESTS_H + +#include "TestCase.h" + +namespace zxing{ +namespace qrcode{ +namespace tests{ + +class EncoderTests : public TestCase +{ +public: + EncoderTests(); + + void execute(); + +private: + void testGetAlphanumericCode(); + void testChooseMode(); + + static std::string shiftJISString(byte bytes[]); +}; + +} +} +} + +#endif // ENCODERTESTS_H