dtoatest.cpp 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // Tencent is pleased to support the open source community by making RapidJSON available.
  2. //
  3. // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved.
  4. //
  5. // Licensed under the MIT License (the "License"); you may not use this file except
  6. // in compliance with the License. You may obtain a copy of the License at
  7. //
  8. // http://opensource.org/licenses/MIT
  9. //
  10. // Unless required by applicable law or agreed to in writing, software distributed
  11. // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  12. // CONDITIONS OF ANY KIND, either express or implied. See the License for the
  13. // specific language governing permissions and limitations under the License.
  14. #include "unittest.h"
  15. #include "rapidjson/internal/dtoa.h"
  16. #ifdef __GNUC__
  17. RAPIDJSON_DIAG_PUSH
  18. RAPIDJSON_DIAG_OFF(type-limits)
  19. #endif
  20. using namespace rapidjson::internal;
  21. TEST(dtoa, normal) {
  22. char buffer[30];
  23. #define TEST_DTOA(d, a)\
  24. *dtoa(d, buffer) = '\0';\
  25. EXPECT_STREQ(a, buffer)
  26. TEST_DTOA(0.0, "0.0");
  27. TEST_DTOA(-0.0, "-0.0");
  28. TEST_DTOA(1.0, "1.0");
  29. TEST_DTOA(-1.0, "-1.0");
  30. TEST_DTOA(1.2345, "1.2345");
  31. TEST_DTOA(1.2345678, "1.2345678");
  32. TEST_DTOA(0.123456789012, "0.123456789012");
  33. TEST_DTOA(1234567.8, "1234567.8");
  34. TEST_DTOA(-79.39773355813419, "-79.39773355813419");
  35. TEST_DTOA(0.000001, "0.000001");
  36. TEST_DTOA(0.0000001, "1e-7");
  37. TEST_DTOA(1e30, "1e30");
  38. TEST_DTOA(1.234567890123456e30, "1.234567890123456e30");
  39. TEST_DTOA(5e-324, "5e-324"); // Min subnormal positive double
  40. TEST_DTOA(2.225073858507201e-308, "2.225073858507201e-308"); // Max subnormal positive double
  41. TEST_DTOA(2.2250738585072014e-308, "2.2250738585072014e-308"); // Min normal positive double
  42. TEST_DTOA(1.7976931348623157e308, "1.7976931348623157e308"); // Max double
  43. #undef TEST_DTOA
  44. }
  45. TEST(dtoa, maxDecimalPlaces) {
  46. char buffer[30];
  47. #define TEST_DTOA(m, d, a)\
  48. *dtoa(d, buffer, m) = '\0';\
  49. EXPECT_STREQ(a, buffer)
  50. TEST_DTOA(3, 0.0, "0.0");
  51. TEST_DTOA(1, 0.0, "0.0");
  52. TEST_DTOA(3, -0.0, "-0.0");
  53. TEST_DTOA(3, 1.0, "1.0");
  54. TEST_DTOA(3, -1.0, "-1.0");
  55. TEST_DTOA(3, 1.2345, "1.234");
  56. TEST_DTOA(2, 1.2345, "1.23");
  57. TEST_DTOA(1, 1.2345, "1.2");
  58. TEST_DTOA(3, 1.2345678, "1.234");
  59. TEST_DTOA(3, 1.0001, "1.0");
  60. TEST_DTOA(2, 1.0001, "1.0");
  61. TEST_DTOA(1, 1.0001, "1.0");
  62. TEST_DTOA(3, 0.123456789012, "0.123");
  63. TEST_DTOA(2, 0.123456789012, "0.12");
  64. TEST_DTOA(1, 0.123456789012, "0.1");
  65. TEST_DTOA(4, 0.0001, "0.0001");
  66. TEST_DTOA(3, 0.0001, "0.0");
  67. TEST_DTOA(2, 0.0001, "0.0");
  68. TEST_DTOA(1, 0.0001, "0.0");
  69. TEST_DTOA(3, 1234567.8, "1234567.8");
  70. TEST_DTOA(3, 1e30, "1e30");
  71. TEST_DTOA(3, 5e-324, "0.0"); // Min subnormal positive double
  72. TEST_DTOA(3, 2.225073858507201e-308, "0.0"); // Max subnormal positive double
  73. TEST_DTOA(3, 2.2250738585072014e-308, "0.0"); // Min normal positive double
  74. TEST_DTOA(3, 1.7976931348623157e308, "1.7976931348623157e308"); // Max double
  75. TEST_DTOA(5, -0.14000000000000001, "-0.14");
  76. TEST_DTOA(4, -0.14000000000000001, "-0.14");
  77. TEST_DTOA(3, -0.14000000000000001, "-0.14");
  78. TEST_DTOA(3, -0.10000000000000001, "-0.1");
  79. TEST_DTOA(2, -0.10000000000000001, "-0.1");
  80. TEST_DTOA(1, -0.10000000000000001, "-0.1");
  81. #undef TEST_DTOA
  82. }
  83. #ifdef __GNUC__
  84. RAPIDJSON_DIAG_POP
  85. #endif