Branch
Hash :
d6f495ad
Author :
Date :
2020-09-28T02:37:46
Fixed some minor bugs in Micro QR Code generation.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "common.h"
#include "../qrinput.h"
static QRinput *gstream;
static void test_numbit(void)
{
QRinput *stream;
char num[9]="01234567";
int bits;
testStart("Estimation of Numeric stream (8 digits)");
stream = QRinput_new();
QRinput_append(stream, QR_MODE_NUM, 8, (unsigned char *)num);
bits = QRinput_estimateBitStreamSize(stream, 0);
testEndExp(bits == 41);
QRinput_append(gstream, QR_MODE_NUM, 8, (unsigned char *)num);
QRinput_free(stream);
}
static void test_numbit2(void)
{
QRinput *stream;
char num[17]="0123456789012345";
int bits;
testStart("Estimation of Numeric stream (16 digits)");
stream = QRinput_new();
QRinput_append(stream, QR_MODE_NUM, 16, (unsigned char *)num);
bits = QRinput_estimateBitStreamSize(stream, 0);
testEndExp(bits == 68);
QRinput_append(gstream, QR_MODE_NUM, 16, (unsigned char *)num);
QRinput_free(stream);
}
static void test_numbit3(void)
{
QRinput *stream;
char *num;
int bits;
testStart("Estimation of Numeric stream (400 digits)");
stream = QRinput_new();
num = (char *)malloc(401);
memset(num, '1', 400);
num[400] = '\0';
QRinput_append(stream, QR_MODE_NUM, 400, (unsigned char *)num);
bits = QRinput_estimateBitStreamSize(stream, 0);
/* 4 + 10 + 133*10 + 4 = 1348 */
testEndExp(bits == 1348);
QRinput_append(gstream, QR_MODE_NUM, 400, (unsigned char *)num);
QRinput_free(stream);
free(num);
}
static void test_an(void)
{
QRinput *stream;
char str[6]="AC-42";
int bits;
testStart("Estimation of Alphabet-Numeric stream (5 chars)");
stream = QRinput_new();
QRinput_append(stream, QR_MODE_AN, 5, (unsigned char *)str);
bits = QRinput_estimateBitStreamSize(stream, 0);
testEndExp(bits == 41);
QRinput_append(gstream, QR_MODE_AN, 5, (unsigned char *)str);
QRinput_free(stream);
}
static void test_8(void)
{
QRinput *stream;
char str[9]="12345678";
int bits;
testStart("Estimation of 8 bit data stream (8 bytes)");
stream = QRinput_new();
QRinput_append(stream, QR_MODE_8, 8, (unsigned char *)str);
bits = QRinput_estimateBitStreamSize(stream, 0);
testEndExp(bits == 76);
QRinput_append(gstream, QR_MODE_8, 8, (unsigned char *)str);
QRinput_free(stream);
}
static void test_structure(void)
{
QRinput *stream;
int bits;
testStart("Estimation of a structure-append header");
stream = QRinput_new();
QRinput_insertStructuredAppendHeader(stream, 10, 1, 0);
bits = QRinput_estimateBitStreamSize(stream, 1);
testEndExp(bits == 20);
QRinput_insertStructuredAppendHeader(gstream, 10, 1, 0);
QRinput_free(stream);
}
static void test_kanji(void)
{
int res;
QRinput *stream;
unsigned char str[4]= {0x93, 0x5f,0xe4, 0xaa};
int bits;
testStart("Estimation of Kanji stream (2 chars)");
stream = QRinput_new();
res = QRinput_append(stream, QR_MODE_KANJI, 4, (unsigned char *)str);
if(res < 0) {
printf("Failed to add.\n");
testEnd(1);
} else {
bits = QRinput_estimateBitStreamSize(stream, 0);
testEndExp(bits == 38);
QRinput_append(gstream, QR_MODE_KANJI, 4, (unsigned char *)str);
}
QRinput_free(stream);
}
static void test_mix(void)
{
int bits;
testStart("Estimation of Mixed stream");
bits = QRinput_estimateBitStreamSize(gstream, 0);
testEndExp(bits == (41 + 68 + 1348 + 41 + 76 + 38 + 20));
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 = 9;
testInit(tests);
test_numbit();
test_numbit2();
test_numbit3();
test_an();
test_8();
test_kanji();
test_structure();
test_mix();
test_numbit1_mqr();
testReport(tests);
return 0;
}