// // json_binder_value_double.t.h // json // // Created by Sam Jaffe on 2/25/17. // #pragma once #include #include "json/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 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 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 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 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 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 binder{}; TS_ASSERT_THROWS_NOTHING(parse(bind(out, binder), data, allow_all)); TS_ASSERT_DELTA(out, std::numeric_limits::max(), 1E303); } void test_throws_exception_on_number_out_of_range_max() { char data[] = "1.8e+308"; double out = 0.0; value_binder 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 binder{}; TS_ASSERT_THROWS_NOTHING(parse(bind(out, binder), data, allow_all)); TS_ASSERT_DELTA(out, std::numeric_limits::lowest(), 1E303); } void test_throws_exception_on_number_out_of_range_lowest() { char data[] = "-1.8e+308"; double out = 0.0; value_binder 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 binder{}; TS_ASSERT_THROWS_NOTHING(parse(bind(out, binder), data, allow_all)); TS_ASSERT_DELTA(out, std::numeric_limits::min(), 1E-303); } void test_throws_exception_on_number_out_of_range_min() { char data[] = "2e-308"; double out = 0.0; value_binder binder{}; TS_ASSERT_THROWS(parse(bind(out, binder), data, allow_all), json::json_numeric_width_exception); } void test_double_number_written_is_equal_exact_binary() { std::stringstream ss; double in = 500., out = 0; value_binder binder{}; write(bind(in, binder), ss); parse(bind(out, binder), ss.str().c_str(), allow_all); TS_ASSERT_EQUALS(out, in); } void test_double_number_written_is_equal_inexact_binary() { std::stringstream ss; double in = 0.3, out = 0; value_binder binder{}; write(bind(in, binder), ss); parse(bind(out, binder), ss.str().c_str(), allow_all); TS_ASSERT_EQUALS(out, in); } void test_double_number_written_is_equal_at_high_absolute_value() { std::stringstream ss; double in = 1.34e300, out = 0.0; value_binder binder{}; write(bind(in, binder), ss); parse(bind(out, binder), ss.str().c_str(), allow_all); TS_ASSERT_EQUALS(out, in); } void test_double_number_written_is_equal_at_low_absolute_value() { std::stringstream ss; double in = 1.34e-300, out = 0.0; value_binder binder{}; write(bind(in, binder), ss); parse(bind(out, binder), ss.str().c_str(), allow_all); TS_ASSERT_DELTA(out, in, 1e-299); } void test_double_number_written_is_equal_at_high_absolute_value_nonscientific() { std::stringstream ss; double in = 52450012.25, out = 0.0; value_binder binder{}; write(bind(in, binder), ss); parse(bind(out, binder), ss.str().c_str(), allow_all); TS_ASSERT_EQUALS(out, in); } void test_double_number_written_is_equal_at_low_absolute_value_nonscientific() { std::stringstream ss; double in = 0.0002517867, out = 0.0; value_binder binder{}; write(bind(in, binder), ss); parse(bind(out, binder), ss.str().c_str(), allow_all); TS_ASSERT_DELTA(out, in, 0.000001); } };