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