// // json_binder_value_int.t.h // json // // Created by Sam Jaffe on 2/24/17. // #pragma once #include #include "json/json_binder.hpp" using namespace json::binder; using namespace json::parser; class json_binder_value_int_TestSuite : public CxxTest::TestSuite { public: void test_bind_to_integer_type() { char data[] = "100"; int out = 0; value_binder binder{}; parse(bind(out, binder), data, allow_all); TS_ASSERT_EQUALS(out, 100); } void test_negative_into_unsigned_throws() { char data[] = "-1"; unsigned int out = 0; value_binder binder{}; TS_ASSERT_THROWS(parse(bind(out, binder), data, allow_all), json::json_numeric_exception); } void test_empty_buffer_to_int_throws() { char data[] = ""; int out = 0; value_binder binder{}; TS_ASSERT_THROWS(parse(bind(out, binder), data, allow_all), json::unterminated_json_exception); TS_ASSERT_EQUALS(out, 0); } void test_non_integral_to_int_throws() { char data[] = "one"; int out = 0; value_binder binder{}; TS_ASSERT_THROWS(parse(bind(out, binder), data, allow_all), json::json_numeric_exception); } void test_oversized_int_throws() { char data[] = "1000000000000"; int out = 0; value_binder binder{}; TS_ASSERT_THROWS(parse(bind(out, binder), data, allow_all), json::json_numeric_exception); TS_ASSERT_EQUALS(out, 0); } void test_unsigned_int_downcast_to_int_throws() { char data[] = "2147483648"; int out = 0; value_binder binder{}; TS_ASSERT_THROWS(parse(bind(out, binder), data, allow_all), json::json_numeric_width_exception); TS_ASSERT_EQUALS(out, 0); } void test_signed_int_max_parses() { char data[] = "2147483647"; int out = 0; value_binder binder{}; TS_ASSERT_THROWS_NOTHING(parse(bind(out, binder), data, allow_all)); } void test_signed_int_min_parses() { char data[] = "-2147483648"; int out = 0; value_binder binder{}; TS_ASSERT_THROWS_NOTHING(parse(bind(out, binder), data, allow_all)); } void test_signed_int_under_min_throws() { char data[] = "-2147483649"; int out = 0; value_binder binder{}; TS_ASSERT_THROWS(parse(bind(out, binder), data, allow_all), json::json_numeric_width_exception); } void test_parsing_short_will_error_over_narrowing_max() { char data[] = "32768"; short out = 0; value_binder binder{}; TS_ASSERT_THROWS(parse(bind(out, binder), data, allow_all), json::json_numeric_width_exception); TS_ASSERT_EQUALS(out, 0); } void test_parsing_short_will_error_over_narrowing_min() { char data[] = "-32769"; short out = 0; value_binder binder{}; TS_ASSERT_THROWS(parse(bind(out, binder), data, allow_all), json::json_numeric_width_exception); } void test_double_to_int_throws() { char data[] = "2.0"; int out = 0; value_binder binder{}; TS_ASSERT_THROWS(parse(bind(out, binder), data, allow_all), json::json_numeric_exception); TS_ASSERT_EQUALS(out, 0); } void test_parse_hexadecimal_number() { char data[] = "0xF"; int out = 0; value_binder binder{}; parse(bind(out, binder), data, allow_all); TS_ASSERT_EQUALS(out, 15); } void test_parse_hexadecimal_number_out_of_bounds() { char data[] = "0x100000000"; int out = 0; value_binder binder{}; TS_ASSERT_THROWS(parse(bind(out, binder), data, allow_all), json::json_numeric_exception); } void test_parse_octal_number() { char data[] = "010"; int out = 0; value_binder binder{}; parse(bind(out, binder), data, allow_all); TS_ASSERT_EQUALS(out, 8); } void test_parse_octal_number_out_of_bounds() { char data[] = "040000000000"; int out = 0; value_binder binder{}; TS_ASSERT_THROWS(parse(bind(out, binder), data, allow_all), json::json_numeric_exception); } void test_output_from_integer_type() { std::string const expected = "100"; std::stringstream ss; int const in = 100; value_binder binder{}; write(bind(in, binder), ss); TS_ASSERT_EQUALS(ss.str(), expected); } void test_unsigned_integer_output() { std::string const expected = "2147483648"; std::stringstream ss; unsigned int const in = 2147483648; value_binder binder{}; write(bind(in, binder), ss); TS_ASSERT_EQUALS(ss.str(), expected); } void test_signed_integer_output() { std::string const expected = "-2147483648"; std::stringstream ss; int const in = -2147483648; value_binder binder{}; write(bind(in, binder), ss); TS_ASSERT_EQUALS(ss.str(), expected); } };