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, write to the Free
|
---|
18 | Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
---|
19 | 02111-1307 USA. */
|
---|
20 |
|
---|
21 | #ifndef __BSD__
|
---|
22 | #include <endian.h>
|
---|
23 | #endif
|
---|
24 | #include <stdio.h>
|
---|
25 | #include <sys/types.h>
|
---|
26 | #include <netinet/in.h>
|
---|
27 |
|
---|
28 | #if BYTE_ORDER == BIG_ENDIAN
|
---|
29 | # define TEST(orig, swapped, fct) \
|
---|
30 | if ((fct (orig)) != (orig)) { \
|
---|
31 | printf ("Failed for %s -> %#x\n", #fct "(" #orig ")", fct (orig)); \
|
---|
32 | result = 1; \
|
---|
33 | }
|
---|
34 | #elif BYTE_ORDER == LITTLE_ENDIAN
|
---|
35 | # define TEST(orig, swapped, fct) \
|
---|
36 | if ((fct (orig)) != (swapped)) { \
|
---|
37 | printf ("Failed for %s -> %#x\n", #fct "(" #orig ")", fct (orig)); \
|
---|
38 | result = 1; \
|
---|
39 | }
|
---|
40 | #else
|
---|
41 | # error "Bah, what kind of system do you use?"
|
---|
42 | #endif
|
---|
43 |
|
---|
44 | u_int32_t lo = 0x67452301;
|
---|
45 | u_int16_t foo = 0x1234;
|
---|
46 |
|
---|
47 | int
|
---|
48 | main (void)
|
---|
49 | {
|
---|
50 | int result = 0;
|
---|
51 |
|
---|
52 | TEST (0x67452301, 0x01234567, htonl);
|
---|
53 | TEST (0x67452301, 0x01234567, (htonl));
|
---|
54 | TEST (0x67452301, 0x01234567, ntohl);
|
---|
55 | TEST (0x67452301, 0x01234567, (ntohl));
|
---|
56 |
|
---|
57 | TEST (lo, 0x01234567, htonl);
|
---|
58 | TEST (lo, 0x01234567, (htonl));
|
---|
59 | TEST (lo, 0x01234567, ntohl);
|
---|
60 | TEST (lo, 0x01234567, (ntohl));
|
---|
61 |
|
---|
62 | TEST (0x1234, 0x3412, htons);
|
---|
63 | TEST (0x1234, 0x3412, (htons));
|
---|
64 | TEST (0x1234, 0x3412, ntohs);
|
---|
65 | TEST (0x1234, 0x3412, (ntohs));
|
---|
66 |
|
---|
67 | TEST (foo, 0x3412, htons);
|
---|
68 | TEST (foo, 0x3412, (htons));
|
---|
69 | TEST (foo, 0x3412, ntohs);
|
---|
70 | TEST (foo, 0x3412, (ntohs));
|
---|
71 |
|
---|
72 | return result;
|
---|
73 | }
|
---|