json_binder_value_int.t.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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/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_negative_into_unsigned_throws() {
  22. char data[] = "-1";
  23. unsigned int out = 0;
  24. value_binder<unsigned int> binder{};
  25. TS_ASSERT_THROWS(parse(bind(out, binder), data, allow_all),
  26. json::json_numeric_exception);
  27. }
  28. void test_empty_buffer_to_int_throws() {
  29. char data[] = "";
  30. int out = 0;
  31. value_binder<int> binder{};
  32. TS_ASSERT_THROWS(parse(bind(out, binder), data, allow_all),
  33. json::unterminated_json_exception);
  34. TS_ASSERT_EQUALS(out, 0);
  35. }
  36. void test_non_integral_to_int_throws() {
  37. char data[] = "one";
  38. int out = 0;
  39. value_binder<int> binder{};
  40. TS_ASSERT_THROWS(parse(bind(out, binder), data, allow_all),
  41. json::json_numeric_exception);
  42. }
  43. void test_oversized_int_throws() {
  44. char data[] = "1000000000000";
  45. int out = 0;
  46. value_binder<int> binder{};
  47. TS_ASSERT_THROWS(parse(bind(out, binder), data, allow_all),
  48. json::json_numeric_exception);
  49. TS_ASSERT_EQUALS(out, 0);
  50. }
  51. void test_unsigned_int_downcast_to_int_throws() {
  52. char data[] = "2147483648";
  53. int out = 0;
  54. value_binder<int> binder{};
  55. TS_ASSERT_THROWS(parse(bind(out, binder), data, allow_all),
  56. json::json_numeric_width_exception);
  57. TS_ASSERT_EQUALS(out, 0);
  58. }
  59. void test_signed_int_max_parses() {
  60. char data[] = "2147483647";
  61. int out = 0;
  62. value_binder<int> binder{};
  63. TS_ASSERT_THROWS_NOTHING(parse(bind(out, binder), data, allow_all));
  64. }
  65. void test_signed_int_min_parses() {
  66. char data[] = "-2147483648";
  67. int out = 0;
  68. value_binder<int> binder{};
  69. TS_ASSERT_THROWS_NOTHING(parse(bind(out, binder), data, allow_all));
  70. }
  71. void test_signed_int_under_min_throws() {
  72. char data[] = "-2147483649";
  73. int out = 0;
  74. value_binder<int> binder{};
  75. TS_ASSERT_THROWS(parse(bind(out, binder), data, allow_all),
  76. json::json_numeric_width_exception);
  77. }
  78. void test_parsing_short_will_error_over_narrowing_max() {
  79. char data[] = "32768";
  80. short out = 0;
  81. value_binder<short> binder{};
  82. TS_ASSERT_THROWS(parse(bind(out, binder), data, allow_all),
  83. json::json_numeric_width_exception);
  84. TS_ASSERT_EQUALS(out, 0);
  85. }
  86. void test_parsing_short_will_error_over_narrowing_min() {
  87. char data[] = "-32769";
  88. short out = 0;
  89. value_binder<short> binder{};
  90. TS_ASSERT_THROWS(parse(bind(out, binder), data, allow_all),
  91. json::json_numeric_width_exception);
  92. }
  93. void test_double_to_int_throws() {
  94. char data[] = "2.0";
  95. int out = 0;
  96. value_binder<int> binder{};
  97. TS_ASSERT_THROWS(parse(bind(out, binder), data, allow_all),
  98. json::json_numeric_exception);
  99. TS_ASSERT_EQUALS(out, 0);
  100. }
  101. void test_parse_hexadecimal_number() {
  102. char data[] = "0xF";
  103. int out = 0;
  104. value_binder<int> binder{};
  105. parse(bind(out, binder), data, allow_all);
  106. TS_ASSERT_EQUALS(out, 15);
  107. }
  108. void test_parse_hexadecimal_number_out_of_bounds() {
  109. char data[] = "0x100000000";
  110. int out = 0;
  111. value_binder<int> binder{};
  112. TS_ASSERT_THROWS(parse(bind(out, binder), data, allow_all),
  113. json::json_numeric_exception);
  114. }
  115. void test_parse_octal_number() {
  116. char data[] = "010";
  117. int out = 0;
  118. value_binder<int> binder{};
  119. parse(bind(out, binder), data, allow_all);
  120. TS_ASSERT_EQUALS(out, 8);
  121. }
  122. void test_parse_octal_number_out_of_bounds() {
  123. char data[] = "040000000000";
  124. int out = 0;
  125. value_binder<int> binder{};
  126. TS_ASSERT_THROWS(parse(bind(out, binder), data, allow_all),
  127. json::json_numeric_exception);
  128. }
  129. void test_output_from_integer_type() {
  130. std::string const expected = "100";
  131. std::stringstream ss;
  132. int const in = 100;
  133. value_binder<int> binder{};
  134. write(bind(in, binder), ss);
  135. TS_ASSERT_EQUALS(ss.str(), expected);
  136. }
  137. void test_unsigned_integer_output() {
  138. std::string const expected = "2147483648";
  139. std::stringstream ss;
  140. unsigned int const in = 2147483648;
  141. value_binder<unsigned int> binder{};
  142. write(bind(in, binder), ss);
  143. TS_ASSERT_EQUALS(ss.str(), expected);
  144. }
  145. void test_signed_integer_output() {
  146. std::string const expected = "-2147483648";
  147. std::stringstream ss;
  148. int const in = -2147483648;
  149. value_binder<int> binder{};
  150. write(bind(in, binder), ss);
  151. TS_ASSERT_EQUALS(ss.str(), expected);
  152. }
  153. };