1 /* Test hton/ntoh functions.
2 Copyright (C) 1997-2015 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
22 #include <sys/types.h>
23 #include <netinet/in.h>
25 #if BYTE_ORDER == BIG_ENDIAN
26 # define TEST(orig, swapped, fct) \
27 if ((fct (orig)) != (orig)) { \
28 printf ("Failed for %s -> %#x\n", #fct "(" #orig ")", fct (orig)); \
31 #elif BYTE_ORDER == LITTLE_ENDIAN
32 # define TEST(orig, swapped, fct) \
33 if ((fct (orig)) != (swapped)) { \
34 printf ("Failed for %s -> %#x\n", #fct "(" #orig ")", fct (orig)); \
38 # error "Bah, what kind of system do you use?"
41 u_int32_t lo
= 0x67452301;
42 u_int16_t foo
= 0x1234;
49 TEST (0x67452301, 0x01234567, htonl
);
50 TEST (0x67452301, 0x01234567, (htonl
));
51 TEST (0x67452301, 0x01234567, ntohl
);
52 TEST (0x67452301, 0x01234567, (ntohl
));
54 TEST (lo
, 0x01234567, htonl
);
55 TEST (lo
, 0x01234567, (htonl
));
56 TEST (lo
, 0x01234567, ntohl
);
57 TEST (lo
, 0x01234567, (ntohl
));
59 TEST (0x1234, 0x3412, htons
);
60 TEST (0x1234, 0x3412, (htons
));
61 TEST (0x1234, 0x3412, ntohs
);
62 TEST (0x1234, 0x3412, (ntohs
));
64 TEST (foo
, 0x3412, htons
);
65 TEST (foo
, 0x3412, (htons
));
66 TEST (foo
, 0x3412, ntohs
);
67 TEST (foo
, 0x3412, (ntohs
));