📄 test_qrencode.c
字号:
}; 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; } } demerit = Mask_evaluateSymbol(w, frame); testEndExp(demerit == N3 * 6 + (N1 + 1) * 4); free(frame);}unsigned int m1pat[8][21] = { {0x1fc77f, 0x105c41, 0x174c5d, 0x174b5d, 0x175b5d, 0x104241, 0x1fd57f, 0x000000, 0x154512, 0x1a16a2, 0x0376ee, 0x19abb2, 0x04eee1, 0x001442, 0x1fc111, 0x10444b, 0x175d5d, 0x174aae, 0x175ae5, 0x1043b8, 0x1fd2e5}, {0x1fdd7f, 0x104641, 0x17565d, 0x17415d, 0x17415d, 0x105841, 0x1fd57f, 0x000a00, 0x146f25, 0x10bc08, 0x09dc44, 0x130118, 0x0e444b, 0x001ee8, 0x1fdbbb, 0x104ee1, 0x1747f7, 0x174004, 0x17504f, 0x104912, 0x1fd84f}, {0x1fcb7f, 0x104f41, 0x17505d, 0x17585d, 0x17575d, 0x105141, 0x1fd57f, 0x001300, 0x17c97c, 0x02b52c, 0x046a9f, 0x01083c, 0x03f290, 0x0017cc, 0x1fcd60, 0x1057c5, 0x17512c, 0x175920, 0x175694, 0x104036, 0x1fde94}, {0x1fdb7f, 0x105441, 0x174d5d, 0x17585d, 0x174c5d, 0x104c41, 0x1fd57f, 0x001800, 0x16e44b, 0x02b52c, 0x12f1f2, 0x1a258a, 0x03f290, 0x001ca1, 0x1fd0d6, 0x1057c5, 0x174a41, 0x175496, 0x175694, 0x104b5b, 0x1fd322}, {0x1fd37f, 0x104741, 0x17475d, 0x175f5d, 0x175f5d, 0x105941, 0x1fd57f, 0x001400, 0x1171f9, 0x0c8dcf, 0x15ed83, 0x108f20, 0x0dca73, 0x001f2f, 0x1fda7c, 0x1040d9, 0x1759cf, 0x1741c3, 0x174188, 0x10472a, 0x1fd677}, {0x1fcd7f, 0x105741, 0x17505d, 0x17545d, 0x17475d, 0x104941, 0x1fd57f, 0x001b00, 0x1059ce, 0x05a95d, 0x046a9f, 0x03001c, 0x0e444b, 0x001fec, 0x1fcd60, 0x104bb4, 0x17412c, 0x174100, 0x17404f, 0x104816, 0x1fde94}, {0x1fdd7f, 0x105741, 0x17545d, 0x17445d, 0x17555d, 0x104f41, 0x1fd57f, 0x000b00, 0x13fd97, 0x05a95d, 0x00f8d6, 0x028604, 0x0e444b, 0x001f2f, 0x1fd9f2, 0x105bb4, 0x175365, 0x175718, 0x17404f, 0x1048d5, 0x1fda06}, {0x1fc77f, 0x104841, 0x174e5d, 0x174b5d, 0x174f5d, 0x105041, 0x1fd57f, 0x000400, 0x12d7a0, 0x1a16a2, 0x0a527c, 0x1d39fb, 0x04eee1, 0x0010d0, 0x1fc358, 0x10544b, 0x1749cf, 0x1758e7, 0x174ae5, 0x10472a, 0x1fd0ac}};void test_encode(void){ QRinput *stream; char num[9] = "01234567"; unsigned char *frame; int err = 0; int x, y, w; int mask; QRcode *qrcode; testStart("Test encode (1-M)"); stream = QRinput_new(); QRinput_append(stream, QR_MODE_NUM, 8, (unsigned char *)num); for(mask=0; mask<8; mask++) { QRinput_setVersion(stream, 1); QRinput_setErrorCorrectionLevel(stream, QR_ECLEVEL_M); qrcode = QRcode_encodeMask(stream, mask); w = qrcode->width; frame = qrcode->data; for(y=0; y<w; y++) { for(x=0; x<w; x++) { if(((m1pat[mask][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_new2(0, QR_ECLEVEL_L); QRinput_append(input, QR_MODE_NUM, 7, (unsigned char *)"0123456"); code2 = QRcode_encodeInput(input); 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, 0xc1, 0xc1, 0xc1, 0xc0, 0x84, 0x03, 0x02, 0x03, 0x03, 0xc0, 0xc1, 0xc1, 0xc1, 0xc1, 0xc1, 0xc1, 0xc1,0xc1, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc1, 0xc0, 0x84, 0x03, 0x03, 0x03, 0x03, 0xc0, 0xc1, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc1,0xc1, 0xc0, 0xc1, 0xc1, 0xc1, 0xc0, 0xc1, 0xc0, 0x85, 0x02, 0x02, 0x02, 0x02, 0xc0, 0xc1, 0xc0, 0xc1, 0xc1, 0xc1, 0xc0, 0xc1,0xc1, 0xc0, 0xc1, 0xc1, 0xc1, 0xc0, 0xc1, 0xc0, 0x85, 0x03, 0x02, 0x02, 0x02, 0xc0, 0xc1, 0xc0, 0xc1, 0xc1, 0xc1, 0xc0, 0xc1,0xc1, 0xc0, 0xc1, 0xc1, 0xc1, 0xc0, 0xc1, 0xc0, 0x85, 0x02, 0x03, 0x03, 0x03, 0xc0, 0xc1, 0xc0, 0xc1, 0xc1, 0xc1, 0xc0, 0xc1,0xc1, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc1, 0xc0, 0x85, 0x02, 0x02, 0x02, 0x03, 0xc0, 0xc1, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc1,0xc1, 0xc1, 0xc1, 0xc1, 0xc1, 0xc1, 0xc1, 0xc0, 0x91, 0x90, 0x91, 0x90, 0x91, 0xc0, 0xc1, 0xc1, 0xc1, 0xc1, 0xc1, 0xc1, 0xc1,0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0x85, 0x02, 0x02, 0x03, 0x03, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0,0x85, 0x84, 0x85, 0x85, 0x85, 0x85, 0x91, 0x84, 0x84, 0x03, 0x02, 0x02, 0x03, 0x84, 0x85, 0x85, 0x85, 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_new2(1, QR_ECLEVEL_M); QRinput_append(stream, QR_MODE_NUM, 8, (unsigned char *)num); qrcode = QRcode_encodeInput(stream); for(i=0; i<qrcode->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_new2(1, QR_ECLEVEL_M); QRinput_append(stream, QR_MODE_NUM, 8, (unsigned char *)num); qrcode = QRcode_encodeInput(stream); w = qrcode->width; frame = qrcode->data; for(y=0; y<w; y++) { for(x=0; x<w; x++) { printf("%02x ", frame[y*w+x]); } printf("\n"); } QRinput_free(stream); QRcode_free(qrcode);}void test_invalid_input(void){ QRinput *input; QRcode *code; testStart("Testing invalid input."); input = QRinput_new(); QRinput_append(input, QR_MODE_AN, 5, (unsigned char *)"TEST1"); input->version = -1; input->level = QR_ECLEVEL_L; code = QRcode_encodeInput(input); assert_null(code, "invalid version(-1) was not checked.\n"); if(code != NULL) QRcode_free(code); input->version = 41; input->level = QR_ECLEVEL_L; code = QRcode_encodeInput(input); assert_null(code, "invalid version(41) access was not checked.\n"); if(code != NULL) QRcode_free(code); input->version = 1; input->level = (QRecLevel)(QR_ECLEVEL_H + 1); code = QRcode_encodeInput(input); assert_null(code, "invalid level(H+1) access was not checked.\n"); if(code != NULL) QRcode_free(code); input->version = 1; input->level = (QRecLevel)-1; code = QRcode_encodeInput(input); assert_null(code, "invalid level(-1) access was not checked.\n"); if(code != NULL) QRcode_free(code); QRinput_free(input); testFinish();}void test_struct_semilong(void){ QRcode_List *codes, *list; const char *str = "asdfasdfasdfasdfasdfASDFASDASDFASDFAsdfasdfasdfasdASDFASDFADSADadsfasdf"; int num, size; testStart("Testing semi-long structured-append symbols"); codes = QRcode_encodeString8bitStructured(str, 1, QR_ECLEVEL_L); list = codes; num = 0; while(list != NULL) { num++; assert_equal(list->code->version, 1, "version number is %d (1 expected)\n", list->code->version); list = list->next; } size = QRcode_List_size(codes); assert_equal(num, size, "QRcode_List_size returns wrong size?"); QRcode_List_free(codes); codes = QRcode_encodeStringStructured(str, 1, QR_ECLEVEL_L, QR_MODE_8, 1); list = codes; num = 0; while(list != NULL) { num++; assert_equal(list->code->version, 1, "version number is %d (1 expected)\n", list->code->version); list = list->next; } size = QRcode_List_size(codes); assert_equal(num, size, "QRcode_List_size returns wrong size?"); QRcode_List_free(codes); testFinish();}void test_struct_example(void){ QRcode_List *codes, *list; const char *str = "an example of four Structured Append symbols,"; int num; testStart("Testing the example of structured-append symbols"); codes = QRcode_encodeString8bitStructured(str, 1, QR_ECLEVEL_M); list = codes; num = 0; while(list != NULL) { num++; assert_equal(list->code->version, 1, "version number is %d (1 expected)\n", list->code->version); list = list->next; } assert_equal(num, 4, "number of symbols is %d (4 expected).", num); testFinish(); QRcode_List_free(codes);}int main(int argc, char **argv){ test_iterate(); test_iterate2();// print_filler();// test_filler();// print_mask(); test_format(); test_eval(); test_eval2(); test_eval3(); test_encode(); test_encode2(); test_encode3(); test_encodeTooLong(); test_01234567(); test_invalid_input();// print_01234567(); test_struct_example(); test_struct_semilong(); QRspec_clearCache(); report(); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -