xref: /netbsd-src/tests/lib/libc/atomic/t_atomic_add.c (revision ecae8bc568195ba950f3f448ebe39e4842db91e9)
1 /*	$NetBSD: t_atomic_add.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_add.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 DST    (0x1122334455667788UL)
40 #define SRC    (0xf0e0d0c0b0a09081UL)
41 #define EXPECT (0x0203040506070809UL)
42 
43 /*
44  * atomic_add_*()
45  */
46 #define atf_add(NAME, DTYPE, STYPE, FMT) \
47 ATF_TC(NAME); \
48 ATF_TC_HEAD(NAME, tc) \
49 { \
50 	atf_tc_set_md_var(tc, "descr", #NAME); \
51 } \
52 ATF_TC_BODY(NAME, tc) \
53 { \
54 	volatile DTYPE val; \
55 	STYPE src; \
56 	DTYPE exp; \
57 	val = (DTYPE)DST; \
58 	src = (STYPE)SRC; \
59 	exp = (DTYPE)EXPECT; \
60 	NAME(&val, src); \
61 	ATF_REQUIRE_MSG(val == exp, \
62 	    "val expects " FMT " but " FMT, exp, val); \
63 }
64 
65 atf_add(atomic_add_32,   uint32_t,      int32_t, "0x%" PRIx32);
66 atf_add(atomic_add_int,  unsigned int,  int,     "0x%x");
67 atf_add(atomic_add_long, unsigned long, long,    "0x%lx");
68 atf_add(atomic_add_ptr,  void *,        ssize_t, "%p");
69 #if defined(__HAVE_ATOMIC64_OPS)
70 atf_add(atomic_add_64,   uint64_t,      int64_t, "0x%" PRIx64);
71 #endif
72 
73 /*
74  * atomic_add_*_nv()
75  */
76 #define atf_add_nv(NAME, DTYPE, STYPE, FMT) \
77 ATF_TC(NAME); \
78 ATF_TC_HEAD(NAME, tc) \
79 { \
80 	atf_tc_set_md_var(tc, "descr", #NAME); \
81 } \
82 ATF_TC_BODY(NAME, tc) \
83 { \
84 	volatile DTYPE val; \
85 	STYPE src; \
86 	DTYPE res; \
87 	DTYPE exp; \
88 	val = (DTYPE)DST; \
89 	src = (STYPE)SRC; \
90 	exp = (DTYPE)EXPECT; \
91 	res = NAME(&val, src); \
92 	ATF_REQUIRE_MSG(val == exp, \
93 	    "val expects " FMT " but " FMT, exp, val); \
94 	ATF_REQUIRE_MSG(res == exp, \
95 	    "res expects " FMT " but " FMT, exp, res); \
96 }
97 
98 atf_add_nv(atomic_add_32_nv,   uint32_t,      int32_t, "0x%" PRIx32);
99 atf_add_nv(atomic_add_int_nv,  unsigned int,  int,     "0x%x");
100 atf_add_nv(atomic_add_long_nv, unsigned long, long,    "0x%lx");
101 atf_add_nv(atomic_add_ptr_nv,  void *,        ssize_t, "%p");
102 #if defined(__HAVE_ATOMIC64_OPS)
103 atf_add_nv(atomic_add_64_nv,   uint64_t,      int64_t, "0x%" PRIx64);
104 #endif
105 
ATF_TP_ADD_TCS(tp)106 ATF_TP_ADD_TCS(tp)
107 {
108 	ATF_TP_ADD_TC(tp, atomic_add_32);
109 	ATF_TP_ADD_TC(tp, atomic_add_int);
110 	ATF_TP_ADD_TC(tp, atomic_add_long);
111 	ATF_TP_ADD_TC(tp, atomic_add_ptr);
112 #if defined(__HAVE_ATOMIC64_OPS)
113 	ATF_TP_ADD_TC(tp, atomic_add_64);
114 #endif
115 
116 	ATF_TP_ADD_TC(tp, atomic_add_32_nv);
117 	ATF_TP_ADD_TC(tp, atomic_add_int_nv);
118 	ATF_TP_ADD_TC(tp, atomic_add_long_nv);
119 	ATF_TP_ADD_TC(tp, atomic_add_ptr_nv);
120 #if defined(__HAVE_ATOMIC64_OPS)
121 	ATF_TP_ADD_TC(tp, atomic_add_64_nv);
122 #endif
123 
124 	return atf_no_error();
125 }
126