>From c341a871800bb04ff9b80582aa053e87e496aca9 Mon Sep 17 00:00:00 2001 From: Collin Funk Date: Sat, 10 Aug 2024 17:16:45 -0700 Subject: [PATCH 2/2] htonl: Add tests. * modules/htonl-tests: New file. * tests/test-htonl.c: New file. --- ChangeLog | 4 ++ modules/htonl-tests | 13 ++++++ tests/test-htonl.c | 104 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 121 insertions(+) create mode 100644 modules/htonl-tests create mode 100644 tests/test-htonl.c diff --git a/ChangeLog b/ChangeLog index 111c08d415..5938c70ea9 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,9 @@ 2024-08-10 Collin Funk + htonl: Add tests. + * modules/htonl-tests: New file. + * tests/test-htonl.c: New file. + htonl: New module. * modules/htonl: New file. * lib/arpa_inet.c: New file. diff --git a/modules/htonl-tests b/modules/htonl-tests new file mode 100644 index 0000000000..c745b48838 --- /dev/null +++ b/modules/htonl-tests @@ -0,0 +1,13 @@ +Files: +tests/test-htonl.c +tests/macros.h + +Depends-on: +endian +stdint + +configure.ac: + +Makefile.am: +TESTS += test-htonl +check_PROGRAMS += test-htonl diff --git a/tests/test-htonl.c b/tests/test-htonl.c new file mode 100644 index 0000000000..f360fb82e7 --- /dev/null +++ b/tests/test-htonl.c @@ -0,0 +1,104 @@ +/* Test of host and network byte order conversion functions. + Copyright (C) 2024 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 . */ + +/* Written by Collin Funk , 2024. */ + +#include + +/* Specification. */ +#include + +#include +#include + +#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 (htons (UINT16_C (0x1234)) == UINT16_C (0x1234)); + ASSERT (ntohs (UINT16_C (0x1234)) == UINT16_C (0x1234)); + + /* 32-bit. */ + ASSERT (htonl (UINT32_C (0x12345678)) == UINT32_C (0x12345678)); + ASSERT (ntohl (UINT32_C (0x12345678)) == UINT32_C (0x12345678)); +#else + /* 16-bit. */ + ASSERT (htons (UINT16_C (0x1234)) == UINT16_C (0x3412)); + ASSERT (ntohs (UINT16_C (0x1234)) == UINT16_C (0x3412)); + + /* 32-bit. */ + ASSERT (htonl (UINT32_C (0x12345678)) == UINT32_C (0x78563412)); + ASSERT (ntohl (UINT32_C (0x12345678)) == UINT32_C (0x78563412)); +#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 (htons (value++) == 0); + ASSERT (value == 1); + } + { + uint16_t value = 0; + ASSERT (ntohs (value++) == 0); + ASSERT (value == 1); + } + + /* 32-bit. */ + { + uint32_t value = 0; + ASSERT (htonl (value++) == 0); + ASSERT (value == 1); + } + { + uint32_t value = 0; + ASSERT (ntohl (value++) == 0); + ASSERT (value == 1); + } +} + +/* Test that the byte order conversion functions accept floating-point + arguments. */ +static void +test_convert_double (void) +{ + /* 16-bit. */ + ASSERT (htons (0.0) == 0); + ASSERT (ntohs (0.0) == 0); + + /* 32-bit. */ + ASSERT (htonl (0.0) == 0); + ASSERT (ntohs (0.0) == 0); +} + +int +main (void) +{ + test_convert_constant (); + test_convert_eval_once (); + test_convert_double (); + + return test_exit_status; +} -- 2.46.0