1 /* $NetBSD: t_bswap.c,v 1.1 2011/05/05 05:39:11 jruoho Exp $ */
4 * Copyright (c) 2011 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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.
31 #include <sys/cdefs.h>
32 __RCSID("$NetBSD: t_bswap.c,v 1.1 2011/05/05 05:39:11 jruoho Exp $");
34 #include <sys/types.h>
35 #include <machine/bswap.h>
43 static uint16_t unconst16(uint16_t);
44 static uint32_t unconst32(uint32_t);
45 static uint64_t unconst64(uint64_t);
48 * Given the use of __builtin_constant_p(3),
49 * these functions try to avoid gcc(1) from
50 * treating the arguments as constants.
53 unconst16(uint16_t val
)
59 unconst32(uint32_t val
)
65 unconst64(uint64_t val
)
70 ATF_TC(bswap16_basic
);
71 ATF_TC_HEAD(bswap16_basic
, tc
)
73 atf_tc_set_md_var(tc
, "descr", "A naive test of bswap16(3), #1");
76 ATF_TC_BODY(bswap16_basic
, tc
)
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);
84 ATF_TC(bswap16_unconst
);
85 ATF_TC_HEAD(bswap16_unconst
, tc
)
87 atf_tc_set_md_var(tc
, "descr", "A naive test of bswap16(3), #2");
90 ATF_TC_BODY(bswap16_unconst
, tc
)
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);
100 ATF_TC(bswap32_basic
);
101 ATF_TC_HEAD(bswap32_basic
, tc
)
103 atf_tc_set_md_var(tc
, "descr", "A naive test of bswap32(3), #1");
106 ATF_TC_BODY(bswap32_basic
, tc
)
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);
114 ATF_TC(bswap32_unconst
);
115 ATF_TC_HEAD(bswap32_unconst
, tc
)
117 atf_tc_set_md_var(tc
, "descr", "A naive test of bswap32(3), #2");
120 ATF_TC_BODY(bswap32_unconst
, tc
)
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);
130 ATF_TC(bswap64_basic
);
131 ATF_TC_HEAD(bswap64_basic
, tc
)
133 atf_tc_set_md_var(tc
, "descr", "A naive test of bswap64(3), #1");
136 ATF_TC_BODY(bswap64_basic
, tc
)
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);
144 ATF_TC(bswap64_unconst
);
145 ATF_TC_HEAD(bswap64_unconst
, tc
)
147 atf_tc_set_md_var(tc
, "descr", "A naive test of bswap64(3), #2");
150 ATF_TC_BODY(bswap64_unconst
, tc
)
154 ATF_REQUIRE_EQ(bswap64(unconst64(0x0000000000000000)),
157 ATF_REQUIRE_EQ(bswap64(unconst64(0xffffffff00000000)),
160 ATF_REQUIRE_EQ(bswap64(unconst64(0xffffffffffffffff)),
163 ATF_REQUIRE_EQ(bswap64(unconst64(0x123456789abcdeff)),
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
);
177 return atf_no_error();