Skip to content

Commit

Permalink
Fixed some minor bugs in Micro QR Code generation.
Browse files Browse the repository at this point in the history
  • Loading branch information
fukuchi committed Sep 27, 2020
1 parent 0f6149e commit d6f495a
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 4 deletions.
9 changes: 9 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
2020.09.28 Kentaro Fukuchi <[email protected]>
[hotfix]
* qrinput.c, tests/test_estimatebit.c:
- Fixed a bug in the estimation of the Micro QR Code's data length
in QRinput_estimateBitStreamSizeOfEntry() has been fixed.
- Fixed a bug in the calculation of the Micro QR Code's data capacity in
QRinput_encodeBitStream().
- A test case to test the bugs above has been added.

2020.08.29 Kentaro Fukuchi <[email protected]>
[release-4.1.0]
* .github/workflows/{cmake,configure,cmake-windows}.yml:
Expand Down
8 changes: 6 additions & 2 deletions qrinput.c
Original file line number Diff line number Diff line change
Expand Up @@ -890,7 +890,7 @@ static int QRinput_estimateBitStreamSizeOfEntry(QRinput_List *entry, int version
}

if(mqr) {
l = QRspec_lengthIndicator(entry->mode, version);
l = MQRspec_lengthIndicator(entry->mode, version);
m = version - 1;
bits += l + m;
} else {
Expand Down Expand Up @@ -1018,7 +1018,11 @@ static int QRinput_encodeBitStream(QRinput_List *entry, BitStream *bstream, int

prevsize = (int)BitStream_size(bstream);

words = QRspec_maximumWords(entry->mode, version);
if(mqr) {
words = MQRspec_maximumWords(entry->mode, version);
} else {
words = QRspec_maximumWords(entry->mode, version);
}
if(words != 0 && entry->size > words) {
st1 = QRinput_List_newEntry(entry->mode, words, entry->data);
if(st1 == NULL) goto ABORT;
Expand Down
26 changes: 25 additions & 1 deletion tests/test_estimatebit.c
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,34 @@ static void test_mix(void)
QRinput_free(gstream);
}

/* Taken from JISX 0510:2018, p.23 */
static void test_numbit1_mqr(void)
{
QRinput *stream;
char *str = "0123456789012345";
int bits;

testStart("Estimation of Numeric stream for Micro QR Code (16 digits)");
stream = QRinput_newMQR(3, QR_ECLEVEL_M);
QRinput_append(stream, QR_MODE_NUM, 16, (const unsigned char *)str);
bits = QRinput_estimateBitStreamSize(stream, QRinput_getVersion(stream));
assert_equal(bits, 61, "Estimated bit length is wrong: %d, expected: %d.\n", bits, 61);
QRinput_free(stream);

stream = QRinput_newMQR(4, QR_ECLEVEL_M);
QRinput_append(stream, QR_MODE_NUM, 16, (const unsigned char *)str);
bits = QRinput_estimateBitStreamSize(stream, QRinput_getVersion(stream));
assert_equal(bits, 63, "Estimated bit length is wrong: %d, expected: %d.\n", bits, 63);
QRinput_free(stream);

testFinish();
}

int main()
{
gstream = QRinput_new();

int tests = 8;
int tests = 9;
testInit(tests);
test_numbit();
test_numbit2();
Expand All @@ -153,6 +176,7 @@ int main()
test_kanji();
test_structure();
test_mix();
test_numbit1_mqr();
testReport(tests);

return 0;
Expand Down
36 changes: 35 additions & 1 deletion tests/test_qrinput.c
Original file line number Diff line number Diff line change
Expand Up @@ -889,6 +889,38 @@ static void test_estimateVersionBoundaryCheck(void)
testFinish();
}

static void test_QRinput_new_invalid(void)
{
testStart("Invalid input to QRinput_new2()");
QRinput *input;

input = QRinput_new2(-1, QR_ECLEVEL_H);
assert_null(input, "QRinput_new2() returns non-null for invalid version (-1).\n");
assert_equal(errno, EINVAL, "Error code is not EINVAL.\n");
input = QRinput_new2(41, QR_ECLEVEL_H);
assert_null(input, "QRinput_new2() returns non-null for invalid version (41).\n");
assert_equal(errno, EINVAL, "Error code is not EINVAL.\n");
input = QRinput_new2(1, -1);
assert_null(input, "QRinput_new2() returns non-null for invalid level (-1).\n");
assert_equal(errno, EINVAL, "Error code is not EINVAL.\n");
input = QRinput_new2(1, 5);
assert_null(input, "QRinput_new2() returns non-null for invalid level (5).\n");
assert_equal(errno, EINVAL, "Error code is not EINVAL.\n");
testFinish();
}

static void test_QRinput_getErrorCorrectionLevel(void)
{
testStart("Invalid input to QRinput_getErrorCorrectionLevel()");
QRinput *input;
QRecLevel level;

input = QRinput_new2(1, QR_ECLEVEL_H);
level = QRinput_getErrorCorrectionLevel(input);
assert_equal(level, QR_ECLEVEL_H, "QRinput_getErrorCorrectionLevel() fails to return expected level.\n");
testFinish();
}

static void test_mqr_new(void)
{
QRinput *input;
Expand Down Expand Up @@ -1044,7 +1076,7 @@ static void test_encodeECI(void)

int main()
{
int tests = 40;
int tests = 42;
testInit(tests);

test_encodeNumeric();
Expand Down Expand Up @@ -1078,6 +1110,8 @@ int main()
test_parity2();
test_null_free();
test_estimateVersionBoundaryCheck();
test_QRinput_new_invalid();
test_QRinput_getErrorCorrectionLevel();

test_mqr_new();
test_mqr_setversion();
Expand Down

0 comments on commit d6f495a

Please sign in to comment.