json_binder_value_int.t.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. //
  2. // json_binder_value_int.t.h
  3. // json
  4. //
  5. // Created by Sam Jaffe on 2/24/17.
  6. //
  7. #pragma once
  8. #include <cxxtest/TestSuite.h>
  9. #include "json_binder.hpp"
  10. using namespace json::binder;
  11. using namespace json::parser;
  12. class json_binder_value_int_TestSuite : public CxxTest::TestSuite {
  13. public:
  14. void test_bind_to_integer_type() {
  15. char data[] = "100";
  16. int out = 0;
  17. value_binder<int> binder{};
  18. parse(bind(out, binder), data, allow_all);
  19. TS_ASSERT_EQUALS(out, 100);
  20. }
  21. void test_output_from_integer_type() {
  22. std::string expected = "100";
  23. std::stringstream ss;
  24. int in = 100;
  25. value_binder<int> binder{};
  26. write(bind(in, binder), ss);
  27. TS_ASSERT_EQUALS(ss.str(), expected);
  28. }
  29. void test_empty_buffer_to_int_throws() {
  30. char data[] = "";
  31. int out = 0;
  32. value_binder<int> binder{};
  33. TS_ASSERT_THROWS(parse(bind(out, binder), data, allow_all),
  34. json::unterminated_json_exception);
  35. TS_ASSERT_EQUALS(out, 0);
  36. }
  37. void test_non_integral_to_int_throws() {
  38. char data[] = "one";
  39. int out = 0;
  40. value_binder<int> binder{};
  41. TS_ASSERT_THROWS(parse(bind(out, binder), data, allow_all),
  42. json::json_numeric_exception);
  43. }
  44. void test_oversized_int_throws() {
  45. char data[] = "1000000000000";
  46. int out = 0;
  47. value_binder<int> binder{};
  48. TS_ASSERT_THROWS(parse(bind(out, binder), data, allow_all),
  49. json::json_numeric_exception);
  50. TS_ASSERT_EQUALS(out, 0);
  51. }
  52. void test_unsigned_int_downcast_to_int_throws() {
  53. char data[] = "2147483648";
  54. int out = 0;
  55. value_binder<int> binder{};
  56. TS_ASSERT_THROWS(parse(bind(out, binder), data, allow_all),
  57. json::json_numeric_width_exception);
  58. TS_ASSERT_EQUALS(out, 0);
  59. }
  60. void test_signed_int_max_parses() {
  61. char data[] = "2147483647";
  62. int out = 0;
  63. value_binder<int> binder{};
  64. TS_ASSERT_THROWS_NOTHING(parse(bind(out, binder), data, allow_all));
  65. }
  66. void test_signed_int_min_parses() {
  67. char data[] = "-2147483648";
  68. int out = 0;
  69. value_binder<int> binder{};
  70. TS_ASSERT_THROWS_NOTHING(parse(bind(out, binder), data, allow_all));
  71. }
  72. void test_signed_int_under_min_throws() {
  73. char data[] = "-2147483649";
  74. int out = 0;
  75. value_binder<int> binder{};
  76. TS_ASSERT_THROWS(parse(bind(out, binder), data, allow_all),
  77. json::json_numeric_width_exception);
  78. }
  79. void test_parsing_short_will_error_over_narrowing_max() {
  80. char data[] = "32768";
  81. short out = 0;
  82. value_binder<short> binder{};
  83. TS_ASSERT_THROWS(parse(bind(out, binder), data, allow_all),
  84. json::json_numeric_width_exception);
  85. TS_ASSERT_EQUALS(out, 0);
  86. }
  87. void test_parsing_short_will_error_over_narrowing_min() {
  88. char data[] = "-32769";
  89. short out = 0;
  90. value_binder<short> binder{};
  91. TS_ASSERT_THROWS(parse(bind(out, binder), data, allow_all),
  92. json::json_numeric_width_exception);
  93. }
  94. void test_double_to_int_throws() {
  95. char data[] = "2.0";
  96. int out = 0;
  97. value_binder<int> binder{};
  98. TS_ASSERT_THROWS(parse(bind(out, binder), data, allow_all),
  99. json::json_numeric_exception);
  100. TS_ASSERT_EQUALS(out, 0);
  101. }
  102. void test_parse_hexadecimal_number() {
  103. char data[] = "0xF";
  104. int out = 0;
  105. value_binder<int> binder{};
  106. parse(bind(out, binder), data, allow_all);
  107. TS_ASSERT_EQUALS(out, 15);
  108. }
  109. void test_parse_hexadecimal_number_out_of_bounds() {
  110. char data[] = "0x100000000";
  111. int out = 0;
  112. value_binder<int> binder{};
  113. TS_ASSERT_THROWS(parse(bind(out, binder), data, allow_all),
  114. json::json_numeric_exception);
  115. }
  116. void test_parse_octal_number() {
  117. char data[] = "010";
  118. int out = 0;
  119. value_binder<int> binder{};
  120. parse(bind(out, binder), data, allow_all);
  121. TS_ASSERT_EQUALS(out, 8);
  122. }
  123. void test_parse_octal_number_out_of_bounds() {
  124. char data[] = "040000000000";
  125. int out = 0;
  126. value_binder<int> binder{};
  127. TS_ASSERT_THROWS(parse(bind(out, binder), data, allow_all),
  128. json::json_numeric_exception);
  129. }
  130. };