xref: /netbsd-src/tests/lib/libc/atomic/t_atomic_cas.c (revision ecae8bc568195ba950f3f448ebe39e4842db91e9)
1 /*	$NetBSD: t_atomic_cas.c,v 1.1 2019/02/17 12:24:17 isaki Exp $	*/
2 
3 /*
4  * Copyright (C) 2019 Tetsuya Isaki. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
20  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
22  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include <sys/cdefs.h>
29 __RCSID("$NetBSD: t_atomic_cas.c,v 1.1 2019/02/17 12:24:17 isaki Exp $");
30 
31 #include <atf-c.h>
32 #include <inttypes.h>
33 #include <sys/atomic.h>
34 
35 /*
36  * These tests don't examine the atomicity.
37  */
38 
39 #define OLDVAL (0x1122334455667788UL)
40 #define NEWVAL (0x8090a0b0c0d0e0f0UL)
41 
42 /*
43  * atomic_cas_*{,_ni}()
44  */
45 #define atf_cas(NAME, TYPE, FMT) \
46 ATF_TC(NAME); \
47 ATF_TC_HEAD(NAME, tc) \
48 { \
49 	atf_tc_set_md_var(tc, "descr", #NAME); \
50 } \
51 ATF_TC_BODY(NAME, tc) \
52 { \
53 	volatile TYPE val; \
54 	TYPE oldval; \
55 	TYPE newval; \
56 	TYPE expval; \
57 	TYPE expres; \
58 	TYPE res; \
59 	/* If successful */ \
60 	val = (TYPE)OLDVAL; \
61 	oldval = (TYPE)OLDVAL; \
62 	newval = (TYPE)NEWVAL; \
63 	expval = (TYPE)NEWVAL; \
64 	expres = (TYPE)OLDVAL; \
65 	res = NAME(&val, oldval, newval); \
66 	ATF_REQUIRE_MSG(val == expval, \
67 	    "successful case: val expects " FMT " but " FMT, expval, val); \
68 	ATF_REQUIRE_MSG(res == expres, \
69 	    "successful case: res expects " FMT " but " FMT, expres, res); \
70 	/* If failure */ \
71 	val = (TYPE)OLDVAL; \
72 	oldval = (TYPE)(OLDVAL + 1); \
73 	newval = (TYPE)NEWVAL; \
74 	expval = (TYPE)OLDVAL; \
75 	expres = (TYPE)OLDVAL; \
76 	res = NAME(&val, oldval, newval); \
77 	ATF_REQUIRE_MSG(val == expval, \
78 	    "failure case: val expects " FMT " but " FMT, expval, val); \
79 	ATF_REQUIRE_MSG(res == expres, \
80 	    "failure case: res expects " FMT " but " FMT, expres, res); \
81 }
82 
83 atf_cas(atomic_cas_32,       uint32_t,      "0x%" PRIx32);
84 atf_cas(atomic_cas_uint,     unsigned int,  "0x%x");
85 atf_cas(atomic_cas_ulong,    unsigned long, "0x%lx");
86 atf_cas(atomic_cas_ptr,      void *,        "%p");
87 #if defined(__HAVE_ATOMIC64_OPS)
88 atf_cas(atomic_cas_64,       uint64_t,      "0x%" PRIx64);
89 #endif
90 
91 atf_cas(atomic_cas_32_ni,    uint32_t,      "0x%" PRIx32);
92 atf_cas(atomic_cas_uint_ni,  unsigned int,  "0x%x");
93 atf_cas(atomic_cas_ulong_ni, unsigned long, "0x%lx");
94 atf_cas(atomic_cas_ptr_ni,   void *,        "%p");
95 #if defined(__HAVE_ATOMIC64_OPS)
96 atf_cas(atomic_cas_64_ni,    uint64_t,      "0x%" PRIx64);
97 #endif
98 
ATF_TP_ADD_TCS(tp)99 ATF_TP_ADD_TCS(tp)
100 {
101 	ATF_TP_ADD_TC(tp, atomic_cas_32);
102 	ATF_TP_ADD_TC(tp, atomic_cas_uint);
103 	ATF_TP_ADD_TC(tp, atomic_cas_ulong);
104 	ATF_TP_ADD_TC(tp, atomic_cas_ptr);
105 #if defined(__HAVE_ATOMIC64_OPS)
106 	ATF_TP_ADD_TC(tp, atomic_cas_64);
107 #endif
108 
109 	ATF_TP_ADD_TC(tp, atomic_cas_32_ni);
110 	ATF_TP_ADD_TC(tp, atomic_cas_uint_ni);
111 	ATF_TP_ADD_TC(tp, atomic_cas_ulong_ni);
112 	ATF_TP_ADD_TC(tp, atomic_cas_ptr_ni);
113 #if defined(__HAVE_ATOMIC64_OPS)
114 	ATF_TP_ADD_TC(tp, atomic_cas_64_ni);
115 #endif
116 
117 	return atf_no_error();
118 }
119