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 39static uint16_t x16; 40static uint32_t x32; 41static uint64_t x64; 42 43static uint16_t unconst16(uint16_t); 44static uint32_t unconst32(uint32_t); 45static 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 */ 52static uint16_t 53unconst16(uint16_t val) 54{ 55 return val + x16; 56} 57 58static uint32_t 59unconst32(uint32_t val) 60{ 61 return val + x32; 62} 63 64static uint64_t 65unconst64(uint64_t val) 66{ 67 return val + x64; 68} 69 70ATF_TC(bswap16_basic); 71ATF_TC_HEAD(bswap16_basic, tc) 72{ 73 atf_tc_set_md_var(tc, "descr", "A naive test of bswap16(3), #1"); 74} 75 76ATF_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 84ATF_TC(bswap16_unconst); 85ATF_TC_HEAD(bswap16_unconst, tc) 86{ 87 atf_tc_set_md_var(tc, "descr", "A naive test of bswap16(3), #2"); 88} 89 90ATF_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 100ATF_TC(bswap32_basic); 101ATF_TC_HEAD(bswap32_basic, tc) 102{ 103 atf_tc_set_md_var(tc, "descr", "A naive test of bswap32(3), #1"); 104} 105 106ATF_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 114ATF_TC(bswap32_unconst); 115ATF_TC_HEAD(bswap32_unconst, tc) 116{ 117 atf_tc_set_md_var(tc, "descr", "A naive test of bswap32(3), #2"); 118} 119 120ATF_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 130ATF_TC(bswap64_basic); 131ATF_TC_HEAD(bswap64_basic, tc) 132{ 133 atf_tc_set_md_var(tc, "descr", "A naive test of bswap64(3), #1"); 134} 135 136ATF_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 144ATF_TC(bswap64_unconst); 145ATF_TC_HEAD(bswap64_unconst, tc) 146{ 147 atf_tc_set_md_var(tc, "descr", "A naive test of bswap64(3), #2"); 148} 149 150ATF_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 167ATF_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