www.pudn.com > qrencode-2.0.0.rar > test_qrencode.c
#include#include #include "common.h" #include "../qrencode_inner.h" #include "../qrspec.h" #include "../qrinput.h" #include "../mask.h" int inputSize(QRinput *input) { BitStream *bstream; int size; bstream = QRinput_mergeBitStream(input); size = BitStream_size(bstream); BitStream_free(bstream); return size; } void test_iterate() { int i; QRinput *stream; char num[9] = "01234567"; unsigned char *data; QRRawCode *raw; int err = 0; testStart("Test getCode (1-L)"); stream = QRinput_new(); QRinput_setVersion(stream, 1); QRinput_setErrorCorrectionLevel(stream, QR_ECLEVEL_L); QRinput_append(stream, QR_MODE_NUM, 8, (unsigned char *)num); raw = QRraw_new(stream); data = raw->datacode; for(i=0; i dataLength; i++) { if(data[i] != QRraw_getCode(raw)) { err++; } } QRinput_free(stream); QRraw_free(raw); testEnd(err); } void test_iterate2() { int i; QRinput *stream; char num[9] = "01234567"; QRRawCode *raw; int err = 0; unsigned char correct[] = { 0x10, 0x11, 0xec, 0xec, 0x20, 0xec, 0x11, 0x11, 0x0c, 0x11, 0xec, 0xec, 0x56, 0xec, 0x11, 0x11, 0x61, 0x11, 0xec, 0xec, 0x80, 0xec, 0x11, 0x11, 0xec, 0x11, 0xec, 0xec, 0x11, 0xec, 0x11, 0x11, 0xec, 0x11, 0xec, 0xec, 0x11, 0xec, 0x11, 0x11, 0xec, 0x11, 0xec, 0xec, 0x11, 0x11, 0x5c, 0xde, 0x68, 0x68, 0x4d, 0xb3, 0xdb, 0xdb, 0xd5, 0x14, 0xe1, 0xe1, 0x5b, 0x2a, 0x1f, 0x1f, 0x49, 0xc4, 0x78, 0x78, 0xf7, 0xe0, 0x5b, 0x5b, 0xc3, 0xa7, 0xc1, 0xc1, 0x5d, 0x9a, 0xea, 0xea, 0x48, 0xad, 0x9d, 0x9d, 0x58, 0xb3, 0x3f, 0x3f, 0x10, 0xdb, 0xbf, 0xbf, 0xeb, 0xec, 0x05, 0x05, 0x98, 0x35, 0x83, 0x83, 0xa9, 0x95, 0xa6, 0xa6, 0xea, 0x7b, 0x8d, 0x8d, 0x04, 0x3c, 0x08, 0x08, 0x64, 0xce, 0x3e, 0x3e, 0x4d, 0x9b, 0x30, 0x30, 0x4e, 0x65, 0xd6, 0xd6, 0xe4, 0x53, 0x2c, 0x2c, 0x46, 0x1d, 0x2e, 0x2e, 0x29, 0x16, 0x27, 0x27 }; testStart("Test getCode (5-H)"); stream = QRinput_new(); QRinput_setVersion(stream, 5); QRinput_setErrorCorrectionLevel(stream, QR_ECLEVEL_H); QRinput_append(stream, QR_MODE_NUM, 8, (unsigned char *)num); raw = QRraw_new(stream); for(i=0; i dataLength; i++) { if(correct[i] != QRraw_getCode(raw)) { err++; } } QRinput_free(stream); QRraw_free(raw); testEnd(err); } void print_filler(void) { int width; int x, y; int version = 5; unsigned char *frame; width = QRspec_getWidth(version); frame = FrameFiller_fillerTest(version); for(y=0; y 5)] & 1; if(decode & 1) b1++; } for(i=0; i<7; i++) { decode = decode << 1; decode |= frame[width * ((6 - i) + (i < 1)) + 8] & 1; if(decode & 1) b1++; } if(decode != format) { printf("Upper-left format information is invalid.\n"); printf("%08x, %08x\n", format, decode); testEnd(1); return; } decode = 0; for(i=0; i<7; i++) { decode = decode << 1; decode |= frame[width * (width - 1 - i) + 8] & 1; if(decode & 1) b1++; } for(i=0; i<8; i++) { decode = decode << 1; decode |= frame[width * 8 + width - 8 + i] & 1; if(decode & 1) b1++; } if(decode != format) { printf("Bottom and right format information is invalid.\n"); printf("%08x, %08x\n", format, decode); testEnd(1); return; } if(b2 != blacks || b1 != b2) { printf("Number of dark modules is incorrect.\n"); printf("Return value: %d, dark modules in frame: %d, should be: %d\n", blacks, b1, b2); testEnd(1); return; } free(frame); testEnd(0); } #define N1 (3) #define N2 (3) #define N3 (40) #define N4 (10) void test_eval(void) { unsigned char *frame; 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); } /* .#.#.#.#.# * #.#.#.#.#. * ..##..##.. * ##..##..## * ...###...# * ###...###. * ....####.. * ####....## * .....##### * #####..... */ void test_eval2(void) { unsigned char *frame; int w = 10; int demerit; int x; frame = (unsigned char *)malloc(w * w); testStart("Test mask evaluation (run length penalty check)"); for(x=0; x width; frame = qrcode->data; for(y=0; y > (20-x)) & 1) != (frame[y*w+x]&1)) { printf("Diff in mask=%d (%d,%d)\n", mask, x, y); err++; } } } QRcode_free(qrcode); } QRinput_free(stream); testEnd(err); } void test_encode2(void) { QRcode *qrcode; testStart("Test encode (2-H) (no padding test)"); qrcode = QRcode_encodeString("abcdefghijk123456789012", 0, QR_ECLEVEL_H, QR_MODE_8, 0); testEndExp(qrcode->version == 2); QRcode_free(qrcode); } void test_encode3(void) { QRcode *code1, *code2; QRinput *input; testStart("Compare encodeString and encodeInput"); code1 = QRcode_encodeString("0123456", 0, QR_ECLEVEL_L, QR_MODE_8, 0); input = QRinput_new(); QRinput_append(input, QR_MODE_NUM, 7, (unsigned char *)"0123456"); code2 = QRcode_encodeInput(input, 0, QR_ECLEVEL_L); testEnd(memcmp(code1->data, code2->data, code1->width * code1->width)); QRcode_free(code1); QRcode_free(code2); QRinput_free(input); } void test_encodeTooLong(void) { QRcode *code; char *data; testStart("Encode too large data"); data = (char *)malloc(4300); memset(data, 'a', 4295); memset(data + 4295, '0', 4); data[4299] = '\0'; code = QRcode_encodeString(data, 0, QR_ECLEVEL_L, QR_MODE_8, 0); testEndExp(code == NULL); if(code != NULL) { printf("%d, %d\n", code->version, code->width); QRcode_free(code); } free(data); } void test_01234567(void) { QRinput *stream; char num[9] = "01234567"; int i, err = 0; QRcode *qrcode; unsigned char correct[] = { 0xc1, 0xc1, 0xc1, 0xc1, 0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x85, 0x85, 0x85, 0x84, 0x84, 0x02, 0x02, 0x02, 0x03, 0x02, 0x03, 0x90, 0x03, 0x03, 0x02, 0x03, 0x02, 0x03, 0x02, 0x02, 0x03, 0x02, 0x03, 0x03, 0x02, 0x02, 0x02, 0x02, 0x03, 0x02, 0x02, 0x02, 0x91, 0x03, 0x02, 0x03, 0x02, 0x03, 0x02, 0x03, 0x02, 0x02, 0x03, 0x03, 0x03, 0x03, 0x03, 0x02, 0x02, 0x02, 0x02, 0x03, 0x02, 0x90, 0x02, 0x02, 0x03, 0x02, 0x02, 0x02, 0x02, 0x02, 0x03, 0x03, 0x03, 0x03, 0x02, 0x02, 0x02, 0x02, 0x02, 0x03, 0x03, 0x03, 0x91, 0x03, 0x03, 0x02, 0x02, 0x03, 0x02, 0x03, 0x02, 0x02, 0x03, 0x02, 0x02, 0x02, 0x02, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0x81, 0x02, 0x03, 0x03, 0x03, 0x03, 0x03, 0x02, 0x02, 0x03, 0x03, 0x02, 0x02, 0xc1, 0xc1, 0xc1, 0xc1, 0xc1, 0xc1, 0xc1, 0xc0, 0x84, 0x03, 0x03, 0x02, 0x03, 0x02, 0x03, 0x03, 0x02, 0x02, 0x02, 0x02, 0x02, 0xc1, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc1, 0xc0, 0x85, 0x02, 0x03, 0x03, 0x03, 0x03, 0x03, 0x02, 0x02, 0x02, 0x03, 0x02, 0x03, 0xc1, 0xc0, 0xc1, 0xc1, 0xc1, 0xc0, 0xc1, 0xc0, 0x85, 0x02, 0x02, 0x02, 0x03, 0x02, 0x02, 0x03, 0x02, 0x03, 0x03, 0x02, 0x02, 0xc1, 0xc0, 0xc1, 0xc1, 0xc1, 0xc0, 0xc1, 0xc0, 0x85, 0x03, 0x02, 0x02, 0x03, 0x02, 0x02, 0x03, 0x02, 0x02, 0x02, 0x02, 0x02, 0xc1, 0xc0, 0xc1, 0xc1, 0xc1, 0xc0, 0xc1, 0xc0, 0x85, 0x02, 0x03, 0x03, 0x02, 0x03, 0x02, 0x02, 0x03, 0x02, 0x03, 0x02, 0x02, 0xc1, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc1, 0xc0, 0x84, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x03, 0x03, 0x02, 0x03, 0x03, 0x02, 0xc1, 0xc1, 0xc1, 0xc1, 0xc1, 0xc1, 0xc1, 0xc0, 0x85, 0x03, 0x03, 0x03, 0x02, 0x03, 0x02, 0x02, 0x03, 0x02, 0x03, 0x02, 0x02}; testStart("Encode 01234567 in 1-M"); stream = QRinput_new(); QRinput_append(stream, QR_MODE_NUM, 8, (unsigned char *)num); qrcode = QRcode_encodeInput(stream, 1, QR_ECLEVEL_M); for(i=0; i width * qrcode->width; i++) { if(qrcode->data[i] != correct[i]) { err++; } } testEnd(err); QRinput_free(stream); QRcode_free(qrcode); } void print_01234567(void) { QRinput *stream; char num[9] = "01234567"; unsigned char *frame; int x, y, w; QRcode *qrcode; stream = QRinput_new(); QRinput_append(stream, QR_MODE_NUM, 8, (unsigned char *)num); qrcode = QRcode_encodeInput(stream, 1, QR_ECLEVEL_M); w = qrcode->width; frame = qrcode->data; for(y=0; y