xref: /netbsd-src/tests/lib/libc/atomic/t_atomic_inc.c (revision ecae8bc568195ba950f3f448ebe39e4842db91e9)
1 /*	$NetBSD: t_atomic_inc.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_inc.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 VAL    (0x1122334455667788UL)
40 #define EXPECT (0x1122334455667789UL)
41 
42 /*
43  * atomic_inc_*()
44  */
45 #define atf_inc(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 exp; \
55 	val = (TYPE)VAL; \
56 	exp = (TYPE)EXPECT; \
57 	NAME(&val); \
58 	ATF_REQUIRE_MSG(val == exp, \
59 	    "val expects " FMT " but " FMT, exp, val); \
60 }
61 
62 atf_inc(atomic_inc_32,    uint32_t,      "0x%" PRIx32);
63 atf_inc(atomic_inc_uint,  unsigned int,  "0x%x");
64 atf_inc(atomic_inc_ulong, unsigned long, "0x%lx");
65 atf_inc(atomic_inc_ptr,   void *,        "%p");
66 #if defined(__HAVE_ATOMIC64_OPS)
67 atf_inc(atomic_inc_64,    uint64_t,      "0x%" PRIx64);
68 #endif
69 
70 /*
71  * atomic_inc_*_nv()
72  */
73 #define atf_inc_nv(NAME, TYPE, FMT) \
74 ATF_TC(NAME); \
75 ATF_TC_HEAD(NAME, tc) \
76 { \
77 	atf_tc_set_md_var(tc, "descr", #NAME); \
78 } \
79 ATF_TC_BODY(NAME, tc) \
80 { \
81 	volatile TYPE val; \
82 	TYPE res; \
83 	TYPE exp; \
84 	val = (TYPE)VAL; \
85 	exp = (TYPE)EXPECT; \
86 	res = NAME(&val); \
87 	ATF_REQUIRE_MSG(val == exp, \
88 	    "val expects " FMT " but " FMT, exp, val); \
89 	ATF_REQUIRE_MSG(res == exp, \
90 	    "res expects " FMT " but " FMT, exp, res); \
91 }
92 
93 atf_inc_nv(atomic_inc_32_nv,    uint32_t,      "0x%" PRIx32);
94 atf_inc_nv(atomic_inc_uint_nv,  unsigned int,  "0x%x");
95 atf_inc_nv(atomic_inc_ulong_nv, unsigned long, "0x%lx");
96 atf_inc_nv(atomic_inc_ptr_nv,   void *,        "%p");
97 #if defined(__HAVE_ATOMIC64_OPS)
98 atf_inc_nv(atomic_inc_64_nv,    uint64_t,      "0x%" PRIx64);
99 #endif
100 
ATF_TP_ADD_TCS(tp)101 ATF_TP_ADD_TCS(tp)
102 {
103 	ATF_TP_ADD_TC(tp, atomic_inc_32);
104 	ATF_TP_ADD_TC(tp, atomic_inc_uint);
105 	ATF_TP_ADD_TC(tp, atomic_inc_ulong);
106 	ATF_TP_ADD_TC(tp, atomic_inc_ptr);
107 #if defined(__HAVE_ATOMIC64_OPS)
108 	ATF_TP_ADD_TC(tp, atomic_inc_64);
109 #endif
110 
111 	ATF_TP_ADD_TC(tp, atomic_inc_32_nv);
112 	ATF_TP_ADD_TC(tp, atomic_inc_uint_nv);
113 	ATF_TP_ADD_TC(tp, atomic_inc_ulong_nv);
114 	ATF_TP_ADD_TC(tp, atomic_inc_ptr_nv);
115 #if defined(__HAVE_ATOMIC64_OPS)
116 	ATF_TP_ADD_TC(tp, atomic_inc_64_nv);
117 #endif
118 
119 	return atf_no_error();
120 }
121