json_binder_value_int.t.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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_negative_into_unsigned_throws() {
  30. char data[] = "-1";
  31. unsigned int out = 0;
  32. value_binder<unsigned int> binder{};
  33. TS_ASSERT_THROWS(parse(bind(out, binder), data, allow_all),
  34. json::json_numeric_exception);
  35. }
  36. void test_empty_buffer_to_int_throws() {
  37. char data[] = "";
  38. int out = 0;
  39. value_binder<int> binder{};
  40. TS_ASSERT_THROWS(parse(bind(out, binder), data, allow_all),
  41. json::unterminated_json_exception);
  42. TS_ASSERT_EQUALS(out, 0);
  43. }
  44. void test_non_integral_to_int_throws() {
  45. char data[] = "one";
  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. }
  51. void test_oversized_int_throws() {
  52. char data[] = "1000000000000";
  53. int out = 0;
  54. value_binder<int> binder{};
  55. TS_ASSERT_THROWS(parse(bind(out, binder), data, allow_all),
  56. json::json_numeric_exception);
  57. TS_ASSERT_EQUALS(out, 0);
  58. }
  59. void test_unsigned_int_downcast_to_int_throws() {
  60. char data[] = "2147483648";
  61. int out = 0;
  62. value_binder<int> binder{};
  63. TS_ASSERT_THROWS(parse(bind(out, binder), data, allow_all),
  64. json::json_numeric_width_exception);
  65. TS_ASSERT_EQUALS(out, 0);
  66. }
  67. void test_signed_int_max_parses() {
  68. char data[] = "2147483647";
  69. int out = 0;
  70. value_binder<int> binder{};
  71. TS_ASSERT_THROWS_NOTHING(parse(bind(out, binder), data, allow_all));
  72. }
  73. void test_signed_int_min_parses() {
  74. char data[] = "-2147483648";
  75. int out = 0;
  76. value_binder<int> binder{};
  77. TS_ASSERT_THROWS_NOTHING(parse(bind(out, binder), data, allow_all));
  78. }
  79. void test_signed_int_under_min_throws() {
  80. char data[] = "-2147483649";
  81. int out = 0;
  82. value_binder<int> binder{};
  83. TS_ASSERT_THROWS(parse(bind(out, binder), data, allow_all),
  84. json::json_numeric_width_exception);
  85. }
  86. void test_parsing_short_will_error_over_narrowing_max() {
  87. char data[] = "32768";
  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. TS_ASSERT_EQUALS(out, 0);
  93. }
  94. void test_parsing_short_will_error_over_narrowing_min() {
  95. char data[] = "-32769";
  96. short out = 0;
  97. value_binder<short> binder{};
  98. TS_ASSERT_THROWS(parse(bind(out, binder), data, allow_all),
  99. json::json_numeric_width_exception);
  100. }
  101. void test_double_to_int_throws() {
  102. char data[] = "2.0";
  103. int out = 0;
  104. value_binder<int> binder{};
  105. TS_ASSERT_THROWS(parse(bind(out, binder), data, allow_all),
  106. json::json_numeric_exception);
  107. TS_ASSERT_EQUALS(out, 0);
  108. }
  109. void test_parse_hexadecimal_number() {
  110. char data[] = "0xF";
  111. int out = 0;
  112. value_binder<int> binder{};
  113. parse(bind(out, binder), data, allow_all);
  114. TS_ASSERT_EQUALS(out, 15);
  115. }
  116. void test_parse_hexadecimal_number_out_of_bounds() {
  117. char data[] = "0x100000000";
  118. int out = 0;
  119. value_binder<int> binder{};
  120. TS_ASSERT_THROWS(parse(bind(out, binder), data, allow_all),
  121. json::json_numeric_exception);
  122. }
  123. void test_parse_octal_number() {
  124. char data[] = "010";
  125. int out = 0;
  126. value_binder<int> binder{};
  127. parse(bind(out, binder), data, allow_all);
  128. TS_ASSERT_EQUALS(out, 8);
  129. }
  130. void test_parse_octal_number_out_of_bounds() {
  131. char data[] = "040000000000";
  132. int out = 0;
  133. value_binder<int> binder{};
  134. TS_ASSERT_THROWS(parse(bind(out, binder), data, allow_all),
  135. json::json_numeric_exception);
  136. }
  137. };