xref: /minix/tests/include/machine/t_bswap.c (revision 00b67f09)
1 /* $NetBSD: t_bswap.c,v 1.1 2011/05/05 05:39:11 jruoho Exp $ */
2 
3 /*-
4  * Copyright (c) 2011 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Jukka Ruohonen.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 #include <sys/cdefs.h>
32 __RCSID("$NetBSD: t_bswap.c,v 1.1 2011/05/05 05:39:11 jruoho Exp $");
33 
34 #include <sys/types.h>
35 #include <machine/bswap.h>
36 
37 #include <atf-c.h>
38 
39 static uint16_t x16;
40 static uint32_t x32;
41 static uint64_t x64;
42 
43 static uint16_t	unconst16(uint16_t);
44 static uint32_t	unconst32(uint32_t);
45 static uint64_t	unconst64(uint64_t);
46 
47 /*
48  * Given the use of __builtin_constant_p(3),
49  * these functions try to avoid gcc(1) from
50  * treating the arguments as constants.
51  */
52 static uint16_t
53 unconst16(uint16_t val)
54 {
55 	return val + x16;
56 }
57 
58 static uint32_t
59 unconst32(uint32_t val)
60 {
61 	return val + x32;
62 }
63 
64 static uint64_t
65 unconst64(uint64_t val)
66 {
67 	return val + x64;
68 }
69 
70 ATF_TC(bswap16_basic);
71 ATF_TC_HEAD(bswap16_basic, tc)
72 {
73 	atf_tc_set_md_var(tc, "descr", "A naive test of bswap16(3), #1");
74 }
75 
76 ATF_TC_BODY(bswap16_basic, tc)
77 {
78 	ATF_REQUIRE_EQ(bswap16(0x0000), 0x0000);
79 	ATF_REQUIRE_EQ(bswap16(0xff00), 0x00ff);
80 	ATF_REQUIRE_EQ(bswap16(0xffff), 0xffff);
81 	ATF_REQUIRE_EQ(bswap16(0x1234), 0x3412);
82 }
83 
84 ATF_TC(bswap16_unconst);
85 ATF_TC_HEAD(bswap16_unconst, tc)
86 {
87 	atf_tc_set_md_var(tc, "descr", "A naive test of bswap16(3), #2");
88 }
89 
90 ATF_TC_BODY(bswap16_unconst, tc)
91 {
92 	x16 = 0;
93 
94 	ATF_REQUIRE_EQ(bswap16(unconst16(0x0000)), 0x0000);
95 	ATF_REQUIRE_EQ(bswap16(unconst16(0xff00)), 0x00ff);
96 	ATF_REQUIRE_EQ(bswap16(unconst16(0xffff)), 0xffff);
97 	ATF_REQUIRE_EQ(bswap16(unconst16(0x1234)), 0x3412);
98 }
99 
100 ATF_TC(bswap32_basic);
101 ATF_TC_HEAD(bswap32_basic, tc)
102 {
103 	atf_tc_set_md_var(tc, "descr", "A naive test of bswap32(3), #1");
104 }
105 
106 ATF_TC_BODY(bswap32_basic, tc)
107 {
108 	ATF_REQUIRE_EQ(bswap32(0x00000000), 0x00000000);
109 	ATF_REQUIRE_EQ(bswap32(0xffff0000), 0x0000ffff);
110 	ATF_REQUIRE_EQ(bswap32(0xffffffff), 0xffffffff);
111 	ATF_REQUIRE_EQ(bswap32(0x12345678), 0x78563412);
112 }
113 
114 ATF_TC(bswap32_unconst);
115 ATF_TC_HEAD(bswap32_unconst, tc)
116 {
117 	atf_tc_set_md_var(tc, "descr", "A naive test of bswap32(3), #2");
118 }
119 
120 ATF_TC_BODY(bswap32_unconst, tc)
121 {
122 	x32 = 0;
123 
124 	ATF_REQUIRE_EQ(bswap32(unconst32(0x00000000)), 0x00000000);
125 	ATF_REQUIRE_EQ(bswap32(unconst32(0xffff0000)), 0x0000ffff);
126 	ATF_REQUIRE_EQ(bswap32(unconst32(0xffffffff)), 0xffffffff);
127 	ATF_REQUIRE_EQ(bswap32(unconst32(0x12345678)), 0x78563412);
128 }
129 
130 ATF_TC(bswap64_basic);
131 ATF_TC_HEAD(bswap64_basic, tc)
132 {
133 	atf_tc_set_md_var(tc, "descr", "A naive test of bswap64(3), #1");
134 }
135 
136 ATF_TC_BODY(bswap64_basic, tc)
137 {
138 	ATF_REQUIRE_EQ(bswap64(0x0000000000000000), 0x0000000000000000);
139 	ATF_REQUIRE_EQ(bswap64(0xffffffff00000000), 0x00000000ffffffff);
140 	ATF_REQUIRE_EQ(bswap64(0xffffffffffffffff), 0xffffffffffffffff);
141 	ATF_REQUIRE_EQ(bswap64(0x123456789abcdeff), 0xffdebc9a78563412);
142 }
143 
144 ATF_TC(bswap64_unconst);
145 ATF_TC_HEAD(bswap64_unconst, tc)
146 {
147 	atf_tc_set_md_var(tc, "descr", "A naive test of bswap64(3), #2");
148 }
149 
150 ATF_TC_BODY(bswap64_unconst, tc)
151 {
152 	x64 = 0;
153 
154 	ATF_REQUIRE_EQ(bswap64(unconst64(0x0000000000000000)),
155 	    0x0000000000000000);
156 
157 	ATF_REQUIRE_EQ(bswap64(unconst64(0xffffffff00000000)),
158 	    0x00000000ffffffff);
159 
160 	ATF_REQUIRE_EQ(bswap64(unconst64(0xffffffffffffffff)),
161 	    0xffffffffffffffff);
162 
163 	ATF_REQUIRE_EQ(bswap64(unconst64(0x123456789abcdeff)),
164 	    0xffdebc9a78563412);
165 }
166 
167 ATF_TP_ADD_TCS(tp)
168 {
169 
170 	ATF_TP_ADD_TC(tp, bswap16_basic);
171 	ATF_TP_ADD_TC(tp, bswap16_unconst);
172 	ATF_TP_ADD_TC(tp, bswap32_basic);
173 	ATF_TP_ADD_TC(tp, bswap32_unconst);
174 	ATF_TP_ADD_TC(tp, bswap64_basic);
175 	ATF_TP_ADD_TC(tp, bswap64_unconst);
176 
177 	return atf_no_error();
178 }
179