| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- //
- // json_binder_value_double.t.h
- // json
- //
- // Created by Sam Jaffe on 2/25/17.
- //
- #pragma once
- #include <cxxtest/TestSuite.h>
- #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<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);
- }
-
- void test_double_number_written_is_equal_exact_binary() {
- std::stringstream ss;
- double in = 500., out = 0;
- value_binder<double> 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<double> 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<double> 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<double> 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<double> 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<double> binder{};
- write(bind(in, binder), ss);
- parse(bind(out, binder), ss.str().c_str(), allow_all);
- TS_ASSERT_DELTA(out, in, 0.000001);
- }
- };
|