]> git.proxmox.com Git - rustc.git/blame - src/jemalloc/test/unit/atomic.c
Imported Upstream version 1.9.0+dfsg1
[rustc.git] / src / jemalloc / test / unit / atomic.c
CommitLineData
1a4d82fc
JJ
1#include "test/jemalloc_test.h"
2
3#define TEST_STRUCT(p, t) \
4struct p##_test_s { \
5 t accum0; \
6 t x; \
54a0048b 7 t s; \
1a4d82fc
JJ
8}; \
9typedef 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
68TEST_STRUCT(uint64, uint64_t)
69TEST_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}
78TEST_END
79
80TEST_STRUCT(uint32, uint32_t)
81TEST_BEGIN(test_atomic_uint32)
82{
83
54a0048b
SL
84 TEST_BODY(uint32, uint32_t, uint32_t, u32, "#"FMTx32);
85}
86TEST_END
87
88TEST_STRUCT(p, void *)
89TEST_BEGIN(test_atomic_p)
90{
91
92 TEST_BODY(p, void *, uintptr_t, ptr, "p");
1a4d82fc
JJ
93}
94TEST_END
95
96TEST_STRUCT(z, size_t)
97TEST_BEGIN(test_atomic_z)
98{
99
54a0048b 100 TEST_BODY(z, size_t, size_t, zu, "#zx");
1a4d82fc
JJ
101}
102TEST_END
103
104TEST_STRUCT(u, unsigned)
105TEST_BEGIN(test_atomic_u)
106{
107
54a0048b 108 TEST_BODY(u, unsigned, unsigned, u, "#x");
1a4d82fc
JJ
109}
110TEST_END
111
112int
113main(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}