comparison mupdf-source/thirdparty/zint/backend/tests/test_reedsol.c @ 2:b50eed0cc0ef upstream

ADD: MuPDF v1.26.7: the MuPDF source as downloaded by a default build of PyMuPDF 1.26.4. The directory name has changed: no version number in the expanded directory now.
author Franz Glasner <fzglas.hg@dom66.de>
date Mon, 15 Sep 2025 11:43:07 +0200
parents
children
comparison
equal deleted inserted replaced
1:1d09e1dec1d9 2:b50eed0cc0ef
1 /*
2 libzint - the open source barcode library
3 Copyright (C) 2020-2022 Robin Stuart <rstuart114@gmail.com>
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
7 are met:
8
9 1. Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
11 2. Redistributions in binary form must reproduce the above copyright
12 notice, this list of conditions and the following disclaimer in the
13 documentation and/or other materials provided with the distribution.
14 3. Neither the name of the project nor the names of its contributors
15 may be used to endorse or promote products derived from this software
16 without specific prior written permission.
17
18 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
22 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 SUCH DAMAGE.
29 */
30 /* SPDX-License-Identifier: BSD-3-Clause */
31
32 #include "testcommon.h"
33 #include "../reedsol.h"
34
35 /* Print out the log/alog tables for "backend/reedsol_logs.h" */
36 static void print_logs(const char *name, int logmod, unsigned int *logt, unsigned int *alog, int u16, int last) {
37 int i;
38 const char *type = u16 ? "short" : "char";
39 const char *format = u16 ? " 0x%04X," : " 0x%02X,";
40
41 printf("static const unsigned %s logt_%s[%d] = {", type, name, logmod + 1);
42 for (i = 0; i < logmod + 1; i++) {
43 if (i % 16 == 0) printf("\n ");
44 printf(format, i ? logt[i] : 0);
45 }
46 printf("\n};\n");
47
48 printf("static const unsigned %s alog_%s[%d] = {", type, name, logmod * 2);
49 for (i = 0; i < logmod; i++) {
50 if (i % 16 == 0) printf("\n ");
51 printf(format, alog[i]);
52 }
53 /* Double antilog table */
54 for (i = 0; i < logmod; i++) {
55 if (i % 16 == 0) printf("\n ");
56 printf(format, alog[i]);
57 }
58 printf("\n};\n");
59 if (!last) {
60 printf("\n");
61 }
62 }
63
64 static void gen_logs(const unsigned int prime_poly, int logmod, unsigned int *logt, unsigned int *alog) {
65 int b, p, v;
66
67 b = logmod + 1;
68
69 /* Calculate the log/alog tables */
70 for (p = 1, v = 0; v < logmod; v++) {
71 alog[v] = p;
72 logt[p] = v;
73 p <<= 1;
74 if (p & b)
75 p ^= prime_poly;
76 }
77 }
78
79 /* Dummy to generate static log/antilog tables for "backend/reedsol_logs.h" */
80 static void test_generate(const testCtx *const p_ctx) {
81
82 struct item {
83 const char *name;
84 int logmod;
85 unsigned int prime_poly;
86 int u16;
87 };
88 struct item data[] = {
89 { "0x13", 15, 0x13, 0 },
90 { "0x25", 31, 0x25, 0 },
91 { "0x43", 63, 0x43, 0 },
92 { "0x89", 127, 0x89, 0 },
93 { "0x11d", 255, 0x11d, 0 },
94 { "0x12d", 255, 0x12d, 0 },
95 { "0x163", 255, 0x163, 0 },
96 };
97 int data_size = ARRAY_SIZE(data);
98 int i;
99
100 unsigned int logt[4096];
101 unsigned int alog[8192];
102
103 if (!p_ctx->generate) {
104 return;
105 }
106
107 for (i = 0; i < data_size; i++) {
108 gen_logs(data[i].prime_poly, data[i].logmod, logt, alog);
109 print_logs(data[i].name, data[i].logmod, logt, alog, data[i].u16, i + 1 == data_size);
110 }
111 }
112
113 static void test_encoding(const testCtx *const p_ctx) {
114 int debug = p_ctx->debug;
115
116 struct item {
117 unsigned int prime_poly;
118 int nsym;
119 int index;
120 int datalen;
121 unsigned char data[256];
122
123 unsigned char expected[256];
124 };
125 /* s/\/\*[ 0-9]*\*\//\=printf("\/\*%3d*\/", line(".") - line("'<")): */
126 struct item data[] = {
127 /* 0*/ { 0x43, 4, 1, 7, { 4, 20, 49, 37, 49, 38, 23 }, { 58, 53, 17, 54 } }, /* AUSPOST Australia Post Customer Barcoding Technical Specifications Diagram 10 */
128 /* 1*/ { 0x43, 7, 1, 10, { 9, 50, 1, 41, 47, 2, 39, 37, 1, 27 }, { 40, 20, 10, 16, 8, 50, 38 } }, /* AZTEC ISO/IEC 24778:2008 Section G.4 */
129 /* 2*/ { 0x13, 5, 1, 2, { 0, 9 }, { 9, 1, 3, 2, 12 } }, /* AZTEC ISO/IEC 24778:2008 Section G.4 Mode Message */
130 /* 3*/ { 0x12d, 5, 1, 3, { 142, 164, 186 }, { 102, 88, 5, 25, 114 } }, /* DATAMATRIX ISO/IEC 16022:2006 Annex O */
131 /* 4*/ { 0x89, 25, 1, 25, { 42, 13, 54, 39, 124, 91, 121, 65, 28, 40, 95, 48, 0, 126, 0, 126, 0, 126, 0, 126, 0, 126, 0, 126, 0 }, { 102, 70, 92, 79, 2, 52, 98, 62, 48, 33, 14, 4, 101, 17, 55, 89, 100, 23, 35, 112, 54, 20, 2, 47, 123 } }, /* GRIDMATRIX AIMD014 Section 6.8 */
132 /* 5*/ { 0x163, 4, 1, 21, { 0x11, 0xED, 0xC8, 0xC5, 0x40, 0x0F, 0xF4 }, { 0x1D, 0x68, 0xB4, 0xEB } }, /* HANXIN ISO/IEC DIS 20830:2019 Annex K.1 */
133 /* 6*/ { 0x163, 24, 1, 27, { 0x11, 0xED, 0xC8, 0xC5, 0x40, 0x0F, 0xF4, 0x8A, 0x2C, 0xC3, 0x4E, 0x3D, 0x09, 0x25, 0x9A, 0x7A, 0x29, 0xAB, 0xEA, 0x3E, 0x46, 0x4C, 0x7E, 0x73, 0xE8, 0x6C, 0xC7 }, { 0x77, 0x49, 0xCD, 0x0B, 0x99, 0x9A, 0x5D, 0x74, 0x84, 0xB0, 0x11, 0xAD, 0x82, 0xA4, 0xCF, 0x99, 0xA2, 0xDD, 0xA5, 0x7A, 0xE0, 0x0C, 0x57, 0x08 } }, /* HANXIN ISO/IEC DIS 20830:2019 Annex K.2 1st block */
134 /* 7*/ { 0x163, 24, 1, 27, { 0xE7, 0x3E, 0x33, 0x29, 0xE8, 0xFC, }, { 0x9B, 0x03, 0x94, 0xB7, 0xFE, 0xA1, 0x79, 0x1B, 0x00, 0x5D, 0x20, 0xCF, 0xCF, 0x4A, 0x69, 0xA6, 0x11, 0xAA, 0xE6, 0x5F, 0x8A, 0x68, 0xA7, 0xA2 } }, /* HANXIN ISO/IEC DIS 20830:2019 Annex K.2 2nd block */
135 /* 8*/ { 0x163, 24, 1, 29, { 0x00 }, { 0x00 } }, /* HANXIN ISO/IEC DIS 20830:2019 Annex K.2 3rd block */
136 /* 9*/ { 0x25, 6, 1, 16, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4 }, { 15, 23, 3, 23, 7, 14 } }, /* MAILMARK_4S Royal Mail Mailmark barcode C encoding and decoding Example 2.3.1 */
137 /* 10*/ { 0x25, 6, 1, 16, { 15, 22, 3, 25, 23, 26, 7, 3, 20, 14, 1, 4, 16, 3, 9, 28 }, { 24, 6, 16, 24, 22, 27 } }, /* MAILMARK_4S Royal Mail Mailmark barcode C encoding and decoding Example 2.3.2 */
138 /* 11*/ { 0x25, 7, 1, 19, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4 }, { 18, 11, 14, 7, 20, 1, 20 } }, /* MAILMARK_4S Royal Mail Mailmark barcode L encoding and decoding Example 2.3.1 */
139 /* 12*/ { 0x25, 7, 1, 19, { 0, 8, 21, 10, 29, 1, 29, 21, 2, 24, 15, 2, 19, 1, 4, 15, 11, 4, 16 }, { 16, 16, 6, 8, 9, 7, 19 } }, /* MAILMARK_4S Royal Mail Mailmark barcode L encoding and decoding Example 2.3.2 */
140 /* 13*/ { 0x43, 10, 1, 10, { 4, 13, 63, 1, 24, 9, 59, 3, 15, 4 }, { 16, 5, 20, 22, 34, 53, 51, 42, 2, 50 } }, /* MAXICODE Annex H Primary */
141 /* 14*/ { 0x43, 20, 1, 42, { 5, 57, 49, 47, 8, 18, 59, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33 }, { 23, 2, 8, 62, 17, 23, 19, 14, 19, 19, 30, 2, 63, 13, 39, 6, 6, 58, 2, 31 } }, /* MAXICODE Annex H Secondary odd */
142 /* 15*/ { 0x43, 20, 1, 42, { 47, 40, 57, 3, 1, 19, 41, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33 }, { 14, 61, 53, 63, 45, 51, 32, 0, 8, 4, 35, 35, 5, 60, 17, 39, 28, 22, 15, 1 } }, /* MAXICODE Annex H Secondary even */
143 /* 16*/ { 0x11d, 10, 0, 16, { 0x10, 0x20, 0x0C, 0x56, 0x61, 0x80, 0xEC, 0x11, 0xEC, 0x11, 0xEC, 0x11, 0xEC, 0x11, 0xEC, 0x11 }, { 0x55, 0x2C, 0x87, 0xC7, 0x36, 0xED, 0xC1, 0xD4, 0x24, 0xA5 } }, /* QRCODE Annex I.2 */
144 /* 17*/ { 0x11d, 5, 0, 5, { 0x40, 0x18, 0xAC, 0xC3, 0x00 }, { 0x30, 0xAE, 0x22, 0x0D, 0x86 } }, /* QRCODE Annex I.3 */
145 /* 18*/ { 0x163, 256, 0, 1, { 0xFF }, { 255, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255 } },
146 };
147 int data_size = ARRAY_SIZE(data);
148 int i;
149
150 testStart("test_encoding");
151
152 for (i = 0; i < data_size; i++) {
153 int j;
154 rs_t rs;
155 unsigned char res[1024];
156
157 if (testContinue(p_ctx, i)) continue;
158
159 rs_init_gf(&rs, data[i].prime_poly);
160 rs_init_code(&rs, data[i].nsym, data[i].index);
161 rs_encode(&rs, data[i].datalen, data[i].data, res);
162
163 if (p_ctx->index != -1 && (debug & ZINT_DEBUG_TEST_PRINT)) {
164 fprintf(stderr, "res "); for (j = data[i].nsym - 1; j >= 0; j--) fprintf(stderr, "%d, ", res[j]); fprintf(stderr, "\n");
165 fprintf(stderr, "exp "); for (j = 0; j < data[i].nsym; j++) fprintf(stderr, "%d, ", data[i].expected[j]); fprintf(stderr, "\n");
166 }
167 for (j = 0; j < data[i].nsym; j++) {
168 int k = data[i].nsym - 1 - j;
169 assert_equal(res[k], data[i].expected[j], "i:%d res[%d] %d != expected[%d] %d\n", i, k, res[k], j, data[i].expected[j]);
170 }
171 }
172
173 testFinish();
174 }
175
176 static void test_encoding_uint(const testCtx *const p_ctx) {
177 int debug = p_ctx->debug;
178
179 struct item {
180 unsigned int prime_poly;
181 int nsym;
182 int index;
183 int datalen;
184 unsigned int data[256];
185
186 unsigned int expected[256];
187 };
188 /* s/\/\*[ 0-9]*\*\//\=printf("\/\*%3d*\/", line(".") - line("'<")): */
189 struct item data[] = {
190 /* 0*/ { 0x43, 4, 1, 7, { 4, 20, 49, 37, 49, 38, 23 }, { 58, 53, 17, 54 } }, /* AUSPOST Australia Post Customer Barcoding Technical Specifications Diagram 10 */
191 /* 1*/ { 0x43, 7, 1, 10, { 9, 50, 1, 41, 47, 2, 39, 37, 1, 27 }, { 40, 20, 10, 16, 8, 50, 38 } }, /* AZTEC ISO/IEC 24778:2008 Section G.4 */
192 /* 2*/ { 0x13, 5, 1, 2, { 0, 9 }, { 9, 1, 3, 2, 12 } }, /* AZTEC ISO/IEC 24778:2008 Section G.4 Mode Message */
193 /* 3*/ { 0x12d, 5, 1, 3, { 142, 164, 186 }, { 102, 88, 5, 25, 114 } }, /* DATAMATRIX ISO/IEC 16022:2006 Annex O */
194 /* 4*/ { 0x89, 25, 1, 25, { 42, 13, 54, 39, 124, 91, 121, 65, 28, 40, 95, 48, 0, 126, 0, 126, 0, 126, 0, 126, 0, 126, 0, 126, 0 }, { 102, 70, 92, 79, 2, 52, 98, 62, 48, 33, 14, 4, 101, 17, 55, 89, 100, 23, 35, 112, 54, 20, 2, 47, 123 } }, /* GRIDMATRIX AIMD014 Section 6.8 */
195 /* 5*/ { 0x163, 4, 1, 21, { 0x11, 0xED, 0xC8, 0xC5, 0x40, 0x0F, 0xF4 }, { 0x1D, 0x68, 0xB4, 0xEB } }, /* HANXIN ISO/IEC DIS 20830:2019 Annex K.1 */
196 /* 6*/ { 0x163, 24, 1, 27, { 0x11, 0xED, 0xC8, 0xC5, 0x40, 0x0F, 0xF4, 0x8A, 0x2C, 0xC3, 0x4E, 0x3D, 0x09, 0x25, 0x9A, 0x7A, 0x29, 0xAB, 0xEA, 0x3E, 0x46, 0x4C, 0x7E, 0x73, 0xE8, 0x6C, 0xC7 }, { 0x77, 0x49, 0xCD, 0x0B, 0x99, 0x9A, 0x5D, 0x74, 0x84, 0xB0, 0x11, 0xAD, 0x82, 0xA4, 0xCF, 0x99, 0xA2, 0xDD, 0xA5, 0x7A, 0xE0, 0x0C, 0x57, 0x08 } }, /* HANXIN ISO/IEC DIS 20830:2019 Annex K.2 1st block */
197 /* 7*/ { 0x163, 24, 1, 27, { 0xE7, 0x3E, 0x33, 0x29, 0xE8, 0xFC, }, { 0x9B, 0x03, 0x94, 0xB7, 0xFE, 0xA1, 0x79, 0x1B, 0x00, 0x5D, 0x20, 0xCF, 0xCF, 0x4A, 0x69, 0xA6, 0x11, 0xAA, 0xE6, 0x5F, 0x8A, 0x68, 0xA7, 0xA2 } }, /* HANXIN ISO/IEC DIS 20830:2019 Annex K.2 2nd block */
198 /* 8*/ { 0x163, 24, 1, 29, { 0x00 }, { 0x00 } }, /* HANXIN ISO/IEC DIS 20830:2019 Annex K.2 3rd block */
199 /* 9*/ { 0x25, 6, 1, 16, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4 }, { 15, 23, 3, 23, 7, 14 } }, /* MAILMARK_4S Royal Mail Mailmark barcode C encoding and decoding Example 2.3.1 */
200 /* 10*/ { 0x25, 6, 1, 16, { 15, 22, 3, 25, 23, 26, 7, 3, 20, 14, 1, 4, 16, 3, 9, 28 }, { 24, 6, 16, 24, 22, 27 } }, /* MAILMARK_4S Royal Mail Mailmark barcode C encoding and decoding Example 2.3.2 */
201 /* 11*/ { 0x25, 7, 1, 19, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4 }, { 18, 11, 14, 7, 20, 1, 20 } }, /* MAILMARK_4S Royal Mail Mailmark barcode L encoding and decoding Example 2.3.1 */
202 /* 12*/ { 0x25, 7, 1, 19, { 0, 8, 21, 10, 29, 1, 29, 21, 2, 24, 15, 2, 19, 1, 4, 15, 11, 4, 16 }, { 16, 16, 6, 8, 9, 7, 19 } }, /* MAILMARK_4S Royal Mail Mailmark barcode L encoding and decoding Example 2.3.2 */
203 /* 13*/ { 0x43, 10, 1, 10, { 4, 13, 63, 1, 24, 9, 59, 3, 15, 4 }, { 16, 5, 20, 22, 34, 53, 51, 42, 2, 50 } }, /* MAXICODE Annex H Primary */
204 /* 14*/ { 0x43, 20, 1, 42, { 5, 57, 49, 47, 8, 18, 59, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33 }, { 23, 2, 8, 62, 17, 23, 19, 14, 19, 19, 30, 2, 63, 13, 39, 6, 6, 58, 2, 31 } }, /* MAXICODE Annex H Secondary odd */
205 /* 15*/ { 0x43, 20, 1, 42, { 47, 40, 57, 3, 1, 19, 41, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33 }, { 14, 61, 53, 63, 45, 51, 32, 0, 8, 4, 35, 35, 5, 60, 17, 39, 28, 22, 15, 1 } }, /* MAXICODE Annex H Secondary even */
206 /* 16*/ { 0x11d, 10, 0, 16, { 0x10, 0x20, 0x0C, 0x56, 0x61, 0x80, 0xEC, 0x11, 0xEC, 0x11, 0xEC, 0x11, 0xEC, 0x11, 0xEC, 0x11 }, { 0x55, 0x2C, 0x87, 0xC7, 0x36, 0xED, 0xC1, 0xD4, 0x24, 0xA5 } }, /* QRCODE Annex I.2 */
207 /* 17*/ { 0x11d, 5, 0, 5, { 0x40, 0x18, 0xAC, 0xC3, 0x00 }, { 0x30, 0xAE, 0x22, 0x0D, 0x86 } }, /* QRCODE Annex I.3 */
208 /* 18*/ { 0x163, 256, 0, 1, { 0xFF }, { 255, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255 } },
209 };
210 int data_size = ARRAY_SIZE(data);
211 int i;
212
213 testStart("test_encoding_uint");
214
215 for (i = 0; i < data_size; i++) {
216 int j;
217 rs_t rs;
218 unsigned int res[1024];
219
220 if (testContinue(p_ctx, i)) continue;
221
222 rs_init_gf(&rs, data[i].prime_poly);
223 rs_init_code(&rs, data[i].nsym, data[i].index);
224 rs_encode_uint(&rs, data[i].datalen, data[i].data, res);
225
226 if (p_ctx->index != -1 && (debug & ZINT_DEBUG_TEST_PRINT)) {
227 fprintf(stderr, "res "); for (j = data[i].nsym - 1; j >= 0; j--) fprintf(stderr, "%d, ", res[j]); fprintf(stderr, "\n");
228 fprintf(stderr, "exp "); for (j = 0; j < data[i].nsym; j++) fprintf(stderr, "%d, ", data[i].expected[j]); fprintf(stderr, "\n");
229 }
230 for (j = 0; j < data[i].nsym; j++) {
231 int k = data[i].nsym - 1 - j;
232 assert_equal(res[k], data[i].expected[j], "i:%d res[%d] %d != expected[%d] %d\n", i, k, res[k], j, data[i].expected[j]);
233 }
234 }
235
236 testFinish();
237 }
238
239 static void test_uint_encoding(const testCtx *const p_ctx) {
240 int debug = p_ctx->debug;
241
242 struct item {
243 unsigned int prime_poly;
244 int logmod;
245 int nsym;
246 int index;
247 int datalen;
248 unsigned int data[256];
249
250 unsigned int expected[256];
251 };
252 /* s/\/\*[ 0-9]*\*\//\=printf("\/\*%3d*\/", line(".") - line("'<")): */
253 struct item data[] = {
254 /* 0*/ { 0x409, 1023, 4, 1, 7, { 0x3FF, 0x000, 0x100, 0x1FF, 0x3FF, 0x000, 0x123 }, { 674, 993, 153, 229 } },
255 /* 1*/ { 0x1069, 4095, 4, 1, 7, { 0xFFF, 0x000, 0x700, 0x7FF, 0xFFF, 0x000, 0x123 }, { 575, 3494, 2350, 3472 } },
256 /* 2*/ { 0x1000, 4095, 4, 0, 7, { 0xFFF, 0x000, 0x700, 0x7FF, 0xFFF, 0x000, 0x123 }, { 64, 0, 65, 1 } },
257 /* 3*/ { 0x1000, 4095, 256, 0, 1, { 0xFFF }, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2048, 0, 512, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1 } },
258 };
259 int data_size = ARRAY_SIZE(data);
260 int i;
261
262 testStart("test_uint_encoding");
263
264 for (i = 0; i < data_size; i++) {
265 int j;
266 rs_uint_t rs_uint;
267 unsigned int res[1024];
268
269 if (testContinue(p_ctx, i)) continue;
270
271 assert_nonzero(rs_uint_init_gf(&rs_uint, data[i].prime_poly, data[i].logmod), "i:%d rs_uint_init_gf() == 0\n", i);
272 rs_uint_init_code(&rs_uint, data[i].nsym, data[i].index);
273 rs_uint_encode(&rs_uint, data[i].datalen, data[i].data, res);
274 rs_uint_free(&rs_uint);
275
276 if (p_ctx->index != -1 && (debug & ZINT_DEBUG_TEST_PRINT)) {
277 fprintf(stderr, "res "); for (j = data[i].nsym - 1; j >= 0; j--) fprintf(stderr, "%d, ", res[j]); fprintf(stderr, "\n");
278 fprintf(stderr, "exp "); for (j = 0; j < data[i].nsym; j++) fprintf(stderr, "%d, ", data[i].expected[j]); fprintf(stderr, "\n");
279 }
280 for (j = 0; j < data[i].nsym; j++) {
281 int k = data[i].nsym - 1 - j;
282 assert_equal(res[k], data[i].expected[j], "i:%d res[%d] %d != expected[%d] %d\n", i, k, (int) res[k], j, (int) data[i].expected[j]);
283 }
284
285 /* Simulate rs_uint_init_gf() malloc() failure and rs_uint_init_gf()'s return val not being checked */
286 assert_nonzero(rs_uint_init_gf(&rs_uint, data[i].prime_poly, data[i].logmod), "i:%d rs_uint_init_gf() == 0\n", i);
287 free(rs_uint.logt);
288 rs_uint.logt = NULL;
289 free(rs_uint.alog);
290 rs_uint.alog = NULL;
291
292 rs_uint_init_code(&rs_uint, data[i].nsym, data[i].index);
293 rs_uint_encode(&rs_uint, data[i].datalen, data[i].data, res);
294 rs_uint_free(&rs_uint);
295
296 for (j = 0; j < data[i].nsym; j++) {
297 int k = data[i].nsym - 1 - j;
298 assert_zero(res[k], "i:%d res[%d] %d != 0\n", i, k, (int) res[k]);
299 }
300 }
301
302 testFinish();
303 }
304
305 int main(int argc, char *argv[]) {
306
307 testFunction funcs[] = { /* name, func */
308 { "test_generate", test_generate },
309 { "test_encoding", test_encoding },
310 { "test_encoding_uint", test_encoding_uint },
311 { "test_uint_encoding", test_uint_encoding },
312 };
313
314 testRun(argc, argv, funcs, ARRAY_SIZE(funcs));
315
316 testReport();
317
318 return 0;
319 }
320
321 /* vim: set ts=4 sw=4 et : */