1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/nsprpub/pr/tests/prftest1.c Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,120 @@ 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 +** File: prftest1.c 1.11 +** Description: 1.12 +** This is a simple test of the PR_snprintf() function defined 1.13 +** in prprf.c. 1.14 +** 1.15 +** Modification History: 1.16 +** 14-May-97 AGarcia- Converted the test to accomodate the debug_mode flag. 1.17 +** The debug mode will print all of the printfs associated with this test. 1.18 +** The regress mode will be the default mode. Since the regress tool limits 1.19 +** the output to a one line status:PASS or FAIL,all of the printf statements 1.20 +** have been handled with an if (debug_mode) statement. 1.21 +***********************************************************************/ 1.22 +/*********************************************************************** 1.23 +** Includes 1.24 +***********************************************************************/ 1.25 +/* Used to get the command line option */ 1.26 +#include "plgetopt.h" 1.27 +#include "prttools.h" 1.28 + 1.29 +#include "prinit.h" 1.30 +#include "prlong.h" 1.31 +#include "prprf.h" 1.32 + 1.33 +#include <string.h> 1.34 + 1.35 +#define BUF_SIZE 128 1.36 + 1.37 +/*********************************************************************** 1.38 +** PRIVATE FUNCTION: Test_Result 1.39 +** DESCRIPTION: Used in conjunction with the regress tool, prints out the 1.40 +** status of the test case. 1.41 +** INPUTS: PASS/FAIL 1.42 +** OUTPUTS: None 1.43 +** RETURN: None 1.44 +** SIDE EFFECTS: 1.45 +** 1.46 +** RESTRICTIONS: 1.47 +** None 1.48 +** MEMORY: NA 1.49 +** ALGORITHM: Determine what the status is and print accordingly. 1.50 +** 1.51 +***********************************************************************/ 1.52 + 1.53 + 1.54 +static void Test_Result (int result) 1.55 +{ 1.56 + if (result == PASS) 1.57 + printf ("PASS\n"); 1.58 + else 1.59 + printf ("FAIL\n"); 1.60 +} 1.61 + 1.62 +int main(int argc, char **argv) 1.63 +{ 1.64 + PRInt16 i16; 1.65 + PRIntn n; 1.66 + PRInt32 i32; 1.67 + PRInt64 i64; 1.68 + char buf[BUF_SIZE]; 1.69 + char answer[BUF_SIZE]; 1.70 + int i; 1.71 + 1.72 + /* The command line argument: -d is used to determine if the test is being run 1.73 + in debug mode. The regress tool requires only one line output:PASS or FAIL. 1.74 + All of the printfs associated with this test has been handled with a if (debug_mode) 1.75 + test. 1.76 + Usage: test_name -d 1.77 + */ 1.78 + PLOptStatus os; 1.79 + PLOptState *opt = PL_CreateOptState(argc, argv, "d:"); 1.80 + while (PL_OPT_EOL != (os = PL_GetNextOpt(opt))) 1.81 + { 1.82 + if (PL_OPT_BAD == os) continue; 1.83 + switch (opt->option) 1.84 + { 1.85 + case 'd': /* debug mode */ 1.86 + debug_mode = 1; 1.87 + break; 1.88 + default: 1.89 + break; 1.90 + } 1.91 + } 1.92 + PL_DestroyOptState(opt); 1.93 + 1.94 + /* main test */ 1.95 + PR_STDIO_INIT(); 1.96 + 1.97 + i16 = -1; 1.98 + n = -1; 1.99 + i32 = -1; 1.100 + LL_I2L(i64, i32); 1.101 + 1.102 + PR_snprintf(buf, BUF_SIZE, "%hx %x %lx %llx", i16, n, i32, i64); 1.103 + strcpy(answer, "ffff "); 1.104 + for (i = PR_BYTES_PER_INT * 2; i; i--) { 1.105 + strcat(answer, "f"); 1.106 + } 1.107 + strcat(answer, " ffffffff ffffffffffffffff"); 1.108 + 1.109 + if (!strcmp(buf, answer)) { 1.110 + if (debug_mode) printf("PR_snprintf test 1 passed\n"); 1.111 + else Test_Result (PASS); 1.112 + } else { 1.113 + if (debug_mode) { 1.114 + printf("PR_snprintf test 1 failed\n"); 1.115 + printf("Converted string is %s\n", buf); 1.116 + printf("Should be %s\n", answer); 1.117 + } 1.118 + else 1.119 + Test_Result (FAIL); 1.120 + } 1.121 + 1.122 + return 0; 1.123 +}