| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- //
- // json_binder_value_double.t.h
- // json
- //
- // Created by Sam Jaffe on 2/25/17.
- //
- #pragma once
- #include <cxxtest/TestSuite.h>
- #include "json_binder.hpp"
- using namespace json::binder;
- using namespace json::parser;
- class json_binder_value_float_TestSuite : public CxxTest::TestSuite {
- public:
- void test_bind_to_double_type() {
- char data[] = "0.5";
- double out = 0.0;
- value_binder<double> binder{};
- parse(bind(out, binder), data, allow_all);
- TS_ASSERT_EQUALS(out, 0.5);
- }
-
- void test_parse_out_scientific_notation() {
- char data[] = "2e4";
- double out = 0.0;
- value_binder<double> binder{};
- TS_ASSERT_THROWS_NOTHING(parse(bind(out, binder), data, allow_all));
- }
-
- void test_parse_throws_on_no_data() {
- char data[] = "";
- double out = 0.0;
- value_binder<double> binder{};
- TS_ASSERT_THROWS(parse(bind(out, binder), data, allow_all),
- json::json_numeric_exception);
- }
- void test_parse_throws_on_non_numeric_data() {
- char data[] = "one half";
- double out = 0.0;
- value_binder<double> binder{};
- TS_ASSERT_THROWS(parse(bind(out, binder), data, allow_all),
- json::json_numeric_exception);
- }
-
- void test_parse_throws_on_incorrectly_terminated_data_with_flag() {
- char data[] = "5.0boo";
- double out = 0.0;
- value_binder<double> binder{};
- TS_ASSERT_THROWS(parse(bind(out, binder), data,
- disable_concatenated_json_bodies),
- json::malformed_json_exception);
- TS_ASSERT_EQUALS(out, 5.0);
- }
-
- void test_parses_double_max_scientific_within_five_decimal_places() {
- char data[] = "1.79769e+308";
- double out = 0.0;
- value_binder<double> binder{};
- TS_ASSERT_THROWS_NOTHING(parse(bind(out, binder), data, allow_all));
- TS_ASSERT_DELTA(out, std::numeric_limits<double>::max(), 1E303);
- }
- void test_throws_exception_on_number_out_of_range_max() {
- char data[] = "1.8e+308";
- double out = 0.0;
- value_binder<double> binder{};
- TS_ASSERT_THROWS(parse(bind(out, binder), data, allow_all),
- json::json_numeric_width_exception);
- }
- void test_parses_double_lowest_scientific_within_five_decimal_places() {
- char data[] = "-1.79769e+308";
- double out = 0.0;
- value_binder<double> binder{};
- TS_ASSERT_THROWS_NOTHING(parse(bind(out, binder), data, allow_all));
- TS_ASSERT_DELTA(out, std::numeric_limits<double>::lowest(), 1E303);
- }
-
- void test_throws_exception_on_number_out_of_range_lowest() {
- char data[] = "-1.8e+308";
- double out = 0.0;
- value_binder<double> binder{};
- TS_ASSERT_THROWS(parse(bind(out, binder), data, allow_all),
- json::json_numeric_width_exception);
- }
-
- void test_parses_double_min_scientific_within_five_decimal_places() {
- char data[] = "2.22508e-308"; // 2.22507e-308 is min according to cppreference
- double out = 0.0;
- value_binder<double> binder{};
- TS_ASSERT_THROWS_NOTHING(parse(bind(out, binder), data, allow_all));
- TS_ASSERT_DELTA(out, std::numeric_limits<double>::min(), 1E-303);
- }
-
- void test_throws_exception_on_number_out_of_range_min() {
- char data[] = "2e-308";
- double out = 0.0;
- value_binder<double> binder{};
- TS_ASSERT_THROWS(parse(bind(out, binder), data, allow_all),
- json::json_numeric_width_exception);
- }
- };
|