diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 15:24:08 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 15:24:08 +0000 |
commit | f449f278dd3c70e479a035f50a9bb817a9b433ba (patch) | |
tree | 8ca2bfb785dda9bb4d573acdf9b42aea9cd51383 /tests/libknot/test_endian.c | |
parent | Initial commit. (diff) | |
download | knot-f449f278dd3c70e479a035f50a9bb817a9b433ba.tar.xz knot-f449f278dd3c70e479a035f50a9bb817a9b433ba.zip |
Adding upstream version 3.2.6.upstream/3.2.6upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'tests/libknot/test_endian.c')
-rw-r--r-- | tests/libknot/test_endian.c | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/tests/libknot/test_endian.c b/tests/libknot/test_endian.c new file mode 100644 index 0000000..30f16f7 --- /dev/null +++ b/tests/libknot/test_endian.c @@ -0,0 +1,70 @@ +/* Copyright (C) 2018 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz> + + This program 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 program 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/>. + */ + +#include <stdlib.h> +#include <stdint.h> +#include <string.h> +#include <tap/basic.h> + +#include "libknot/endian.h" + +int main(int argc, char *argv[]) +{ + plan(12); + + typedef union { + uint16_t value; + uint8_t array[2]; + } trafo16_t; + + const uint16_t host16 = 0x0102; + const trafo16_t be16 = { .array = { 0x01, 0x02 } }; + const trafo16_t le16 = { .array = { 0x02, 0x01 } }; + ok(htobe16(host16) == be16.value, "htobe16"); + ok(htole16(host16) == le16.value, "htole16"); + ok(be16toh(be16.value) == host16, "be16toh"); + ok(le16toh(le16.value) == host16, "le16toh"); + + typedef union { + uint32_t value; + uint8_t array[4]; + } trafo32_t; + + const uint32_t host32 = 0x01020304; + const trafo32_t be32 = { .array = { 0x01, 0x02, 0x03, 0x04 } }; + const trafo32_t le32 = { .array = { 0x04, 0x03, 0x02, 0x01 } }; + ok(htobe32(host32) == be32.value, "htobe32"); + ok(htole32(host32) == le32.value, "htole32"); + ok(be32toh(be32.value) == host32, "be32toh"); + ok(le32toh(le32.value) == host32, "le32toh"); + + typedef union { + uint64_t value; + uint8_t array[8]; + } trafo64_t; + + const uint64_t host64 = 0x0102030405060708; + const trafo64_t be64 = { .array = { 0x01, 0x02, 0x03, 0x04, + 0x05, 0x06, 0x07, 0x08 } }; + const trafo64_t le64 = { .array = { 0x08, 0x07, 0x06, 0x05, + 0x04, 0x03, 0x02, 0x01 } }; + ok(htobe64(host64) == be64.value, "htobe64"); + ok(htole64(host64) == le64.value, "htole64"); + ok(be64toh(be64.value) == host64, "be64toh"); + ok(le64toh(le64.value) == host64, "le64toh"); + + return 0; +} |