Updated the tests to use the correct way of handling the zxing exceptions. Enriched EncoderTests with further tests.

This commit is contained in:
favoritas37 2016-07-28 21:26:43 +03:00
parent a25e39cba6
commit 59ed259a15
4 changed files with 91 additions and 15 deletions

View File

@ -35,9 +35,7 @@ void EncodeValidator::execute()
EncoderTests t4;
t4.execute();
}
catch(zxing::Exception &e)
{
} catch(zxing::Exception &e) {
qDebug() << e.what();
}
}

View File

@ -34,10 +34,10 @@ private:
protected:
template<class T> void assertEquals(T expected, T actual) {
if(expected != actual) {
QString message = QString("Expected: ") + itemToString(expected) +
QString(", Got: ") + itemToString(actual);
Q_ASSERT_X(false, "assertEquals", message.toStdString().c_str());
throw new zxing::Exception(message.toStdString().c_str());
QString message = QString("Expected: \n") + itemToString(expected) +
QString(", Got: \n") + itemToString(actual);
//Q_ASSERT_X(false, "assertEquals", message.toStdString().c_str());
throw zxing::Exception(message.toStdString().c_str());
}
}

View File

@ -14,6 +14,10 @@ void EncoderTests::execute()
{
testGetAlphanumericCode();
testChooseMode();
testAppendModeInfo();
testAppendLengthInfo();
testAppendBytes();
}
void EncoderTests::testGetAlphanumericCode()
@ -69,18 +73,87 @@ void EncoderTests::testChooseMode()
// 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_);
// 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_);
// // 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_);
// // Sou-Utsu-Byou in Kanji in Shift_JIS.
// mode_ = Encoder::chooseMode(shiftJISString(new byte[]{0xe, 0x4, 0x9, 0x5, 0x9, 0x61}));
// assertSame(Mode::BYTE, mode_);
}
void EncoderTests::testAppendModeInfo()
{
BitArray bits;
Encoder::appendModeInfo(Mode::NUMERIC, bits);
assertEquals(std::string(" ...X"), bits.toString());
}
void EncoderTests::testAppendLengthInfo()
{
BitArray bits;
Encoder::appendLengthInfo(1, // 1 letter (1/1).
*Version::getVersionForNumber(1),
Mode::NUMERIC,
bits);
assertEquals(std::string(" ........ .X"), bits.toString()); // 10 bits.
bits = BitArray();
Encoder::appendLengthInfo(2, // 2 letters (2/1).
*Version::getVersionForNumber(10),
Mode::ALPHANUMERIC,
bits);
assertEquals(std::string(" ........ .X."), bits.toString()); // 11 bits.
bits = BitArray();
Encoder::appendLengthInfo(255, // 255 letter (255/1).
*Version::getVersionForNumber(27),
Mode::BYTE,
bits);
assertEquals(std::string(" ........ XXXXXXXX"), bits.toString()); // 16 bits.
// bits = BitArray();
// Encoder::appendLengthInfo(512, // 512 letters (1024/2).
// *Version::getVersionForNumber(40),
// Mode::KANJI,
// bits);
// assertEquals(" ..X..... ....", bits.toString()); // 12 bits.
}
void EncoderTests::testAppendBytes()
{
// Should use appendNumericBytes.
// 1 = 01 = 0001 in 4 bits.
BitArray bits;
Encoder::appendBytes("1", Mode::NUMERIC, bits, Encoder::DEFAULT_BYTE_MODE_ENCODING);
assertEquals(std::string(" ...X") , bits.toString());
// Should use appendAlphanumericBytes.
// A = 10 = 0xa = 001010 in 6 bits
bits = BitArray();
Encoder::appendBytes("A", Mode::ALPHANUMERIC, bits, Encoder::DEFAULT_BYTE_MODE_ENCODING);
assertEquals(std::string(" ..X.X.") , bits.toString());
// Lower letters such as 'a' cannot be encoded in MODE_ALPHANUMERIC.
try {
Encoder::appendBytes("a", Mode::ALPHANUMERIC, bits, Encoder::DEFAULT_BYTE_MODE_ENCODING);
} catch(zxing::Exception &/*e*/) {
// good
}
// Should use append8BitBytes.
// 0x61, 0x62, 0x63
bits = BitArray();
Encoder::appendBytes("abc", Mode::BYTE, bits, Encoder::DEFAULT_BYTE_MODE_ENCODING);
assertEquals(std::string(" .XX....X .XX...X. .XX...XX"), bits.toString());
// Anything can be encoded in QRCode.MODE_8BIT_BYTE.
Encoder::appendBytes("\0", Mode::BYTE, bits, Encoder::DEFAULT_BYTE_MODE_ENCODING);
// Should use appendKanjiBytes.
// 0x93, 0x5f
// bits = new BitArray();
// Encoder.appendBytes(shiftJISString(new byte[] {(byte)0x93,0x5f}), Mode.KANJI, bits, Encoder.DEFAULT_BYTE_MODE_ENCODING);
// assertEquals(" .XX.XX.. XXXXX", bits.toString());
}
}
}

View File

@ -17,6 +17,11 @@ public:
private:
void testGetAlphanumericCode();
void testChooseMode();
//void testEncode();
//void testSimpleUTF8ECI();
void testAppendModeInfo();
void testAppendLengthInfo();
void testAppendBytes();
static std::string shiftJISString(byte bytes[]);
};