nsprpub/pr/tests/dtoa.c

Wed, 31 Dec 2014 06:55:46 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:55:46 +0100
changeset 1
ca08bd8f51b2
permissions
-rw-r--r--

Added tag TORBROWSER_REPLICA for changeset 6474c204b198

     1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     2 /* This Source Code Form is subject to the terms of the Mozilla Public
     3  * License, v. 2.0. If a copy of the MPL was not distributed with this
     4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     6 /******************************************************************************
     7  *
     8  * This file contains a test program for the function conversion functions
     9  * for double precision code:
    10  * PR_strtod
    11  * PR_dtoa
    12  * PR_cnvtf
    13  *
    14  * This file was ns/nspr/tests/dtoa.c, created by rrj on 1996/06/22.
    15  *
    16  *****************************************************************************/
    17 #include <stdio.h>
    18 #include <stdlib.h>
    19 #include <sys/types.h>
    20 #include <string.h>
    21 #include <locale.h>
    22 #include "prprf.h"
    23 #include "prdtoa.h"
    25 static int failed_already = 0;
    27 int main(int argc, char **argv)
    28 {
    29     double num;
    30     double num1;
    31     double zero = 0.0;
    32     char   cnvt[50];
    33     char  *thousands;
    35     num = 1e24;
    36     num1 = PR_strtod("1e24",NULL);
    37     if(num1 != num){
    38 	fprintf(stderr,"Failed to convert numeric value %s\n","1e24");
    39         failed_already = 1;
    40     }
    42     PR_cnvtf(cnvt,sizeof(cnvt),20,num);
    43     if(strcmp("1e+24",cnvt) != 0){
    44 	fprintf(stderr,"Failed to convert numeric value %lf %s\n",num,cnvt);
    45         failed_already = 1;
    46     }
    48     num = 0.001e7;
    49     num1 = PR_strtod("0.001e7",NULL);
    50     if(num1 != num){
    51 	fprintf(stderr,"Failed to convert numeric value %s\n","0.001e7");
    52         failed_already = 1;
    53     }
    54     PR_cnvtf(cnvt,sizeof(cnvt),20,num);
    55     if(strcmp("10000",cnvt) != 0){
    56 	fprintf(stderr,"Failed to convert numeric value %lf %s\n",num,cnvt);
    57         failed_already = 1;
    58     }
    60     num = 0.0000000000000753;
    61     num1 = PR_strtod("0.0000000000000753",NULL);
    62     if(num1 != num){
    63 	fprintf(stderr,"Failed to convert numeric value %s\n",
    64 		"0.0000000000000753");
    65         failed_already = 1;
    66     }
    67     PR_cnvtf(cnvt,sizeof(cnvt),20,num);
    68     if(strcmp("7.53e-14",cnvt) != 0){
    69 	fprintf(stderr,"Failed to convert numeric value %lf %s\n",num,cnvt);
    70         failed_already = 1;
    71     }
    73     num = 1.867e73;
    74     num1 = PR_strtod("1.867e73",NULL);
    75     if(num1 != num){
    76 	fprintf(stderr,"Failed to convert numeric value %s\n","1.867e73");
    77         failed_already = 1;
    78     }
    79     PR_cnvtf(cnvt,sizeof(cnvt),20,num);
    80     if(strcmp("1.867e+73",cnvt) != 0){
    81 	fprintf(stderr,"Failed to convert numeric value %lf %s\n",num,cnvt);
    82         failed_already = 1;
    83     }
    86     num = -1.867e73;
    87     num1 = PR_strtod("-1.867e73",NULL);
    88     if(num1 != num){
    89 	fprintf(stderr,"Failed to convert numeric value %s\n","-1.867e73");
    90         failed_already = 1;
    91     }
    92     PR_cnvtf(cnvt,sizeof(cnvt),20,num);
    93     if(strcmp("-1.867e+73",cnvt) != 0){
    94 	fprintf(stderr,"Failed to convert numeric value %lf %s\n",num,cnvt);
    95         failed_already = 1;
    96     }
    98     num = -1.867e-73;
    99     num1 = PR_strtod("-1.867e-73",NULL);
   100     if(num1 != num){
   101 	fprintf(stderr,"Failed to convert numeric value %s\n","-1.867e-73");
   102         failed_already = 1;
   103     }
   105     PR_cnvtf(cnvt,sizeof(cnvt),20,num);
   106     if(strcmp("-1.867e-73",cnvt) != 0){
   107 	fprintf(stderr,"Failed to convert numeric value %lf %s\n",num,cnvt);
   108         failed_already = 1;
   109     }
   111     /* Testing for infinity */
   112     num = 1.0 / zero;
   113     num1 = PR_strtod("1.867e765",NULL);
   114     if(num1 != num){
   115 	fprintf(stderr,"Failed to convert numeric value %s\n","1.867e765");
   116         failed_already = 1;
   117     }
   119     PR_cnvtf(cnvt,sizeof(cnvt),20,num);
   120     if(strcmp("Infinity",cnvt) != 0){
   121 	fprintf(stderr,"Failed to convert numeric value %lf %s\n",num,cnvt);
   122         failed_already = 1;
   123     }
   125     num = -1.0 / zero;
   126     num1 = PR_strtod("-1.867e765",NULL);
   127     if(num1 != num){
   128 	fprintf(stderr,"Failed to convert numeric value %s\n","-1.867e765");
   129         failed_already = 1;
   130     }
   132     PR_cnvtf(cnvt,sizeof(cnvt),20,num);
   133     if(strcmp("-Infinity",cnvt) != 0){
   134 	fprintf(stderr,"Failed to convert numeric value %lf %s\n",num,cnvt);
   135         failed_already = 1;
   136     }
   138     /* Testing for NaN. PR_strtod can't parse "NaN" and "Infinity" */
   139     num = zero / zero;
   141     PR_cnvtf(cnvt,sizeof(cnvt),20,num);
   142     if(strcmp("NaN",cnvt) != 0){
   143 	fprintf(stderr,"Failed to convert numeric value %lf %s\n",num,cnvt);
   144         failed_already = 1;
   145     }
   147     num = - zero / zero;
   148     PR_cnvtf(cnvt,sizeof(cnvt),20,num);
   149     if(strcmp("NaN",cnvt) != 0){
   150 	fprintf(stderr,"Failed to convert numeric value %lf %s\n",num,cnvt);
   151         failed_already = 1;
   152     }
   154     num = 1.0000000001e21;
   155     num1 = PR_strtod("1.0000000001e21",NULL);
   156     if(num1 != num){
   157 	fprintf(stderr,"Failed to convert numeric value %s\n",
   158 		"1.0000000001e21");
   159         failed_already = 1;
   160     }
   162     PR_cnvtf(cnvt,sizeof(cnvt),20,num);
   163     if(strcmp("1.0000000001e+21",cnvt) != 0){
   164 	fprintf(stderr,"Failed to convert numeric value %lf %s\n",num,cnvt);
   165         failed_already = 1;
   166     }
   168     num = -1.0000000001e-21;
   169     num1 = PR_strtod("-1.0000000001e-21",NULL);
   170     if(num1 != num){
   171 	fprintf(stderr,"Failed to convert numeric value %s\n",
   172 		"-1.0000000001e-21");
   173         failed_already = 1;
   174     }
   175     PR_cnvtf(cnvt,sizeof(cnvt),20,num);
   176     if(strcmp("-1.0000000001e-21",cnvt) != 0){
   177 	fprintf(stderr,"Failed to convert numeric value %lf %s\n",num,cnvt);
   178         failed_already = 1;
   179     }
   181     /*
   182      * Bug 414772: should not exit with "Zero passed to d2b" in debug
   183      * build.
   184      */
   185     num1 = PR_strtod("4e-356",NULL);
   187     /*
   188      * A very long input with ~384K digits.
   189      * Bug 516396: Should not crash.
   190      * Bug 521306: Should return 0 without converting the input.
   191      */
   192 #define LENGTH (384 * 1024)
   193     thousands = (char *)malloc(LENGTH);
   194     thousands[0] = '0';
   195     thousands[1] = '.';
   196     memset(&thousands[2], '1', LENGTH - 3);
   197     thousands[LENGTH - 1] = '\0';
   198     num = 0;
   199     num1 = PR_strtod(thousands,NULL);
   200     free(thousands);
   201     if(num1 != num){
   202         fprintf(stderr,"Failed to convert numeric value %s\n",
   203                 "0.1111111111111111...");
   204         failed_already = 1;
   205     }
   207     if (failed_already) {
   208         printf("FAILED\n");
   209     } else {
   210         printf("PASSED\n");
   211     }
   212     return failed_already;
   213 }

mercurial