blob: 43b34387af59f1f98d2ec487cba55803b9e2ac8f [file] [log] [blame]
Willy Tarreaub6b3f662017-08-09 23:36:48 +02001/* ist.c: test code for ist.h
2 *
3 * Build with :
4 * gcc -Iinclude -Wall -W -fomit-frame-pointer -Os tests/ist.c
5 * gcc -Iinclude -Wall -W -fomit-frame-pointer -O1 tests/ist.c
6 * gcc -Iinclude -Wall -W -fomit-frame-pointer -O2 tests/ist.c
7 * gcc -Iinclude -Wall -W -fomit-frame-pointer -O3 tests/ist.c
8 */
9
10#include <stdio.h>
11#include <stdlib.h>
Willy Tarreau38ceb552021-04-02 10:33:38 +020012#include <import/ist.h>
Willy Tarreaub6b3f662017-08-09 23:36:48 +020013
14
15// pre-extracted from ist.h using the following expression :
16// sed -n '/^static inline/s:^\([^ ]\+\) \([^ ]\+\) \(.*[* ]\)\([^* ]\+(\)\(.*\):\3f_\4\5 { return \4); }\nstatic int test_\4)\n{\n\treturn 0;\n}\n:p' include/common/ist.h
17// sed -n '/^static inline/s:^\([^ ]\+\) \([^ ]\+\) \(.*[* ]\)\([^* ]\+(\)\(.*\):\tif (test_\4)) printf("\4)\\n");:p' include/common/ist.h
18// sed -n '/^static inline/s:^\([^ ]\+\) \([^ ]\+\) \(.*[* ]\)\([^* ]\+(\)\(.*\):\tprintf("%4d \4)\\n", test_\4));:p' include/common/ist.h
19
20struct ist f_ist(const void *str) { return ist(str); }
21static int test_ist()
22{
23 if (ist("foo").ptr == NULL)
24 return __LINE__;
25 if (ist("foo").len != 3)
26 return __LINE__;
27 if (strncmp(ist("foo").ptr, "foo", 3) != 0)
28 return 3;
29 return 0;
30}
31
32struct ist f_ist2(const void *ptr, size_t len) { return ist2(ptr, len); }
33int test_ist2()
34{
35 if (ist2("foo", 3).ptr == NULL)
36 return __LINE__;
37 if (ist2("foo", 3).len != 3)
38 return __LINE__;
39 if (strncmp(ist2("foo", 3).ptr, "foo", 3) != 0)
40 return __LINE__;
41 return 0;
42}
43
44size_t f_istlen(struct ist ist) { return istlen(ist); }
45int test_istlen()
46{
47 if (istlen(ist("foo")) != 3)
48 return __LINE__;
49 if (istlen(ist("")) != 0)
50 return __LINE__;
51 if (istlen(ist(NULL)) != 0)
52 return __LINE__;
53 return 0;
54}
55
56struct ist f_istnext(struct ist ist) { return istnext(ist); }
57int test_istnext()
58{
59 if (istlen(istnext(ist("foo"))) != 2)
60 return __LINE__;
61 if (strncmp(istnext(ist("foo")).ptr, "oo", 2) != 0)
62 return __LINE__;
63 if (istnext(istnext(istnext(istnext(ist("foo"))))).len != 0)
64 return __LINE__;
65 return 0;
66}
67
68struct ist f_istpad(void *buf, const struct ist ist) { return istpad(buf, ist); }
69int test_istpad()
70{
71 char buf[5] = "xxxxx";
72
73 if (strncmp(istpad(buf, ist("foo")).ptr, "foo", 3) != 0)
74 return __LINE__;
75 if (strncmp(buf, "foo", 3) != 0)
76 return __LINE__;
77 if (buf[3] != 0 || buf[4] != 'x')
78 return __LINE__;
79 return 0;
80}
81
82struct ist f_isttrim(struct ist ist, size_t size) { return isttrim(ist, size); }
83int test_isttrim()
84{
85 if (isttrim(ist("foo"), 5).ptr == NULL)
86 return __LINE__;
87
88 if (isttrim(ist("foo"), 5).len != 3)
89 return __LINE__;
90
91 if (strncmp(isttrim(ist("foo"), 5).ptr, "foo", 3) != 0)
92 return __LINE__;
93
94 if (isttrim(ist("foo"), 2).ptr == NULL)
95 return __LINE__;
96
97 if (isttrim(ist("foo"), 2).len != 2)
98 return __LINE__;
99
100 if (strncmp(isttrim(ist("foo"), 2).ptr, "fo", 2) != 0)
101 return __LINE__;
102
103 return 0;
104}
105
106struct ist f_istzero(struct ist ist, size_t size) { return istzero(ist, size); }
107int test_istzero()
108{
109 char buf[5] = "xxxxx";
110
111 if (istzero(ist2(buf, 5), 10).ptr != buf)
112 return __LINE__;
113
114 if (istzero(ist2(buf, 5), 10).len != 5)
115 return __LINE__;
116
117 if (istzero(ist2(buf, 5), 5).len != 4)
118 return __LINE__;
119
120 if (buf[4] != 0)
121 return __LINE__;
122
123 if (istzero(ist2(buf, 5), 0).len != 0)
124 return __LINE__;
125
126 if (buf[0] == 0)
127 return __LINE__;
128
129 return 0;
130}
131
132int f_istdiff(const struct ist ist1, const struct ist ist2) { return istdiff(ist1, ist2); }
133int test_istdiff()
134{
135 if (istdiff(ist(""), ist("")) != 0)
136 return __LINE__;
137
138 if (istdiff(ist("bar"), ist("bar")) != 0)
139 return __LINE__;
140
141 if (istdiff(ist("foo"), ist("")) <= 0)
142 return __LINE__;
143
144 if (istdiff(ist(""), ist("bar")) >= 0)
145 return __LINE__;
146
147 if (istdiff(ist("foo"), ist("bar")) <= 0)
148 return __LINE__;
149
150 if (istdiff(ist("fo"), ist("bar")) <= 0)
151 return __LINE__;
152
153 if (istdiff(ist("bar"), ist("foo")) >= 0)
154 return __LINE__;
155
156 if (istdiff(ist("bar"), ist("fo")) >= 0)
157 return __LINE__;
158
159 return 0;
160}
161
162int f_istmatch(const struct ist ist1, const struct ist ist2) { return istmatch(ist1, ist2); }
163int test_istmatch()
164{
165 if (istmatch(ist(""), ist("")) == 0)
166 return __LINE__;
167
168 if (istmatch(ist("bar"), ist("bar")) == 0)
169 return __LINE__;
170
171 if (istmatch(ist("foo"), ist("")) == 0)
172 return __LINE__;
173
174 if (istmatch(ist(""), ist("bar")) != 0)
175 return __LINE__;
176
177 if (istmatch(ist("foo"), ist("bar")) != 0)
178 return __LINE__;
179
180 if (istmatch(ist("fo"), ist("bar")) != 0)
181 return __LINE__;
182
183 if (istmatch(ist("bar"), ist("foo")) != 0)
184 return __LINE__;
185
186 if (istmatch(ist("bar"), ist("fo")) != 0)
187 return __LINE__;
188
189 if (istmatch(ist("foo"), ist("foobar")) != 0)
190 return __LINE__;
191
192 if (istmatch(ist("foobar"), ist("foo")) == 0)
193 return __LINE__;
194
195 if (istmatch(ist("foobar"), ist("bar")) != 0)
196 return __LINE__;
197
198 return 0;
199}
200
201int f_istnmatch(struct ist ist1, struct ist ist2, size_t count) { return istnmatch(ist1, ist2, count); }
202int test_istnmatch()
203{
204 if (istnmatch(ist(""), ist(""), 1) == 0)
205 return __LINE__;
206
207 if (istnmatch(ist(""), ist(""), 0) == 0)
208 return __LINE__;
209
210 if (istnmatch(ist("bar"), ist("bar"), 4) == 0)
211 return __LINE__;
212
213 if (istnmatch(ist("bar"), ist("bar"), 2) == 0)
214 return __LINE__;
215
216 if (istnmatch(ist("bar"), ist("baz"), 2) == 0)
217 return __LINE__;
218
219 if (istnmatch(ist("foo"), ist(""), 1) == 0)
220 return __LINE__;
221
222 if (istnmatch(ist("foo"), ist(""), 0) == 0)
223 return __LINE__;
224
225 if (istnmatch(ist(""), ist("bar"), 3) != 0)
226 return __LINE__;
227
228 if (istnmatch(ist(""), ist("bar"), 0) == 0)
229 return __LINE__;
230
231 if (istnmatch(ist("foo"), ist("bar"), 4) != 0)
232 return __LINE__;
233
234 if (istnmatch(ist("foo"), ist("bar"), 0) == 0)
235 return __LINE__;
236
237 if (istnmatch(ist("fo"), ist("bar"), 2) != 0)
238 return __LINE__;
239
240 if (istnmatch(ist("bar"), ist("foo"), 3) != 0)
241 return __LINE__;
242
243 if (istnmatch(ist("bar"), ist("fo"), 2) != 0)
244 return __LINE__;
245
246 if (istnmatch(ist("foo"), ist("foobar"), 4) != 0)
247 return __LINE__;
248
249 if (istnmatch(ist("foo"), ist("foobar"), 3) == 0)
250 return __LINE__;
251
252 if (istnmatch(ist("foobar"), ist("fooz"), 4) != 0)
253 return __LINE__;
254
255 if (istnmatch(ist("foobar"), ist("fooz"), 3) == 0)
256 return __LINE__;
257
258 if (istnmatch(ist("foobar"), ist("fooz"), 2) == 0)
259 return __LINE__;
260
261 if (istnmatch(ist("foobar"), ist("bar"), 3) != 0)
262 return __LINE__;
263
264 return 0;
265}
266
267int f_isteq(const struct ist ist1, const struct ist ist2) { return isteq(ist1, ist2); }
268int test_isteq()
269{
270 if (isteq(ist(""), ist("")) == 0)
271 return __LINE__;
272
273 if (isteq(ist("bar"), ist("bar")) == 0)
274 return __LINE__;
275
276 if (isteq(ist("foo"), ist("")) != 0)
277 return __LINE__;
278
279 if (isteq(ist(""), ist("bar")) != 0)
280 return __LINE__;
281
282 if (isteq(ist("foo"), ist("bar")) != 0)
283 return __LINE__;
284
285 if (isteq(ist("fo"), ist("bar")) != 0)
286 return __LINE__;
287
288 if (isteq(ist("bar"), ist("foo")) != 0)
289 return __LINE__;
290
291 if (isteq(ist("bar"), ist("fo")) != 0)
292 return __LINE__;
293
294 if (isteq(ist("foo"), ist("foobar")) != 0)
295 return __LINE__;
296
297 if (isteq(ist("foobar"), ist("foo")) != 0)
298 return __LINE__;
299
300 if (isteq(ist("foobar"), ist("bar")) != 0)
301 return __LINE__;
302
303 return 0;
304}
305
306int f_istneq(struct ist ist1, struct ist ist2, size_t count) { return istneq(ist1, ist2, count); }
307int test_istneq()
308{
309 if (istneq(ist(""), ist(""), 1) == 0)
310 return __LINE__;
311
312 if (istneq(ist(""), ist(""), 0) == 0)
313 return __LINE__;
314
315 if (istneq(ist("bar"), ist("bar"), 4) == 0)
316 return __LINE__;
317
318 if (istneq(ist("bar"), ist("bar"), 2) == 0)
319 return __LINE__;
320
321 if (istneq(ist("bar"), ist("baz"), 2) == 0)
322 return __LINE__;
323
324 if (istneq(ist("foo"), ist(""), 1) != 0)
325 return __LINE__;
326
327 if (istneq(ist("foo"), ist(""), 0) == 0)
328 return __LINE__;
329
330 if (istneq(ist(""), ist("bar"), 3) != 0)
331 return __LINE__;
332
333 if (istneq(ist(""), ist("bar"), 0) == 0)
334 return __LINE__;
335
336 if (istneq(ist("foo"), ist("bar"), 4) != 0)
337 return __LINE__;
338
339 if (istneq(ist("foo"), ist("bar"), 0) == 0)
340 return __LINE__;
341
342 if (istneq(ist("fo"), ist("bar"), 2) != 0)
343 return __LINE__;
344
345 if (istneq(ist("bar"), ist("foo"), 3) != 0)
346 return __LINE__;
347
348 if (istneq(ist("bar"), ist("fo"), 2) != 0)
349 return __LINE__;
350
351 if (istneq(ist("foo"), ist("foobar"), 4) != 0)
352 return __LINE__;
353
354 if (istneq(ist("foo"), ist("foobar"), 3) == 0)
355 return __LINE__;
356
357 if (istneq(ist("foobar"), ist("fooz"), 4) != 0)
358 return __LINE__;
359
360 if (istneq(ist("foobar"), ist("fooz"), 3) == 0)
361 return __LINE__;
362
363 if (istneq(ist("foobar"), ist("fooz"), 2) == 0)
364 return __LINE__;
365
366 if (istneq(ist("foobar"), ist("bar"), 3) != 0)
367 return __LINE__;
368
369 return 0;
370}
371
372ssize_t f_istcpy(struct ist *dst, const struct ist src, size_t count) { return istcpy(dst, src, count); }
373int test_istcpy()
374{
375 char buf[100] = "foobar";
376 struct ist dst = ist(buf);
377
378 if (istcpy(&dst, ist("FOO"), sizeof(buf)) != 3)
379 return __LINE__;
380
381 if (dst.len != 3)
382 return __LINE__;
383
384 if (strcmp(buf, "FOObar") != 0)
385 return __LINE__;
386
387 if (istcpy(&dst, ist("foo"), 2) != -1)
388 return __LINE__;
389
390 if (strcmp(buf, "foObar") != 0)
391 return __LINE__;
392
393 if (istcpy(&dst, ist("foo"), 3) != 3)
394 return __LINE__;
395
396 if (strcmp(buf, "foobar") != 0)
397 return __LINE__;
398
399 return 0;
400}
401
402ssize_t f_istscpy(struct ist *dst, const struct ist src, size_t count) { return istscpy(dst, src, count); }
403int test_istscpy()
404{
405 char buf[100] = "foobar";
406 struct ist dst = ist(buf);
407
408 if (istscpy(&dst, ist("FOO"), sizeof(buf)) != 3)
409 return __LINE__;
410
411 if (dst.len != 3)
412 return __LINE__;
413
414 if (memcmp(buf, "FOO\0ar", 6) != 0)
415 return __LINE__;
416
417 if (istscpy(&dst, ist("foo"), 3) != -1)
418 return __LINE__;
419
420 if (memcmp(buf, "fo\0\0ar", 6) != 0)
421 return __LINE__;
422
423 if (istscpy(&dst, ist("foo"), 3) != -1)
424 return __LINE__;
425
426 if (istscpy(&dst, ist("foo"), 4) != 3)
427 return __LINE__;
428
429 if (memcmp(buf, "foo\0ar", 6) != 0)
430 return __LINE__;
431
432 return 0;
433}
434
435ssize_t f_istcat(struct ist *dst, const struct ist src, size_t count) { return istcat(dst, src, count); }
436int test_istcat()
437{
438 char buf[11] = "foobar";
439 struct ist dst = ist(buf);
440
441 if (istcat(&dst, ist("FOO"), sizeof(buf)) != 9)
442 return __LINE__;
443
444 if (strcmp(buf, "foobarFOO") != 0)
445 return __LINE__;
446
447 if (istcat(&dst, ist("foo"), 10) != -1)
448 return __LINE__;
449
450 if (dst.len != 10)
451 return __LINE__;
452
453 if (strncmp(buf, "foobarFOOf", 10) != 0)
454 return __LINE__;
455
456 if (istcat(&dst, ist("foo"), 3) != -1)
457 return __LINE__;
458
459 if (dst.len != 10)
460 return __LINE__;
461
462 if (strncmp(buf, "foobar", 6) != 0)
463 return __LINE__;
464
465 return 0;
466}
467
468ssize_t f_istscat(struct ist *dst, const struct ist src, size_t count) { return istscat(dst, src, count); }
469int test_istscat()
470{
471 char buf[11] = "foobar";
472 struct ist dst = ist(buf);
473
474 if (istscat(&dst, ist("FOO"), sizeof(buf)) != 9)
475 return __LINE__;
476
477 if (strcmp(buf, "foobarFOO") != 0)
478 return __LINE__;
479
480 if (istscat(&dst, ist("foo"), sizeof(buf)) != -1)
481 return __LINE__;
482
483 if (dst.len != 10)
484 return __LINE__;
485
486 if (strncmp(buf, "foobarFOOf", 10) != 0)
487 return __LINE__;
488
489 if (istscat(&dst, ist("foo"), 3) != -1)
490 return __LINE__;
491
492 if (dst.len != 10)
493 return __LINE__;
494
495 if (strncmp(buf, "foobar", 6) != 0)
496 return __LINE__;
497
498 return 0;
499}
500
501char *f_istchr(const struct ist ist, char chr) { return istchr(ist, chr); }
502int test_istchr()
503{
504 struct ist foobar = ist("foobar");
505
506 if (istchr(foobar, 'f') != foobar.ptr)
507 return __LINE__;
508
509 if (istchr(foobar, 'o') != foobar.ptr + 1)
510 return __LINE__;
511
512 if (istchr(foobar, 'r') != foobar.ptr + 5)
513 return __LINE__;
514
515 if (istchr(foobar, 'X') != NULL)
516 return __LINE__;
517
518 if (istchr(foobar, 0) != NULL)
519 return __LINE__;
520
521 return 0;
522}
523
524struct ist f_istfind(struct ist ist, char chr) { return istfind(ist, chr); }
525int test_istfind()
526{
527 struct ist foobar = ist("foobar");
528
529 if (istfind(foobar, 'f').ptr != foobar.ptr)
530 return __LINE__;
531
532 if (istfind(foobar, 'f').len != 6)
533 return __LINE__;
534
535 if (istfind(foobar, 'o').ptr != foobar.ptr + 1)
536 return __LINE__;
537
538 if (istfind(foobar, 'o').len != 5)
539 return __LINE__;
540
541 if (istfind(foobar, 'r').ptr != foobar.ptr + 5)
542 return __LINE__;
543
544 if (istfind(foobar, 'r').len != 1)
545 return __LINE__;
546
547 if (istfind(foobar, 'X').ptr != foobar.ptr + foobar.len)
548 return __LINE__;
549
550 if (istfind(foobar, 'X').len != 0)
551 return __LINE__;
552
553 if (istfind(foobar, 0).ptr != foobar.ptr + foobar.len)
554 return __LINE__;
555
556 if (istfind(foobar, 0).len != 0)
557 return __LINE__;
558
559 return 0;
560}
561
562struct ist f_istskip(struct ist ist, char chr) { return istskip(ist, chr); }
563int test_istskip()
564{
565 struct ist foobar = ist("foobar");
566 struct ist r = ist("r");
567
568 if (istskip(foobar, 'X').ptr != foobar.ptr)
569 return __LINE__;
570
571 if (istskip(foobar, 'X').len != foobar.len)
572 return __LINE__;
573
574 if (istskip(foobar, 'o').ptr != foobar.ptr)
575 return __LINE__;
576
577 if (istskip(foobar, 'o').len != foobar.len)
578 return __LINE__;
579
580 if (istskip(foobar, 'f').ptr != foobar.ptr + 1)
581 return __LINE__;
582
583 if (istskip(foobar, 'f').len != foobar.len - 1)
584 return __LINE__;
585
586 if (istskip(r, 'r').ptr != r.ptr + 1)
587 return __LINE__;
588
589 if (istskip(r, 'r').len != r.len - 1)
590 return __LINE__;
591
592 if (istskip(foobar, 'X').ptr != foobar.ptr)
593 return __LINE__;
594
595 if (istskip(foobar, 'X').len != foobar.len)
596 return __LINE__;
597
598 if (istskip(r, 0).ptr != r.ptr)
599 return __LINE__;
600
601 if (istskip(r, 0).len != r.len)
602 return __LINE__;
603
604 return 0;
605}
606
607struct ist f_istist(struct ist ist, const struct ist pat) { return istist(ist, pat); }
608int test_istist()
609{
610 struct ist foobar = ist("foobar");
611
612 if (istist(foobar, ist("f")).ptr != foobar.ptr)
613 return __LINE__;
614
615 if (istist(foobar, ist("f")).len != foobar.len)
616 return __LINE__;
617
618 if (istist(foobar, ist("foob")).ptr != foobar.ptr)
619 return __LINE__;
620
621 if (istist(foobar, ist("foob")).len != foobar.len)
622 return __LINE__;
623
624 if (istist(foobar, ist("foobar")).ptr != foobar.ptr)
625 return __LINE__;
626
627 if (istist(foobar, ist("foobar")).len != foobar.len)
628 return __LINE__;
629
630 if (istist(foobar, ist("o")).ptr != foobar.ptr + 1)
631 return __LINE__;
632
633 if (istist(foobar, ist("o")).len != foobar.len - 1)
634 return __LINE__;
635
636 if (istist(foobar, ist("ooba")).ptr != foobar.ptr + 1)
637 return __LINE__;
638
639 if (istist(foobar, ist("ooba")).len != foobar.len - 1)
640 return __LINE__;
641
642 if (istist(foobar, ist("r")).ptr != foobar.ptr + 5)
643 return __LINE__;
644
645 if (istist(foobar, ist("r")).len != foobar.len - 5)
646 return __LINE__;
647
648 if (istist(foobar, ist("X")).ptr != NULL)
649 return __LINE__;
650
651 if (istist(foobar, ist("X")).len != 0)
652 return __LINE__;
653
654 if (istist(foobar, ist("oobaX")).ptr != NULL)
655 return __LINE__;
656
657 if (istist(foobar, ist("oobaX")).len != 0)
658 return __LINE__;
659
660 if (istist(foobar, ist("oobarX")).ptr != NULL)
661 return __LINE__;
662
663 if (istist(foobar, ist("oobarX")).len != 0)
664 return __LINE__;
665
666 if (istist(foobar, ist("")).ptr != foobar.ptr)
667 return __LINE__;
668
669 if (istist(foobar, ist("")).len != foobar.len)
670 return __LINE__;
671
672 return 0;
673}
674
675
676int main(void)
677{
678 printf("%4d ist()\n", test_ist());
679 printf("%4d ist2()\n", test_ist2());
680 printf("%4d istlen()\n", test_istlen());
681 printf("%4d istnext()\n", test_istnext());
682 printf("%4d istpad()\n", test_istpad());
683 printf("%4d isttrim()\n", test_isttrim());
684 printf("%4d istzero()\n", test_istzero());
685 printf("%4d istdiff()\n", test_istdiff());
686 printf("%4d istmatch()\n", test_istmatch());
687 printf("%4d istnmatch()\n", test_istnmatch());
688 printf("%4d isteq()\n", test_isteq());
689 printf("%4d istneq()\n", test_istneq());
690 printf("%4d istcpy()\n", test_istcpy());
691 printf("%4d istscpy()\n", test_istscpy());
692 printf("%4d istcat()\n", test_istcat());
693 printf("%4d istscat()\n", test_istscat());
694 printf("%4d istchr()\n", test_istchr());
695 printf("%4d istfind()\n", test_istfind());
696 printf("%4d istskip()\n", test_istskip());
697 printf("%4d istist()\n", test_istist());
698
699 return 0;
700}