Merge branch 'master' into bug13658-branch
[platform/upstream/glibc.git] / inet / htontest.c
1 /* Test hton/ntoh functions.
2    Copyright (C) 1997, 2002 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4    Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
5
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.
10
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.
15
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/>.  */
19
20 #include <endian.h>
21 #include <stdio.h>
22 #include <sys/types.h>
23 #include <netinet/in.h>
24
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));        \
29     result = 1;                                                               \
30   }
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));        \
35     result = 1;                                                               \
36   }
37 #else
38 # error "Bah, what kind of system do you use?"
39 #endif
40
41 u_int32_t lo = 0x67452301;
42 u_int16_t foo = 0x1234;
43
44 int
45 main (void)
46 {
47   int result = 0;
48
49   TEST (0x67452301, 0x01234567, htonl);
50   TEST (0x67452301, 0x01234567, (htonl));
51   TEST (0x67452301, 0x01234567, ntohl);
52   TEST (0x67452301, 0x01234567, (ntohl));
53
54   TEST (lo, 0x01234567, htonl);
55   TEST (lo, 0x01234567, (htonl));
56   TEST (lo, 0x01234567, ntohl);
57   TEST (lo, 0x01234567, (ntohl));
58
59   TEST (0x1234, 0x3412, htons);
60   TEST (0x1234, 0x3412, (htons));
61   TEST (0x1234, 0x3412, ntohs);
62   TEST (0x1234, 0x3412, (ntohs));
63
64   TEST (foo, 0x3412, htons);
65   TEST (foo, 0x3412, (htons));
66   TEST (foo, 0x3412, ntohs);
67   TEST (foo, 0x3412, (ntohs));
68
69   return result;
70 }