dtoa.c 6.2 KB

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