tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

dtoa.c (6011B)


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