Branch
Hash :
7b089321
Author :
Date :
2025-01-01T09:24:36
maint: run 'make update-copyright'
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
/* Test of <endian.h> substitute.
Copyright (C) 2024-2025 Free Software Foundation, Inc.
This file is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published
by the Free Software Foundation, either version 3 of the License,
or (at your option) any later version.
This file is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. */
/* Written by Collin Funk <collin.funk1@gmail.com>, 2024. */
#include <config.h>
/* Specification. */
#include <endian.h>
/* Check for uint16_t and uint32_t. */
uint16_t t1;
uint32_t t2;
/* The next POSIX revision requires 64-bit types. Gnulib doesn't. */
#if 0
uint64_t t3;
#endif
/* "These macros shall be suitable for use in #if preprocessing directives." */
#if BYTE_ORDER == LITTLE_ENDIAN
int a = 17;
#endif
#if BYTE_ORDER == BIG_ENDIAN
int a = 19;
#endif
/* "The macros BIG_ENDIAN and LITTLE_ENDIAN shall have distinct values." */
static_assert (LITTLE_ENDIAN != BIG_ENDIAN);
static_assert (BYTE_ORDER == LITTLE_ENDIAN || BYTE_ORDER == BIG_ENDIAN);
#include <stdint.h>
#include "macros.h"
/* Test byte order conversion functions with constant values. */
static void
test_convert_constant (void)
{
#if BYTE_ORDER == BIG_ENDIAN
/* 16-bit. */
ASSERT (be16toh (UINT16_C (0x1234)) == UINT16_C (0x1234));
ASSERT (htobe16 (UINT16_C (0x1234)) == UINT16_C (0x1234));
ASSERT (le16toh (UINT16_C (0x1234)) == UINT16_C (0x3412));
ASSERT (htole16 (UINT16_C (0x1234)) == UINT16_C (0x3412));
/* 32-bit. */
ASSERT (be32toh (UINT32_C (0x12345678)) == UINT32_C (0x12345678));
ASSERT (htobe32 (UINT32_C (0x12345678)) == UINT32_C (0x12345678));
ASSERT (le32toh (UINT32_C (0x12345678)) == UINT32_C (0x78563412));
ASSERT (htole32 (UINT32_C (0x12345678)) == UINT32_C (0x78563412));
/* 64-bit. */
# ifdef UINT64_MAX
ASSERT (be64toh (UINT64_C (0x1234567890ABCDEF))
== UINT64_C (0x1234567890ABCDEF));
ASSERT (htobe64 (UINT64_C (0x1234567890ABCDEF))
== UINT64_C (0x1234567890ABCDEF));
ASSERT (le64toh (UINT64_C (0x1234567890ABCDEF))
== UINT64_C (0xEFCDAB9078563412));
ASSERT (htole64 (UINT64_C (0x1234567890ABCDEF))
== UINT64_C (0xEFCDAB9078563412));
# endif
#else
/* 16-bit. */
ASSERT (be16toh (UINT16_C (0x1234)) == UINT16_C (0x3412));
ASSERT (htobe16 (UINT16_C (0x1234)) == UINT16_C (0x3412));
ASSERT (le16toh (UINT16_C (0x1234)) == UINT16_C (0x1234));
ASSERT (htole16 (UINT16_C (0x1234)) == UINT16_C (0x1234));
/* 32-bit. */
ASSERT (be32toh (UINT32_C (0x12345678)) == UINT32_C (0x78563412));
ASSERT (htobe32 (UINT32_C (0x12345678)) == UINT32_C (0x78563412));
ASSERT (le32toh (UINT32_C (0x12345678)) == UINT32_C (0x12345678));
ASSERT (htole32 (UINT32_C (0x12345678)) == UINT32_C (0x12345678));
/* 64-bit. */
# ifdef UINT64_MAX
ASSERT (be64toh (UINT64_C (0x1234567890ABCDEF))
== UINT64_C (0xEFCDAB9078563412));
ASSERT (htobe64 (UINT64_C (0x1234567890ABCDEF))
== UINT64_C (0xEFCDAB9078563412));
ASSERT (le64toh (UINT64_C (0x1234567890ABCDEF))
== UINT64_C (0x1234567890ABCDEF));
ASSERT (htole64 (UINT64_C (0x1234567890ABCDEF))
== UINT64_C (0x1234567890ABCDEF));
# endif
#endif
}
/* Test that the byte order conversion functions evaluate their
arguments once. */
static void
test_convert_eval_once (void)
{
/* 16-bit. */
{
uint16_t value = 0;
ASSERT (be16toh (value++) == 0);
ASSERT (value == 1);
}
{
uint16_t value = 0;
ASSERT (htobe16 (value++) == 0);
ASSERT (value == 1);
}
{
uint16_t value = 0;
ASSERT (le16toh (value++) == 0);
ASSERT (value == 1);
}
{
uint16_t value = 0;
ASSERT (htole16 (value++) == 0);
ASSERT (value == 1);
}
/* 32-bit. */
{
uint32_t value = 0;
ASSERT (be32toh (value++) == 0);
ASSERT (value == 1);
}
{
uint32_t value = 0;
ASSERT (htobe32 (value++) == 0);
ASSERT (value == 1);
}
{
uint32_t value = 0;
ASSERT (le32toh (value++) == 0);
ASSERT (value == 1);
}
{
uint32_t value = 0;
ASSERT (htole32 (value++) == 0);
ASSERT (value == 1);
}
/* 64-bit. */
#ifdef UINT64_MAX
{
uint64_t value = 0;
ASSERT (be64toh (value++) == 0);
ASSERT (value == 1);
}
{
uint64_t value = 0;
ASSERT (htobe64 (value++) == 0);
ASSERT (value == 1);
}
{
uint64_t value = 0;
ASSERT (le64toh (value++) == 0);
ASSERT (value == 1);
}
{
uint64_t value = 0;
ASSERT (htole64 (value++) == 0);
ASSERT (value == 1);
}
#endif
}
/* Test that the byte order conversion functions accept floating-point
arguments. */
static void
test_convert_double (void)
{
/* 16-bit. */
ASSERT (be16toh (0.0) == 0);
ASSERT (htobe16 (0.0) == 0);
ASSERT (le16toh (0.0) == 0);
ASSERT (htole16 (0.0) == 0);
/* 32-bit. */
ASSERT (be32toh (0.0) == 0);
ASSERT (htobe32 (0.0) == 0);
ASSERT (le32toh (0.0) == 0);
ASSERT (htole32 (0.0) == 0);
/* 64-bit. */
#ifdef UINT64_MAX
ASSERT (be64toh (0.0) == 0);
ASSERT (htobe64 (0.0) == 0);
ASSERT (le64toh (0.0) == 0);
ASSERT (htole64 (0.0) == 0);
#endif
}
int
main (void)
{
test_convert_constant ();
test_convert_eval_once ();
test_convert_double ();
return test_exit_status;
}