comparison mupdf-source/thirdparty/zint/backend/tests/test_output.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) 2021-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 #include "../output.h"
34 #ifdef _WIN32
35 #include <windows.h>
36 #include <direct.h>
37 #endif
38
39 static void test_check_colour_options(const testCtx *const p_ctx) {
40 struct item {
41 char *fgcolour;
42 char *bgcolour;
43 int ret;
44 char *expected;
45 };
46 /* s/\/\*[ 0-9]*\*\//\=printf("\/\*%3d*\/", line(".") - line("'<")): */
47 static const struct item data[] = {
48 /* 0*/ { "FFFFFF", "000000", 0, "" },
49 /* 1*/ { "ffffff", "ffffff", 0, "" },
50 /* 2*/ { "77777777", "33333333", 0, "" },
51 /* 3*/ { "FFFFF", "000000", ZINT_ERROR_INVALID_OPTION, "880: Malformed foreground RGB colour (6 or 8 characters only)" },
52 /* 4*/ { "FFFFFFF", "000000", ZINT_ERROR_INVALID_OPTION, "880: Malformed foreground RGB colour (6 or 8 characters only)" },
53 /* 5*/ { "FFFFFG", "000000", ZINT_ERROR_INVALID_OPTION, "881: Malformed foreground RGB colour 'FFFFFG' (hexadecimal only)" },
54 /* 6*/ { "FFFFFF", "000000000", ZINT_ERROR_INVALID_OPTION, "880: Malformed background RGB colour (6 or 8 characters only)" },
55 /* 7*/ { "FFFFFF", "0000000Z", ZINT_ERROR_INVALID_OPTION, "881: Malformed background RGB colour '0000000Z' (hexadecimal only)" },
56 /* 8*/ { "100,100,100,100", "0,1,2,3", 0, "" },
57 /* 9*/ { "100,,100,100", ",1,2,", 0, "" },
58 /* 10*/ { "100,100,100", "0,1,2,3", ZINT_ERROR_INVALID_OPTION, "882: Malformed foreground CMYK colour (4 decimal numbers, comma-separated)" },
59 /* 11*/ { "100,100,99,1001", "0,1,2,3", ZINT_ERROR_INVALID_OPTION, "883: Malformed foreground CMYK colour (3 digit maximum per number)" },
60 /* 12*/ { "101,100,100,100", "0,1,2,3", ZINT_ERROR_INVALID_OPTION, "884: Malformed foreground CMYK colour C (decimal 0 to 100 only)" },
61 /* 13*/ { "100,101,100,100", "0,1,2,3", ZINT_ERROR_INVALID_OPTION, "885: Malformed foreground CMYK colour M (decimal 0 to 100 only)" },
62 /* 14*/ { "100,100,101,100", "0,1,2,3", ZINT_ERROR_INVALID_OPTION, "886: Malformed foreground CMYK colour Y (decimal 0 to 100 only)" },
63 /* 15*/ { "100,100,100,101", "0,1,2,3", ZINT_ERROR_INVALID_OPTION, "887: Malformed foreground CMYK colour K (decimal 0 to 100 only)" },
64 /* 16*/ { "100,100,100,100", "0,1,", ZINT_ERROR_INVALID_OPTION, "882: Malformed background CMYK colour (4 decimal numbers, comma-separated)" },
65 /* 17*/ { "100,100,100,100", "0,0123,3,4", ZINT_ERROR_INVALID_OPTION, "883: Malformed background CMYK colour (3 digit maximum per number)" },
66 /* 18*/ { "100,100,100,100", "0,1,2,101", ZINT_ERROR_INVALID_OPTION, "887: Malformed background CMYK colour K (decimal 0 to 100 only)" },
67 /* 19*/ { "100,100,100,100", "0,1,2,3,", ZINT_ERROR_INVALID_OPTION, "882: Malformed background CMYK colour (4 decimal numbers, comma-separated)" },
68 };
69 const int data_size = ARRAY_SIZE(data);
70 int i, ret;
71 struct zint_symbol symbol;
72
73 testStart("test_check_colour_options");
74
75 for (i = 0; i < data_size; i++) {
76
77 if (testContinue(p_ctx, i)) continue;
78
79 strcpy(symbol.fgcolour, data[i].fgcolour);
80 strcpy(symbol.bgcolour, data[i].bgcolour);
81 symbol.errtxt[0] = '\0';
82
83 ret = out_check_colour_options(&symbol);
84 assert_equal(ret, data[i].ret, "i:%d ret %d != %d (%s)\n", i, ret, data[i].ret, symbol.errtxt);
85 assert_zero(strcmp(symbol.errtxt, data[i].expected), "i:%d symbol.errtxt (%s) != expected (%s)\n", i, symbol.errtxt, data[i].expected);
86 }
87
88 testFinish();
89 }
90
91 static void test_colour_get_rgb(const testCtx *const p_ctx) {
92 struct item {
93 char *colour;
94 int ret;
95 unsigned char red;
96 unsigned char green;
97 unsigned char blue;
98 unsigned char alpha;
99 char *expected_cmyk;
100 };
101 /* s/\/\*[ 0-9]*\*\//\=printf("\/\*%3d*\/", line(".") - line("'<")): */
102 static const struct item data[] = {
103 /* 0*/ { "FFFFFF", 0, 0xFF, 0xFF, 0xFF, 0xFF, "0,0,0,0" },
104 /* 1*/ { "000000", 0, 0x00, 0x00, 0x00, 0xFF, "0,0,0,100" },
105 /* 2*/ { "FEDCBA", 0, 0xFE, 0xDC, 0xBA, 0xFF, "0,13,27,0" },
106 /* 3*/ { "EEDD9900", 1, 0xEE, 0xDD, 0x99, 0x00, "0,7,36,7" },
107 /* 4*/ { "98765432", 1, 0x98, 0x76, 0x54, 0x32, "0,22,45,40" },
108 /* 5*/ { "147AD0", 0, 0x14, 0x7A, 0xD0, 0xFF, "90,41,0,18" },
109 /* 6*/ { "FC9630", 0, 0xFC, 0x96, 0x30, 0xFF, "0,40,81,1" },
110 /* 7*/ { "112233", 0, 0x11, 0x22, 0x33, 0xFF, "67,33,0,80" },
111 /* 8*/ { "CCDDEE", 0, 0xCC, 0xDD, 0xEE, 0xFF, "14,7,0,7" },
112 /* 9*/ { "0,0,0,0", 0, 0xFF, 0xFF, 0xFF, 0xFF, "0,0,0,0" },
113 /* 10*/ { "80,30,60,0", 0, 0x33, 0xB3, 0x66, 0xFF, "72,0,43,30" },
114 /* 11*/ { "50,50,50,50", 0, 0x40, 0x40, 0x40, 0xFF, "0,0,0,75" },
115 };
116 const int data_size = ARRAY_SIZE(data);
117 int i, ret;
118
119 testStart("test_colour_get_rgb");
120
121 for (i = 0; i < data_size; i++) {
122 /* Suppress clang-16 run-time exception MemorySanitizer: use-of-uninitialized-value (fixed in clang-17) */
123 unsigned char red = 0, green = 0, blue = 0, alpha = 0, rgb_alpha = 0;
124 int cyan, magenta, yellow, black;
125 int have_alpha;
126 char rgb[64];
127 char cmyk[64];
128
129 if (testContinue(p_ctx, i)) continue;
130
131 ret = out_colour_get_rgb(data[i].colour, &red, &green, &blue, &alpha);
132 assert_equal(ret, data[i].ret, "i:%d ret %d != %d\n", i, ret, data[i].ret);
133 assert_equal(red, data[i].red, "i:%d red 0x%02X (%d) != 0x%02X (%d) (green 0x%02X, blue 0x%02X)\n", i, red, red, data[i].red, data[i].red, green, blue);
134 assert_equal(green, data[i].green, "i:%d green %d (0x%02X) != %d (0x%02X)\n", i, green, green, data[i].green, data[i].green);
135 assert_equal(blue, data[i].blue, "i:%d blue %d (0x%02X) != %d (0x%02X)\n", i, blue, blue, data[i].blue, data[i].blue);
136 assert_equal(alpha, data[i].alpha, "i:%d alpha %d (0x%02X) != %d (0x%02X)\n", i, alpha, alpha, data[i].alpha, data[i].alpha);
137
138 have_alpha = ret == 1;
139 if (have_alpha) {
140 sprintf(rgb, "%02X%02X%02X%02X", red, green, blue, alpha);
141 } else {
142 sprintf(rgb, "%02X%02X%02X", red, green, blue);
143 }
144 ret = out_colour_get_cmyk(rgb, &cyan, &magenta, &yellow, &black, &rgb_alpha);
145 assert_equal(ret, 1 + have_alpha, "i:%d out_colour_get_cmyk(%s) ret %d != %d\n", i, rgb, ret, 1 + have_alpha);
146 assert_equal(rgb_alpha, alpha, "i:%d rgb_alpha %d (0x%02X) != %d (0x%02X)\n", i, rgb_alpha, rgb_alpha, alpha, alpha);
147
148 sprintf(cmyk, "%d,%d,%d,%d", cyan, magenta, yellow, black);
149 assert_zero(strcmp(cmyk, data[i].expected_cmyk), "i:%d strcmp(%s, %s) != 0\n", i, cmyk, data[i].expected_cmyk);
150 }
151
152 testFinish();
153 }
154
155 static void test_colour_get_cmyk(const testCtx *const p_ctx) {
156 struct item {
157 char *colour;
158 int ret;
159 int cyan;
160 int magenta;
161 int yellow;
162 int black;
163 unsigned char alpha;
164 char *expected_rgb;
165 int ret_rgb;
166 };
167 /* s/\/\*[ 0-9]*\*\//\=printf("\/\*%3d*\/", line(".") - line("'<")): */
168 static const struct item data[] = {
169 /* 0*/ { "80,30,60,0", 0, 80, 30, 60, 0, 0xFF, "33B366FF", 0 },
170 /* 1*/ { "50,50,50,50", 0, 50, 50, 50, 50, 0xFF, "404040FF", 0 },
171 /* 2*/ { "0,0,0,100", 0, 0, 0, 0, 100, 0xFF, "000000FF", 0 },
172 /* 3*/ { "71,0,40,44", 0, 71, 0, 40, 44, 0xFF, "298F56FF", 0 },
173 /* 4*/ { "123456", 1, 79, 40, 0, 44, 0xFF, "123456FF", 0 },
174 /* 5*/ { "12345678", 2, 79, 40, 0, 44, 0x78, "12345678", 1 },
175 };
176 const int data_size = ARRAY_SIZE(data);
177 int i, ret;
178
179 testStart("test_colour_get_cmyk");
180
181 for (i = 0; i < data_size; i++) {
182 int cyan, magenta, yellow, black;
183 unsigned char red, green, blue, alpha, rgb_alpha;
184 char rgb[16];
185
186 if (testContinue(p_ctx, i)) continue;
187
188 ret = out_colour_get_cmyk(data[i].colour, &cyan, &magenta, &yellow, &black, &alpha);
189 assert_equal(ret, data[i].ret, "i:%d ret %d != %d\n", i, ret, data[i].ret);
190 assert_equal(cyan, data[i].cyan, "i:%d cyan %d != %d (magenta %d, yellow %d, black %d)\n", i, cyan, data[i].cyan, magenta, yellow, black);
191 assert_equal(magenta, data[i].magenta, "i:%d magenta %d != %d\n", i, magenta, data[i].magenta);
192 assert_equal(yellow, data[i].yellow, "i:%d yellow %d != %d\n", i, yellow, data[i].yellow);
193 assert_equal(alpha, data[i].alpha, "i:%d alpha %d != %d\n", i, alpha, data[i].alpha);
194
195 ret = out_colour_get_rgb(data[i].colour, &red, &green, &blue, &rgb_alpha);
196 assert_equal(ret, data[i].ret_rgb, "i:%d out_colour_get_rgb(%s) ret %d != %d\n", i, rgb, ret, data[i].ret_rgb);
197 assert_equal(rgb_alpha, alpha, "i:%d rgb_alpha %d != %d\n", i, rgb_alpha, alpha);
198
199 sprintf(rgb, "%02X%02X%02X%02X", red, green, blue, rgb_alpha);
200 assert_zero(strcmp(rgb, data[i].expected_rgb), "i:%d strcmp(%s, %s) != 0\n", i, rgb, data[i].expected_rgb);
201 }
202
203 testFinish();
204 }
205
206 INTERNAL int out_quiet_zones_test(const struct zint_symbol *symbol, const int hide_text, const int comp_xoffset,
207 float *left, float *right, float *top, float *bottom);
208
209 static void test_quiet_zones(const testCtx *const p_ctx) {
210 int i, ret;
211 struct zint_symbol symbol = {0};
212 int hide_text = 0;
213 int comp_xoffset = 0;
214 float left, right, top, bottom;
215
216 testStart("test_quiet_zones");
217
218 for (i = 1; i <= BARCODE_LAST; i++) {
219 if (!ZBarcode_ValidID(i)) continue;
220 if (testContinue(p_ctx, i)) continue;
221
222 symbol.symbology = i;
223 symbol.output_options = BARCODE_QUIET_ZONES;
224 ret = out_quiet_zones_test(&symbol, hide_text, comp_xoffset, &left, &right, &top, &bottom);
225 if (i != BARCODE_FLAT && i != BARCODE_BC412) { /* Only two which aren't marked as done */
226 assert_nonzero(ret, "i:%d %s not done\n", i, testUtilBarcodeName(i));
227 }
228 }
229
230 testFinish();
231 }
232
233 static void test_set_whitespace_offsets(const testCtx *const p_ctx) {
234
235 struct item {
236 int symbology;
237 int whitespace_width;
238 int whitespace_height;
239 int border_width;
240 int output_options;
241 int hide_text;
242 int comp_xoffset;
243 float scaler;
244
245 float expected_xoffset;
246 float expected_yoffset;
247 float expected_roffset;
248 float expected_boffset;
249 float expected_qz_right;
250 };
251 /* s/\/\*[ 0-9]*\*\//\=printf("\/\*%3d*\/", line(".") - line("'<")): */
252 static const struct item data[] = {
253 /* 0*/ { BARCODE_CODE128, 1, 0, 0, 0, 0, 0, 0.0f, /*expected*/ 1.0f, 0.0f, 1.0f, 0.0f, 0.0f },
254 /* 1*/ { BARCODE_CODE128, 2, 0, 0, 0, 0, 0, 1.0f, /*expected*/ 2.0f, 0.0f, 2.0f, 0.0f, 0.0f },
255 /* 2*/ { BARCODE_CODE128, 2, 3, 0, 0, 0, 0, 1.0f, /*expected*/ 2.0f, 3.0f, 2.0f, 3.0f, 0.0f },
256 /* 3*/ { BARCODE_CODE128, 2, 3, 1, 0, 0, 0, 1.0f, /*expected*/ 2.0f, 3.0f, 2.0f, 3.0f, 0.0f },
257 /* 4*/ { BARCODE_CODE128, 2, 3, 1, BARCODE_BIND, 0, 0, 1.0f, /*expected*/ 2.0f, 4.0f, 2.0f, 4.0f, 0.0f },
258 /* 5*/ { BARCODE_CODE128, 2, 3, 1, BARCODE_BIND_TOP, 0, 0, 1.0f, /*expected*/ 2.0f, 4.0f, 2.0f, 3.0f, 0.0f },
259 /* 6*/ { BARCODE_CODE128, 2, 3, 1, BARCODE_BIND_TOP | BARCODE_BIND, 0, 0, 1.0f, /*expected*/ 2.0f, 4.0f, 2.0f, 3.0f, 0.0f }, /* BIND_TOP wins */
260 /* 7*/ { BARCODE_CODE128, 2, 3, 1, BARCODE_BOX, 0, 0, 1.0f, /*expected*/ 3.0f, 4.0f, 3.0f, 4.0f, 0.0f },
261 /* 8*/ { BARCODE_CODE128, 2, 3, 1, BARCODE_BIND_TOP | BARCODE_BOX, 0, 0, 1.0f, /*expected*/ 3.0f, 4.0f, 3.0f, 3.0f, 0.0f }, /* BIND_TOP wins */
262 /* 9*/ { BARCODE_CODE128, 2, 3, 1, BARCODE_BOX | BARCODE_BIND, 0, 0, 1.0f, /*expected*/ 3.0f, 4.0f, 3.0f, 4.0f, 0.0f }, /* BIND_BOX wins */
263 /* 10*/ { BARCODE_CODE128, 2, 3, 1, BARCODE_BIND_TOP | BARCODE_BOX | BARCODE_BIND, 0, 0, 1.0f, /*expected*/ 3.0f, 4.0f, 3.0f, 3.0f, 0.0f }, /* BIND_TOP wins */
264 /* 11*/ { BARCODE_CODE128, 2, 3, 1, BARCODE_BOX | BARCODE_QUIET_ZONES, 0, 0, 1.0f, /*expected*/ 13.0f, 4.0f, 13.0f, 4.0f, 10.0f },
265 };
266 const int data_size = ARRAY_SIZE(data);
267 int i;
268 struct zint_symbol symbol = {0};
269 float xoffset, yoffset, roffset, boffset, qz_right;
270 int xoffset_si, yoffset_si, roffset_si, boffset_si, qz_right_si;
271
272 testStart("test_set_whitespace_offsets");
273
274 for (i = 0; i < data_size; i++) {
275 if (testContinue(p_ctx, i)) continue;
276
277 xoffset = yoffset = roffset = boffset = qz_right = 0.0f;
278 xoffset_si = yoffset_si = roffset_si = boffset_si = qz_right_si = 0;
279
280 symbol.symbology = data[i].symbology;
281 symbol.whitespace_width = data[i].whitespace_width;
282 symbol.whitespace_height = data[i].whitespace_height;
283 symbol.border_width = data[i].border_width;
284 symbol.output_options = data[i].output_options;
285 out_set_whitespace_offsets(&symbol, data[i].hide_text, data[i].comp_xoffset,
286 &xoffset, &yoffset, &roffset, &boffset, &qz_right, data[i].scaler,
287 &xoffset_si, &yoffset_si, &roffset_si, &boffset_si, &qz_right_si);
288 assert_equal(xoffset, data[i].expected_xoffset, "i:%d xoffset %g != %g\n", i, xoffset, data[i].expected_xoffset);
289 assert_equal(yoffset, data[i].expected_yoffset, "i:%d yoffset %g != %g\n", i, yoffset, data[i].expected_yoffset);
290 assert_equal(roffset, data[i].expected_roffset, "i:%d roffset %g != %g\n", i, roffset, data[i].expected_roffset);
291 assert_equal(boffset, data[i].expected_boffset, "i:%d boffset %g != %g\n", i, boffset, data[i].expected_boffset);
292 assert_equal(qz_right, data[i].expected_qz_right, "i:%d qz_right %g != %g\n", i, qz_right, data[i].expected_qz_right);
293 if (data[i].scaler) {
294 int expected_xoffset_si = (int) (data[i].expected_xoffset * data[i].scaler);
295 int expected_yoffset_si = (int) (data[i].expected_yoffset * data[i].scaler);
296 int expected_roffset_si = (int) (data[i].expected_roffset * data[i].scaler);
297 int expected_boffset_si = (int) (data[i].expected_boffset * data[i].scaler);
298 int expected_qz_right_si = (int) (data[i].expected_qz_right * data[i].scaler);
299 assert_equal(xoffset_si, expected_xoffset_si, "i:%d xoffset_si %d != %d\n", i, xoffset_si, expected_xoffset_si);
300 assert_equal(yoffset_si, expected_yoffset_si, "i:%d yoffset_si %d != %d\n", i, yoffset_si, expected_yoffset_si);
301 assert_equal(roffset_si, expected_roffset_si, "i:%d roffset_si %d != %d\n", i, roffset_si, expected_roffset_si);
302 assert_equal(boffset_si, expected_boffset_si, "i:%d boffset_si %d != %d\n", i, boffset_si, expected_boffset_si);
303 assert_equal(qz_right_si, expected_qz_right_si, "i:%d qz_right_si %d != %d\n", i, qz_right_si, expected_qz_right_si);
304 } else {
305 assert_zero(xoffset_si, "i:%d xoffset_si %d non-zero\n", i, xoffset_si);
306 assert_zero(yoffset_si, "i:%d yoffset_si %d non-zero\n", i, yoffset_si);
307 assert_zero(roffset_si, "i:%d roffset_si %d non-zero\n", i, roffset_si);
308 assert_zero(boffset_si, "i:%d boffset_si %d non-zero\n", i, boffset_si);
309 assert_zero(qz_right_si, "i:%d qz_right_si %d non-zero\n", i, qz_right_si);
310 }
311 }
312
313 testFinish();
314 }
315
316 #ifdef _WIN32
317 #define TEST_OUT_SEP '\\'
318 #define TEST_OUT_SEP_STR "\\"
319 #else
320 #define TEST_OUT_SEP '/'
321 #define TEST_OUT_SEP_STR "/"
322 #endif
323
324 static void test_fopen(const testCtx *const p_ctx) {
325 struct item {
326 char dir[32];
327 char subdir[32];
328 char *filename;
329 int succeed;
330 };
331 /* s/\/\*[ 0-9]*\*\//\=printf("\/\*%3d*\/", line(".") - line("'<")): */
332 static const struct item data[] = {
333 /* 0*/ { "", "", "out.png", 1 },
334 /* 1*/ { "out_test", "", "out.png", 1 },
335 /* 2*/ { "out_test_with_subdir", "out_test_subdir", "out.png", 1 },
336 /* 3*/ { "out_test_with_subdir", "out_test_subdir", "out.bmp", 1 },
337 /* 4*/ { "out_test_with_subdir", "out_test_subdir", "out.emf", 1 },
338 /* 5*/ { "out_test_with_subdir", "out_test_subdir", "out.eps", 1 },
339 /* 6*/ { "out_test_with_subdir", "out_test_subdir", "out.gif", 1 },
340 /* 7*/ { "out_test_with_subdir", "out_test_subdir", "out.pcx", 1 },
341 /* 8*/ { "out_test_with_subdir", "out_test_subdir", "out.svg", 1 },
342 /* 9*/ { "out_test_with_subdir", "out_test_subdir", "out.tif", 1 },
343 /* 10*/ { "out_test/", "", "out.png", 1 },
344 /* 11*/ { "out_test//", "", "out.png", 1 },
345 /* 12*/ { "out_test/", "/out_test_subdir/", "out.png", 1 },
346 /* 13*/ { "out_test\\", "\\out_test_subdir\\", "out.png", 1 },
347 /* 14*/ { "", "", "outé.png", 1 },
348 /* 15*/ { "outé_test", "", "outé.png", 1 },
349 };
350 const int data_size = ARRAY_SIZE(data);
351 int i, len;
352
353 FILE *ret;
354 char cwdbuf[1024];
355 char outfile[1024 + 256];
356 char dirname[1024 + 256];
357 char subdirname[1024 + 256];
358 int dir_exists, subdir_exists;
359
360 testStart("test_fopen");
361
362 assert_nonnull(getcwd(cwdbuf, sizeof(cwdbuf)), "getcwd NULL (%d, %s)\n", errno, strerror(errno));
363
364 for (i = 0; i < data_size; i++) {
365
366 if (testContinue(p_ctx, i)) continue;
367
368 strcpy(outfile, cwdbuf);
369 len = (int) strlen(outfile);
370 if (len && outfile[len - 1] != TEST_OUT_SEP) { outfile[len++] = TEST_OUT_SEP; outfile[len] = '\0'; }
371 dir_exists = subdir_exists = 0;
372
373 if (data[i].dir[0]) {
374 strcat(outfile, data[i].dir);
375 strcat(outfile, TEST_OUT_SEP_STR);
376 strcpy(dirname, outfile);
377 dir_exists = testUtilDirExists(dirname);
378 if (data[i].subdir[0]) {
379 strcat(outfile, data[i].subdir);
380 strcat(outfile, TEST_OUT_SEP_STR);
381 strcpy(subdirname, outfile);
382 subdir_exists = testUtilDirExists(subdirname);
383 }
384 }
385 strcat(outfile, data[i].filename);
386
387 ret = out_fopen(outfile, "w");
388 if (data[i].succeed) {
389 assert_nonnull(ret, "i:%d out_fopen(%s) == NULL (%d: %s)\n", i, outfile, errno, strerror(errno));
390 assert_zero(fclose(ret), "i:%d fclose(%s) != 0 (%d: %s)\n", i, outfile, errno, strerror(errno));
391 assert_nonzero(testUtilExists(outfile), "i:%d testUtilExists(%s) != 0 (%d: %s)\n", i, outfile, errno, strerror(errno));
392 if (data[i].dir[0]) {
393 assert_nonzero(testUtilDirExists(dirname), "i:%d testUtilDirExists(%s) != 0 (%d: %s)\n", i, dirname, errno, strerror(errno));
394 }
395 assert_zero(testUtilRemove(outfile), "i:%d testUtilRemove(%s) != 0 (%d: %s)\n", i, outfile, errno, strerror(errno));
396 if (data[i].dir[0]) {
397 if (data[i].subdir[0] && !subdir_exists) {
398 assert_zero(testUtilRmDir(subdirname), "i:%d rmdir(%s) != 0 (%d: %s)\n", i, subdirname, errno, strerror(errno));
399 }
400 if (!dir_exists && strcmp(dirname, "/") != 0 && strcmp(dirname, "\\") != 0) {
401 assert_zero(testUtilRmDir(dirname), "i:%d rmdir(%s) != 0 (%d: %s)\n", i, dirname, errno, strerror(errno));
402 }
403 }
404 } else {
405 assert_null(ret, "i:%d out_fopen(%s) == NULL (%d: %s)\n", i, outfile, errno, strerror(errno));
406 /* TODO: may have left junk around */
407 }
408 }
409
410 testFinish();
411 }
412
413 int main(int argc, char *argv[]) {
414
415 testFunction funcs[] = { /* name, func */
416 { "test_check_colour_options", test_check_colour_options },
417 { "test_colour_get_rgb", test_colour_get_rgb },
418 { "test_colour_get_cmyk", test_colour_get_cmyk },
419 { "test_quiet_zones", test_quiet_zones },
420 { "test_set_whitespace_offsets", test_set_whitespace_offsets },
421 { "test_fopen", test_fopen },
422 };
423
424 testRun(argc, argv, funcs, ARRAY_SIZE(funcs));
425
426 testReport();
427
428 return 0;
429 }
430
431 /* vim: set ts=4 sw=4 et : */