123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320 |
- // SPDX-License-Identifier: GPL-2.0 or GPL-3.0
- // Copyright © 2019 Ariadne Devos
- #include <limits.h>
- #include <stddef.h>
- #include <stdint.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/types.h>
- #include <sHT/string.h>
- #include <sHT/test.h>
- #include <sHT/logic/failbit.h>
- /* Test arithmetic and logical operators.
- No mutation or loops are present. */
- /* Some positive, negative,
- some even, some odd (x86),
- a bit here and there,
- and the same about their differences
- (cmp: A <- B - C) */
- static const size_t interesting[] = {
- 0,
- 1,
- 2,
- 3,
- SIZE_MAX/3 - 3,
- SIZE_MAX/3 - 2,
- SIZE_MAX/3 - 1,
- SIZE_MAX/3,
- SIZE_MAX/3 + 1,
- SIZE_MAX/3 + 2,
- SIZE_MAX/3 + 3,
- SIZE_MAX/2 - 3,
- SIZE_MAX/2 - 2,
- SIZE_MAX/2 - 1,
- SIZE_MAX/2,
- SIZE_MAX/2 + 1,
- SIZE_MAX/2 + 2,
- SIZE_MAX/2 + 3,
- 2 * (SIZE_MAX/3) - 3,
- 2 * (SIZE_MAX/3) - 2,
- 2 * (SIZE_MAX/3) - 1,
- 2 * (SIZE_MAX/3),
- 2 * (SIZE_MAX/3) + 1,
- 2 * (SIZE_MAX/3) + 2,
- 2 * (SIZE_MAX/3) + 3,
- SIZE_MAX - 3,
- SIZE_MAX - 2,
- SIZE_MAX - 1,
- SIZE_MAX - 0,
- };
- #define N (sizeof(interesting)/sizeof(size_t))
- static int ret = 0;
- static void
- fail(const char *fun, size_t a, size_t b)
- {
- if (printf("FAIL: test/%s (%zu, %zu)\n", fun, a, b) < 0)
- exit(2);
- ret = 1;
- }
- static void
- pass(const char *fun)
- {
- if (printf("PASS: test/%s\n", fun) < 0)
- exit(2);
- }
- /* test different immediate modes (x86, x86-64, in this case) */
- #define beef0 0xde
- #define beef1 0xdead
- #if UINT32_MAX && (SIZE_MAX >= UINT32_MAX)
- # define beef2 0xdeadbe
- # define beef3 0xdeadbeef
- #else
- # define beef2 beef0
- # define beef3 beef1
- #endif
- #if UINT64_MAX && (SIZE_MAX >= UINT64_MAX)
- # define beef4 0xdeadbeefdeafca
- # define beef5 0xdeadbeefdeafcafe
- #else
- # define beef4 beef2
- # define beef5 beef3
- #endif
- /* Don't do sHT_index_iterate here, as that would be
- a rather cyclic test. */
- #define test_op(fun, check) \
- for (unsigned i = 0; i < N; i++) { \
- for (unsigned j = 0; j < N; j++) { \
- size_t a = interesting[i]; \
- size_t b = interesting[j]; \
- /* register / register */ \
- if (!(check)) { \
- fail(#fun, a, b); \
- goto nexttest_##fun; \
- } \
- } \
- /* register / immediate (32 / 64) */ \
- size_t a = interesting[i]; \
- size_t b = beef0; \
- if (!(check)) \
- fail(#fun, a, b); \
- b = beef1; \
- if (!(check)) \
- fail(#fun, a, b); \
- b = beef2; \
- if (!(check)) \
- fail(#fun, a, b); \
- b = beef3; \
- if (!(check)) \
- fail(#fun, a, b); \
- b = beef4; \
- if (!(check)) \
- fail(#fun, a, b); \
- b = beef5; \
- if (!(check)) \
- fail(#fun, a, b); \
- } \
- pass(#fun); \
- nexttest_##fun: \
- #define test_unary(fun) test_op(fun, fun(a) == 1)
- #define test_op1(fun, op) test_op(fun, sHT_##fun(a) == (a op))
- #define test_op1_bool(fun, op) test_op(fun, !!sHT_##fun(a) == !!((a) op))
- #define test_op1_signedbool(fun, op) test_op(fun, !!sHT_##fun((ssize_t) a) == !!(((ssize_t) a) op))
- #define test_op2(fun, op) test_op(fun, sHT_##fun(a, b) == (a op b))
- #define test_op2_bool(fun, op) test_op(fun, !!sHT_##fun(a, b) == !!(a op b))
- #define test_op2_negatebool(fun, op) test_op(fun, !!sHT_##fun(a, b) == !!!(a op b))
- #define test_fun2(fun) test_op(fun, sHT_##fun(a, b) == ref_##fun(a, b))
- /* TODO: test performance (static branch prediction) */
- static size_t
- ref_min_size(size_t a, size_t b)
- {
- if (a > b)
- return b;
- return a;
- }
- static _Bool
- failbit_goodbad(size_t a)
- {
- return !!sHT_good(a) == !sHT_bad(a);
- }
- static _Bool
- failbit_success_good(size_t a)
- {
- /* Fails precondition, discard case */
- if (a >= sHT_failbit_limit)
- return 1;
- return sHT_good(sHT_success(a));
- }
- static _Bool
- failbit_fail_bad(size_t a)
- {
- /* Fails precondition, discard case */
- if (a >= sHT_failbit_limit)
- return 1;
- return sHT_bad(sHT_fail(a));
- }
- static _Bool
- failbit_0failif1_bad(size_t a)
- {
- /* Fails precondition, discard case */
- if (a >= sHT_failbit_limit)
- return 1;
- a = sHT_success(a);
- sHT_fail_if(&a, 1);
- return sHT_bad(a);
- }
- static _Bool
- failbit_1failif1_bad(size_t a)
- {
- /* Fails precondition, discard case */
- if (a >= sHT_failbit_limit)
- return 1;
- a = sHT_fail(a);
- sHT_fail_if(&a, 1);
- return sHT_bad(a);
- }
- static _Bool
- failbit_0failif0_good(size_t a)
- {
- /* Fails precondition, discard case */
- if (a >= sHT_failbit_limit)
- return 1;
- a = sHT_success(a);
- sHT_fail_if(&a, 0);
- return sHT_good(a);
- }
- /* += won't work */
- static _Bool
- failbit_1failif0_bad(size_t a)
- {
- /* Fails precondition, discard case */
- if (a >= sHT_failbit_limit)
- return 1;
- a = sHT_fail(a);
- sHT_fail_if(&a, 0);
- return sHT_bad(a);
- }
- static _Bool
- failbit_success_preserves_value(size_t a)
- {
- /* Fails precondition, discard case */
- if (a >= sHT_failbit_limit)
- return 1;
- return sHT_fail_value(sHT_success(a)) == a;
- }
- static _Bool
- failbit_fail_preserves_value(size_t a)
- {
- /* Fails precondition, discard case */
- if (a >= sHT_failbit_limit)
- return 1;
- return sHT_fail_value(sHT_fail(a)) == a;
- }
- static _Bool
- failbit_0failif0_preserves_value(size_t a)
- {
- /* Fails precondition, discard case */
- if (a >= sHT_failbit_limit)
- return 1;
- size_t b = sHT_success(a);
- sHT_fail_if(&b, 0);
- return sHT_fail_value(b) == a;
- }
- static _Bool
- failbit_1failif0_preserves_value(size_t a)
- {
- /* Fails precondition, discard case */
- if (a >= sHT_failbit_limit)
- return 1;
- size_t b = sHT_fail(a);
- sHT_fail_if(&b, 0);
- return sHT_fail_value(b) == a;
- }
- static _Bool
- failbit_0failif1_preserves_value(size_t a)
- {
- /* Fails precondition, discard case */
- if (a >= sHT_failbit_limit)
- return 1;
- size_t b = sHT_success(a);
- sHT_fail_if(&b, 1);
- return sHT_fail_value(b) == a;
- }
- static _Bool
- failbit_1failif1_preserves_value(size_t a)
- {
- /* Fails precondition, discard case */
- if (a >= sHT_failbit_limit)
- return 1;
- size_t b = sHT_fail(a);
- sHT_fail_if(&b, 1);
- return sHT_fail_value(b) == a;
- }
- int
- main(void)
- {
- test_op2_bool(lt, <);
- test_op2_bool(gt, >);
- test_op2_bool(ge, >=);
- test_op2_bool(eq, ==);
- test_op2_bool(eq_bool, ==);
- test_op2_bool(neq, !=);
- test_op1_signedbool(lt0, < 0);
- test_op1_bool(zero_p, == 0);
- test_op1_bool(nonzero_p, != 0);
- test_op2_bool(and_any, &);
- test_op2_negatebool(and_none, &);
- test_fun2(min_size);
- test_unary(failbit_goodbad);
- test_unary(failbit_success_good);
- test_unary(failbit_fail_bad);
- test_unary(failbit_0failif1_bad);
- test_unary(failbit_0failif0_good);
- test_unary(failbit_1failif1_bad);
- test_unary(failbit_1failif0_bad);
- test_unary(failbit_success_preserves_value);
- test_unary(failbit_fail_preserves_value);
- test_unary(failbit_0failif0_preserves_value);
- test_unary(failbit_0failif1_preserves_value);
- test_unary(failbit_1failif0_preserves_value);
- test_unary(failbit_1failif1_preserves_value);
- return ret;
- }
|