Update Red Hat Copyright Notices
[nbdkit.git] / common / include / test-byte-swapping.c
blob417293b9f022937e8e95163028787eaab7d8fae1
1 /* nbdkit
2 * Copyright Red Hat
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
11 * * 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.
15 * * Neither the name of Red Hat nor the names of its contributors may be
16 * used to endorse or promote products derived from this software without
17 * specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
22 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
26 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
29 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
33 #include <config.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <stdint.h>
38 #include <string.h>
39 #undef NDEBUG /* Keep test strong even for nbdkit built without assertions */
40 #include <assert.h>
42 #include "byte-swapping.h"
44 /* Little-endian test strings. */
45 static uint8_t le16[] = { 0x34, 0x12 };
46 static uint8_t le32[] = { 0x78, 0x56, 0x34, 0x12 };
47 static uint8_t le64[] = { 0xf0, 0xde, 0xbc, 0x9a, 0x78, 0x56, 0x34, 0x12 };
49 /* Big-endian test strings. */
50 static uint8_t be16[] = { 0x12, 0x34 };
51 static uint8_t be32[] = { 0x12, 0x34, 0x56, 0x78 };
52 static uint8_t be64[] = { 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0 };
54 int
55 main (void)
57 uint16_t i16;
58 uint32_t i32;
59 uint64_t i64;
61 memcpy (&i16, le16, 2);
62 assert (le16toh (i16) == 0x1234);
63 memcpy (&i32, le32, 4);
64 assert (le32toh (i32) == 0x12345678);
65 memcpy (&i64, le64, 8);
66 assert (le64toh (i64) == 0x123456789abcdef0);
68 memcpy (&i16, be16, 2);
69 assert (be16toh (i16) == 0x1234);
70 memcpy (&i32, be32, 4);
71 assert (be32toh (i32) == 0x12345678);
72 memcpy (&i64, be64, 8);
73 assert (be64toh (i64) == 0x123456789abcdef0);
75 i16 = htole16 (0x1234);
76 assert (memcmp (&i16, le16, 2) == 0);
77 i32 = htole32 (0x12345678);
78 assert (memcmp (&i32, le32, 4) == 0);
79 i64 = htole64 (0x123456789abcdef0);
80 assert (memcmp (&i64, le64, 8) == 0);
82 i16 = htobe16 (0x1234);
83 assert (memcmp (&i16, be16, 2) == 0);
84 i32 = htobe32 (0x12345678);
85 assert (memcmp (&i32, be32, 4) == 0);
86 i64 = htobe64 (0x123456789abcdef0);
87 assert (memcmp (&i64, be64, 8) == 0);
89 memcpy (&i16, le16, 2);
90 i16 = bswap_16 (i16);
91 assert (memcmp (&i16, be16, 2) == 0);
92 i16 = bswap_16 (i16);
93 assert (memcmp (&i16, le16, 2) == 0);
95 memcpy (&i32, le32, 4);
96 i32 = bswap_32 (i32);
97 assert (memcmp (&i32, be32, 4) == 0);
98 i32 = bswap_32 (i32);
99 assert (memcmp (&i32, le32, 4) == 0);
101 memcpy (&i64, le64, 8);
102 i64 = bswap_64 (i64);
103 assert (memcmp (&i64, be64, 8) == 0);
104 i64 = bswap_64 (i64);
105 assert (memcmp (&i64, le64, 8) == 0);
107 exit (EXIT_SUCCESS);