1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
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:
42: u_int32_t lo = 0x67452301;
43: u_int16_t foo = 0x1234;
44:
45: int
46: main (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: }