_atof.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. #include "_atof.h"
  2. #include "../../Source/NumberToString.h"
  3. #ifdef JSON_SAFE
  4. #define assertNaN(one) assertNAN(json_number, one)
  5. #else
  6. #define assertNaN(one)
  7. #endif
  8. testNumberToString__atof::testNumberToString__atof(const std::string & name) : BaseTest(name){
  9. //ScopeCoverageHeap(_atof, 14);
  10. }
  11. testNumberToString__atof::~testNumberToString__atof(){
  12. //AssertScopeCoverageHeap(_atof);
  13. }
  14. /**
  15. * Tests regular positive numbers in various forms
  16. */
  17. void testNumberToString__atof::testPositive(void){
  18. #ifdef JSON_STRICT
  19. assertFloatEquals(123, NumberToString::_atof(JSON_TEXT("123")));
  20. assertFloatEquals(12.3, NumberToString::_atof(JSON_TEXT("12.3")));
  21. assertFloatEquals(0.123, NumberToString::_atof(JSON_TEXT("0.123")));
  22. assertFloatEquals(0, NumberToString::_atof(JSON_TEXT("0")));
  23. assertFloatEquals(0, NumberToString::_atof(JSON_TEXT("0.")));
  24. assertFloatEquals(1, NumberToString::_atof(JSON_TEXT("1.")));
  25. assertFloatEquals(1, NumberToString::_atof(JSON_TEXT("1")));
  26. assertFloatEquals(0, NumberToString::_atof(JSON_TEXT("0.0")));
  27. assertFloatEquals(1, NumberToString::_atof(JSON_TEXT("1.0")));
  28. assertFloatEquals(1.01, NumberToString::_atof(JSON_TEXT("1.01")));
  29. #endif
  30. }
  31. /**
  32. * Tests negative numbers with regular scientifc notation
  33. */
  34. void testNumberToString__atof::testNegative(void){
  35. #ifdef JSON_STRICT
  36. assertFloatEquals(-123, NumberToString::_atof(JSON_TEXT("-123")));
  37. assertFloatEquals(-12.3, NumberToString::_atof(JSON_TEXT("-12.3")));
  38. assertFloatEquals(-.123, NumberToString::_atof(JSON_TEXT("-0.123")));
  39. assertFloatEquals(0, NumberToString::_atof(JSON_TEXT("-0")));
  40. assertFloatEquals(0, NumberToString::_atof(JSON_TEXT("-0.")));
  41. assertFloatEquals(-1, NumberToString::_atof(JSON_TEXT("-1")));
  42. assertFloatEquals(-1, NumberToString::_atof(JSON_TEXT("-1.")));
  43. assertFloatEquals(0, NumberToString::_atof(JSON_TEXT("-0.0")));
  44. assertFloatEquals(-1, NumberToString::_atof(JSON_TEXT("-1.0")));
  45. #endif
  46. }
  47. /**
  48. * Tests positive numbers with scientific notiation that has a sign in it
  49. */
  50. void testNumberToString__atof::testPositive_ScientificNotation(void){
  51. #ifdef JSON_STRICT
  52. assertNAN(json_number, std::numeric_limits<json_number>::signaling_NaN()); //sanity check
  53. assertFloatEquals(0e3, NumberToString::_atof(JSON_TEXT("0e3")));
  54. assertNaN(NumberToString::_atof(JSON_TEXT("0e3.3")));
  55. assertFloatEquals(1e3, NumberToString::_atof(JSON_TEXT("1.e3")));
  56. assertNaN(NumberToString::_atof(JSON_TEXT("1.e3.3")));
  57. assertFloatEquals(1e3, NumberToString::_atof(JSON_TEXT("1.0e3")));
  58. assertNaN(NumberToString::_atof(JSON_TEXT("1.0e3.3")));
  59. assertFloatEquals(0e2, NumberToString::_atof(JSON_TEXT("0e2")));
  60. assertFloatEquals(1e2, NumberToString::_atof(JSON_TEXT("1e2")));
  61. assertFloatEquals(0e2, NumberToString::_atof(JSON_TEXT("0.e2")));
  62. assertFloatEquals(1e2, NumberToString::_atof(JSON_TEXT("1.e2")));
  63. assertFloatEquals(0e2, NumberToString::_atof(JSON_TEXT("0.0e2")));
  64. assertFloatEquals(1e2, NumberToString::_atof(JSON_TEXT("1.0e2")));
  65. #endif
  66. }
  67. /**
  68. * Tests negative numbers with regular scientifc notation
  69. */
  70. void testNumberToString__atof::testNegative_ScientificNotation(void){
  71. #ifdef JSON_STRICT
  72. assertFloatEquals(0e3, NumberToString::_atof(JSON_TEXT("-0e3")));
  73. assertNaN(NumberToString::_atof(JSON_TEXT("-0e3.3")));
  74. assertFloatEquals(-1e3, NumberToString::_atof(JSON_TEXT("-1.e3")));
  75. assertNaN(NumberToString::_atof(JSON_TEXT("-1.e3.3")));
  76. assertFloatEquals(-1e3, NumberToString::_atof(JSON_TEXT("-1.0e3")));
  77. assertNaN(NumberToString::_atof(JSON_TEXT("-1.0e3.3")));
  78. assertFloatEquals(0e2, NumberToString::_atof(JSON_TEXT("-0e2")));
  79. assertFloatEquals(-1e2, NumberToString::_atof(JSON_TEXT("-1e2")));
  80. assertFloatEquals(0e2, NumberToString::_atof(JSON_TEXT("-0.e2")));
  81. assertFloatEquals(-1e2, NumberToString::_atof(JSON_TEXT("-1.e2")));
  82. assertFloatEquals(0e2, NumberToString::_atof(JSON_TEXT("-0.0e2")));
  83. assertFloatEquals(-1e2, NumberToString::_atof(JSON_TEXT("-1.0e2")));
  84. #endif
  85. }
  86. /**
  87. * Tests positive numbers with scientific notiation that has a sign in it
  88. */
  89. void testNumberToString__atof::testPositive_SignedScientificNotation(void){
  90. #ifdef JSON_STRICT
  91. assertFloatEquals(0e-3, NumberToString::_atof(JSON_TEXT("0e-3")));
  92. assertFloatEquals(0e+3, NumberToString::_atof(JSON_TEXT("0e+3")));
  93. assertNaN(NumberToString::_atof(JSON_TEXT("0e-3.3")));
  94. assertNaN(NumberToString::_atof(JSON_TEXT("0e+3.3")));
  95. assertFloatEquals(1e-3, NumberToString::_atof(JSON_TEXT("1.e-3")));
  96. assertFloatEquals(1e3, NumberToString::_atof(JSON_TEXT("1.e+3")));
  97. assertNaN(NumberToString::_atof(JSON_TEXT("1.e-3.3")));
  98. assertNaN(NumberToString::_atof(JSON_TEXT("1.e+3.3")));
  99. assertFloatEquals(1e-3, NumberToString::_atof(JSON_TEXT("1.0e-3")));
  100. assertFloatEquals(1e3, NumberToString::_atof(JSON_TEXT("1.0e+3")));
  101. assertNaN(NumberToString::_atof(JSON_TEXT("1.0e-3.3")));
  102. assertNaN(NumberToString::_atof(JSON_TEXT("1.0e+3.3")));
  103. assertFloatEquals(0e2, NumberToString::_atof(JSON_TEXT("0e2")));
  104. assertFloatEquals(1e2, NumberToString::_atof(JSON_TEXT("1e2")));
  105. assertFloatEquals(0e2, NumberToString::_atof(JSON_TEXT("0.e2")));
  106. assertFloatEquals(1e2, NumberToString::_atof(JSON_TEXT("1.e2")));
  107. assertFloatEquals(0e2, NumberToString::_atof(JSON_TEXT("0.0e2")));
  108. assertFloatEquals(1e2, NumberToString::_atof(JSON_TEXT("1.0e2")));
  109. #endif
  110. }
  111. /**
  112. * Tests negative numbers with scientific notiation that has a sign in it
  113. */
  114. void testNumberToString__atof::testNegative_SignedScientificNotation(void){
  115. #ifdef JSON_STRICT
  116. assertFloatEquals(0e-3, NumberToString::_atof(JSON_TEXT("-0e-3")));
  117. assertFloatEquals(0e3, NumberToString::_atof(JSON_TEXT("-0e+3")));
  118. assertNaN(NumberToString::_atof(JSON_TEXT("-0.e-3.3")));
  119. assertNaN(NumberToString::_atof(JSON_TEXT("-0.e+3.3")));
  120. assertFloatEquals(-1e-3, NumberToString::_atof(JSON_TEXT("-1.e-3")));
  121. assertFloatEquals(-1e3, NumberToString::_atof(JSON_TEXT("-1.e+3")));
  122. assertNaN(NumberToString::_atof(JSON_TEXT("-1.e-3.3")));
  123. assertNaN(NumberToString::_atof(JSON_TEXT("-1.e+3.3")));
  124. assertNaN(NumberToString::_atof(JSON_TEXT("-0.0e-3.3")));
  125. assertNaN( NumberToString::_atof(JSON_TEXT("-0.0e+3.3")));
  126. assertFloatEquals(-1e-3, NumberToString::_atof(JSON_TEXT("-1.0e-3")));
  127. assertFloatEquals(-1e3, NumberToString::_atof(JSON_TEXT("-1.0e+3")));
  128. assertNaN(NumberToString::_atof(JSON_TEXT("-1.0e-3.3")));
  129. assertNaN(NumberToString::_atof(JSON_TEXT("-1.0e+3.3")));
  130. assertFloatEquals(0e-2, NumberToString::_atof(JSON_TEXT("-0e-2")));
  131. assertFloatEquals(-1e-2, NumberToString::_atof(JSON_TEXT("-1e-2")));
  132. assertFloatEquals(0e-2, NumberToString::_atof(JSON_TEXT("-0.e-2")));
  133. assertFloatEquals(-1e-2, NumberToString::_atof(JSON_TEXT("-1.e-2")));
  134. assertFloatEquals(0e-2, NumberToString::_atof(JSON_TEXT("-0.0e-2")));
  135. assertFloatEquals(-1e-2, NumberToString::_atof(JSON_TEXT("-1.0e-2")));
  136. assertFloatEquals(0e2, NumberToString::_atof(JSON_TEXT("-0e+2")));
  137. assertFloatEquals(-1e2, NumberToString::_atof(JSON_TEXT("-1e+2")));
  138. assertFloatEquals(0e2, NumberToString::_atof(JSON_TEXT("-0.e+2")));
  139. assertFloatEquals(-1e2, NumberToString::_atof(JSON_TEXT("-1.e+2")));
  140. assertFloatEquals(0e2, NumberToString::_atof(JSON_TEXT("-0.0e+2")));
  141. assertFloatEquals(-1e2, NumberToString::_atof(JSON_TEXT("-1.0e+2")));
  142. assertNaN(NumberToString::_atof(JSON_TEXT("1e-0123"))); //not valid because of negative and leading zero
  143. #endif
  144. }
  145. void testNumberToString__atof::testStrict(void){
  146. #if defined(JSON_SAFE) || defined(JSON_DEBUG)
  147. #ifdef JSON_STRICT
  148. assertNaN(NumberToString::_atof(JSON_TEXT("00")));
  149. assertNaN(NumberToString::_atof(JSON_TEXT("00.01")));
  150. assertNaN(NumberToString::_atof(JSON_TEXT(".01")));
  151. assertNaN(NumberToString::_atof(JSON_TEXT("-.01")));
  152. assertNaN(NumberToString::_atof(JSON_TEXT("+123")));
  153. assertNaN(NumberToString::_atof(JSON_TEXT("+12.3")));
  154. assertNaN(NumberToString::_atof(JSON_TEXT("+0.123")));
  155. assertNaN(NumberToString::_atof(JSON_TEXT("+0")));
  156. assertNaN(NumberToString::_atof(JSON_TEXT("+0.")));
  157. assertNaN(NumberToString::_atof(JSON_TEXT("+0e3")));
  158. assertNaN(NumberToString::_atof(JSON_TEXT("+0e-3")));
  159. assertNaN(NumberToString::_atof(JSON_TEXT("+0e+3")));
  160. assertNaN(NumberToString::_atof(JSON_TEXT("+1.e3")));
  161. assertNaN(NumberToString::_atof(JSON_TEXT("+1.e-3")));
  162. assertNaN(NumberToString::_atof(JSON_TEXT("+1.e+3")));
  163. assertNaN(NumberToString::_atof(JSON_TEXT("+1.0e3")));
  164. assertNaN(NumberToString::_atof(JSON_TEXT("+1.0e-3")));
  165. assertNaN(NumberToString::_atof(JSON_TEXT("+1.0e+3")));
  166. assertNaN(NumberToString::_atof(JSON_TEXT("+1.e3")));
  167. assertNaN(NumberToString::_atof(JSON_TEXT("+0e3.3")));
  168. assertNaN(NumberToString::_atof(JSON_TEXT("+0e-3.3")));
  169. assertNaN(NumberToString::_atof(JSON_TEXT("+0e+3.3")));
  170. assertNaN(NumberToString::_atof(JSON_TEXT("+1.e3.3")));
  171. assertNaN(NumberToString::_atof(JSON_TEXT("+1.e-3.3")));
  172. assertNaN(NumberToString::_atof(JSON_TEXT("+1.e+3.3")));
  173. assertNaN(NumberToString::_atof(JSON_TEXT("+1.0e3.3")));
  174. assertNaN(NumberToString::_atof(JSON_TEXT("+1.0e-3.3")));
  175. assertNaN(NumberToString::_atof(JSON_TEXT("+1.0e+3.3")));
  176. assertNaN(NumberToString::_atof(JSON_TEXT("+1.e3.3")));
  177. assertNaN(NumberToString::_atof(JSON_TEXT("0x12FF")));
  178. assertNaN(NumberToString::_atof(JSON_TEXT("0128")));
  179. assertNaN(NumberToString::_atof(JSON_TEXT("0123")));
  180. assertNaN(NumberToString::_atof(JSON_TEXT("-0123")));
  181. assertNaN(NumberToString::_atof(JSON_TEXT("0xABCD")));
  182. assertNaN(NumberToString::_atof(JSON_TEXT("0124")));
  183. assertNaN(NumberToString::_atof(JSON_TEXT("+1")));
  184. assertNaN(NumberToString::_atof(JSON_TEXT("+1.")));
  185. assertNaN(NumberToString::_atof(JSON_TEXT("+0.0")));
  186. assertNaN(NumberToString::_atof(JSON_TEXT("+1.0")));
  187. assertNaN(NumberToString::_atof(JSON_TEXT("+0e2")));
  188. assertNaN(NumberToString::_atof(JSON_TEXT("+1e2")));
  189. assertNaN(NumberToString::_atof(JSON_TEXT("+0.e2")));
  190. assertNaN(NumberToString::_atof(JSON_TEXT("+1.e2")));
  191. assertNaN(NumberToString::_atof(JSON_TEXT("+0.0e2")));
  192. assertNaN(NumberToString::_atof(JSON_TEXT("+1.0e2")));
  193. assertNaN(NumberToString::_atof(JSON_TEXT("+0e-2")));
  194. assertNaN(NumberToString::_atof(JSON_TEXT("+1e-2")));
  195. assertNaN(NumberToString::_atof(JSON_TEXT("+0.e-2")));
  196. assertNaN(NumberToString::_atof(JSON_TEXT("+1.e-2")));
  197. assertNaN(NumberToString::_atof(JSON_TEXT("+0.0e-2")));
  198. assertNaN(NumberToString::_atof(JSON_TEXT("+1.0e-2")));
  199. assertNaN(NumberToString::_atof(JSON_TEXT("+0e+2")));
  200. assertNaN(NumberToString::_atof(JSON_TEXT("+1e+2")));
  201. assertNaN(NumberToString::_atof(JSON_TEXT("+0.e+2")));
  202. assertNaN(NumberToString::_atof(JSON_TEXT("+1.e+2")));
  203. assertNaN(NumberToString::_atof(JSON_TEXT("+0.0e+2")));
  204. assertNaN(NumberToString::_atof(JSON_TEXT("+1.0e+2")));
  205. #endif
  206. #endif
  207. }
  208. void testNumberToString__atof::testNotNumbers(void){
  209. #if defined(JSON_SAFE) || defined(JSON_DEBUG)
  210. #ifdef JSON_STRICT
  211. assertNaN(NumberToString::_atof(JSON_TEXT("-.")));
  212. assertNaN(NumberToString::_atof(JSON_TEXT("-e3")));
  213. assertNaN(NumberToString::_atof(JSON_TEXT("0xABCDv")));
  214. assertNaN(NumberToString::_atof(JSON_TEXT("00124")));
  215. assertNaN(NumberToString::_atof(JSON_TEXT("09124")));
  216. assertNaN(NumberToString::_atof(JSON_TEXT("0no")));
  217. assertNaN(NumberToString::_atof(JSON_TEXT("no")));
  218. assertNaN(NumberToString::_atof(JSON_TEXT("n1234")));
  219. assertNaN(NumberToString::_atof(JSON_TEXT("12no")));
  220. assertNaN(NumberToString::_atof(JSON_TEXT("0en5")));
  221. assertNaN(NumberToString::_atof(JSON_TEXT("0e")));
  222. assertNaN(NumberToString::_atof(JSON_TEXT("0E")));
  223. #endif
  224. #endif
  225. }