diff options
Diffstat (limited to 'examples/redis-unstable/deps/jemalloc/test/unit/nstime.c')
| -rw-r--r-- | examples/redis-unstable/deps/jemalloc/test/unit/nstime.c | 252 |
1 files changed, 252 insertions, 0 deletions
diff --git a/examples/redis-unstable/deps/jemalloc/test/unit/nstime.c b/examples/redis-unstable/deps/jemalloc/test/unit/nstime.c new file mode 100644 index 0000000..56238ab --- /dev/null +++ b/examples/redis-unstable/deps/jemalloc/test/unit/nstime.c | |||
| @@ -0,0 +1,252 @@ | |||
| 1 | #include "test/jemalloc_test.h" | ||
| 2 | |||
| 3 | #define BILLION UINT64_C(1000000000) | ||
| 4 | |||
| 5 | TEST_BEGIN(test_nstime_init) { | ||
| 6 | nstime_t nst; | ||
| 7 | |||
| 8 | nstime_init(&nst, 42000000043); | ||
| 9 | expect_u64_eq(nstime_ns(&nst), 42000000043, "ns incorrectly read"); | ||
| 10 | expect_u64_eq(nstime_sec(&nst), 42, "sec incorrectly read"); | ||
| 11 | expect_u64_eq(nstime_nsec(&nst), 43, "nsec incorrectly read"); | ||
| 12 | } | ||
| 13 | TEST_END | ||
| 14 | |||
| 15 | TEST_BEGIN(test_nstime_init2) { | ||
| 16 | nstime_t nst; | ||
| 17 | |||
| 18 | nstime_init2(&nst, 42, 43); | ||
| 19 | expect_u64_eq(nstime_sec(&nst), 42, "sec incorrectly read"); | ||
| 20 | expect_u64_eq(nstime_nsec(&nst), 43, "nsec incorrectly read"); | ||
| 21 | } | ||
| 22 | TEST_END | ||
| 23 | |||
| 24 | TEST_BEGIN(test_nstime_copy) { | ||
| 25 | nstime_t nsta, nstb; | ||
| 26 | |||
| 27 | nstime_init2(&nsta, 42, 43); | ||
| 28 | nstime_init_zero(&nstb); | ||
| 29 | nstime_copy(&nstb, &nsta); | ||
| 30 | expect_u64_eq(nstime_sec(&nstb), 42, "sec incorrectly copied"); | ||
| 31 | expect_u64_eq(nstime_nsec(&nstb), 43, "nsec incorrectly copied"); | ||
| 32 | } | ||
| 33 | TEST_END | ||
| 34 | |||
| 35 | TEST_BEGIN(test_nstime_compare) { | ||
| 36 | nstime_t nsta, nstb; | ||
| 37 | |||
| 38 | nstime_init2(&nsta, 42, 43); | ||
| 39 | nstime_copy(&nstb, &nsta); | ||
| 40 | expect_d_eq(nstime_compare(&nsta, &nstb), 0, "Times should be equal"); | ||
| 41 | expect_d_eq(nstime_compare(&nstb, &nsta), 0, "Times should be equal"); | ||
| 42 | |||
| 43 | nstime_init2(&nstb, 42, 42); | ||
| 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 | |||
| 49 | nstime_init2(&nstb, 42, 44); | ||
| 50 | expect_d_eq(nstime_compare(&nsta, &nstb), -1, | ||
| 51 | "nsta should be less than nstb"); | ||
| 52 | expect_d_eq(nstime_compare(&nstb, &nsta), 1, | ||
| 53 | "nstb should be greater than nsta"); | ||
| 54 | |||
| 55 | nstime_init2(&nstb, 41, BILLION - 1); | ||
| 56 | expect_d_eq(nstime_compare(&nsta, &nstb), 1, | ||
| 57 | "nsta should be greater than nstb"); | ||
| 58 | expect_d_eq(nstime_compare(&nstb, &nsta), -1, | ||
| 59 | "nstb should be less than nsta"); | ||
| 60 | |||
| 61 | nstime_init2(&nstb, 43, 0); | ||
| 62 | expect_d_eq(nstime_compare(&nsta, &nstb), -1, | ||
| 63 | "nsta should be less than nstb"); | ||
| 64 | expect_d_eq(nstime_compare(&nstb, &nsta), 1, | ||
| 65 | "nstb should be greater than nsta"); | ||
| 66 | } | ||
| 67 | TEST_END | ||
| 68 | |||
| 69 | TEST_BEGIN(test_nstime_add) { | ||
| 70 | nstime_t nsta, nstb; | ||
| 71 | |||
| 72 | nstime_init2(&nsta, 42, 43); | ||
| 73 | nstime_copy(&nstb, &nsta); | ||
| 74 | nstime_add(&nsta, &nstb); | ||
| 75 | nstime_init2(&nstb, 84, 86); | ||
| 76 | expect_d_eq(nstime_compare(&nsta, &nstb), 0, | ||
| 77 | "Incorrect addition result"); | ||
| 78 | |||
| 79 | nstime_init2(&nsta, 42, BILLION - 1); | ||
| 80 | nstime_copy(&nstb, &nsta); | ||
| 81 | nstime_add(&nsta, &nstb); | ||
| 82 | nstime_init2(&nstb, 85, BILLION - 2); | ||
| 83 | expect_d_eq(nstime_compare(&nsta, &nstb), 0, | ||
| 84 | "Incorrect addition result"); | ||
| 85 | } | ||
| 86 | TEST_END | ||
| 87 | |||
| 88 | TEST_BEGIN(test_nstime_iadd) { | ||
| 89 | nstime_t nsta, nstb; | ||
| 90 | |||
| 91 | nstime_init2(&nsta, 42, BILLION - 1); | ||
| 92 | nstime_iadd(&nsta, 1); | ||
| 93 | nstime_init2(&nstb, 43, 0); | ||
| 94 | expect_d_eq(nstime_compare(&nsta, &nstb), 0, | ||
| 95 | "Incorrect addition result"); | ||
| 96 | |||
| 97 | nstime_init2(&nsta, 42, 1); | ||
| 98 | nstime_iadd(&nsta, BILLION + 1); | ||
| 99 | nstime_init2(&nstb, 43, 2); | ||
| 100 | expect_d_eq(nstime_compare(&nsta, &nstb), 0, | ||
| 101 | "Incorrect addition result"); | ||
| 102 | } | ||
| 103 | TEST_END | ||
| 104 | |||
| 105 | TEST_BEGIN(test_nstime_subtract) { | ||
| 106 | nstime_t nsta, nstb; | ||
| 107 | |||
| 108 | nstime_init2(&nsta, 42, 43); | ||
| 109 | nstime_copy(&nstb, &nsta); | ||
| 110 | nstime_subtract(&nsta, &nstb); | ||
| 111 | nstime_init_zero(&nstb); | ||
| 112 | expect_d_eq(nstime_compare(&nsta, &nstb), 0, | ||
| 113 | "Incorrect subtraction result"); | ||
| 114 | |||
| 115 | nstime_init2(&nsta, 42, 43); | ||
| 116 | nstime_init2(&nstb, 41, 44); | ||
| 117 | nstime_subtract(&nsta, &nstb); | ||
| 118 | nstime_init2(&nstb, 0, BILLION - 1); | ||
| 119 | expect_d_eq(nstime_compare(&nsta, &nstb), 0, | ||
| 120 | "Incorrect subtraction result"); | ||
| 121 | } | ||
| 122 | TEST_END | ||
| 123 | |||
| 124 | TEST_BEGIN(test_nstime_isubtract) { | ||
| 125 | nstime_t nsta, nstb; | ||
| 126 | |||
| 127 | nstime_init2(&nsta, 42, 43); | ||
| 128 | nstime_isubtract(&nsta, 42*BILLION + 43); | ||
| 129 | nstime_init_zero(&nstb); | ||
| 130 | expect_d_eq(nstime_compare(&nsta, &nstb), 0, | ||
| 131 | "Incorrect subtraction result"); | ||
| 132 | |||
| 133 | nstime_init2(&nsta, 42, 43); | ||
| 134 | nstime_isubtract(&nsta, 41*BILLION + 44); | ||
| 135 | nstime_init2(&nstb, 0, BILLION - 1); | ||
| 136 | expect_d_eq(nstime_compare(&nsta, &nstb), 0, | ||
| 137 | "Incorrect subtraction result"); | ||
| 138 | } | ||
| 139 | TEST_END | ||
| 140 | |||
| 141 | TEST_BEGIN(test_nstime_imultiply) { | ||
| 142 | nstime_t nsta, nstb; | ||
| 143 | |||
| 144 | nstime_init2(&nsta, 42, 43); | ||
| 145 | nstime_imultiply(&nsta, 10); | ||
| 146 | nstime_init2(&nstb, 420, 430); | ||
| 147 | expect_d_eq(nstime_compare(&nsta, &nstb), 0, | ||
| 148 | "Incorrect multiplication result"); | ||
| 149 | |||
| 150 | nstime_init2(&nsta, 42, 666666666); | ||
| 151 | nstime_imultiply(&nsta, 3); | ||
| 152 | nstime_init2(&nstb, 127, 999999998); | ||
| 153 | expect_d_eq(nstime_compare(&nsta, &nstb), 0, | ||
| 154 | "Incorrect multiplication result"); | ||
| 155 | } | ||
| 156 | TEST_END | ||
| 157 | |||
| 158 | TEST_BEGIN(test_nstime_idivide) { | ||
| 159 | nstime_t nsta, nstb; | ||
| 160 | |||
| 161 | nstime_init2(&nsta, 42, 43); | ||
| 162 | nstime_copy(&nstb, &nsta); | ||
| 163 | nstime_imultiply(&nsta, 10); | ||
| 164 | nstime_idivide(&nsta, 10); | ||
| 165 | expect_d_eq(nstime_compare(&nsta, &nstb), 0, | ||
| 166 | "Incorrect division result"); | ||
| 167 | |||
| 168 | nstime_init2(&nsta, 42, 666666666); | ||
| 169 | nstime_copy(&nstb, &nsta); | ||
| 170 | nstime_imultiply(&nsta, 3); | ||
| 171 | nstime_idivide(&nsta, 3); | ||
| 172 | expect_d_eq(nstime_compare(&nsta, &nstb), 0, | ||
| 173 | "Incorrect division result"); | ||
| 174 | } | ||
| 175 | TEST_END | ||
| 176 | |||
| 177 | TEST_BEGIN(test_nstime_divide) { | ||
| 178 | nstime_t nsta, nstb, nstc; | ||
| 179 | |||
| 180 | nstime_init2(&nsta, 42, 43); | ||
| 181 | nstime_copy(&nstb, &nsta); | ||
| 182 | nstime_imultiply(&nsta, 10); | ||
| 183 | expect_u64_eq(nstime_divide(&nsta, &nstb), 10, | ||
| 184 | "Incorrect division result"); | ||
| 185 | |||
| 186 | nstime_init2(&nsta, 42, 43); | ||
| 187 | nstime_copy(&nstb, &nsta); | ||
| 188 | nstime_imultiply(&nsta, 10); | ||
| 189 | nstime_init(&nstc, 1); | ||
| 190 | nstime_add(&nsta, &nstc); | ||
| 191 | expect_u64_eq(nstime_divide(&nsta, &nstb), 10, | ||
| 192 | "Incorrect division result"); | ||
| 193 | |||
| 194 | nstime_init2(&nsta, 42, 43); | ||
| 195 | nstime_copy(&nstb, &nsta); | ||
| 196 | nstime_imultiply(&nsta, 10); | ||
| 197 | nstime_init(&nstc, 1); | ||
| 198 | nstime_subtract(&nsta, &nstc); | ||
| 199 | expect_u64_eq(nstime_divide(&nsta, &nstb), 9, | ||
| 200 | "Incorrect division result"); | ||
| 201 | } | ||
| 202 | TEST_END | ||
| 203 | |||
| 204 | void | ||
| 205 | test_nstime_since_once(nstime_t *t) { | ||
| 206 | nstime_t old_t; | ||
| 207 | nstime_copy(&old_t, t); | ||
| 208 | |||
| 209 | uint64_t ns_since = nstime_ns_since(t); | ||
| 210 | nstime_update(t); | ||
| 211 | |||
| 212 | nstime_t new_t; | ||
| 213 | nstime_copy(&new_t, t); | ||
| 214 | nstime_subtract(&new_t, &old_t); | ||
| 215 | |||
| 216 | expect_u64_ge(nstime_ns(&new_t), ns_since, | ||
| 217 | "Incorrect time since result"); | ||
| 218 | } | ||
| 219 | |||
| 220 | TEST_BEGIN(test_nstime_ns_since) { | ||
| 221 | nstime_t t; | ||
| 222 | |||
| 223 | nstime_init_update(&t); | ||
| 224 | for (uint64_t i = 0; i < 10000; i++) { | ||
| 225 | /* Keeps updating t and verifies ns_since is valid. */ | ||
| 226 | test_nstime_since_once(&t); | ||
| 227 | } | ||
| 228 | } | ||
| 229 | TEST_END | ||
| 230 | |||
| 231 | TEST_BEGIN(test_nstime_monotonic) { | ||
| 232 | nstime_monotonic(); | ||
| 233 | } | ||
| 234 | TEST_END | ||
| 235 | |||
| 236 | int | ||
| 237 | main(void) { | ||
| 238 | return test( | ||
| 239 | test_nstime_init, | ||
| 240 | test_nstime_init2, | ||
| 241 | test_nstime_copy, | ||
| 242 | test_nstime_compare, | ||
| 243 | test_nstime_add, | ||
| 244 | test_nstime_iadd, | ||
| 245 | test_nstime_subtract, | ||
| 246 | test_nstime_isubtract, | ||
| 247 | test_nstime_imultiply, | ||
| 248 | test_nstime_idivide, | ||
| 249 | test_nstime_divide, | ||
| 250 | test_nstime_ns_since, | ||
| 251 | test_nstime_monotonic); | ||
| 252 | } | ||
