2
0

nstime.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. assert_u64_eq(nstime_ns(&nst), 42000000043, "ns incorrectly read");
  7. assert_u64_eq(nstime_sec(&nst), 42, "sec incorrectly read");
  8. assert_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. assert_u64_eq(nstime_sec(&nst), 42, "sec incorrectly read");
  15. assert_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(&nstb, 0);
  22. nstime_copy(&nstb, &nsta);
  23. assert_u64_eq(nstime_sec(&nstb), 42, "sec incorrectly copied");
  24. assert_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. assert_d_eq(nstime_compare(&nsta, &nstb), 0, "Times should be equal");
  32. assert_d_eq(nstime_compare(&nstb, &nsta), 0, "Times should be equal");
  33. nstime_init2(&nstb, 42, 42);
  34. assert_d_eq(nstime_compare(&nsta, &nstb), 1,
  35. "nsta should be greater than nstb");
  36. assert_d_eq(nstime_compare(&nstb, &nsta), -1,
  37. "nstb should be less than nsta");
  38. nstime_init2(&nstb, 42, 44);
  39. assert_d_eq(nstime_compare(&nsta, &nstb), -1,
  40. "nsta should be less than nstb");
  41. assert_d_eq(nstime_compare(&nstb, &nsta), 1,
  42. "nstb should be greater than nsta");
  43. nstime_init2(&nstb, 41, BILLION - 1);
  44. assert_d_eq(nstime_compare(&nsta, &nstb), 1,
  45. "nsta should be greater than nstb");
  46. assert_d_eq(nstime_compare(&nstb, &nsta), -1,
  47. "nstb should be less than nsta");
  48. nstime_init2(&nstb, 43, 0);
  49. assert_d_eq(nstime_compare(&nsta, &nstb), -1,
  50. "nsta should be less than nstb");
  51. assert_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. assert_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. assert_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. assert_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. assert_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(&nstb, 0);
  91. assert_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. assert_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(&nstb, 0);
  106. assert_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. assert_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. assert_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. assert_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. assert_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. assert_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. assert_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. assert_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. assert_u64_eq(nstime_divide(&nsta, &nstb), 9,
  165. "Incorrect division result");
  166. }
  167. TEST_END
  168. TEST_BEGIN(test_nstime_monotonic) {
  169. nstime_monotonic();
  170. }
  171. TEST_END
  172. TEST_BEGIN(test_nstime_update) {
  173. nstime_t nst;
  174. nstime_init(&nst, 0);
  175. assert_false(nstime_update(&nst), "Basic time update failed.");
  176. /* Only Rip Van Winkle sleeps this long. */
  177. {
  178. nstime_t addend;
  179. nstime_init2(&addend, 631152000, 0);
  180. nstime_add(&nst, &addend);
  181. }
  182. {
  183. nstime_t nst0;
  184. nstime_copy(&nst0, &nst);
  185. assert_true(nstime_update(&nst),
  186. "Update should detect time roll-back.");
  187. assert_d_eq(nstime_compare(&nst, &nst0), 0,
  188. "Time should not have been modified");
  189. }
  190. }
  191. TEST_END
  192. int
  193. main(void) {
  194. return test(
  195. test_nstime_init,
  196. test_nstime_init2,
  197. test_nstime_copy,
  198. test_nstime_compare,
  199. test_nstime_add,
  200. test_nstime_iadd,
  201. test_nstime_subtract,
  202. test_nstime_isubtract,
  203. test_nstime_imultiply,
  204. test_nstime_idivide,
  205. test_nstime_divide,
  206. test_nstime_monotonic,
  207. test_nstime_update);
  208. }