nsprpub/pr/tests/dtoa.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/nsprpub/pr/tests/dtoa.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,213 @@
     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 + *
    1.11 + * This file contains a test program for the function conversion functions
    1.12 + * for double precision code:
    1.13 + * PR_strtod
    1.14 + * PR_dtoa
    1.15 + * PR_cnvtf
    1.16 + *
    1.17 + * This file was ns/nspr/tests/dtoa.c, created by rrj on 1996/06/22.
    1.18 + *
    1.19 + *****************************************************************************/
    1.20 +#include <stdio.h>
    1.21 +#include <stdlib.h>
    1.22 +#include <sys/types.h>
    1.23 +#include <string.h>
    1.24 +#include <locale.h>
    1.25 +#include "prprf.h"
    1.26 +#include "prdtoa.h"
    1.27 +
    1.28 +static int failed_already = 0;
    1.29 +
    1.30 +int main(int argc, char **argv)
    1.31 +{
    1.32 +    double num;
    1.33 +    double num1;
    1.34 +    double zero = 0.0;
    1.35 +    char   cnvt[50];
    1.36 +    char  *thousands;
    1.37 +    
    1.38 +    num = 1e24;
    1.39 +    num1 = PR_strtod("1e24",NULL);
    1.40 +    if(num1 != num){
    1.41 +	fprintf(stderr,"Failed to convert numeric value %s\n","1e24");
    1.42 +        failed_already = 1;
    1.43 +    }
    1.44 +
    1.45 +    PR_cnvtf(cnvt,sizeof(cnvt),20,num);
    1.46 +    if(strcmp("1e+24",cnvt) != 0){
    1.47 +	fprintf(stderr,"Failed to convert numeric value %lf %s\n",num,cnvt);
    1.48 +        failed_already = 1;
    1.49 +    }
    1.50 +
    1.51 +    num = 0.001e7;
    1.52 +    num1 = PR_strtod("0.001e7",NULL);
    1.53 +    if(num1 != num){
    1.54 +	fprintf(stderr,"Failed to convert numeric value %s\n","0.001e7");
    1.55 +        failed_already = 1;
    1.56 +    }
    1.57 +    PR_cnvtf(cnvt,sizeof(cnvt),20,num);
    1.58 +    if(strcmp("10000",cnvt) != 0){
    1.59 +	fprintf(stderr,"Failed to convert numeric value %lf %s\n",num,cnvt);
    1.60 +        failed_already = 1;
    1.61 +    }
    1.62 +
    1.63 +    num = 0.0000000000000753;
    1.64 +    num1 = PR_strtod("0.0000000000000753",NULL);
    1.65 +    if(num1 != num){
    1.66 +	fprintf(stderr,"Failed to convert numeric value %s\n",
    1.67 +		"0.0000000000000753");
    1.68 +        failed_already = 1;
    1.69 +    }
    1.70 +    PR_cnvtf(cnvt,sizeof(cnvt),20,num);
    1.71 +    if(strcmp("7.53e-14",cnvt) != 0){
    1.72 +	fprintf(stderr,"Failed to convert numeric value %lf %s\n",num,cnvt);
    1.73 +        failed_already = 1;
    1.74 +    }
    1.75 +
    1.76 +    num = 1.867e73;
    1.77 +    num1 = PR_strtod("1.867e73",NULL);
    1.78 +    if(num1 != num){
    1.79 +	fprintf(stderr,"Failed to convert numeric value %s\n","1.867e73");
    1.80 +        failed_already = 1;
    1.81 +    }
    1.82 +    PR_cnvtf(cnvt,sizeof(cnvt),20,num);
    1.83 +    if(strcmp("1.867e+73",cnvt) != 0){
    1.84 +	fprintf(stderr,"Failed to convert numeric value %lf %s\n",num,cnvt);
    1.85 +        failed_already = 1;
    1.86 +    }
    1.87 +
    1.88 +
    1.89 +    num = -1.867e73;
    1.90 +    num1 = PR_strtod("-1.867e73",NULL);
    1.91 +    if(num1 != num){
    1.92 +	fprintf(stderr,"Failed to convert numeric value %s\n","-1.867e73");
    1.93 +        failed_already = 1;
    1.94 +    }
    1.95 +    PR_cnvtf(cnvt,sizeof(cnvt),20,num);
    1.96 +    if(strcmp("-1.867e+73",cnvt) != 0){
    1.97 +	fprintf(stderr,"Failed to convert numeric value %lf %s\n",num,cnvt);
    1.98 +        failed_already = 1;
    1.99 +    }
   1.100 +
   1.101 +    num = -1.867e-73;
   1.102 +    num1 = PR_strtod("-1.867e-73",NULL);
   1.103 +    if(num1 != num){
   1.104 +	fprintf(stderr,"Failed to convert numeric value %s\n","-1.867e-73");
   1.105 +        failed_already = 1;
   1.106 +    }
   1.107 +
   1.108 +    PR_cnvtf(cnvt,sizeof(cnvt),20,num);
   1.109 +    if(strcmp("-1.867e-73",cnvt) != 0){
   1.110 +	fprintf(stderr,"Failed to convert numeric value %lf %s\n",num,cnvt);
   1.111 +        failed_already = 1;
   1.112 +    }
   1.113 +
   1.114 +    /* Testing for infinity */
   1.115 +    num = 1.0 / zero;
   1.116 +    num1 = PR_strtod("1.867e765",NULL);
   1.117 +    if(num1 != num){
   1.118 +	fprintf(stderr,"Failed to convert numeric value %s\n","1.867e765");
   1.119 +        failed_already = 1;
   1.120 +    }
   1.121 +
   1.122 +    PR_cnvtf(cnvt,sizeof(cnvt),20,num);
   1.123 +    if(strcmp("Infinity",cnvt) != 0){
   1.124 +	fprintf(stderr,"Failed to convert numeric value %lf %s\n",num,cnvt);
   1.125 +        failed_already = 1;
   1.126 +    }
   1.127 +
   1.128 +    num = -1.0 / zero;
   1.129 +    num1 = PR_strtod("-1.867e765",NULL);
   1.130 +    if(num1 != num){
   1.131 +	fprintf(stderr,"Failed to convert numeric value %s\n","-1.867e765");
   1.132 +        failed_already = 1;
   1.133 +    }
   1.134 +
   1.135 +    PR_cnvtf(cnvt,sizeof(cnvt),20,num);
   1.136 +    if(strcmp("-Infinity",cnvt) != 0){
   1.137 +	fprintf(stderr,"Failed to convert numeric value %lf %s\n",num,cnvt);
   1.138 +        failed_already = 1;
   1.139 +    }
   1.140 +
   1.141 +    /* Testing for NaN. PR_strtod can't parse "NaN" and "Infinity" */
   1.142 +    num = zero / zero;
   1.143 +
   1.144 +    PR_cnvtf(cnvt,sizeof(cnvt),20,num);
   1.145 +    if(strcmp("NaN",cnvt) != 0){
   1.146 +	fprintf(stderr,"Failed to convert numeric value %lf %s\n",num,cnvt);
   1.147 +        failed_already = 1;
   1.148 +    }
   1.149 +
   1.150 +    num = - zero / zero;
   1.151 +    PR_cnvtf(cnvt,sizeof(cnvt),20,num);
   1.152 +    if(strcmp("NaN",cnvt) != 0){
   1.153 +	fprintf(stderr,"Failed to convert numeric value %lf %s\n",num,cnvt);
   1.154 +        failed_already = 1;
   1.155 +    }
   1.156 +
   1.157 +    num = 1.0000000001e21;
   1.158 +    num1 = PR_strtod("1.0000000001e21",NULL);
   1.159 +    if(num1 != num){
   1.160 +	fprintf(stderr,"Failed to convert numeric value %s\n",
   1.161 +		"1.0000000001e21");
   1.162 +        failed_already = 1;
   1.163 +    }
   1.164 +
   1.165 +    PR_cnvtf(cnvt,sizeof(cnvt),20,num);
   1.166 +    if(strcmp("1.0000000001e+21",cnvt) != 0){
   1.167 +	fprintf(stderr,"Failed to convert numeric value %lf %s\n",num,cnvt);
   1.168 +        failed_already = 1;
   1.169 +    }
   1.170 +
   1.171 +    num = -1.0000000001e-21;
   1.172 +    num1 = PR_strtod("-1.0000000001e-21",NULL);
   1.173 +    if(num1 != num){
   1.174 +	fprintf(stderr,"Failed to convert numeric value %s\n",
   1.175 +		"-1.0000000001e-21");
   1.176 +        failed_already = 1;
   1.177 +    }
   1.178 +    PR_cnvtf(cnvt,sizeof(cnvt),20,num);
   1.179 +    if(strcmp("-1.0000000001e-21",cnvt) != 0){
   1.180 +	fprintf(stderr,"Failed to convert numeric value %lf %s\n",num,cnvt);
   1.181 +        failed_already = 1;
   1.182 +    }
   1.183 +
   1.184 +    /*
   1.185 +     * Bug 414772: should not exit with "Zero passed to d2b" in debug
   1.186 +     * build.
   1.187 +     */
   1.188 +    num1 = PR_strtod("4e-356",NULL);
   1.189 +
   1.190 +    /*
   1.191 +     * A very long input with ~384K digits.
   1.192 +     * Bug 516396: Should not crash.
   1.193 +     * Bug 521306: Should return 0 without converting the input.
   1.194 +     */
   1.195 +#define LENGTH (384 * 1024)
   1.196 +    thousands = (char *)malloc(LENGTH);
   1.197 +    thousands[0] = '0';
   1.198 +    thousands[1] = '.';
   1.199 +    memset(&thousands[2], '1', LENGTH - 3);
   1.200 +    thousands[LENGTH - 1] = '\0';
   1.201 +    num = 0;
   1.202 +    num1 = PR_strtod(thousands,NULL);
   1.203 +    free(thousands);
   1.204 +    if(num1 != num){
   1.205 +        fprintf(stderr,"Failed to convert numeric value %s\n",
   1.206 +                "0.1111111111111111...");
   1.207 +        failed_already = 1;
   1.208 +    }
   1.209 +
   1.210 +    if (failed_already) {
   1.211 +        printf("FAILED\n");
   1.212 +    } else {
   1.213 +        printf("PASSED\n");
   1.214 +    }
   1.215 +    return failed_already;
   1.216 +}

mercurial