File: test-htonl.c

package info (click to toggle)
coreutils 9.7-3
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 67,780 kB
  • sloc: ansic: 243,477; sh: 29,063; perl: 7,908; yacc: 1,858; makefile: 196; python: 47; sed: 16
file content (104 lines) | stat: -rw-r--r-- 2,634 bytes parent folder | download | duplicates (6)
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
/* Test of host and network byte order conversion functions.
   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 <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;
}