json_binder_value_double.t.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. //
  2. // json_binder_value_double.t.h
  3. // json
  4. //
  5. // Created by Sam Jaffe on 2/25/17.
  6. //
  7. #pragma once
  8. #include <cxxtest/TestSuite.h>
  9. #include "json_binder.hpp"
  10. using namespace json::binder;
  11. using namespace json::parser;
  12. class json_binder_value_float_TestSuite : public CxxTest::TestSuite {
  13. public:
  14. void test_bind_to_double_type() {
  15. char data[] = "0.5";
  16. double out = 0.0;
  17. value_binder<double> binder{};
  18. parse(bind(out, binder), data, allow_all);
  19. TS_ASSERT_EQUALS(out, 0.5);
  20. }
  21. void test_parse_out_scientific_notation() {
  22. char data[] = "2e4";
  23. double out = 0.0;
  24. value_binder<double> binder{};
  25. TS_ASSERT_THROWS_NOTHING(parse(bind(out, binder), data, allow_all));
  26. }
  27. void test_parse_throws_on_no_data() {
  28. char data[] = "";
  29. double out = 0.0;
  30. value_binder<double> binder{};
  31. TS_ASSERT_THROWS(parse(bind(out, binder), data, allow_all),
  32. json::json_numeric_exception);
  33. }
  34. void test_parse_throws_on_non_numeric_data() {
  35. char data[] = "one half";
  36. double out = 0.0;
  37. value_binder<double> binder{};
  38. TS_ASSERT_THROWS(parse(bind(out, binder), data, allow_all),
  39. json::json_numeric_exception);
  40. }
  41. void test_parse_throws_on_incorrectly_terminated_data_with_flag() {
  42. char data[] = "5.0boo";
  43. double out = 0.0;
  44. value_binder<double> binder{};
  45. TS_ASSERT_THROWS(parse(bind(out, binder), data,
  46. disable_concatenated_json_bodies),
  47. json::malformed_json_exception);
  48. TS_ASSERT_EQUALS(out, 5.0);
  49. }
  50. void test_parses_double_max_scientific_within_five_decimal_places() {
  51. char data[] = "1.79769e+308";
  52. double out = 0.0;
  53. value_binder<double> binder{};
  54. TS_ASSERT_THROWS_NOTHING(parse(bind(out, binder), data, allow_all));
  55. TS_ASSERT_DELTA(out, std::numeric_limits<double>::max(), 1E303);
  56. }
  57. void test_throws_exception_on_number_out_of_range_max() {
  58. char data[] = "1.8e+308";
  59. double out = 0.0;
  60. value_binder<double> binder{};
  61. TS_ASSERT_THROWS(parse(bind(out, binder), data, allow_all),
  62. json::json_numeric_width_exception);
  63. }
  64. void test_parses_double_lowest_scientific_within_five_decimal_places() {
  65. char data[] = "-1.79769e+308";
  66. double out = 0.0;
  67. value_binder<double> binder{};
  68. TS_ASSERT_THROWS_NOTHING(parse(bind(out, binder), data, allow_all));
  69. TS_ASSERT_DELTA(out, std::numeric_limits<double>::lowest(), 1E303);
  70. }
  71. void test_throws_exception_on_number_out_of_range_lowest() {
  72. char data[] = "-1.8e+308";
  73. double out = 0.0;
  74. value_binder<double> binder{};
  75. TS_ASSERT_THROWS(parse(bind(out, binder), data, allow_all),
  76. json::json_numeric_width_exception);
  77. }
  78. void test_parses_double_min_scientific_within_five_decimal_places() {
  79. char data[] = "2.22508e-308"; // 2.22507e-308 is min according to cppreference
  80. double out = 0.0;
  81. value_binder<double> binder{};
  82. TS_ASSERT_THROWS_NOTHING(parse(bind(out, binder), data, allow_all));
  83. TS_ASSERT_DELTA(out, std::numeric_limits<double>::min(), 1E-303);
  84. }
  85. void test_throws_exception_on_number_out_of_range_min() {
  86. char data[] = "2e-308";
  87. double out = 0.0;
  88. value_binder<double> binder{};
  89. TS_ASSERT_THROWS(parse(bind(out, binder), data, allow_all),
  90. json::json_numeric_width_exception);
  91. }
  92. };