michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /****************************************************************************** michael@0: * michael@0: * This file contains a test program for the function conversion functions michael@0: * for double precision code: michael@0: * PR_strtod michael@0: * PR_dtoa michael@0: * PR_cnvtf michael@0: * michael@0: * This file was ns/nspr/tests/dtoa.c, created by rrj on 1996/06/22. michael@0: * michael@0: *****************************************************************************/ michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include "prprf.h" michael@0: #include "prdtoa.h" michael@0: michael@0: static int failed_already = 0; michael@0: michael@0: int main(int argc, char **argv) michael@0: { michael@0: double num; michael@0: double num1; michael@0: double zero = 0.0; michael@0: char cnvt[50]; michael@0: char *thousands; michael@0: michael@0: num = 1e24; michael@0: num1 = PR_strtod("1e24",NULL); michael@0: if(num1 != num){ michael@0: fprintf(stderr,"Failed to convert numeric value %s\n","1e24"); michael@0: failed_already = 1; michael@0: } michael@0: michael@0: PR_cnvtf(cnvt,sizeof(cnvt),20,num); michael@0: if(strcmp("1e+24",cnvt) != 0){ michael@0: fprintf(stderr,"Failed to convert numeric value %lf %s\n",num,cnvt); michael@0: failed_already = 1; michael@0: } michael@0: michael@0: num = 0.001e7; michael@0: num1 = PR_strtod("0.001e7",NULL); michael@0: if(num1 != num){ michael@0: fprintf(stderr,"Failed to convert numeric value %s\n","0.001e7"); michael@0: failed_already = 1; michael@0: } michael@0: PR_cnvtf(cnvt,sizeof(cnvt),20,num); michael@0: if(strcmp("10000",cnvt) != 0){ michael@0: fprintf(stderr,"Failed to convert numeric value %lf %s\n",num,cnvt); michael@0: failed_already = 1; michael@0: } michael@0: michael@0: num = 0.0000000000000753; michael@0: num1 = PR_strtod("0.0000000000000753",NULL); michael@0: if(num1 != num){ michael@0: fprintf(stderr,"Failed to convert numeric value %s\n", michael@0: "0.0000000000000753"); michael@0: failed_already = 1; michael@0: } michael@0: PR_cnvtf(cnvt,sizeof(cnvt),20,num); michael@0: if(strcmp("7.53e-14",cnvt) != 0){ michael@0: fprintf(stderr,"Failed to convert numeric value %lf %s\n",num,cnvt); michael@0: failed_already = 1; michael@0: } michael@0: michael@0: num = 1.867e73; michael@0: num1 = PR_strtod("1.867e73",NULL); michael@0: if(num1 != num){ michael@0: fprintf(stderr,"Failed to convert numeric value %s\n","1.867e73"); michael@0: failed_already = 1; michael@0: } michael@0: PR_cnvtf(cnvt,sizeof(cnvt),20,num); michael@0: if(strcmp("1.867e+73",cnvt) != 0){ michael@0: fprintf(stderr,"Failed to convert numeric value %lf %s\n",num,cnvt); michael@0: failed_already = 1; michael@0: } michael@0: michael@0: michael@0: num = -1.867e73; michael@0: num1 = PR_strtod("-1.867e73",NULL); michael@0: if(num1 != num){ michael@0: fprintf(stderr,"Failed to convert numeric value %s\n","-1.867e73"); michael@0: failed_already = 1; michael@0: } michael@0: PR_cnvtf(cnvt,sizeof(cnvt),20,num); michael@0: if(strcmp("-1.867e+73",cnvt) != 0){ michael@0: fprintf(stderr,"Failed to convert numeric value %lf %s\n",num,cnvt); michael@0: failed_already = 1; michael@0: } michael@0: michael@0: num = -1.867e-73; michael@0: num1 = PR_strtod("-1.867e-73",NULL); michael@0: if(num1 != num){ michael@0: fprintf(stderr,"Failed to convert numeric value %s\n","-1.867e-73"); michael@0: failed_already = 1; michael@0: } michael@0: michael@0: PR_cnvtf(cnvt,sizeof(cnvt),20,num); michael@0: if(strcmp("-1.867e-73",cnvt) != 0){ michael@0: fprintf(stderr,"Failed to convert numeric value %lf %s\n",num,cnvt); michael@0: failed_already = 1; michael@0: } michael@0: michael@0: /* Testing for infinity */ michael@0: num = 1.0 / zero; michael@0: num1 = PR_strtod("1.867e765",NULL); michael@0: if(num1 != num){ michael@0: fprintf(stderr,"Failed to convert numeric value %s\n","1.867e765"); michael@0: failed_already = 1; michael@0: } michael@0: michael@0: PR_cnvtf(cnvt,sizeof(cnvt),20,num); michael@0: if(strcmp("Infinity",cnvt) != 0){ michael@0: fprintf(stderr,"Failed to convert numeric value %lf %s\n",num,cnvt); michael@0: failed_already = 1; michael@0: } michael@0: michael@0: num = -1.0 / zero; michael@0: num1 = PR_strtod("-1.867e765",NULL); michael@0: if(num1 != num){ michael@0: fprintf(stderr,"Failed to convert numeric value %s\n","-1.867e765"); michael@0: failed_already = 1; michael@0: } michael@0: michael@0: PR_cnvtf(cnvt,sizeof(cnvt),20,num); michael@0: if(strcmp("-Infinity",cnvt) != 0){ michael@0: fprintf(stderr,"Failed to convert numeric value %lf %s\n",num,cnvt); michael@0: failed_already = 1; michael@0: } michael@0: michael@0: /* Testing for NaN. PR_strtod can't parse "NaN" and "Infinity" */ michael@0: num = zero / zero; michael@0: michael@0: PR_cnvtf(cnvt,sizeof(cnvt),20,num); michael@0: if(strcmp("NaN",cnvt) != 0){ michael@0: fprintf(stderr,"Failed to convert numeric value %lf %s\n",num,cnvt); michael@0: failed_already = 1; michael@0: } michael@0: michael@0: num = - zero / zero; michael@0: PR_cnvtf(cnvt,sizeof(cnvt),20,num); michael@0: if(strcmp("NaN",cnvt) != 0){ michael@0: fprintf(stderr,"Failed to convert numeric value %lf %s\n",num,cnvt); michael@0: failed_already = 1; michael@0: } michael@0: michael@0: num = 1.0000000001e21; michael@0: num1 = PR_strtod("1.0000000001e21",NULL); michael@0: if(num1 != num){ michael@0: fprintf(stderr,"Failed to convert numeric value %s\n", michael@0: "1.0000000001e21"); michael@0: failed_already = 1; michael@0: } michael@0: michael@0: PR_cnvtf(cnvt,sizeof(cnvt),20,num); michael@0: if(strcmp("1.0000000001e+21",cnvt) != 0){ michael@0: fprintf(stderr,"Failed to convert numeric value %lf %s\n",num,cnvt); michael@0: failed_already = 1; michael@0: } michael@0: michael@0: num = -1.0000000001e-21; michael@0: num1 = PR_strtod("-1.0000000001e-21",NULL); michael@0: if(num1 != num){ michael@0: fprintf(stderr,"Failed to convert numeric value %s\n", michael@0: "-1.0000000001e-21"); michael@0: failed_already = 1; michael@0: } michael@0: PR_cnvtf(cnvt,sizeof(cnvt),20,num); michael@0: if(strcmp("-1.0000000001e-21",cnvt) != 0){ michael@0: fprintf(stderr,"Failed to convert numeric value %lf %s\n",num,cnvt); michael@0: failed_already = 1; michael@0: } michael@0: michael@0: /* michael@0: * Bug 414772: should not exit with "Zero passed to d2b" in debug michael@0: * build. michael@0: */ michael@0: num1 = PR_strtod("4e-356",NULL); michael@0: michael@0: /* michael@0: * A very long input with ~384K digits. michael@0: * Bug 516396: Should not crash. michael@0: * Bug 521306: Should return 0 without converting the input. michael@0: */ michael@0: #define LENGTH (384 * 1024) michael@0: thousands = (char *)malloc(LENGTH); michael@0: thousands[0] = '0'; michael@0: thousands[1] = '.'; michael@0: memset(&thousands[2], '1', LENGTH - 3); michael@0: thousands[LENGTH - 1] = '\0'; michael@0: num = 0; michael@0: num1 = PR_strtod(thousands,NULL); michael@0: free(thousands); michael@0: if(num1 != num){ michael@0: fprintf(stderr,"Failed to convert numeric value %s\n", michael@0: "0.1111111111111111..."); michael@0: failed_already = 1; michael@0: } michael@0: michael@0: if (failed_already) { michael@0: printf("FAILED\n"); michael@0: } else { michael@0: printf("PASSED\n"); michael@0: } michael@0: return failed_already; michael@0: }