iPXE
byteswap_test.c
Go to the documentation of this file.
1/*
2 * Copyright (C) 2012 Michael Brown <mbrown@fensystems.co.uk>.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA.
18 *
19 * You can also choose to distribute this program under the terms of
20 * the Unmodified Binary Distribution Licence (as given in the file
21 * COPYING.UBDL), provided that you have satisfied its requirements.
22 */
23
24FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25
26/** @file
27 *
28 * Byte-order swapping test functions
29 *
30 */
31
32/* Forcibly enable assertions */
33#undef NDEBUG
34
35#include <stdint.h>
36#include <assert.h>
37#include <byteswap.h>
38#include <ipxe/test.h>
39
40/* Provide global functions to allow inspection of generated assembly code */
41
43 return __bswap_16 ( x );
44}
45
47 return __bswap_32 ( x );
48}
49
51 return __bswap_64 ( x );
52}
53
55 __bswap_16s ( x );
56}
57
59 __bswap_32s ( x );
60}
61
63 __bswap_64s ( x );
64}
65
66/**
67 * Perform byte-order swapping
68 *
69 */
70static void byteswap_test_exec ( void ) {
71 uint16_t test16;
72 uint32_t test32;
73 uint64_t test64;
74
75 ok ( test_bswap16 ( 0x1234 ) == 0x3412 );
76 ok ( test_bswap32 ( 0x12345678UL ) == 0x78563412UL );
77 ok ( test_bswap64 ( 0x123456789abcdef0ULL ) == 0xf0debc9a78563412ULL );
78
79 test16 = 0xabcd;
80 test_bswap16s ( &test16 );
81 ok ( test16 == 0xcdab );
82
83 test32 = 0xabcdef01UL;
84 test_bswap32s ( &test32 );
85 ok ( test32 == 0x01efcdabUL );
86
87 test64 = 0xabcdef0123456789ULL;
88 test_bswap64s ( &test64 );
89 ok ( test64 == 0x8967452301efcdabULL );
90}
91
92/** Byte-order swapping self-test */
93struct self_test byteswap_test __self_test = {
94 .name = "byteswap",
95 .exec = byteswap_test_exec,
96};
unsigned short uint16_t
Definition stdint.h:11
unsigned int uint32_t
Definition stdint.h:12
unsigned long long uint64_t
Definition stdint.h:13
Assertions.
uint16_t test_bswap16(uint16_t x)
uint64_t test_bswap64(uint64_t x)
void test_bswap64s(uint64_t *x)
void test_bswap32s(uint32_t *x)
uint32_t test_bswap32(uint32_t x)
static void byteswap_test_exec(void)
Perform byte-order swapping.
void test_bswap16s(uint16_t *x)
#define FILE_LICENCE(_licence)
Declare a particular licence as applying to a file.
Definition compiler.h:896
#define __bswap_64(value)
Byte-swap a 64-bit value.
Definition byteswap.h:79
#define __bswap_16(value)
Byte-swap a 16-bit value.
Definition byteswap.h:55
#define __bswap_32(value)
Byte-swap a 32-bit value.
Definition byteswap.h:67
static unsigned int x
Definition pixbuf.h:63
A self-test set.
Definition test.h:15
Self-test infrastructure.
#define ok(success)
Definition test.h:46
#define __self_test
Declare a self-test.
Definition test.h:32