123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- // SPDX-License-Identifier: GPL-2.0 or GPL-3.0
- // Copyright © 2018-2019 Ariadne Devos
- /* sHT -- test the sHT_memeq function */
- #include <sHT/string.h>
- #include <string.h>
- #include <stdio.h>
- /* Test a few special cases -- code that uses memeq and streq must come
- with their own tests, which supplements these.
- Symmetric cases are automatically generated, and the expected values are
- scrambled. */
- static const struct {
- const char *a;
- const char *b;
- size_t length;
- size_t expect;
- } cases[] = {
- /* It should not be accessed, because that's unnecessary
- -- it is allowed, however. */
- { .a = (char *) -1, .b = (char *) -2, .length = 0, .expect = 0 },
- /* zero-length */
- { .a = "", .b = "a", .length = 0, .expect = 0},
- /* don't be afraid of NUL */
- { .a = "", .b = "a", .length = 1, .expect = 1 },
- { .a = "", .b = "", .length = 1, .expect = 0 },
- { .a = "\0a", .b = "\0b", .length = 2, .expect = 1 },
- { .a = "\0a", .b = "\0b\xff", .length = 2, .expect = 1 },
- /* longer test cases -- exercise word-at-a-time implementations a little */
- { .a = "abcdefg\x00""abcdefg\x11", "abcdefg\x00""abcdefg\x11", .length = 16, .expect = 0 },
- { .a = "abcdefg\x00""abcdefg\x11""0", "abcdefg\x00""abcdefg\x11""0", .length = 17, .expect = 0 },
- { .a = "abcdefg\x00""abcdefg\x11", "abcdefg\x00""abcdefg\x12", .length = 16, .expect = 1 },
- { .a = "abcdefg\x00""abcdefg\x11""0", "abcdefg\x00""abcdefg\x11""1", .length = 17, .expect = 1 },
- };
- int
- main(void)
- {
- _Bool ioerr = 0;
- for (size_t i = 0; i < sizeof(cases)/sizeof(*cases); i++) {
- /* test the test case */
- if (!!memcmp(cases[i].a, cases[i].b, cases[i].length) != cases[i].expect)
- goto fail_testcase;
- if (sHT_memeq(cases[i].a, cases[i].b, 0, 1, cases[i].length) != cases[i].expect)
- goto fail;
- if (sHT_memeq(cases[i].b, cases[i].a, 0, 1, cases[i].length) != cases[i].expect)
- goto fail;
- if (sHT_memeq(cases[i].a, cases[i].b, 2000, 78, cases[i].length) != (cases[i].expect ? 78 : 2000))
- goto fail;
- if (sHT_memeq(cases[i].b, cases[i].a, 2000, 78, cases[i].length) != (cases[i].expect ? 78 : 2000))
- goto fail;
- }
- ioerr |= puts("PASS: memeq/special") < 0;
- ioerr |= puts("PASS: memeq/equi-stdlib") < 0;
- return ioerr ? 2 : 0;
- fail:
- ioerr |= puts("FAIL: memeq/special") < 0;
- ioerr |= puts("SKIP: memeq/equi-stdlib") < 0;
- return ioerr ? 2 : 1;
- fail_testcase:
- ioerr |= puts("FAIL: memeq/equi-stdlib") < 0;
- ioerr |= puts("SKIP: memeq/special") < 0;
- return ioerr ? 2 : 1;
- }
|