1 #include "../../include/sane/config.h"
2
3 #include <stdlib.h>
4 #include <sys/types.h>
5 #include <sys/stat.h>
6 #include <fcntl.h>
7 #include <errno.h>
8 #include <string.h>
9 #include <assert.h>
10
11 /* sane includes for the sanei functions called */
12 #include "../../include/sane/sane.h"
13 #include "../../include/sane/saneopts.h"
14 #include "../../include/sane/sanei.h"
15
16 /* range for constraint */
17 static const SANE_Range int_range = {
18 3, /* minimum */
19 18, /* maximum */
20 3 /* quantization */
21 };
22
23 static SANE_Option_Descriptor int_opt = {
24 SANE_NAME_SCAN_TL_X,
25 SANE_TITLE_SCAN_TL_X,
26 SANE_DESC_SCAN_TL_X,
27 SANE_TYPE_FIXED,
28 SANE_UNIT_MM,
29 sizeof (SANE_Word),
30 0,
31 SANE_CONSTRAINT_RANGE,
32 {NULL}
33 };
34
35 #define ARRAY_SIZE 7
36
37 static SANE_Option_Descriptor array_opt = {
38 SANE_NAME_SCAN_TL_X,
39 SANE_TITLE_SCAN_TL_X,
40 SANE_DESC_SCAN_TL_X,
41 SANE_TYPE_FIXED,
42 SANE_UNIT_MM,
43 sizeof (SANE_Word) * ARRAY_SIZE,
44 0,
45 SANE_CONSTRAINT_RANGE,
46 {NULL}
47 };
48
49 static SANE_Option_Descriptor bool_opt = {
50 SANE_NAME_SCAN_TL_X,
51 SANE_TITLE_SCAN_TL_X,
52 SANE_DESC_SCAN_TL_X,
53 SANE_TYPE_BOOL,
54 SANE_UNIT_MM,
55 sizeof (SANE_Bool),
56 0,
57 SANE_CONSTRAINT_NONE,
58 {NULL}
59 };
60
61 static SANE_Option_Descriptor bool_array_opt = {
62 SANE_NAME_SCAN_TL_X,
63 SANE_TITLE_SCAN_TL_X,
64 SANE_DESC_SCAN_TL_X,
65 SANE_TYPE_BOOL,
66 SANE_UNIT_MM,
67 sizeof (SANE_Bool) * ARRAY_SIZE,
68 0,
69 SANE_CONSTRAINT_NONE,
70 {NULL}
71 };
72
73
74 #define WORD_SIZE 9
75 static const SANE_Int dpi_list[] =
76 { WORD_SIZE - 1, 100, 200, 300, 400, 500, 600, 700, 800 };
77
78 static SANE_Option_Descriptor word_array_opt = {
79 SANE_NAME_SCAN_RESOLUTION,
80 SANE_TITLE_SCAN_RESOLUTION,
81 SANE_DESC_SCAN_RESOLUTION,
82 SANE_TYPE_INT,
83 SANE_UNIT_DPI,
84 sizeof (SANE_Word) * WORD_SIZE,
85 100,
86 SANE_CONSTRAINT_WORD_LIST,
87 {NULL}
88 };
89
90 /******************************/
91 /* start of tests definitions */
92 /******************************/
93
94 /*
95 * constrained int
96 */
97 static void
min_int_value(void)98 min_int_value (void)
99 {
100 SANE_Int value = int_range.min;
101 SANE_Status status;
102
103 status = sanei_check_value (&int_opt, &value);
104
105 /* check results */
106 assert (status == SANE_STATUS_GOOD);
107 assert (value == int_range.min);
108 }
109
110
111 static void
max_int_value(void)112 max_int_value (void)
113 {
114 SANE_Int value = int_range.max;
115 SANE_Status status;
116
117 status = sanei_check_value (&int_opt, &value);
118
119 /* check results */
120 assert (status == SANE_STATUS_GOOD);
121 assert (value == int_range.max);
122 }
123
124
125 static void
below_min_int_value(void)126 below_min_int_value (void)
127 {
128 SANE_Int value = int_range.min - 1;
129 SANE_Status status;
130
131 status = sanei_check_value (&int_opt, &value);
132
133 /* check results */
134 assert (status == SANE_STATUS_INVAL);
135 }
136
137
138 /* rounded to lower value */
139 static void
quant1_int_value(void)140 quant1_int_value (void)
141 {
142 SANE_Int value = int_range.min + 1;
143 SANE_Status status;
144
145 status = sanei_check_value (&int_opt, &value);
146
147 /* check results */
148 assert (status == SANE_STATUS_INVAL);
149 }
150
151
152 /* close to higher value */
153 static void
quant2_int_value(void)154 quant2_int_value (void)
155 {
156 SANE_Int value = int_range.min + int_range.quant - 1;
157 SANE_Status status;
158
159 status = sanei_check_value (&int_opt, &value);
160
161 /* check results */
162 assert (status == SANE_STATUS_INVAL);
163 }
164
165
166 static void
in_range_int_value(void)167 in_range_int_value (void)
168 {
169 SANE_Int value = int_range.min + int_range.quant;
170 SANE_Status status;
171
172 status = sanei_check_value (&int_opt, &value);
173
174 /* check results */
175 assert (status == SANE_STATUS_GOOD);
176 assert (value == int_range.min + int_range.quant);
177 }
178
179
180 static void
above_max_int_value(void)181 above_max_int_value (void)
182 {
183 SANE_Int value = int_range.max + 1;
184 SANE_Status status;
185
186 status = sanei_check_value (&int_opt, &value);
187
188 /* check results */
189 assert (status == SANE_STATUS_INVAL);
190 }
191
192
193 /*
194 * constrained int array
195 */
196 static void
min_int_array(void)197 min_int_array (void)
198 {
199 SANE_Int value[ARRAY_SIZE];
200 SANE_Status status;
201 int i;
202
203 for (i = 0; i < ARRAY_SIZE; i++)
204 {
205 value[i] = int_range.min;
206 }
207 status = sanei_check_value (&array_opt, value);
208
209 /* check results */
210 assert (status == SANE_STATUS_GOOD);
211 for (i = 0; i < ARRAY_SIZE; i++)
212 {
213 assert (value[i] == int_range.min);
214 }
215 }
216
217
218 static void
max_int_array(void)219 max_int_array (void)
220 {
221 SANE_Int value[ARRAY_SIZE];
222 SANE_Status status;
223 int i;
224
225 for (i = 0; i < ARRAY_SIZE; i++)
226 {
227 value[i] = int_range.max;
228 }
229
230 status = sanei_check_value (&array_opt, value);
231
232 /* check results */
233 assert (status == SANE_STATUS_GOOD);
234 for (i = 0; i < ARRAY_SIZE; i++)
235 {
236 assert (value[i] == int_range.max);
237 }
238 }
239
240
241 static void
below_min_int_array(void)242 below_min_int_array (void)
243 {
244 SANE_Int value[ARRAY_SIZE];
245 SANE_Status status;
246 int i;
247
248 for (i = 0; i < ARRAY_SIZE; i++)
249 {
250 value[i] = int_range.min - 1;
251 }
252
253 status = sanei_check_value (&array_opt, &value);
254
255 /* check results */
256 assert (status == SANE_STATUS_INVAL);
257 }
258
259
260 /* rounded to lower value */
261 static void
quant1_int_array(void)262 quant1_int_array (void)
263 {
264 SANE_Int value[ARRAY_SIZE];
265 SANE_Status status;
266 int i;
267
268 for (i = 0; i < ARRAY_SIZE; i++)
269 {
270 value[i] = int_range.min + 1;
271 }
272 status = sanei_check_value (&array_opt, &value);
273
274 /* check results */
275 assert (status == SANE_STATUS_INVAL);
276 }
277
278
279 /* rounded to higher value */
280 static void
quant2_int_array(void)281 quant2_int_array (void)
282 {
283 SANE_Int value[ARRAY_SIZE];
284 SANE_Status status;
285 int i;
286
287 for (i = 0; i < ARRAY_SIZE; i++)
288 {
289 value[i] = int_range.min + int_range.quant - 1;
290 }
291 status = sanei_check_value (&array_opt, &value);
292
293 /* check results */
294 assert (status == SANE_STATUS_INVAL);
295 }
296
297
298 static void
in_range_int_array(void)299 in_range_int_array (void)
300 {
301 SANE_Int value[ARRAY_SIZE];
302 SANE_Status status;
303 int i;
304
305 for (i = 0; i < ARRAY_SIZE; i++)
306 {
307 value[i] = int_range.min + int_range.quant;
308 }
309
310 status = sanei_check_value (&array_opt, &value);
311
312 /* check results */
313 assert (status == SANE_STATUS_GOOD);
314 for (i = 0; i < ARRAY_SIZE; i++)
315 {
316 assert (value[i] == int_range.min + int_range.quant);
317 }
318 }
319
320
321 static void
above_max_int_array(void)322 above_max_int_array (void)
323 {
324 SANE_Int value[ARRAY_SIZE];
325 SANE_Status status;
326 int i;
327
328 for (i = 0; i < ARRAY_SIZE; i++)
329 {
330 value[i] = int_range.max + 1;
331 }
332 status = sanei_check_value (&array_opt, &value);
333
334 /* check results */
335 assert (status == SANE_STATUS_INVAL);
336 }
337
338
339 static void
bool_true(void)340 bool_true (void)
341 {
342 SANE_Bool value = SANE_TRUE;
343 SANE_Status status;
344 status = sanei_check_value (&bool_opt, &value);
345
346 /* check results */
347 assert (status == SANE_STATUS_GOOD);
348 }
349
350
351 static void
bool_false(void)352 bool_false (void)
353 {
354 SANE_Bool value = SANE_FALSE;
355 SANE_Status status;
356 status = sanei_check_value (&bool_opt, &value);
357
358 /* check results */
359 assert (status == SANE_STATUS_GOOD);
360 }
361
362
363 static void
wrong_bool(void)364 wrong_bool (void)
365 {
366 SANE_Bool value = 2;
367 SANE_Status status;
368 status = sanei_check_value (&bool_opt, &value);
369
370 /* check results */
371 assert (status == SANE_STATUS_INVAL);
372 }
373
374
375 static void
bool_array(void)376 bool_array (void)
377 {
378 SANE_Bool value[ARRAY_SIZE];
379 SANE_Status status;
380 int i;
381 for (i = 0; i < ARRAY_SIZE; i++)
382 value[i] = i % 2;
383 status = sanei_check_value (&bool_array_opt, &value);
384
385 /* check results */
386 assert (status == SANE_STATUS_GOOD);
387 }
388
389
390 static void
word_array_ok(void)391 word_array_ok (void)
392 {
393 SANE_Word value = 400;
394 SANE_Status status;
395 status = sanei_check_value (&word_array_opt, &value);
396
397 /* check results */
398 assert (status == SANE_STATUS_GOOD);
399 }
400
401
402 static void
word_array_nok(void)403 word_array_nok (void)
404 {
405 SANE_Word value = 444;
406 SANE_Status status;
407 status = sanei_check_value (&word_array_opt, &value);
408
409 /* check results */
410 assert (status == SANE_STATUS_INVAL);
411 }
412
413 static void
wrong_bool_array(void)414 wrong_bool_array (void)
415 {
416 SANE_Bool value[ARRAY_SIZE];
417 SANE_Status status;
418 int i;
419 for (i = 0; i < ARRAY_SIZE; i++)
420 value[i] = i % 2;
421 value[3] = 4;
422 status = sanei_check_value (&bool_array_opt, &value);
423
424 /* check results */
425 assert (status == SANE_STATUS_INVAL);
426 }
427
428
429 /**
430 * run the test suite for sanei_check_value related tests
431 */
432 static void
sanei_check_suite(void)433 sanei_check_suite (void)
434 {
435 /* to be compatible with pre-C99 compilers */
436 int_opt.constraint.range = &int_range;
437 array_opt.constraint.range = &int_range;
438 word_array_opt.constraint.word_list = dpi_list;
439
440 /* tests for constrained int value */
441 min_int_value ();
442 max_int_value ();
443 below_min_int_value ();
444 above_max_int_value ();
445 quant1_int_value ();
446 quant2_int_value ();
447 in_range_int_value ();
448
449 /* tests for constrained int array */
450 min_int_array ();
451 max_int_array ();
452 below_min_int_array ();
453 above_max_int_array ();
454 quant1_int_array ();
455 quant2_int_array ();
456 in_range_int_array ();
457
458 /* tests for boolean value */
459 bool_true ();
460 bool_false ();
461 wrong_bool ();
462 bool_array ();
463 wrong_bool_array ();
464
465 /* word array test */
466 word_array_ok ();
467 word_array_nok ();
468 }
469
470
471 int
main(void)472 main (void)
473 {
474 sanei_check_suite ();
475 return 0;
476 }
477
478 /* vim: set sw=2 cino=>2se-1sn-1s{s^-1st0(0u0 smarttab expandtab: */
479