Branch
Hash :
f8ceb0d2
Author :
Date :
2020-08-28T18:38:00
All test programs are now TAP-ready.
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 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
#include <stdio.h>
#include <string.h>
#include "common.h"
#include "../mask.h"
#include "../qrspec.h"
#include "decoder.h"
static char dot[2] = {'_', '#'};
static char *maskPatterns[8] = {
/* (i + j) mod 2 = 0 */
"#_#_#_"
"_#_#_#"
"#_#_#_"
"_#_#_#"
"#_#_#_"
"_#_#_#",
/* i mod 2 = 0 */
"######"
"______"
"######"
"______"
"######"
"______",
/* j mod 3 = 0 */
"#__#__"
"#__#__"
"#__#__"
"#__#__"
"#__#__"
"#__#__",
/* (i + j) mod 3 = 0 */
"#__#__"
"__#__#"
"_#__#_"
"#__#__"
"__#__#"
"_#__#_",
/* ((i div 2) + (j div 3)) mod 2 = 0 */
"###___"
"###___"
"___###"
"___###"
"###___"
"###___",
/* (ij) mod 2 + (ij) mod 3 = 0 */
"######"
"#_____"
"#__#__"
"#_#_#_"
"#__#__"
"#_____",
/* ((ij) mod 2 + (ij) mod 3) mod 2 = 0 */
"######"
"###___"
"##_##_"
"#_#_#_"
"#_##_#"
"#___##",
/* ((ij) mod 3 + (i+j) mod 2) mod 2 = 0 */
"#_#_#_"
"___###"
"#___##"
"_#_#_#"
"###___"
"_###__"
};
static void print_mask(int mask)
{
const unsigned int w = 6;
unsigned char frame[w * w], *masked, *p;
int x, y;
memset(frame, 0, w * w);
masked = Mask_makeMaskedFrame(w, frame, mask);
p = masked;
for(y=0; y<w; y++) {
for(x=0; x<w; x++) {
putchar(dot[*p&1]);
p++;
}
printf("\n");
}
printf("\n");
free(masked);
}
static void print_masks(void)
{
int i;
puts("\nPrinting mask patterns.");
for(i=0; i<8; i++) {
print_mask(i);
}
}
static int test_mask(int mask)
{
const int w = 6;
unsigned char frame[w * w], *masked, *p;
char *q;
int x, y;
int err = 0;
memset(frame, 0, w * w);
masked = Mask_makeMaskedFrame(w, frame, mask);
p = masked;
q = maskPatterns[mask];
for(y=0; y<w; y++) {
for(x=0; x<w; x++) {
if(dot[*p&1] != *q) {
err++;
}
p++;
q++;
}
}
free(masked);
return err;
}
static void test_masks(void)
{
int i;
testStart("Mask pattern checks");
for(i=0; i<8; i++) {
assert_zero(test_mask(i), "Mask pattern %d incorrect.\n", i);
}
testFinish();
}
#define N1 (3)
#define N2 (3)
#define N3 (40)
#define N4 (10)
static void test_eval(void)
{
unsigned char *frame;
unsigned int w = 6;
int demerit;
frame = (unsigned char *)malloc(w * w);
testStart("Test mask evaluation (all white)");
memset(frame, 0, w * w);
demerit = Mask_evaluateSymbol(w, frame);
testEndExp(demerit == ((N1 + 1)*w*2 + N2 * (w - 1) * (w - 1)));
testStart("Test mask evaluation (all black)");
memset(frame, 1, w * w);
demerit = Mask_evaluateSymbol(w, frame);
testEndExp(demerit == ((N1 + 1)*w*2 + N2 * (w - 1) * (w - 1)));
free(frame);
}
/* .#.#.#.#.#
* #.#.#.#.#.
* ..##..##..
* ##..##..##
* ...###...#
* ###...###.
* ....####..
* ####....##
* .....#####
* #####.....
*/
static void test_eval2(void)
{
unsigned char *frame;
unsigned int w = 10;
int demerit;
unsigned int x;
frame = (unsigned char *)malloc(w * w);
testStart("Test mask evaluation (run length penalty check)");
for(x=0; x<w; x++) {
frame[ x] = x & 1;
frame[w + x] = (x & 1) ^ 1;
frame[w*2 + x] = (x / 2) & 1;
frame[w*3 + x] = ((x / 2) & 1) ^ 1;
frame[w*4 + x] = (x / 3) & 1;
frame[w*5 + x] = ((x / 3) & 1) ^ 1;
frame[w*6 + x] = (x / 4) & 1;
frame[w*7 + x] = ((x / 4) & 1) ^ 1;
frame[w*8 + x] = (x / 5) & 1;
frame[w*9 + x] = ((x / 5) & 1) ^ 1;
}
demerit = Mask_evaluateSymbol(w, frame);
testEndExp(demerit == N1 * 4 + N2 * 4);
free(frame);
}
static void test_calcN2(void)
{
unsigned char frame[64];
int width;
int demerit;
int x, y;
testStart("Test mask evaluation (2x2 block check)");
width = 4;
for(y = 0; y < width; y++) {
for(x = 0; x < width; x++) {
frame[y * width + x] = ((x & 2) ^ (y & 2)) >> 1;
}
}
demerit = Mask_calcN2(width, frame);
assert_equal(demerit, N2 * 4, "Calculation of N2 demerit is wrong: %d, expected %d", demerit, N2 * 4);
width = 4;
for(y = 0; y < width; y++) {
for(x = 0; x < width; x++) {
frame[y * width + x] = (((x + 1) & 2) ^ (y & 2)) >> 1;
}
}
demerit = Mask_calcN2(width, frame);
assert_equal(demerit, N2 * 2, "Calculation of N2 demerit is wrong: %d, expected %d", demerit, N2 * 2);
width = 6;
for(y = 0; y < width; y++) {
for(x = 0; x < width; x++) {
frame[y * width + x] = (x / 3) ^ (y / 3);
}
}
demerit = Mask_calcN2(width, frame);
assert_equal(demerit, N2 * 16, "Calculation of N2 demerit is wrong: %d, expected %d", demerit, N2 * 16);
testFinish();
}
static void test_eval3(void)
{
unsigned char *frame;
int w = 15;
int demerit;
int x, y;
static unsigned char pattern[7][15] = {
{0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 1, 0, 0, 0, 0}, // N3x1
{1, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 1, 0, 0, 1}, // N3x1
{1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1}, // N3x1
{1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0}, // 0
{1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1}, // N3x2
{1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0}, // N3 + (N1+1)
{1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1} // (N1+1)
};
frame = (unsigned char *)malloc(w * w);
testStart("Test mask evaluation (1:1:3:1:1 check)");
for(y=0; y<5; y++) {
for(x=0; x<w; x++) {
frame[w*y*2 + x] = pattern[y][x];
frame[w*(y*2+1) + x] = pattern[y][x]^1;
}
}
for(x=0; x<w; x++) {
frame[w*10 + x] = x & 1;
}
for(y=5; y<7; y++) {
for(x=0; x<w; x++) {
frame[w*(y*2+1) + x] = pattern[y][x];
frame[w*(y*2+2) + x] = pattern[y][x]^1;
}
}
/*
for(y=0; y<w; y++) {
for(x=0; x<w; x++) {
printf("%s", frame[w*y+x]?"##":"..");
}
printf("\n");
}
*/
demerit = Mask_evaluateSymbol(w, frame);
testEndExp(demerit == N3 * 10 + (N1 + 1) * 4);
free(frame);
}
static void test_format(void)
{
unsigned char *frame, *masked;
int version, mask, width, dmask;
QRecLevel level, dlevel;
QRcode *code;
int ret;
testStart("Checking format info.");
for(version=1; version<=QRSPEC_VERSION_MAX; version++) {
frame = QRspec_newFrame(version);
width = QRspec_getWidth(version);
for(level=QR_ECLEVEL_L; level<=QR_ECLEVEL_H; level++) {
for(mask=0; mask<8; mask++) {
masked = Mask_makeMask(width, frame, mask, level);
code = QRcode_new(version, width, masked);
ret = QRcode_decodeFormat(code, &dlevel, &dmask);
assert_zero(ret, "Something wrong in format info.\n");
assert_equal(dlevel, level, "Decoded level is wrong: %d, expected %d", dlevel, level);
assert_equal(dmask, mask, "Decoded mask is wrong: %d, expected %d", dlevel, level);
QRcode_free(code);
}
}
free(frame);
}
testFinish();
}
static void test_calcRunLength(void)
{
int width = 5;
unsigned char frame[width * width];
int runLength[width + 1];
int i, j;
int length;
static unsigned char pattern[6][5] = {
{0, 1, 0, 1, 0},
{1, 0, 1, 0, 1},
{0, 0, 0, 0, 0},
{1, 1, 1, 1, 1},
{0, 0, 1, 1, 1},
{1, 1, 0, 0, 0}
};
static int expected[6][7] = {
{ 1, 1, 1, 1, 1, 0, 5},
{-1, 1, 1, 1, 1, 1, 6},
{ 5, 0, 0, 0, 0, 0, 1},
{-1, 5, 0, 0, 0, 0, 2},
{ 2, 3, 0, 0, 0, 0, 2},
{-1, 2, 3, 0, 0, 0, 3}
};
testStart("Test runlength calc function");
for(i=0; i<6; i++) {
length = Mask_calcRunLengthH(width, pattern[i], runLength);
assert_equal(expected[i][6], length, "Length incorrect: %d, expected %d.\n", length, expected[i][6]);
assert_zero(memcmp(runLength, expected[i], sizeof(int) * expected[i][6]), "Run length does not match: pattern %d, horizontal access.\n", i);
for(j=0; j<width; j++) {
frame[j * width] = pattern[i][j];
}
length = Mask_calcRunLengthV(width, frame, runLength);
assert_equal(expected[i][6], length, "Length incorrect: %d, expected %d.\n", length, expected[i][6]);
assert_zero(memcmp(runLength, expected[i], sizeof(int) * expected[i][6]), "Run length does not match: pattern %d, vertical access.\n", i);
}
testFinish();
}
static void test_calcN1N3(void)
{
int runLength[26];
int length;
int demerit;
int i;
static unsigned char pattern[][16] = {
{1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 1, 1, N3},
{0, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, N3},
{1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0},
{1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 0, 0, 0, 1, 1, N3},
{1, 1, 1, 0, 1, 0, 0, 0, 1, 0, 1, 1, 1, 0, 1, N3},
{1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, N3 * 2},
};
static unsigned char pattern2[][19] = {
{1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, N3 + N1 + 1},
{0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, N3 + N1 + 1},
{1, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, N1 + 1},
{1, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, N3 + N1 + 1},
{1, 1, 1, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, N3 + N1 + 1}
};
testStart("Test N3 penalty calculation");
for(i=0; i<6; i++) {
length = Mask_calcRunLengthH(15, pattern[i], runLength);
demerit = Mask_calcN1N3(length, runLength);
assert_equal(pattern[i][15], demerit, "N3 penalty is wrong: %d, expected %d\n", demerit, pattern[i][15]);
}
for(i=0; i<5; i++) {
length = Mask_calcRunLengthH(18, pattern2[i], runLength);
demerit = Mask_calcN1N3(length, runLength);
assert_equal(pattern2[i][18], demerit, "N3 penalty is wrong: %d, expected %d\n", demerit, pattern2[i][18]);
}
testFinish();
}
int main(int argc, char **argv)
{
int tests = 9;
testInit(tests);
test_masks();
test_eval();
test_eval2();
test_eval3();
test_format();
test_calcN2();
test_calcRunLength();
test_calcN1N3();
testReport(tests);
if(argc > 1) {
print_masks();
}
return 0;
}