VirtualBox

source: vbox/trunk/src/libs/xpcom18a4/nsprpub/pr/tests/ntoh.c@ 1

Last change on this file since 1 was 1, checked in by vboxsync, 55 years ago

import

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.1 KB
Line 
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
4 *
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/
9 *
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
13 * License.
14 *
15 * The Original Code is the Netscape Portable Runtime (NSPR).
16 *
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.
21 *
22 * Contributor(s):
23 *
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.
35 *
36 * ***** END LICENSE BLOCK ***** */
37
38/*
39 * A test program for PR_htons, PR_ntohs, PR_htonl, PR_ntohl,
40 * PR_htonll, and PR_ntohll.
41 */
42
43#include "prnetdb.h"
44
45#include <stdio.h>
46#include <string.h>
47#include <stdlib.h>
48
49/* Byte sequence in network byte order */
50static unsigned char bytes_n[8] = { 1, 2, 3, 4, 5, 6, 7, 8 };
51
52/* Integers in host byte order */
53static PRUint16 s_h = 0x0102;
54static PRUint32 l_h = 0x01020304;
55static PRUint64 ll_h = LL_INIT(0x01020304, 0x05060708);
56
57int main()
58{
59 union {
60 PRUint16 s;
61 PRUint32 l;
62 PRUint64 ll;
63 unsigned char bytes[8];
64 } un;
65
66 un.s = s_h;
67 printf("%u %u\n",
68 un.bytes[0], un.bytes[1]);
69 un.s = PR_htons(un.s);
70 printf("%u %u\n",
71 un.bytes[0], un.bytes[1]);
72 if (memcmp(un.bytes, bytes_n, 2)) {
73 fprintf(stderr, "PR_htons failed\n");
74 exit(1);
75 }
76 un.s = PR_ntohs(un.s);
77 printf("%u %u\n",
78 un.bytes[0], un.bytes[1]);
79 if (un.s != s_h) {
80 fprintf(stderr, "PR_ntohs failed\n");
81 exit(1);
82 }
83
84 un.l = l_h;
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");
92 exit(1);
93 }
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]);
97 if (un.l != l_h) {
98 fprintf(stderr, "PR_ntohl failed\n");
99 exit(1);
100 }
101
102 un.ll = ll_h;
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");
112 exit(1);
113 }
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");
120 exit(1);
121 }
122
123 printf("PASS\n");
124 return 0;
125}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette