cmp.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. // SPDX-License-Identifier: GPL-2.0 or GPL-3.0
  2. // Copyright © 2019 Ariadne Devos
  3. #include <limits.h>
  4. #include <stddef.h>
  5. #include <stdint.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <sys/types.h>
  9. #include <sHT/string.h>
  10. #include <sHT/test.h>
  11. #include <sHT/logic/failbit.h>
  12. /* Test arithmetic and logical operators.
  13. No mutation or loops are present. */
  14. /* Some positive, negative,
  15. some even, some odd (x86),
  16. a bit here and there,
  17. and the same about their differences
  18. (cmp: A <- B - C) */
  19. static const size_t interesting[] = {
  20. 0,
  21. 1,
  22. 2,
  23. 3,
  24. SIZE_MAX/3 - 3,
  25. SIZE_MAX/3 - 2,
  26. SIZE_MAX/3 - 1,
  27. SIZE_MAX/3,
  28. SIZE_MAX/3 + 1,
  29. SIZE_MAX/3 + 2,
  30. SIZE_MAX/3 + 3,
  31. SIZE_MAX/2 - 3,
  32. SIZE_MAX/2 - 2,
  33. SIZE_MAX/2 - 1,
  34. SIZE_MAX/2,
  35. SIZE_MAX/2 + 1,
  36. SIZE_MAX/2 + 2,
  37. SIZE_MAX/2 + 3,
  38. 2 * (SIZE_MAX/3) - 3,
  39. 2 * (SIZE_MAX/3) - 2,
  40. 2 * (SIZE_MAX/3) - 1,
  41. 2 * (SIZE_MAX/3),
  42. 2 * (SIZE_MAX/3) + 1,
  43. 2 * (SIZE_MAX/3) + 2,
  44. 2 * (SIZE_MAX/3) + 3,
  45. SIZE_MAX - 3,
  46. SIZE_MAX - 2,
  47. SIZE_MAX - 1,
  48. SIZE_MAX - 0,
  49. };
  50. #define N (sizeof(interesting)/sizeof(size_t))
  51. static int ret = 0;
  52. static void
  53. fail(const char *fun, size_t a, size_t b)
  54. {
  55. if (printf("FAIL: test/%s (%zu, %zu)\n", fun, a, b) < 0)
  56. exit(2);
  57. ret = 1;
  58. }
  59. static void
  60. pass(const char *fun)
  61. {
  62. if (printf("PASS: test/%s\n", fun) < 0)
  63. exit(2);
  64. }
  65. /* test different immediate modes (x86, x86-64, in this case) */
  66. #define beef0 0xde
  67. #define beef1 0xdead
  68. #if UINT32_MAX && (SIZE_MAX >= UINT32_MAX)
  69. # define beef2 0xdeadbe
  70. # define beef3 0xdeadbeef
  71. #else
  72. # define beef2 beef0
  73. # define beef3 beef1
  74. #endif
  75. #if UINT64_MAX && (SIZE_MAX >= UINT64_MAX)
  76. # define beef4 0xdeadbeefdeafca
  77. # define beef5 0xdeadbeefdeafcafe
  78. #else
  79. # define beef4 beef2
  80. # define beef5 beef3
  81. #endif
  82. /* Don't do sHT_index_iterate here, as that would be
  83. a rather cyclic test. */
  84. #define test_op(fun, check) \
  85. for (unsigned i = 0; i < N; i++) { \
  86. for (unsigned j = 0; j < N; j++) { \
  87. size_t a = interesting[i]; \
  88. size_t b = interesting[j]; \
  89. /* register / register */ \
  90. if (!(check)) { \
  91. fail(#fun, a, b); \
  92. goto nexttest_##fun; \
  93. } \
  94. } \
  95. /* register / immediate (32 / 64) */ \
  96. size_t a = interesting[i]; \
  97. size_t b = beef0; \
  98. if (!(check)) \
  99. fail(#fun, a, b); \
  100. b = beef1; \
  101. if (!(check)) \
  102. fail(#fun, a, b); \
  103. b = beef2; \
  104. if (!(check)) \
  105. fail(#fun, a, b); \
  106. b = beef3; \
  107. if (!(check)) \
  108. fail(#fun, a, b); \
  109. b = beef4; \
  110. if (!(check)) \
  111. fail(#fun, a, b); \
  112. b = beef5; \
  113. if (!(check)) \
  114. fail(#fun, a, b); \
  115. } \
  116. pass(#fun); \
  117. nexttest_##fun: \
  118. #define test_unary(fun) test_op(fun, fun(a) == 1)
  119. #define test_op1(fun, op) test_op(fun, sHT_##fun(a) == (a op))
  120. #define test_op1_bool(fun, op) test_op(fun, !!sHT_##fun(a) == !!((a) op))
  121. #define test_op1_signedbool(fun, op) test_op(fun, !!sHT_##fun((ssize_t) a) == !!(((ssize_t) a) op))
  122. #define test_op2(fun, op) test_op(fun, sHT_##fun(a, b) == (a op b))
  123. #define test_op2_bool(fun, op) test_op(fun, !!sHT_##fun(a, b) == !!(a op b))
  124. #define test_op2_negatebool(fun, op) test_op(fun, !!sHT_##fun(a, b) == !!!(a op b))
  125. #define test_fun2(fun) test_op(fun, sHT_##fun(a, b) == ref_##fun(a, b))
  126. /* TODO: test performance (static branch prediction) */
  127. static size_t
  128. ref_min_size(size_t a, size_t b)
  129. {
  130. if (a > b)
  131. return b;
  132. return a;
  133. }
  134. static _Bool
  135. failbit_goodbad(size_t a)
  136. {
  137. return !!sHT_good(a) == !sHT_bad(a);
  138. }
  139. static _Bool
  140. failbit_success_good(size_t a)
  141. {
  142. /* Fails precondition, discard case */
  143. if (a >= sHT_failbit_limit)
  144. return 1;
  145. return sHT_good(sHT_success(a));
  146. }
  147. static _Bool
  148. failbit_fail_bad(size_t a)
  149. {
  150. /* Fails precondition, discard case */
  151. if (a >= sHT_failbit_limit)
  152. return 1;
  153. return sHT_bad(sHT_fail(a));
  154. }
  155. static _Bool
  156. failbit_0failif1_bad(size_t a)
  157. {
  158. /* Fails precondition, discard case */
  159. if (a >= sHT_failbit_limit)
  160. return 1;
  161. a = sHT_success(a);
  162. sHT_fail_if(&a, 1);
  163. return sHT_bad(a);
  164. }
  165. static _Bool
  166. failbit_1failif1_bad(size_t a)
  167. {
  168. /* Fails precondition, discard case */
  169. if (a >= sHT_failbit_limit)
  170. return 1;
  171. a = sHT_fail(a);
  172. sHT_fail_if(&a, 1);
  173. return sHT_bad(a);
  174. }
  175. static _Bool
  176. failbit_0failif0_good(size_t a)
  177. {
  178. /* Fails precondition, discard case */
  179. if (a >= sHT_failbit_limit)
  180. return 1;
  181. a = sHT_success(a);
  182. sHT_fail_if(&a, 0);
  183. return sHT_good(a);
  184. }
  185. /* += won't work */
  186. static _Bool
  187. failbit_1failif0_bad(size_t a)
  188. {
  189. /* Fails precondition, discard case */
  190. if (a >= sHT_failbit_limit)
  191. return 1;
  192. a = sHT_fail(a);
  193. sHT_fail_if(&a, 0);
  194. return sHT_bad(a);
  195. }
  196. static _Bool
  197. failbit_success_preserves_value(size_t a)
  198. {
  199. /* Fails precondition, discard case */
  200. if (a >= sHT_failbit_limit)
  201. return 1;
  202. return sHT_fail_value(sHT_success(a)) == a;
  203. }
  204. static _Bool
  205. failbit_fail_preserves_value(size_t a)
  206. {
  207. /* Fails precondition, discard case */
  208. if (a >= sHT_failbit_limit)
  209. return 1;
  210. return sHT_fail_value(sHT_fail(a)) == a;
  211. }
  212. static _Bool
  213. failbit_0failif0_preserves_value(size_t a)
  214. {
  215. /* Fails precondition, discard case */
  216. if (a >= sHT_failbit_limit)
  217. return 1;
  218. size_t b = sHT_success(a);
  219. sHT_fail_if(&b, 0);
  220. return sHT_fail_value(b) == a;
  221. }
  222. static _Bool
  223. failbit_1failif0_preserves_value(size_t a)
  224. {
  225. /* Fails precondition, discard case */
  226. if (a >= sHT_failbit_limit)
  227. return 1;
  228. size_t b = sHT_fail(a);
  229. sHT_fail_if(&b, 0);
  230. return sHT_fail_value(b) == a;
  231. }
  232. static _Bool
  233. failbit_0failif1_preserves_value(size_t a)
  234. {
  235. /* Fails precondition, discard case */
  236. if (a >= sHT_failbit_limit)
  237. return 1;
  238. size_t b = sHT_success(a);
  239. sHT_fail_if(&b, 1);
  240. return sHT_fail_value(b) == a;
  241. }
  242. static _Bool
  243. failbit_1failif1_preserves_value(size_t a)
  244. {
  245. /* Fails precondition, discard case */
  246. if (a >= sHT_failbit_limit)
  247. return 1;
  248. size_t b = sHT_fail(a);
  249. sHT_fail_if(&b, 1);
  250. return sHT_fail_value(b) == a;
  251. }
  252. int
  253. main(void)
  254. {
  255. test_op2_bool(lt, <);
  256. test_op2_bool(gt, >);
  257. test_op2_bool(ge, >=);
  258. test_op2_bool(eq, ==);
  259. test_op2_bool(eq_bool, ==);
  260. test_op2_bool(neq, !=);
  261. test_op1_signedbool(lt0, < 0);
  262. test_op1_bool(zero_p, == 0);
  263. test_op1_bool(nonzero_p, != 0);
  264. test_op2_bool(and_any, &);
  265. test_op2_negatebool(and_none, &);
  266. test_fun2(min_size);
  267. test_unary(failbit_goodbad);
  268. test_unary(failbit_success_good);
  269. test_unary(failbit_fail_bad);
  270. test_unary(failbit_0failif1_bad);
  271. test_unary(failbit_0failif0_good);
  272. test_unary(failbit_1failif1_bad);
  273. test_unary(failbit_1failif0_bad);
  274. test_unary(failbit_success_preserves_value);
  275. test_unary(failbit_fail_preserves_value);
  276. test_unary(failbit_0failif0_preserves_value);
  277. test_unary(failbit_0failif1_preserves_value);
  278. test_unary(failbit_1failif0_preserves_value);
  279. test_unary(failbit_1failif1_preserves_value);
  280. return ret;
  281. }