1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* ***** BEGIN LICENSE BLOCK *****
3
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
5
* The contents of this file are subject to the Mozilla Public License Version
6
* 1.1 (the "License"); you may not use this file except in compliance with
7
* the License. You may obtain a copy of the License at
8
* http://www.mozilla.org/MPL/
10
* Software distributed under the License is distributed on an "AS IS" basis,
11
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12
* for the specific language governing rights and limitations under the
15
* The Original Code is the Netscape Portable Runtime (NSPR).
17
* The Initial Developer of the Original Code is
18
* Netscape Communications Corporation.
19
* Portions created by the Initial Developer are Copyright (C) 2000
20
* the Initial Developer. All Rights Reserved.
24
* Alternatively, the contents of this file may be used under the terms of
25
* either the GNU General Public License Version 2 or later (the "GPL"), or
26
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27
* in which case the provisions of the GPL or the LGPL are applicable instead
28
* of those above. If you wish to allow use of your version of this file only
29
* under the terms of either the GPL or the LGPL, and not to allow others to
30
* use your version of this file under the terms of the MPL, indicate your
31
* decision by deleting the provisions above and replace them with the notice
32
* and other provisions required by the GPL or the LGPL. If you do not delete
33
* the provisions above, a recipient may use your version of this file under
34
* the terms of any one of the MPL, the GPL or the LGPL.
36
* ***** END LICENSE BLOCK ***** */
39
* A test program for PR_htons, PR_ntohs, PR_htonl, PR_ntohl,
40
* PR_htonll, and PR_ntohll.
49
/* Byte sequence in network byte order */
50
static unsigned char bytes_n[8] = { 1, 2, 3, 4, 5, 6, 7, 8 };
52
/* Integers in host byte order */
53
static PRUint16 s_h = 0x0102;
54
static PRUint32 l_h = 0x01020304;
55
static PRUint64 ll_h = LL_INIT(0x01020304, 0x05060708);
63
unsigned char bytes[8];
68
un.bytes[0], un.bytes[1]);
69
un.s = PR_htons(un.s);
71
un.bytes[0], un.bytes[1]);
72
if (memcmp(un.bytes, bytes_n, 2)) {
73
fprintf(stderr, "PR_htons failed\n");
76
un.s = PR_ntohs(un.s);
78
un.bytes[0], un.bytes[1]);
80
fprintf(stderr, "PR_ntohs failed\n");
85
printf("%u %u %u %u\n",
86
un.bytes[0], un.bytes[1], un.bytes[2], un.bytes[3]);
87
un.l = PR_htonl(un.l);
88
printf("%u %u %u %u\n",
89
un.bytes[0], un.bytes[1], un.bytes[2], un.bytes[3]);
90
if (memcmp(un.bytes, bytes_n, 4)) {
91
fprintf(stderr, "PR_htonl failed\n");
94
un.l = PR_ntohl(un.l);
95
printf("%u %u %u %u\n",
96
un.bytes[0], un.bytes[1], un.bytes[2], un.bytes[3]);
98
fprintf(stderr, "PR_ntohl failed\n");
103
printf("%u %u %u %u %u %u %u %u\n",
104
un.bytes[0], un.bytes[1], un.bytes[2], un.bytes[3],
105
un.bytes[4], un.bytes[5], un.bytes[6], un.bytes[7]);
106
un.ll = PR_htonll(un.ll);
107
printf("%u %u %u %u %u %u %u %u\n",
108
un.bytes[0], un.bytes[1], un.bytes[2], un.bytes[3],
109
un.bytes[4], un.bytes[5], un.bytes[6], un.bytes[7]);
110
if (memcmp(un.bytes, bytes_n, 8)) {
111
fprintf(stderr, "PR_htonll failed\n");
114
un.ll = PR_ntohll(un.ll);
115
printf("%u %u %u %u %u %u %u %u\n",
116
un.bytes[0], un.bytes[1], un.bytes[2], un.bytes[3],
117
un.bytes[4], un.bytes[5], un.bytes[6], un.bytes[7]);
118
if (LL_NE(un.ll, ll_h)) {
119
fprintf(stderr, "PR_ntohll failed\n");