1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/nsprpub/pr/tests/ntoh.c Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,93 @@ 1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +/* 1.10 + * A test program for PR_htons, PR_ntohs, PR_htonl, PR_ntohl, 1.11 + * PR_htonll, and PR_ntohll. 1.12 + */ 1.13 + 1.14 +#include "prnetdb.h" 1.15 + 1.16 +#include <stdio.h> 1.17 +#include <string.h> 1.18 +#include <stdlib.h> 1.19 + 1.20 +/* Byte sequence in network byte order */ 1.21 +static unsigned char bytes_n[8] = { 1, 2, 3, 4, 5, 6, 7, 8 }; 1.22 + 1.23 +/* Integers in host byte order */ 1.24 +static PRUint16 s_h = 0x0102; 1.25 +static PRUint32 l_h = 0x01020304; 1.26 +static PRUint64 ll_h = LL_INIT(0x01020304, 0x05060708); 1.27 + 1.28 +int main(int argc, char **argv) 1.29 +{ 1.30 + union { 1.31 + PRUint16 s; 1.32 + PRUint32 l; 1.33 + PRUint64 ll; 1.34 + unsigned char bytes[8]; 1.35 + } un; 1.36 + 1.37 + un.s = s_h; 1.38 + printf("%u %u\n", 1.39 + un.bytes[0], un.bytes[1]); 1.40 + un.s = PR_htons(un.s); 1.41 + printf("%u %u\n", 1.42 + un.bytes[0], un.bytes[1]); 1.43 + if (memcmp(un.bytes, bytes_n, 2)) { 1.44 + fprintf(stderr, "PR_htons failed\n"); 1.45 + exit(1); 1.46 + } 1.47 + un.s = PR_ntohs(un.s); 1.48 + printf("%u %u\n", 1.49 + un.bytes[0], un.bytes[1]); 1.50 + if (un.s != s_h) { 1.51 + fprintf(stderr, "PR_ntohs failed\n"); 1.52 + exit(1); 1.53 + } 1.54 + 1.55 + un.l = l_h; 1.56 + printf("%u %u %u %u\n", 1.57 + un.bytes[0], un.bytes[1], un.bytes[2], un.bytes[3]); 1.58 + un.l = PR_htonl(un.l); 1.59 + printf("%u %u %u %u\n", 1.60 + un.bytes[0], un.bytes[1], un.bytes[2], un.bytes[3]); 1.61 + if (memcmp(un.bytes, bytes_n, 4)) { 1.62 + fprintf(stderr, "PR_htonl failed\n"); 1.63 + exit(1); 1.64 + } 1.65 + un.l = PR_ntohl(un.l); 1.66 + printf("%u %u %u %u\n", 1.67 + un.bytes[0], un.bytes[1], un.bytes[2], un.bytes[3]); 1.68 + if (un.l != l_h) { 1.69 + fprintf(stderr, "PR_ntohl failed\n"); 1.70 + exit(1); 1.71 + } 1.72 + 1.73 + un.ll = ll_h; 1.74 + printf("%u %u %u %u %u %u %u %u\n", 1.75 + un.bytes[0], un.bytes[1], un.bytes[2], un.bytes[3], 1.76 + un.bytes[4], un.bytes[5], un.bytes[6], un.bytes[7]); 1.77 + un.ll = PR_htonll(un.ll); 1.78 + printf("%u %u %u %u %u %u %u %u\n", 1.79 + un.bytes[0], un.bytes[1], un.bytes[2], un.bytes[3], 1.80 + un.bytes[4], un.bytes[5], un.bytes[6], un.bytes[7]); 1.81 + if (memcmp(un.bytes, bytes_n, 8)) { 1.82 + fprintf(stderr, "PR_htonll failed\n"); 1.83 + exit(1); 1.84 + } 1.85 + un.ll = PR_ntohll(un.ll); 1.86 + printf("%u %u %u %u %u %u %u %u\n", 1.87 + un.bytes[0], un.bytes[1], un.bytes[2], un.bytes[3], 1.88 + un.bytes[4], un.bytes[5], un.bytes[6], un.bytes[7]); 1.89 + if (LL_NE(un.ll, ll_h)) { 1.90 + fprintf(stderr, "PR_ntohll failed\n"); 1.91 + exit(1); 1.92 + } 1.93 + 1.94 + printf("PASS\n"); 1.95 + return 0; 1.96 +}