]> Savannah Git Hosting - gnulib.git/commitdiff
htonl: Add tests.
authorCollin Funk <collin.funk1@gmail.com>
Sun, 11 Aug 2024 00:16:45 +0000 (17:16 -0700)
committerCollin Funk <collin.funk1@gmail.com>
Sun, 11 Aug 2024 00:16:45 +0000 (17:16 -0700)
* modules/htonl-tests: New file.
* tests/test-htonl.c: New file.

ChangeLog
modules/htonl-tests [new file with mode: 0644]
tests/test-htonl.c [new file with mode: 0644]

index 111c08d415f790c0ac6572cf1bd3d4497b417dbd..5938c70ea992a3e08c5932ebc20f5710e4712673 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,9 @@
 2024-08-10  Collin Funk  <collin.funk1@gmail.com>
 
+       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 (file)
index 0000000..c745b48
--- /dev/null
@@ -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 (file)
index 0000000..f360fb8
--- /dev/null
@@ -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 <https://www.gnu.org/licenses/>.  */
+
+/* Written by Collin Funk <collin.funk1@gmail.com>, 2024.  */
+
+#include <config.h>
+
+/* Specification.  */
+#include <arpa/inet.h>
+
+#include <endian.h>
+#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 (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;
+}