2014-08-07 14:36:19 +08:00
|
|
|
#include "test/jemalloc_test.h"
|
|
|
|
|
|
|
|
#define TEST_STRUCT(p, t) \
|
|
|
|
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;
|
|
|
|
|
2015-02-03 05:49:08 +08:00
|
|
|
#define TEST_BODY(p, t, tc, ta, PRI) 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), \
|
|
|
|
"i=%u, accum=%"PRI", x=%"PRI, \
|
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), \
|
|
|
|
"i=%u, accum=%"PRI", x=%"PRI, \
|
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)
|
|
|
|
|
|
|
|
TEST_STRUCT(uint64, uint64_t)
|
|
|
|
TEST_BEGIN(test_atomic_uint64)
|
|
|
|
{
|
|
|
|
|
|
|
|
#if !(LG_SIZEOF_PTR == 3 || LG_SIZEOF_INT == 3)
|
|
|
|
test_skip("64-bit atomic operations not supported");
|
|
|
|
#else
|
2015-02-03 05:49:08 +08:00
|
|
|
TEST_BODY(uint64, uint64_t, uint64_t, u64, PRIx64);
|
2014-08-07 14:36:19 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
TEST_END
|
|
|
|
|
|
|
|
TEST_STRUCT(uint32, uint32_t)
|
|
|
|
TEST_BEGIN(test_atomic_uint32)
|
|
|
|
{
|
|
|
|
|
2015-02-03 05:49:08 +08:00
|
|
|
TEST_BODY(uint32, uint32_t, uint32_t, u32, "#"PRIx32);
|
|
|
|
}
|
|
|
|
TEST_END
|
|
|
|
|
|
|
|
TEST_STRUCT(p, void *)
|
|
|
|
TEST_BEGIN(test_atomic_p)
|
|
|
|
{
|
|
|
|
|
|
|
|
TEST_BODY(p, void *, uintptr_t, ptr, "p");
|
2014-08-07 14:36:19 +08:00
|
|
|
}
|
|
|
|
TEST_END
|
|
|
|
|
|
|
|
TEST_STRUCT(z, size_t)
|
|
|
|
TEST_BEGIN(test_atomic_z)
|
|
|
|
{
|
|
|
|
|
2015-02-03 05:49:08 +08:00
|
|
|
TEST_BODY(z, size_t, size_t, zu, "#zx");
|
2014-08-07 14:36:19 +08:00
|
|
|
}
|
|
|
|
TEST_END
|
|
|
|
|
|
|
|
TEST_STRUCT(u, unsigned)
|
|
|
|
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
|
|
|
|
main(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
return (test(
|
|
|
|
test_atomic_uint64,
|
|
|
|
test_atomic_uint32,
|
2015-02-03 05:49:08 +08:00
|
|
|
test_atomic_p,
|
2014-08-07 14:36:19 +08:00
|
|
|
test_atomic_z,
|
|
|
|
test_atomic_u));
|
|
|
|
}
|