michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /* michael@0: * A test program for PR_htons, PR_ntohs, PR_htonl, PR_ntohl, michael@0: * PR_htonll, and PR_ntohll. michael@0: */ michael@0: michael@0: #include "prnetdb.h" michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: /* Byte sequence in network byte order */ michael@0: static unsigned char bytes_n[8] = { 1, 2, 3, 4, 5, 6, 7, 8 }; michael@0: michael@0: /* Integers in host byte order */ michael@0: static PRUint16 s_h = 0x0102; michael@0: static PRUint32 l_h = 0x01020304; michael@0: static PRUint64 ll_h = LL_INIT(0x01020304, 0x05060708); michael@0: michael@0: int main(int argc, char **argv) michael@0: { michael@0: union { michael@0: PRUint16 s; michael@0: PRUint32 l; michael@0: PRUint64 ll; michael@0: unsigned char bytes[8]; michael@0: } un; michael@0: michael@0: un.s = s_h; michael@0: printf("%u %u\n", michael@0: un.bytes[0], un.bytes[1]); michael@0: un.s = PR_htons(un.s); michael@0: printf("%u %u\n", michael@0: un.bytes[0], un.bytes[1]); michael@0: if (memcmp(un.bytes, bytes_n, 2)) { michael@0: fprintf(stderr, "PR_htons failed\n"); michael@0: exit(1); michael@0: } michael@0: un.s = PR_ntohs(un.s); michael@0: printf("%u %u\n", michael@0: un.bytes[0], un.bytes[1]); michael@0: if (un.s != s_h) { michael@0: fprintf(stderr, "PR_ntohs failed\n"); michael@0: exit(1); michael@0: } michael@0: michael@0: un.l = l_h; michael@0: printf("%u %u %u %u\n", michael@0: un.bytes[0], un.bytes[1], un.bytes[2], un.bytes[3]); michael@0: un.l = PR_htonl(un.l); michael@0: printf("%u %u %u %u\n", michael@0: un.bytes[0], un.bytes[1], un.bytes[2], un.bytes[3]); michael@0: if (memcmp(un.bytes, bytes_n, 4)) { michael@0: fprintf(stderr, "PR_htonl failed\n"); michael@0: exit(1); michael@0: } michael@0: un.l = PR_ntohl(un.l); michael@0: printf("%u %u %u %u\n", michael@0: un.bytes[0], un.bytes[1], un.bytes[2], un.bytes[3]); michael@0: if (un.l != l_h) { michael@0: fprintf(stderr, "PR_ntohl failed\n"); michael@0: exit(1); michael@0: } michael@0: michael@0: un.ll = ll_h; michael@0: printf("%u %u %u %u %u %u %u %u\n", michael@0: un.bytes[0], un.bytes[1], un.bytes[2], un.bytes[3], michael@0: un.bytes[4], un.bytes[5], un.bytes[6], un.bytes[7]); michael@0: un.ll = PR_htonll(un.ll); michael@0: printf("%u %u %u %u %u %u %u %u\n", michael@0: un.bytes[0], un.bytes[1], un.bytes[2], un.bytes[3], michael@0: un.bytes[4], un.bytes[5], un.bytes[6], un.bytes[7]); michael@0: if (memcmp(un.bytes, bytes_n, 8)) { michael@0: fprintf(stderr, "PR_htonll failed\n"); michael@0: exit(1); michael@0: } michael@0: un.ll = PR_ntohll(un.ll); michael@0: printf("%u %u %u %u %u %u %u %u\n", michael@0: un.bytes[0], un.bytes[1], un.bytes[2], un.bytes[3], michael@0: un.bytes[4], un.bytes[5], un.bytes[6], un.bytes[7]); michael@0: if (LL_NE(un.ll, ll_h)) { michael@0: fprintf(stderr, "PR_ntohll failed\n"); michael@0: exit(1); michael@0: } michael@0: michael@0: printf("PASS\n"); michael@0: return 0; michael@0: }