geniconvtbl: small cstyle fix
[unleashed.git] / contrib / libjeffpc / test_atomic-single-thread.c
blob2c5a6bdb883bca576ad87676a5e7b527f9c3d09f
1 /*
2 * Copyright (c) 2016 Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
23 #include <jeffpc/atomic.h>
24 #include <jeffpc/int.h>
26 #include "test.c"
28 #define INITIAL 5ll
30 static void check(const char *type, const char *msg, uint64_t got, uint64_t exp)
32 fprintf(stderr, "%s: checking: %s\n", type, msg);
33 fprintf(stderr, "%s: expected: %"PRIu64"\n", type, exp);
34 fprintf(stderr, "%s: got: %"PRIu64"\n", type, got);
36 if (exp != got)
37 fail("%s mismatch!", type);
40 #define TEST(type, negone) \
41 do { \
42 const char *t = #type; \
43 fprintf(stderr, "%s: testing...\n", t); \
45 do { \
46 type v; \
48 atomic_set(&v, INITIAL); \
50 /* yes, we are reaching into the implementation here */ \
51 check(t, "contents valid", v.v, INITIAL); \
53 check(t, "read-after-init", atomic_read(&v), INITIAL); \
55 check(t, "increment-return", atomic_inc(&v), INITIAL + 1);\
56 check(t, "read-after-inc", atomic_read(&v), INITIAL + 1);\
57 check(t, "add-return", atomic_add(&v, 10), INITIAL + 11);\
58 check(t, "read-after-add", atomic_read(&v), INITIAL + 11);\
60 check(t, "decrement-return", atomic_dec(&v), INITIAL + 10);\
61 check(t, "read-after-dec", atomic_read(&v), INITIAL + 10);\
62 check(t, "sub-return", atomic_sub(&v, 10), INITIAL); \
63 check(t, "read-after-sub", atomic_read(&v), INITIAL); \
65 check(t, "go-to-zero", atomic_sub(&v, INITIAL), 0); \
66 check(t, "go-negative", atomic_dec(&v), negone); \
67 check(t, "go-more-negative", atomic_sub(&v, 10), \
68 negone - 10); \
70 check(t, "go-positive", atomic_add(&v, 20), 9); \
72 check(t, "cas-match", atomic_cas(&v, 9, 50), 9); \
73 check(t, "cas-mismatch", atomic_cas(&v, 50, 1), 50); \
74 } while (0); \
76 fprintf(stderr, "%s: ok.\n", t); \
77 } while (0)
79 void test(void)
81 TEST(atomic_t, 4294967295);
82 TEST(atomic64_t, 18446744073709551615ull);