| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- //
- // json_binder_value_int.t.h
- // json
- //
- // Created by Sam Jaffe on 2/24/17.
- //
- #pragma once
- #include <cxxtest/TestSuite.h>
- #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<int> 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<unsigned int> 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<int> 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<int> 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<int> 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<int> 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<int> 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<int> 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<int> 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<short> 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<short> 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<int> 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<int> 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<int> 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<int> 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<int> 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<int> 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<unsigned int> 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<int> binder{};
- write(bind(in, binder), ss);
- TS_ASSERT_EQUALS(ss.str(), expected);
- }
- };
|