nsprpub/pr/tests/prftest.c

Fri, 16 Jan 2015 04:50:19 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 04:50:19 +0100
branch
TOR_BUG_9701
changeset 13
44a2da4a2ab2
permissions
-rw-r--r--

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 * File: prftest.c
michael@0 8 * Description:
michael@0 9 * This is a simple test of the PR_snprintf() function defined
michael@0 10 * in prprf.c.
michael@0 11 */
michael@0 12
michael@0 13 #include "prlong.h"
michael@0 14 #include "prprf.h"
michael@0 15
michael@0 16 #include <string.h>
michael@0 17
michael@0 18 #define BUF_SIZE 128
michael@0 19
michael@0 20 int main(int argc, char **argv)
michael@0 21 {
michael@0 22 PRInt16 i16;
michael@0 23 PRIntn n;
michael@0 24 PRInt32 i32;
michael@0 25 PRInt64 i64;
michael@0 26 char buf[BUF_SIZE];
michael@0 27 char answer[BUF_SIZE];
michael@0 28 int i, rv = 0;
michael@0 29
michael@0 30 i16 = -1;
michael@0 31 n = -1;
michael@0 32 i32 = -1;
michael@0 33 LL_I2L(i64, i32);
michael@0 34
michael@0 35 PR_snprintf(buf, BUF_SIZE, "%hx %x %lx %llx", i16, n, i32, i64);
michael@0 36 strcpy(answer, "ffff ");
michael@0 37 for (i = PR_BYTES_PER_INT * 2; i; i--) {
michael@0 38 strcat(answer, "f");
michael@0 39 }
michael@0 40 strcat(answer, " ffffffff ffffffffffffffff");
michael@0 41
michael@0 42 if (!strcmp(buf, answer)) {
michael@0 43 printf("PR_snprintf test 1 passed\n");
michael@0 44 } else {
michael@0 45 printf("PR_snprintf test 1 failed\n");
michael@0 46 printf("Converted string is %s\n", buf);
michael@0 47 printf("Should be %s\n", answer);
michael@0 48 rv = 1;
michael@0 49 }
michael@0 50
michael@0 51 i16 = -32;
michael@0 52 n = 30;
michael@0 53 i32 = 64;
michael@0 54 LL_I2L(i64, 333);
michael@0 55 PR_snprintf(buf, BUF_SIZE, "%d %hd %lld %ld", n, i16, i64, i32);
michael@0 56 if (!strcmp(buf, "30 -32 333 64")) {
michael@0 57 printf("PR_snprintf test 2 passed\n");
michael@0 58 } else {
michael@0 59 printf("PR_snprintf test 2 failed\n");
michael@0 60 printf("Converted string is %s\n", buf);
michael@0 61 printf("Should be 30 -32 333 64\n");
michael@0 62 rv = 1;
michael@0 63 }
michael@0 64
michael@0 65 return rv;
michael@0 66 }

mercurial