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

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

mercurial