fxp.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. #include "test/jemalloc_test.h"
  2. #include "jemalloc/internal/fxp.h"
  3. static double
  4. fxp2double(fxp_t a) {
  5. double intpart = (double)(a >> 16);
  6. double fracpart = (double)(a & ((1U << 16) - 1)) / (1U << 16);
  7. return intpart + fracpart;
  8. }
  9. /* Is a close to b? */
  10. static bool
  11. double_close(double a, double b) {
  12. /*
  13. * Our implementation doesn't try for precision. Correspondingly, don't
  14. * enforce it too strenuously here; accept values that are close in
  15. * either relative or absolute terms.
  16. */
  17. return fabs(a - b) < 0.01 || fabs(a - b) / a < 0.01;
  18. }
  19. static bool
  20. fxp_close(fxp_t a, fxp_t b) {
  21. return double_close(fxp2double(a), fxp2double(b));
  22. }
  23. static fxp_t
  24. xparse_fxp(const char *str) {
  25. fxp_t result;
  26. bool err = fxp_parse(&result, str, NULL);
  27. assert_false(err, "Invalid fxp string: %s", str);
  28. return result;
  29. }
  30. static void
  31. expect_parse_accurate(const char *str, const char *parse_str) {
  32. double true_val = strtod(str, NULL);
  33. fxp_t fxp_val;
  34. char *end;
  35. bool err = fxp_parse(&fxp_val, parse_str, &end);
  36. expect_false(err, "Unexpected parse failure");
  37. expect_ptr_eq(parse_str + strlen(str), end,
  38. "Didn't parse whole string");
  39. expect_true(double_close(fxp2double(fxp_val), true_val),
  40. "Misparsed %s", str);
  41. }
  42. static void
  43. parse_valid_trial(const char *str) {
  44. /* The value it parses should be correct. */
  45. expect_parse_accurate(str, str);
  46. char buf[100];
  47. snprintf(buf, sizeof(buf), "%swith_some_trailing_text", str);
  48. expect_parse_accurate(str, buf);
  49. snprintf(buf, sizeof(buf), "%s with a space", str);
  50. expect_parse_accurate(str, buf);
  51. snprintf(buf, sizeof(buf), "%s,in_a_malloc_conf_string:1", str);
  52. expect_parse_accurate(str, buf);
  53. }
  54. TEST_BEGIN(test_parse_valid) {
  55. parse_valid_trial("0");
  56. parse_valid_trial("1");
  57. parse_valid_trial("2");
  58. parse_valid_trial("100");
  59. parse_valid_trial("345");
  60. parse_valid_trial("00000000123");
  61. parse_valid_trial("00000000987");
  62. parse_valid_trial("0.0");
  63. parse_valid_trial("0.00000000000456456456");
  64. parse_valid_trial("100.00000000000456456456");
  65. parse_valid_trial("123.1");
  66. parse_valid_trial("123.01");
  67. parse_valid_trial("123.001");
  68. parse_valid_trial("123.0001");
  69. parse_valid_trial("123.00001");
  70. parse_valid_trial("123.000001");
  71. parse_valid_trial("123.0000001");
  72. parse_valid_trial(".0");
  73. parse_valid_trial(".1");
  74. parse_valid_trial(".01");
  75. parse_valid_trial(".001");
  76. parse_valid_trial(".0001");
  77. parse_valid_trial(".00001");
  78. parse_valid_trial(".000001");
  79. parse_valid_trial(".1");
  80. parse_valid_trial(".10");
  81. parse_valid_trial(".100");
  82. parse_valid_trial(".1000");
  83. parse_valid_trial(".100000");
  84. }
  85. TEST_END
  86. static void
  87. expect_parse_failure(const char *str) {
  88. fxp_t result = FXP_INIT_INT(333);
  89. char *end = (void *)0x123;
  90. bool err = fxp_parse(&result, str, &end);
  91. expect_true(err, "Expected a parse error on: %s", str);
  92. expect_ptr_eq((void *)0x123, end,
  93. "Parse error shouldn't change results");
  94. expect_u32_eq(result, FXP_INIT_INT(333),
  95. "Parse error shouldn't change results");
  96. }
  97. TEST_BEGIN(test_parse_invalid) {
  98. expect_parse_failure("123.");
  99. expect_parse_failure("3.a");
  100. expect_parse_failure(".a");
  101. expect_parse_failure("a.1");
  102. expect_parse_failure("a");
  103. /* A valid string, but one that overflows. */
  104. expect_parse_failure("123456789");
  105. expect_parse_failure("0000000123456789");
  106. expect_parse_failure("1000000");
  107. }
  108. TEST_END
  109. static void
  110. expect_init_percent(unsigned percent, const char *str) {
  111. fxp_t result_init = FXP_INIT_PERCENT(percent);
  112. fxp_t result_parse = xparse_fxp(str);
  113. expect_u32_eq(result_init, result_parse,
  114. "Expect representations of FXP_INIT_PERCENT(%u) and "
  115. "fxp_parse(\"%s\") to be equal; got %x and %x",
  116. percent, str, result_init, result_parse);
  117. }
  118. /*
  119. * Every other test uses either parsing or FXP_INIT_INT; it gets tested in those
  120. * ways. We need a one-off for the percent-based initialization, though.
  121. */
  122. TEST_BEGIN(test_init_percent) {
  123. expect_init_percent(100, "1");
  124. expect_init_percent(75, ".75");
  125. expect_init_percent(1, ".01");
  126. expect_init_percent(50, ".5");
  127. }
  128. TEST_END
  129. static void
  130. expect_add(const char *astr, const char *bstr, const char* resultstr) {
  131. fxp_t a = xparse_fxp(astr);
  132. fxp_t b = xparse_fxp(bstr);
  133. fxp_t result = xparse_fxp(resultstr);
  134. expect_true(fxp_close(fxp_add(a, b), result),
  135. "Expected %s + %s == %s", astr, bstr, resultstr);
  136. }
  137. TEST_BEGIN(test_add_simple) {
  138. expect_add("0", "0", "0");
  139. expect_add("0", "1", "1");
  140. expect_add("1", "1", "2");
  141. expect_add("1.5", "1.5", "3");
  142. expect_add("0.1", "0.1", "0.2");
  143. expect_add("123", "456", "579");
  144. }
  145. TEST_END
  146. static void
  147. expect_sub(const char *astr, const char *bstr, const char* resultstr) {
  148. fxp_t a = xparse_fxp(astr);
  149. fxp_t b = xparse_fxp(bstr);
  150. fxp_t result = xparse_fxp(resultstr);
  151. expect_true(fxp_close(fxp_sub(a, b), result),
  152. "Expected %s - %s == %s", astr, bstr, resultstr);
  153. }
  154. TEST_BEGIN(test_sub_simple) {
  155. expect_sub("0", "0", "0");
  156. expect_sub("1", "0", "1");
  157. expect_sub("1", "1", "0");
  158. expect_sub("3.5", "1.5", "2");
  159. expect_sub("0.3", "0.1", "0.2");
  160. expect_sub("456", "123", "333");
  161. }
  162. TEST_END
  163. static void
  164. expect_mul(const char *astr, const char *bstr, const char* resultstr) {
  165. fxp_t a = xparse_fxp(astr);
  166. fxp_t b = xparse_fxp(bstr);
  167. fxp_t result = xparse_fxp(resultstr);
  168. expect_true(fxp_close(fxp_mul(a, b), result),
  169. "Expected %s * %s == %s", astr, bstr, resultstr);
  170. }
  171. TEST_BEGIN(test_mul_simple) {
  172. expect_mul("0", "0", "0");
  173. expect_mul("1", "0", "0");
  174. expect_mul("1", "1", "1");
  175. expect_mul("1.5", "1.5", "2.25");
  176. expect_mul("100.0", "10", "1000");
  177. expect_mul(".1", "10", "1");
  178. }
  179. TEST_END
  180. static void
  181. expect_div(const char *astr, const char *bstr, const char* resultstr) {
  182. fxp_t a = xparse_fxp(astr);
  183. fxp_t b = xparse_fxp(bstr);
  184. fxp_t result = xparse_fxp(resultstr);
  185. expect_true(fxp_close(fxp_div(a, b), result),
  186. "Expected %s / %s == %s", astr, bstr, resultstr);
  187. }
  188. TEST_BEGIN(test_div_simple) {
  189. expect_div("1", "1", "1");
  190. expect_div("0", "1", "0");
  191. expect_div("2", "1", "2");
  192. expect_div("3", "2", "1.5");
  193. expect_div("3", "1.5", "2");
  194. expect_div("10", ".1", "100");
  195. expect_div("123", "456", ".2697368421");
  196. }
  197. TEST_END
  198. static void
  199. expect_round(const char *str, uint32_t rounded_down, uint32_t rounded_nearest) {
  200. fxp_t fxp = xparse_fxp(str);
  201. uint32_t fxp_rounded_down = fxp_round_down(fxp);
  202. uint32_t fxp_rounded_nearest = fxp_round_nearest(fxp);
  203. expect_u32_eq(rounded_down, fxp_rounded_down,
  204. "Mistake rounding %s down", str);
  205. expect_u32_eq(rounded_nearest, fxp_rounded_nearest,
  206. "Mistake rounding %s to nearest", str);
  207. }
  208. TEST_BEGIN(test_round_simple) {
  209. expect_round("1.5", 1, 2);
  210. expect_round("0", 0, 0);
  211. expect_round("0.1", 0, 0);
  212. expect_round("0.4", 0, 0);
  213. expect_round("0.40000", 0, 0);
  214. expect_round("0.5", 0, 1);
  215. expect_round("0.6", 0, 1);
  216. expect_round("123", 123, 123);
  217. expect_round("123.4", 123, 123);
  218. expect_round("123.5", 123, 124);
  219. }
  220. TEST_END
  221. static void
  222. expect_mul_frac(size_t a, const char *fracstr, size_t expected) {
  223. fxp_t frac = xparse_fxp(fracstr);
  224. size_t result = fxp_mul_frac(a, frac);
  225. expect_true(double_close(expected, result),
  226. "Expected %zu * %s == %zu (fracmul); got %zu", a, fracstr,
  227. expected, result);
  228. }
  229. TEST_BEGIN(test_mul_frac_simple) {
  230. expect_mul_frac(SIZE_MAX, "1.0", SIZE_MAX);
  231. expect_mul_frac(SIZE_MAX, ".75", SIZE_MAX / 4 * 3);
  232. expect_mul_frac(SIZE_MAX, ".5", SIZE_MAX / 2);
  233. expect_mul_frac(SIZE_MAX, ".25", SIZE_MAX / 4);
  234. expect_mul_frac(1U << 16, "1.0", 1U << 16);
  235. expect_mul_frac(1U << 30, "0.5", 1U << 29);
  236. expect_mul_frac(1U << 30, "0.25", 1U << 28);
  237. expect_mul_frac(1U << 30, "0.125", 1U << 27);
  238. expect_mul_frac((1U << 30) + 1, "0.125", 1U << 27);
  239. expect_mul_frac(100, "0.25", 25);
  240. expect_mul_frac(1000 * 1000, "0.001", 1000);
  241. }
  242. TEST_END
  243. static void
  244. expect_print(const char *str) {
  245. fxp_t fxp = xparse_fxp(str);
  246. char buf[FXP_BUF_SIZE];
  247. fxp_print(fxp, buf);
  248. expect_d_eq(0, strcmp(str, buf), "Couldn't round-trip print %s", str);
  249. }
  250. TEST_BEGIN(test_print_simple) {
  251. expect_print("0.0");
  252. expect_print("1.0");
  253. expect_print("2.0");
  254. expect_print("123.0");
  255. /*
  256. * We hit the possibility of roundoff errors whenever the fractional
  257. * component isn't a round binary number; only check these here (we
  258. * round-trip properly in the stress test).
  259. */
  260. expect_print("1.5");
  261. expect_print("3.375");
  262. expect_print("0.25");
  263. expect_print("0.125");
  264. /* 1 / 2**14 */
  265. expect_print("0.00006103515625");
  266. }
  267. TEST_END
  268. TEST_BEGIN(test_stress) {
  269. const char *numbers[] = {
  270. "0.0", "0.1", "0.2", "0.3", "0.4",
  271. "0.5", "0.6", "0.7", "0.8", "0.9",
  272. "1.0", "1.1", "1.2", "1.3", "1.4",
  273. "1.5", "1.6", "1.7", "1.8", "1.9",
  274. "2.0", "2.1", "2.2", "2.3", "2.4",
  275. "2.5", "2.6", "2.7", "2.8", "2.9",
  276. "17.0", "17.1", "17.2", "17.3", "17.4",
  277. "17.5", "17.6", "17.7", "17.8", "17.9",
  278. "18.0", "18.1", "18.2", "18.3", "18.4",
  279. "18.5", "18.6", "18.7", "18.8", "18.9",
  280. "123.0", "123.1", "123.2", "123.3", "123.4",
  281. "123.5", "123.6", "123.7", "123.8", "123.9",
  282. "124.0", "124.1", "124.2", "124.3", "124.4",
  283. "124.5", "124.6", "124.7", "124.8", "124.9",
  284. "125.0", "125.1", "125.2", "125.3", "125.4",
  285. "125.5", "125.6", "125.7", "125.8", "125.9"};
  286. size_t numbers_len = sizeof(numbers)/sizeof(numbers[0]);
  287. for (size_t i = 0; i < numbers_len; i++) {
  288. fxp_t fxp_a = xparse_fxp(numbers[i]);
  289. double double_a = strtod(numbers[i], NULL);
  290. uint32_t fxp_rounded_down = fxp_round_down(fxp_a);
  291. uint32_t fxp_rounded_nearest = fxp_round_nearest(fxp_a);
  292. uint32_t double_rounded_down = (uint32_t)double_a;
  293. uint32_t double_rounded_nearest = (uint32_t)round(double_a);
  294. expect_u32_eq(double_rounded_down, fxp_rounded_down,
  295. "Incorrectly rounded down %s", numbers[i]);
  296. expect_u32_eq(double_rounded_nearest, fxp_rounded_nearest,
  297. "Incorrectly rounded-to-nearest %s", numbers[i]);
  298. for (size_t j = 0; j < numbers_len; j++) {
  299. fxp_t fxp_b = xparse_fxp(numbers[j]);
  300. double double_b = strtod(numbers[j], NULL);
  301. fxp_t fxp_sum = fxp_add(fxp_a, fxp_b);
  302. double double_sum = double_a + double_b;
  303. expect_true(
  304. double_close(fxp2double(fxp_sum), double_sum),
  305. "Miscomputed %s + %s", numbers[i], numbers[j]);
  306. if (double_a > double_b) {
  307. fxp_t fxp_diff = fxp_sub(fxp_a, fxp_b);
  308. double double_diff = double_a - double_b;
  309. expect_true(
  310. double_close(fxp2double(fxp_diff),
  311. double_diff),
  312. "Miscomputed %s - %s", numbers[i],
  313. numbers[j]);
  314. }
  315. fxp_t fxp_prod = fxp_mul(fxp_a, fxp_b);
  316. double double_prod = double_a * double_b;
  317. expect_true(
  318. double_close(fxp2double(fxp_prod), double_prod),
  319. "Miscomputed %s * %s", numbers[i], numbers[j]);
  320. if (double_b != 0.0) {
  321. fxp_t fxp_quot = fxp_div(fxp_a, fxp_b);
  322. double double_quot = double_a / double_b;
  323. expect_true(
  324. double_close(fxp2double(fxp_quot),
  325. double_quot),
  326. "Miscomputed %s / %s", numbers[i],
  327. numbers[j]);
  328. }
  329. }
  330. }
  331. }
  332. TEST_END
  333. int
  334. main(void) {
  335. return test_no_reentrancy(
  336. test_parse_valid,
  337. test_parse_invalid,
  338. test_init_percent,
  339. test_add_simple,
  340. test_sub_simple,
  341. test_mul_simple,
  342. test_div_simple,
  343. test_round_simple,
  344. test_mul_frac_simple,
  345. test_print_simple,
  346. test_stress);
  347. }