Started implementation of zxing/qrcode/encoder/EncoderTests

minor refactoring on BitArrayTests.
This commit is contained in:
favoritas37 2016-07-22 03:03:12 +03:00
parent 450afbe54b
commit 48ee696c42
8 changed files with 150 additions and 7 deletions

View File

@ -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();
}
};
}
}

View File

@ -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));
}

View File

@ -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)
{

View File

@ -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)

View File

@ -4,6 +4,7 @@
#include <QZXing.h>
#include <zxing/Exception.h>
#include <QtGlobal>
#include <zxing/qrcode/decoder/Mode.h>
namespace zxing{
@ -26,6 +27,10 @@ private:
return QString::number(item);
}
static QString itemToString(qrcode::Mode& item) {
return QString::fromStdString(item.toString());
}
protected:
template<class T> 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);
}

View File

@ -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();
};
}

View File

@ -0,0 +1,89 @@
#include "EncoderTests.h"
#include <zxing/qrcode/encoder/Encoder.h>
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_);
}
}
}
}

View File

@ -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