|
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 /* |
|
7 * A test program for PR_htons, PR_ntohs, PR_htonl, PR_ntohl, |
|
8 * PR_htonll, and PR_ntohll. |
|
9 */ |
|
10 |
|
11 #include "prnetdb.h" |
|
12 |
|
13 #include <stdio.h> |
|
14 #include <string.h> |
|
15 #include <stdlib.h> |
|
16 |
|
17 /* Byte sequence in network byte order */ |
|
18 static unsigned char bytes_n[8] = { 1, 2, 3, 4, 5, 6, 7, 8 }; |
|
19 |
|
20 /* Integers in host byte order */ |
|
21 static PRUint16 s_h = 0x0102; |
|
22 static PRUint32 l_h = 0x01020304; |
|
23 static PRUint64 ll_h = LL_INIT(0x01020304, 0x05060708); |
|
24 |
|
25 int main(int argc, char **argv) |
|
26 { |
|
27 union { |
|
28 PRUint16 s; |
|
29 PRUint32 l; |
|
30 PRUint64 ll; |
|
31 unsigned char bytes[8]; |
|
32 } un; |
|
33 |
|
34 un.s = s_h; |
|
35 printf("%u %u\n", |
|
36 un.bytes[0], un.bytes[1]); |
|
37 un.s = PR_htons(un.s); |
|
38 printf("%u %u\n", |
|
39 un.bytes[0], un.bytes[1]); |
|
40 if (memcmp(un.bytes, bytes_n, 2)) { |
|
41 fprintf(stderr, "PR_htons failed\n"); |
|
42 exit(1); |
|
43 } |
|
44 un.s = PR_ntohs(un.s); |
|
45 printf("%u %u\n", |
|
46 un.bytes[0], un.bytes[1]); |
|
47 if (un.s != s_h) { |
|
48 fprintf(stderr, "PR_ntohs failed\n"); |
|
49 exit(1); |
|
50 } |
|
51 |
|
52 un.l = l_h; |
|
53 printf("%u %u %u %u\n", |
|
54 un.bytes[0], un.bytes[1], un.bytes[2], un.bytes[3]); |
|
55 un.l = PR_htonl(un.l); |
|
56 printf("%u %u %u %u\n", |
|
57 un.bytes[0], un.bytes[1], un.bytes[2], un.bytes[3]); |
|
58 if (memcmp(un.bytes, bytes_n, 4)) { |
|
59 fprintf(stderr, "PR_htonl failed\n"); |
|
60 exit(1); |
|
61 } |
|
62 un.l = PR_ntohl(un.l); |
|
63 printf("%u %u %u %u\n", |
|
64 un.bytes[0], un.bytes[1], un.bytes[2], un.bytes[3]); |
|
65 if (un.l != l_h) { |
|
66 fprintf(stderr, "PR_ntohl failed\n"); |
|
67 exit(1); |
|
68 } |
|
69 |
|
70 un.ll = ll_h; |
|
71 printf("%u %u %u %u %u %u %u %u\n", |
|
72 un.bytes[0], un.bytes[1], un.bytes[2], un.bytes[3], |
|
73 un.bytes[4], un.bytes[5], un.bytes[6], un.bytes[7]); |
|
74 un.ll = PR_htonll(un.ll); |
|
75 printf("%u %u %u %u %u %u %u %u\n", |
|
76 un.bytes[0], un.bytes[1], un.bytes[2], un.bytes[3], |
|
77 un.bytes[4], un.bytes[5], un.bytes[6], un.bytes[7]); |
|
78 if (memcmp(un.bytes, bytes_n, 8)) { |
|
79 fprintf(stderr, "PR_htonll failed\n"); |
|
80 exit(1); |
|
81 } |
|
82 un.ll = PR_ntohll(un.ll); |
|
83 printf("%u %u %u %u %u %u %u %u\n", |
|
84 un.bytes[0], un.bytes[1], un.bytes[2], un.bytes[3], |
|
85 un.bytes[4], un.bytes[5], un.bytes[6], un.bytes[7]); |
|
86 if (LL_NE(un.ll, ll_h)) { |
|
87 fprintf(stderr, "PR_ntohll failed\n"); |
|
88 exit(1); |
|
89 } |
|
90 |
|
91 printf("PASS\n"); |
|
92 return 0; |
|
93 } |