source: vendor/glibc-tests/glibc/inet/htontest.c

Last change on this file was 2036, checked in by bird, 20 years ago

Initial revision

  • Property cvs2svn:cvs-rev set to 1.1
  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 2.2 KB
Line 
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#include <endian.h>
22#include <stdio.h>
23#include <sys/types.h>
24#include <netinet/in.h>
25
26#if BYTE_ORDER == BIG_ENDIAN
27# define TEST(orig, swapped, fct) \
28 if ((fct (orig)) != (orig)) { \
29 printf ("Failed for %s -> %#x\n", #fct "(" #orig ")", fct (orig)); \
30 result = 1; \
31 }
32#elif BYTE_ORDER == LITTLE_ENDIAN
33# define TEST(orig, swapped, fct) \
34 if ((fct (orig)) != (swapped)) { \
35 printf ("Failed for %s -> %#x\n", #fct "(" #orig ")", fct (orig)); \
36 result = 1; \
37 }
38#else
39# error "Bah, what kind of system do you use?"
40#endif
41
42u_int32_t lo = 0x67452301;
43u_int16_t foo = 0x1234;
44
45int
46main (void)
47{
48 int result = 0;
49
50 TEST (0x67452301, 0x01234567, htonl);
51 TEST (0x67452301, 0x01234567, (htonl));
52 TEST (0x67452301, 0x01234567, ntohl);
53 TEST (0x67452301, 0x01234567, (ntohl));
54
55 TEST (lo, 0x01234567, htonl);
56 TEST (lo, 0x01234567, (htonl));
57 TEST (lo, 0x01234567, ntohl);
58 TEST (lo, 0x01234567, (ntohl));
59
60 TEST (0x1234, 0x3412, htons);
61 TEST (0x1234, 0x3412, (htons));
62 TEST (0x1234, 0x3412, ntohs);
63 TEST (0x1234, 0x3412, (ntohs));
64
65 TEST (foo, 0x3412, htons);
66 TEST (foo, 0x3412, (htons));
67 TEST (foo, 0x3412, ntohs);
68 TEST (foo, 0x3412, (ntohs));
69
70 return result;
71}
Note: See TracBrowser for help on using the repository browser.