cmp.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  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. /* Test arithmetic and logical operators.
  12. No mutation or loops are present. */
  13. /* Some positive, negative,
  14. some even, some odd (x86),
  15. a bit here and there,
  16. and the same about their differences
  17. (cmp: A <- B - C) */
  18. static const size_t interesting[] = {
  19. 0,
  20. 1,
  21. 2,
  22. 3,
  23. SIZE_MAX/3 - 2,
  24. SIZE_MAX/3 - 1,
  25. SIZE_MAX/3,
  26. SIZE_MAX/3 + 1,
  27. SIZE_MAX/3 + 2,
  28. SIZE_MAX/2 - 2,
  29. SIZE_MAX/2 - 1,
  30. SIZE_MAX/2,
  31. SIZE_MAX/2 + 1,
  32. SIZE_MAX/2 + 2,
  33. 2 * (SIZE_MAX/3) - 2,
  34. 2 * (SIZE_MAX/3) - 1,
  35. 2 * (SIZE_MAX/3),
  36. 2 * (SIZE_MAX/3) + 1,
  37. 2 * (SIZE_MAX/3) + 2,
  38. SIZE_MAX - 3,
  39. SIZE_MAX - 2,
  40. SIZE_MAX - 1,
  41. SIZE_MAX - 0,
  42. };
  43. #define N (sizeof(interesting)/sizeof(size_t))
  44. static int ret = 0;
  45. static void
  46. fail(const char *fun, size_t a, size_t b)
  47. {
  48. /* a and b are unused, but GCC doesn't optimise
  49. them away -- a good thing for debugging */
  50. if (printf("FAIL: test/%s\n", fun, a, b) < 0)
  51. exit(2);
  52. ret = 1;
  53. }
  54. static void
  55. pass(const char *fun)
  56. {
  57. if (printf("PASS: test/%s\n", fun) < 0)
  58. exit(2);
  59. }
  60. /* test different immediate modes (x86, x86-64, in this case) */
  61. #define beef0 0xde
  62. #define beef1 0xdead
  63. #if UINT32_MAX && (SIZE_MAX >= UINT32_MAX)
  64. # define beef2 0xdeadbe
  65. # define beef3 0xdeadbeef
  66. #else
  67. # define beef2 beef0
  68. # define beef3 beef1
  69. #endif
  70. #if UINT64_MAX && (SIZE_MAX >= UINT64_MAX)
  71. # define beef4 0xdeadbeefdeafca
  72. # define beef5 0xdeadbeefdeafcafe
  73. #else
  74. # define beef4 beef2
  75. # define beef5 beef3
  76. #endif
  77. /* Don't do sHT_index_iterate here, as that would be
  78. a rather cyclic test. */
  79. #define test_op(fun, check) \
  80. for (unsigned i = 0; i < N; i++) { \
  81. for (unsigned j = 0; j < N; j++) { \
  82. size_t a = interesting[i]; \
  83. size_t b = interesting[j]; \
  84. /* register / register */ \
  85. if (!(check)) { \
  86. fail(#fun, a, b); \
  87. goto nexttest_##fun; \
  88. } \
  89. } \
  90. /* register / immediate (32 / 64) */ \
  91. size_t a = interesting[i]; \
  92. size_t b = beef0; \
  93. if (!(check)) \
  94. fail(#fun, a, b); \
  95. b = beef1; \
  96. if (!(check)) \
  97. fail(#fun, a, b); \
  98. b = beef2; \
  99. if (!(check)) \
  100. fail(#fun, a, b); \
  101. b = beef3; \
  102. if (!(check)) \
  103. fail(#fun, a, b); \
  104. b = beef4; \
  105. if (!(check)) \
  106. fail(#fun, a, b); \
  107. b = beef5; \
  108. if (!(check)) \
  109. fail(#fun, a, b); \
  110. } \
  111. pass(#fun); \
  112. nexttest_##fun: \
  113. #define test_op1(fun, op) test_op(fun, sHT_##fun(a) == (a op))
  114. #define test_op1_bool(fun, op) test_op(fun, !!sHT_##fun(a) == !!((a) op))
  115. #define test_op1_signedbool(fun, op) test_op(fun, !!sHT_##fun((ssize_t) a) == !!(((ssize_t) a) op))
  116. #define test_op2(fun, op) test_op(fun, sHT_##fun(a, b) == (a op b))
  117. #define test_op2_bool(fun, op) test_op(fun, !!sHT_##fun(a, b) == !!(a op b))
  118. #define test_op2_negatebool(fun, op) test_op(fun, !!sHT_##fun(a, b) == !!!(a op b))
  119. #define test_fun2(fun) test_op(fun, sHT_##fun(a, b) == ref_##fun(a, b))
  120. /* TODO: test performance (static branch prediction) */
  121. static size_t
  122. ref_min_size(size_t a, size_t b)
  123. {
  124. if (a > b)
  125. return b;
  126. return a;
  127. }
  128. int
  129. main(void)
  130. {
  131. test_op2_bool(gt, >);
  132. test_op2_bool(ge, >=);
  133. test_op2_bool(eq, ==);
  134. test_op2_bool(eq_bool, ==);
  135. test_op2_bool(neq, !=);
  136. test_op1_signedbool(lt0, < 0);
  137. test_op1_bool(zero_p, == 0);
  138. test_op1_bool(nonzero_p, != 0);
  139. test_op2_bool(and_any, &);
  140. test_op2_negatebool(and_none, &);
  141. test_fun2(min_size);
  142. return ret;
  143. }