2014-08-07 14:36:19 +08:00
|
|
|
#include "test/jemalloc_test.h"
|
|
|
|
|
2017-01-20 13:41:41 +08:00
|
|
|
#define TEST_STRUCT(p, t) \
|
2014-08-07 14:36:19 +08:00
|
|
|
struct p##_test_s { \
|
|
|
|
t accum0; \
|
|
|
|
t x; \
|
2015-02-03 05:49:08 +08:00
|
|
|
t s; \
|
2014-08-07 14:36:19 +08:00
|
|
|
}; \
|
|
|
|
typedef struct p##_test_s p##_test_t;
|
|
|
|
|
2017-01-20 13:41:41 +08:00
|
|
|
#define TEST_BODY(p, t, tc, ta, FMT) do { \
|
2014-08-07 14:36:19 +08:00
|
|
|
const p##_test_t tests[] = { \
|
2015-02-03 05:49:08 +08:00
|
|
|
{(t)-1, (t)-1, (t)-2}, \
|
|
|
|
{(t)-1, (t) 0, (t)-2}, \
|
|
|
|
{(t)-1, (t) 1, (t)-2}, \
|
2014-08-07 14:36:19 +08:00
|
|
|
\
|
2015-02-03 05:49:08 +08:00
|
|
|
{(t) 0, (t)-1, (t)-2}, \
|
|
|
|
{(t) 0, (t) 0, (t)-2}, \
|
|
|
|
{(t) 0, (t) 1, (t)-2}, \
|
2014-08-07 14:36:19 +08:00
|
|
|
\
|
2015-02-03 05:49:08 +08:00
|
|
|
{(t) 1, (t)-1, (t)-2}, \
|
|
|
|
{(t) 1, (t) 0, (t)-2}, \
|
|
|
|
{(t) 1, (t) 1, (t)-2}, \
|
2014-08-07 14:36:19 +08:00
|
|
|
\
|
2015-02-03 05:49:08 +08:00
|
|
|
{(t)0, (t)-(1 << 22), (t)-2}, \
|
|
|
|
{(t)0, (t)(1 << 22), (t)-2}, \
|
|
|
|
{(t)(1 << 22), (t)-(1 << 22), (t)-2}, \
|
|
|
|
{(t)(1 << 22), (t)(1 << 22), (t)-2} \
|
2014-08-07 14:36:19 +08:00
|
|
|
}; \
|
|
|
|
unsigned i; \
|
|
|
|
\
|
|
|
|
for (i = 0; i < sizeof(tests)/sizeof(p##_test_t); i++) { \
|
2015-02-03 05:49:08 +08:00
|
|
|
bool err; \
|
2014-08-07 14:36:19 +08:00
|
|
|
t accum = tests[i].accum0; \
|
2015-02-03 05:49:08 +08:00
|
|
|
assert_##ta##_eq(atomic_read_##p(&accum), \
|
|
|
|
tests[i].accum0, \
|
|
|
|
"Erroneous read, i=%u", i); \
|
|
|
|
\
|
|
|
|
assert_##ta##_eq(atomic_add_##p(&accum, tests[i].x), \
|
|
|
|
(t)((tc)tests[i].accum0 + (tc)tests[i].x), \
|
2015-07-24 04:56:25 +08:00
|
|
|
"i=%u, accum=%"FMT", x=%"FMT, \
|
2014-08-07 14:36:19 +08:00
|
|
|
i, tests[i].accum0, tests[i].x); \
|
2015-02-03 05:49:08 +08:00
|
|
|
assert_##ta##_eq(atomic_read_##p(&accum), accum, \
|
|
|
|
"Erroneous add, i=%u", i); \
|
2014-08-07 14:36:19 +08:00
|
|
|
\
|
|
|
|
accum = tests[i].accum0; \
|
2015-02-03 05:49:08 +08:00
|
|
|
assert_##ta##_eq(atomic_sub_##p(&accum, tests[i].x), \
|
|
|
|
(t)((tc)tests[i].accum0 - (tc)tests[i].x), \
|
2015-07-24 04:56:25 +08:00
|
|
|
"i=%u, accum=%"FMT", x=%"FMT, \
|
2014-08-07 14:36:19 +08:00
|
|
|
i, tests[i].accum0, tests[i].x); \
|
2015-02-03 05:49:08 +08:00
|
|
|
assert_##ta##_eq(atomic_read_##p(&accum), accum, \
|
|
|
|
"Erroneous sub, i=%u", i); \
|
|
|
|
\
|
|
|
|
accum = tests[i].accum0; \
|
|
|
|
err = atomic_cas_##p(&accum, tests[i].x, tests[i].s); \
|
|
|
|
assert_b_eq(err, tests[i].accum0 != tests[i].x, \
|
|
|
|
"Erroneous cas success/failure result"); \
|
|
|
|
assert_##ta##_eq(accum, err ? tests[i].accum0 : \
|
|
|
|
tests[i].s, "Erroneous cas effect, i=%u", i); \
|
|
|
|
\
|
|
|
|
accum = tests[i].accum0; \
|
|
|
|
atomic_write_##p(&accum, tests[i].s); \
|
|
|
|
assert_##ta##_eq(accum, tests[i].s, \
|
|
|
|
"Erroneous write, i=%u", i); \
|
2014-08-07 14:36:19 +08:00
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
|
2016-11-08 03:27:48 +08:00
|
|
|
TEST_STRUCT(u64, uint64_t)
|
2017-01-16 08:56:30 +08:00
|
|
|
TEST_BEGIN(test_atomic_u64) {
|
2014-08-07 14:36:19 +08:00
|
|
|
#if !(LG_SIZEOF_PTR == 3 || LG_SIZEOF_INT == 3)
|
|
|
|
test_skip("64-bit atomic operations not supported");
|
|
|
|
#else
|
2016-11-08 03:27:48 +08:00
|
|
|
TEST_BODY(u64, uint64_t, uint64_t, u64, FMTx64);
|
2014-08-07 14:36:19 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
TEST_END
|
|
|
|
|
2016-11-08 03:27:48 +08:00
|
|
|
TEST_STRUCT(u32, uint32_t)
|
2017-01-16 08:56:30 +08:00
|
|
|
TEST_BEGIN(test_atomic_u32) {
|
2016-11-08 03:27:48 +08:00
|
|
|
TEST_BODY(u32, uint32_t, uint32_t, u32, "#"FMTx32);
|
2015-02-03 05:49:08 +08:00
|
|
|
}
|
|
|
|
TEST_END
|
|
|
|
|
|
|
|
TEST_STRUCT(p, void *)
|
2017-01-16 08:56:30 +08:00
|
|
|
TEST_BEGIN(test_atomic_p) {
|
2015-02-03 05:49:08 +08:00
|
|
|
TEST_BODY(p, void *, uintptr_t, ptr, "p");
|
2014-08-07 14:36:19 +08:00
|
|
|
}
|
|
|
|
TEST_END
|
|
|
|
|
2016-11-08 03:27:48 +08:00
|
|
|
TEST_STRUCT(zu, size_t)
|
2017-01-16 08:56:30 +08:00
|
|
|
TEST_BEGIN(test_atomic_zu) {
|
2016-11-08 03:27:48 +08:00
|
|
|
TEST_BODY(zu, size_t, size_t, zu, "#zx");
|
2014-08-07 14:36:19 +08:00
|
|
|
}
|
|
|
|
TEST_END
|
|
|
|
|
|
|
|
TEST_STRUCT(u, unsigned)
|
2017-01-16 08:56:30 +08:00
|
|
|
TEST_BEGIN(test_atomic_u) {
|
2015-02-03 05:49:08 +08:00
|
|
|
TEST_BODY(u, unsigned, unsigned, u, "#x");
|
2014-08-07 14:36:19 +08:00
|
|
|
}
|
|
|
|
TEST_END
|
|
|
|
|
|
|
|
int
|
2017-01-16 08:56:30 +08:00
|
|
|
main(void) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return test(
|
2016-11-08 03:27:48 +08:00
|
|
|
test_atomic_u64,
|
|
|
|
test_atomic_u32,
|
2015-02-03 05:49:08 +08:00
|
|
|
test_atomic_p,
|
2016-11-08 03:27:48 +08:00
|
|
|
test_atomic_zu,
|
2017-01-20 10:15:45 +08:00
|
|
|
test_atomic_u);
|
2014-08-07 14:36:19 +08:00
|
|
|
}
|