nstime.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. #include "test/jemalloc_test.h"
  2. #define BILLION UINT64_C(1000000000)
  3. TEST_BEGIN(test_nstime_init) {
  4. nstime_t nst;
  5. nstime_init(&nst, 42000000043);
  6. expect_u64_eq(nstime_ns(&nst), 42000000043, "ns incorrectly read");
  7. expect_u64_eq(nstime_sec(&nst), 42, "sec incorrectly read");
  8. expect_u64_eq(nstime_nsec(&nst), 43, "nsec incorrectly read");
  9. }
  10. TEST_END
  11. TEST_BEGIN(test_nstime_init2) {
  12. nstime_t nst;
  13. nstime_init2(&nst, 42, 43);
  14. expect_u64_eq(nstime_sec(&nst), 42, "sec incorrectly read");
  15. expect_u64_eq(nstime_nsec(&nst), 43, "nsec incorrectly read");
  16. }
  17. TEST_END
  18. TEST_BEGIN(test_nstime_copy) {
  19. nstime_t nsta, nstb;
  20. nstime_init2(&nsta, 42, 43);
  21. nstime_init_zero(&nstb);
  22. nstime_copy(&nstb, &nsta);
  23. expect_u64_eq(nstime_sec(&nstb), 42, "sec incorrectly copied");
  24. expect_u64_eq(nstime_nsec(&nstb), 43, "nsec incorrectly copied");
  25. }
  26. TEST_END
  27. TEST_BEGIN(test_nstime_compare) {
  28. nstime_t nsta, nstb;
  29. nstime_init2(&nsta, 42, 43);
  30. nstime_copy(&nstb, &nsta);
  31. expect_d_eq(nstime_compare(&nsta, &nstb), 0, "Times should be equal");
  32. expect_d_eq(nstime_compare(&nstb, &nsta), 0, "Times should be equal");
  33. nstime_init2(&nstb, 42, 42);
  34. expect_d_eq(nstime_compare(&nsta, &nstb), 1,
  35. "nsta should be greater than nstb");
  36. expect_d_eq(nstime_compare(&nstb, &nsta), -1,
  37. "nstb should be less than nsta");
  38. nstime_init2(&nstb, 42, 44);
  39. expect_d_eq(nstime_compare(&nsta, &nstb), -1,
  40. "nsta should be less than nstb");
  41. expect_d_eq(nstime_compare(&nstb, &nsta), 1,
  42. "nstb should be greater than nsta");
  43. nstime_init2(&nstb, 41, BILLION - 1);
  44. expect_d_eq(nstime_compare(&nsta, &nstb), 1,
  45. "nsta should be greater than nstb");
  46. expect_d_eq(nstime_compare(&nstb, &nsta), -1,
  47. "nstb should be less than nsta");
  48. nstime_init2(&nstb, 43, 0);
  49. expect_d_eq(nstime_compare(&nsta, &nstb), -1,
  50. "nsta should be less than nstb");
  51. expect_d_eq(nstime_compare(&nstb, &nsta), 1,
  52. "nstb should be greater than nsta");
  53. }
  54. TEST_END
  55. TEST_BEGIN(test_nstime_add) {
  56. nstime_t nsta, nstb;
  57. nstime_init2(&nsta, 42, 43);
  58. nstime_copy(&nstb, &nsta);
  59. nstime_add(&nsta, &nstb);
  60. nstime_init2(&nstb, 84, 86);
  61. expect_d_eq(nstime_compare(&nsta, &nstb), 0,
  62. "Incorrect addition result");
  63. nstime_init2(&nsta, 42, BILLION - 1);
  64. nstime_copy(&nstb, &nsta);
  65. nstime_add(&nsta, &nstb);
  66. nstime_init2(&nstb, 85, BILLION - 2);
  67. expect_d_eq(nstime_compare(&nsta, &nstb), 0,
  68. "Incorrect addition result");
  69. }
  70. TEST_END
  71. TEST_BEGIN(test_nstime_iadd) {
  72. nstime_t nsta, nstb;
  73. nstime_init2(&nsta, 42, BILLION - 1);
  74. nstime_iadd(&nsta, 1);
  75. nstime_init2(&nstb, 43, 0);
  76. expect_d_eq(nstime_compare(&nsta, &nstb), 0,
  77. "Incorrect addition result");
  78. nstime_init2(&nsta, 42, 1);
  79. nstime_iadd(&nsta, BILLION + 1);
  80. nstime_init2(&nstb, 43, 2);
  81. expect_d_eq(nstime_compare(&nsta, &nstb), 0,
  82. "Incorrect addition result");
  83. }
  84. TEST_END
  85. TEST_BEGIN(test_nstime_subtract) {
  86. nstime_t nsta, nstb;
  87. nstime_init2(&nsta, 42, 43);
  88. nstime_copy(&nstb, &nsta);
  89. nstime_subtract(&nsta, &nstb);
  90. nstime_init_zero(&nstb);
  91. expect_d_eq(nstime_compare(&nsta, &nstb), 0,
  92. "Incorrect subtraction result");
  93. nstime_init2(&nsta, 42, 43);
  94. nstime_init2(&nstb, 41, 44);
  95. nstime_subtract(&nsta, &nstb);
  96. nstime_init2(&nstb, 0, BILLION - 1);
  97. expect_d_eq(nstime_compare(&nsta, &nstb), 0,
  98. "Incorrect subtraction result");
  99. }
  100. TEST_END
  101. TEST_BEGIN(test_nstime_isubtract) {
  102. nstime_t nsta, nstb;
  103. nstime_init2(&nsta, 42, 43);
  104. nstime_isubtract(&nsta, 42*BILLION + 43);
  105. nstime_init_zero(&nstb);
  106. expect_d_eq(nstime_compare(&nsta, &nstb), 0,
  107. "Incorrect subtraction result");
  108. nstime_init2(&nsta, 42, 43);
  109. nstime_isubtract(&nsta, 41*BILLION + 44);
  110. nstime_init2(&nstb, 0, BILLION - 1);
  111. expect_d_eq(nstime_compare(&nsta, &nstb), 0,
  112. "Incorrect subtraction result");
  113. }
  114. TEST_END
  115. TEST_BEGIN(test_nstime_imultiply) {
  116. nstime_t nsta, nstb;
  117. nstime_init2(&nsta, 42, 43);
  118. nstime_imultiply(&nsta, 10);
  119. nstime_init2(&nstb, 420, 430);
  120. expect_d_eq(nstime_compare(&nsta, &nstb), 0,
  121. "Incorrect multiplication result");
  122. nstime_init2(&nsta, 42, 666666666);
  123. nstime_imultiply(&nsta, 3);
  124. nstime_init2(&nstb, 127, 999999998);
  125. expect_d_eq(nstime_compare(&nsta, &nstb), 0,
  126. "Incorrect multiplication result");
  127. }
  128. TEST_END
  129. TEST_BEGIN(test_nstime_idivide) {
  130. nstime_t nsta, nstb;
  131. nstime_init2(&nsta, 42, 43);
  132. nstime_copy(&nstb, &nsta);
  133. nstime_imultiply(&nsta, 10);
  134. nstime_idivide(&nsta, 10);
  135. expect_d_eq(nstime_compare(&nsta, &nstb), 0,
  136. "Incorrect division result");
  137. nstime_init2(&nsta, 42, 666666666);
  138. nstime_copy(&nstb, &nsta);
  139. nstime_imultiply(&nsta, 3);
  140. nstime_idivide(&nsta, 3);
  141. expect_d_eq(nstime_compare(&nsta, &nstb), 0,
  142. "Incorrect division result");
  143. }
  144. TEST_END
  145. TEST_BEGIN(test_nstime_divide) {
  146. nstime_t nsta, nstb, nstc;
  147. nstime_init2(&nsta, 42, 43);
  148. nstime_copy(&nstb, &nsta);
  149. nstime_imultiply(&nsta, 10);
  150. expect_u64_eq(nstime_divide(&nsta, &nstb), 10,
  151. "Incorrect division result");
  152. nstime_init2(&nsta, 42, 43);
  153. nstime_copy(&nstb, &nsta);
  154. nstime_imultiply(&nsta, 10);
  155. nstime_init(&nstc, 1);
  156. nstime_add(&nsta, &nstc);
  157. expect_u64_eq(nstime_divide(&nsta, &nstb), 10,
  158. "Incorrect division result");
  159. nstime_init2(&nsta, 42, 43);
  160. nstime_copy(&nstb, &nsta);
  161. nstime_imultiply(&nsta, 10);
  162. nstime_init(&nstc, 1);
  163. nstime_subtract(&nsta, &nstc);
  164. expect_u64_eq(nstime_divide(&nsta, &nstb), 9,
  165. "Incorrect division result");
  166. }
  167. TEST_END
  168. void
  169. test_nstime_since_once(nstime_t *t) {
  170. nstime_t old_t;
  171. nstime_copy(&old_t, t);
  172. uint64_t ns_since = nstime_ns_since(t);
  173. nstime_update(t);
  174. nstime_t new_t;
  175. nstime_copy(&new_t, t);
  176. nstime_subtract(&new_t, &old_t);
  177. expect_u64_ge(nstime_ns(&new_t), ns_since,
  178. "Incorrect time since result");
  179. }
  180. TEST_BEGIN(test_nstime_ns_since) {
  181. nstime_t t;
  182. nstime_init_update(&t);
  183. for (uint64_t i = 0; i < 10000; i++) {
  184. /* Keeps updating t and verifies ns_since is valid. */
  185. test_nstime_since_once(&t);
  186. }
  187. }
  188. TEST_END
  189. TEST_BEGIN(test_nstime_monotonic) {
  190. nstime_monotonic();
  191. }
  192. TEST_END
  193. int
  194. main(void) {
  195. return test(
  196. test_nstime_init,
  197. test_nstime_init2,
  198. test_nstime_copy,
  199. test_nstime_compare,
  200. test_nstime_add,
  201. test_nstime_iadd,
  202. test_nstime_subtract,
  203. test_nstime_isubtract,
  204. test_nstime_imultiply,
  205. test_nstime_idivide,
  206. test_nstime_divide,
  207. test_nstime_ns_since,
  208. test_nstime_monotonic);
  209. }