Mercurial > hgrepos > Python2 > PyMuPDF
comparison mupdf-source/thirdparty/zint/backend/tests/test_pcx.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-2024 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 | |
| 34 static void test_print(const testCtx *const p_ctx) { | |
| 35 int debug = p_ctx->debug; | |
| 36 | |
| 37 struct item { | |
| 38 int symbology; | |
| 39 int border_width; | |
| 40 int output_options; | |
| 41 int whitespace_width; | |
| 42 int whitespace_height; | |
| 43 int option_1; | |
| 44 int option_2; | |
| 45 char *fgcolour; | |
| 46 char *bgcolour; | |
| 47 float scale; | |
| 48 char *data; | |
| 49 char *expected_file; | |
| 50 }; | |
| 51 /* s/\/\*[ 0-9]*\*\//\=printf("\/\*%3d*\/", line(".") - line("'<")): */ | |
| 52 static const struct item data[] = { | |
| 53 /* 0*/ { BARCODE_GRIDMATRIX, -1, -1, -1, -1, -1, -1, "C3C3C3", "", 0.75, "Grid Matrix", "gridmatrix_fg_0.75.pcx" }, | |
| 54 /* 1*/ { BARCODE_CODABLOCKF, -1, -1, -1, -1, -1, 20, "FFFFFF", "000000", 0, "1234567890123456789012345678901234567890", "codeblockf_reverse.pcx" }, | |
| 55 /* 2*/ { BARCODE_QRCODE, -1, -1, -1, -1, 2, 1, "", "D2E3F4", 0, "1234567890", "qr_bg.pcx" }, | |
| 56 /* 3*/ { BARCODE_ULTRA, 1, BARCODE_BOX, 1, 1, -1, -1, "FF0000", "0000FF", 0, "ULTRACODE_123456789!", "ultra_fg_bg_hvwsp1_box1.pcx" }, | |
| 57 /* 4*/ { BARCODE_CODE11, -1, -1, -1, -1, -1, -1, "12345678", "FEDCBA98", 0, "123", "code11_fgbgtrans.pcx" }, | |
| 58 }; | |
| 59 const int data_size = ARRAY_SIZE(data); | |
| 60 int i, length, ret; | |
| 61 struct zint_symbol *symbol; | |
| 62 | |
| 63 const char *data_dir = "/backend/tests/data/pcx"; | |
| 64 char *pcx = "out.pcx"; | |
| 65 char expected_file[4096]; | |
| 66 char escaped[1024]; | |
| 67 int escaped_size = 1024; | |
| 68 unsigned char filebuf[36864]; | |
| 69 int filebuf_size; | |
| 70 | |
| 71 const char *const have_identify = testUtilHaveIdentify(); | |
| 72 | |
| 73 testStart("test_pcx"); | |
| 74 | |
| 75 if (p_ctx->generate) { | |
| 76 char data_dir_path[1024]; | |
| 77 assert_nonzero(testUtilDataPath(data_dir_path, sizeof(data_dir_path), data_dir, NULL), "testUtilDataPath(%s) == 0\n", data_dir); | |
| 78 if (!testUtilDirExists(data_dir_path)) { | |
| 79 ret = testUtilMkDir(data_dir_path); | |
| 80 assert_zero(ret, "testUtilMkDir(%s) ret %d != 0 (%d: %s)\n", data_dir_path, ret, errno, strerror(errno)); | |
| 81 } | |
| 82 } | |
| 83 | |
| 84 for (i = 0; i < data_size; i++) { | |
| 85 | |
| 86 if (testContinue(p_ctx, i)) continue; | |
| 87 | |
| 88 symbol = ZBarcode_Create(); | |
| 89 assert_nonnull(symbol, "Symbol not created\n"); | |
| 90 | |
| 91 length = testUtilSetSymbol(symbol, data[i].symbology, -1 /*input_mode*/, -1 /*eci*/, data[i].option_1, data[i].option_2, -1, data[i].output_options, data[i].data, -1, debug); | |
| 92 if (data[i].border_width != -1) { | |
| 93 symbol->border_width = data[i].border_width; | |
| 94 } | |
| 95 if (data[i].whitespace_width != -1) { | |
| 96 symbol->whitespace_width = data[i].whitespace_width; | |
| 97 } | |
| 98 if (data[i].whitespace_height != -1) { | |
| 99 symbol->whitespace_height = data[i].whitespace_height; | |
| 100 } | |
| 101 if (*data[i].fgcolour) { | |
| 102 strcpy(symbol->fgcolour, data[i].fgcolour); | |
| 103 } | |
| 104 if (*data[i].bgcolour) { | |
| 105 strcpy(symbol->bgcolour, data[i].bgcolour); | |
| 106 } | |
| 107 if (data[i].scale != 0) { | |
| 108 symbol->scale = data[i].scale; | |
| 109 } | |
| 110 symbol->debug |= debug; | |
| 111 | |
| 112 ret = ZBarcode_Encode(symbol, (unsigned char *) data[i].data, length); | |
| 113 assert_zero(ret, "i:%d %s ZBarcode_Encode ret %d != 0 %s\n", i, testUtilBarcodeName(data[i].symbology), ret, symbol->errtxt); | |
| 114 | |
| 115 strcpy(symbol->outfile, pcx); | |
| 116 ret = ZBarcode_Print(symbol, 0); | |
| 117 assert_zero(ret, "i:%d %s ZBarcode_Print %s ret %d != 0\n", i, testUtilBarcodeName(data[i].symbology), symbol->outfile, ret); | |
| 118 | |
| 119 assert_nonzero(testUtilDataPath(expected_file, sizeof(expected_file), data_dir, data[i].expected_file), "i:%d testUtilDataPath == 0\n", i); | |
| 120 | |
| 121 if (p_ctx->generate) { | |
| 122 printf(" /*%3d*/ { %s, %d, %s, %d, %d, %d, %d, \"%s\", \"%s\", %.5g, \"%s\", \"%s\"},\n", | |
| 123 i, testUtilBarcodeName(data[i].symbology), data[i].border_width, testUtilOutputOptionsName(data[i].output_options), | |
| 124 data[i].whitespace_width, data[i].whitespace_height, | |
| 125 data[i].option_1, data[i].option_2, data[i].fgcolour, data[i].bgcolour, data[i].scale, | |
| 126 testUtilEscape(data[i].data, length, escaped, escaped_size), data[i].expected_file); | |
| 127 ret = testUtilRename(symbol->outfile, expected_file); | |
| 128 assert_zero(ret, "i:%d testUtilRename(%s, %s) ret %d != 0 (%d: %s)\n", i, symbol->outfile, expected_file, ret, errno, strerror(errno)); | |
| 129 if (have_identify) { | |
| 130 ret = testUtilVerifyIdentify(have_identify, expected_file, debug); | |
| 131 assert_zero(ret, "i:%d %s identify %s ret %d != 0\n", i, testUtilBarcodeName(data[i].symbology), expected_file, ret); | |
| 132 } | |
| 133 } else { | |
| 134 assert_nonzero(testUtilExists(symbol->outfile), "i:%d testUtilExists(%s) == 0\n", i, symbol->outfile); | |
| 135 assert_nonzero(testUtilExists(expected_file), "i:%d testUtilExists(%s) == 0\n", i, expected_file); | |
| 136 | |
| 137 ret = testUtilCmpBins(symbol->outfile, expected_file); | |
| 138 assert_zero(ret, "i:%d %s testUtilCmpBins(%s, %s) %d != 0\n", i, testUtilBarcodeName(data[i].symbology), symbol->outfile, expected_file, ret); | |
| 139 | |
| 140 ret = testUtilReadFile(symbol->outfile, filebuf, sizeof(filebuf), &filebuf_size); /* For BARCODE_MEMORY_FILE */ | |
| 141 assert_zero(ret, "i:%d %s testUtilReadFile(%s) %d != 0\n", i, testUtilBarcodeName(data[i].symbology), symbol->outfile, ret); | |
| 142 | |
| 143 if (!(debug & ZINT_DEBUG_TEST_KEEP_OUTFILE)) { | |
| 144 assert_zero(testUtilRemove(symbol->outfile), "i:%d testUtilRemove(%s) != 0\n", i, symbol->outfile); | |
| 145 } | |
| 146 | |
| 147 symbol->output_options |= BARCODE_MEMORY_FILE; | |
| 148 ret = ZBarcode_Print(symbol, 0); | |
| 149 assert_zero(ret, "i:%d %s ZBarcode_Print %s ret %d != 0 (%s)\n", | |
| 150 i, testUtilBarcodeName(data[i].symbology), symbol->outfile, ret, symbol->errtxt); | |
| 151 assert_nonnull(symbol->memfile, "i:%d %s memfile NULL\n", i, testUtilBarcodeName(data[i].symbology)); | |
| 152 assert_equal(symbol->memfile_size, filebuf_size, "i:%d %s memfile_size %d != %d\n", | |
| 153 i, testUtilBarcodeName(data[i].symbology), symbol->memfile_size, filebuf_size); | |
| 154 assert_zero(memcmp(symbol->memfile, filebuf, symbol->memfile_size), "i:%d %s memcmp(memfile, filebuf) != 0\n", | |
| 155 i, testUtilBarcodeName(data[i].symbology)); | |
| 156 } | |
| 157 | |
| 158 ZBarcode_Delete(symbol); | |
| 159 } | |
| 160 | |
| 161 testFinish(); | |
| 162 } | |
| 163 | |
| 164 INTERNAL int pcx_pixel_plot(struct zint_symbol *symbol, unsigned char *pixelbuf); | |
| 165 | |
| 166 static void test_outfile(const testCtx *const p_ctx) { | |
| 167 int ret; | |
| 168 int skip_readonly_test = 0; | |
| 169 struct zint_symbol symbol = {0}; | |
| 170 unsigned char data[] = { "1" }; | |
| 171 | |
| 172 (void)p_ctx; | |
| 173 | |
| 174 testStart("test_outfile"); | |
| 175 | |
| 176 symbol.symbology = BARCODE_CODE128; | |
| 177 symbol.bitmap = data; | |
| 178 symbol.bitmap_width = symbol.bitmap_height = 1; | |
| 179 | |
| 180 strcpy(symbol.outfile, "test_pcx_out.pcx"); | |
| 181 #ifndef _WIN32 | |
| 182 skip_readonly_test = getuid() == 0; /* Skip if running as root on Unix as can't create read-only file */ | |
| 183 #endif | |
| 184 if (!skip_readonly_test) { | |
| 185 static char expected_errtxt[] = "621: Could not open PCX output file ("; /* Excluding OS-dependent `errno` stuff */ | |
| 186 | |
| 187 (void) testUtilRmROFile(symbol.outfile); /* In case lying around from previous fail */ | |
| 188 assert_nonzero(testUtilCreateROFile(symbol.outfile), "pcx_pixel_plot testUtilCreateROFile(%s) fail (%d: %s)\n", symbol.outfile, errno, strerror(errno)); | |
| 189 | |
| 190 ret = pcx_pixel_plot(&symbol, data); | |
| 191 assert_equal(ret, ZINT_ERROR_FILE_ACCESS, "pcx_pixel_plot ret %d != ZINT_ERROR_FILE_ACCESS (%d) (%s)\n", ret, ZINT_ERROR_FILE_ACCESS, symbol.errtxt); | |
| 192 assert_zero(testUtilRmROFile(symbol.outfile), "pcx_pixel_plot testUtilRmROFile(%s) != 0 (%d: %s)\n", symbol.outfile, errno, strerror(errno)); | |
| 193 assert_zero(strncmp(symbol.errtxt, expected_errtxt, sizeof(expected_errtxt) - 1), "strncmp(%s, %s) != 0\n", symbol.errtxt, expected_errtxt); | |
| 194 } | |
| 195 | |
| 196 symbol.output_options |= BARCODE_STDOUT; | |
| 197 | |
| 198 ret = pcx_pixel_plot(&symbol, data); | |
| 199 printf(" - ignore (PCX to stdout)\n"); fflush(stdout); | |
| 200 assert_zero(ret, "pcx_pixel_plot ret %d != 0 (%s)\n", ret, symbol.errtxt); | |
| 201 | |
| 202 testFinish(); | |
| 203 } | |
| 204 | |
| 205 int main(int argc, char *argv[]) { | |
| 206 | |
| 207 testFunction funcs[] = { /* name, func */ | |
| 208 { "test_print", test_print }, | |
| 209 { "test_outfile", test_outfile }, | |
| 210 }; | |
| 211 | |
| 212 testRun(argc, argv, funcs, ARRAY_SIZE(funcs)); | |
| 213 | |
| 214 testReport(); | |
| 215 | |
| 216 return 0; | |
| 217 } | |
| 218 | |
| 219 /* vim: set ts=4 sw=4 et : */ |
