]>
Commit | Line | Data |
---|---|---|
1a4d82fc JJ |
1 | #include "test/jemalloc_test.h" |
2 | ||
3 | #define TEST_STRUCT(p, t) \ | |
4 | struct p##_test_s { \ | |
5 | t accum0; \ | |
6 | t x; \ | |
54a0048b | 7 | t s; \ |
1a4d82fc JJ |
8 | }; \ |
9 | typedef struct p##_test_s p##_test_t; | |
10 | ||
54a0048b | 11 | #define TEST_BODY(p, t, tc, ta, FMT) do { \ |
1a4d82fc | 12 | const p##_test_t tests[] = { \ |
54a0048b SL |
13 | {(t)-1, (t)-1, (t)-2}, \ |
14 | {(t)-1, (t) 0, (t)-2}, \ | |
15 | {(t)-1, (t) 1, (t)-2}, \ | |
1a4d82fc | 16 | \ |
54a0048b SL |
17 | {(t) 0, (t)-1, (t)-2}, \ |
18 | {(t) 0, (t) 0, (t)-2}, \ | |
19 | {(t) 0, (t) 1, (t)-2}, \ | |
1a4d82fc | 20 | \ |
54a0048b SL |
21 | {(t) 1, (t)-1, (t)-2}, \ |
22 | {(t) 1, (t) 0, (t)-2}, \ | |
23 | {(t) 1, (t) 1, (t)-2}, \ | |
1a4d82fc | 24 | \ |
54a0048b SL |
25 | {(t)0, (t)-(1 << 22), (t)-2}, \ |
26 | {(t)0, (t)(1 << 22), (t)-2}, \ | |
27 | {(t)(1 << 22), (t)-(1 << 22), (t)-2}, \ | |
28 | {(t)(1 << 22), (t)(1 << 22), (t)-2} \ | |
1a4d82fc JJ |
29 | }; \ |
30 | unsigned i; \ | |
31 | \ | |
32 | for (i = 0; i < sizeof(tests)/sizeof(p##_test_t); i++) { \ | |
54a0048b | 33 | bool err; \ |
1a4d82fc | 34 | t accum = tests[i].accum0; \ |
54a0048b SL |
35 | assert_##ta##_eq(atomic_read_##p(&accum), \ |
36 | tests[i].accum0, \ | |
37 | "Erroneous read, i=%u", i); \ | |
38 | \ | |
39 | assert_##ta##_eq(atomic_add_##p(&accum, tests[i].x), \ | |
40 | (t)((tc)tests[i].accum0 + (tc)tests[i].x), \ | |
41 | "i=%u, accum=%"FMT", x=%"FMT, \ | |
1a4d82fc | 42 | i, tests[i].accum0, tests[i].x); \ |
54a0048b SL |
43 | assert_##ta##_eq(atomic_read_##p(&accum), accum, \ |
44 | "Erroneous add, i=%u", i); \ | |
1a4d82fc JJ |
45 | \ |
46 | accum = tests[i].accum0; \ | |
54a0048b SL |
47 | assert_##ta##_eq(atomic_sub_##p(&accum, tests[i].x), \ |
48 | (t)((tc)tests[i].accum0 - (tc)tests[i].x), \ | |
49 | "i=%u, accum=%"FMT", x=%"FMT, \ | |
1a4d82fc | 50 | i, tests[i].accum0, tests[i].x); \ |
54a0048b SL |
51 | assert_##ta##_eq(atomic_read_##p(&accum), accum, \ |
52 | "Erroneous sub, i=%u", i); \ | |
53 | \ | |
54 | accum = tests[i].accum0; \ | |
55 | err = atomic_cas_##p(&accum, tests[i].x, tests[i].s); \ | |
56 | assert_b_eq(err, tests[i].accum0 != tests[i].x, \ | |
57 | "Erroneous cas success/failure result"); \ | |
58 | assert_##ta##_eq(accum, err ? tests[i].accum0 : \ | |
59 | tests[i].s, "Erroneous cas effect, i=%u", i); \ | |
60 | \ | |
61 | accum = tests[i].accum0; \ | |
62 | atomic_write_##p(&accum, tests[i].s); \ | |
63 | assert_##ta##_eq(accum, tests[i].s, \ | |
64 | "Erroneous write, i=%u", i); \ | |
1a4d82fc JJ |
65 | } \ |
66 | } while (0) | |
67 | ||
68 | TEST_STRUCT(uint64, uint64_t) | |
69 | TEST_BEGIN(test_atomic_uint64) | |
70 | { | |
71 | ||
72 | #if !(LG_SIZEOF_PTR == 3 || LG_SIZEOF_INT == 3) | |
73 | test_skip("64-bit atomic operations not supported"); | |
74 | #else | |
54a0048b | 75 | TEST_BODY(uint64, uint64_t, uint64_t, u64, FMTx64); |
1a4d82fc JJ |
76 | #endif |
77 | } | |
78 | TEST_END | |
79 | ||
80 | TEST_STRUCT(uint32, uint32_t) | |
81 | TEST_BEGIN(test_atomic_uint32) | |
82 | { | |
83 | ||
54a0048b SL |
84 | TEST_BODY(uint32, uint32_t, uint32_t, u32, "#"FMTx32); |
85 | } | |
86 | TEST_END | |
87 | ||
88 | TEST_STRUCT(p, void *) | |
89 | TEST_BEGIN(test_atomic_p) | |
90 | { | |
91 | ||
92 | TEST_BODY(p, void *, uintptr_t, ptr, "p"); | |
1a4d82fc JJ |
93 | } |
94 | TEST_END | |
95 | ||
96 | TEST_STRUCT(z, size_t) | |
97 | TEST_BEGIN(test_atomic_z) | |
98 | { | |
99 | ||
54a0048b | 100 | TEST_BODY(z, size_t, size_t, zu, "#zx"); |
1a4d82fc JJ |
101 | } |
102 | TEST_END | |
103 | ||
104 | TEST_STRUCT(u, unsigned) | |
105 | TEST_BEGIN(test_atomic_u) | |
106 | { | |
107 | ||
54a0048b | 108 | TEST_BODY(u, unsigned, unsigned, u, "#x"); |
1a4d82fc JJ |
109 | } |
110 | TEST_END | |
111 | ||
112 | int | |
113 | main(void) | |
114 | { | |
115 | ||
116 | return (test( | |
117 | test_atomic_uint64, | |
118 | test_atomic_uint32, | |
54a0048b | 119 | test_atomic_p, |
1a4d82fc JJ |
120 | test_atomic_z, |
121 | test_atomic_u)); | |
122 | } |