unit-32bit.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // __ _____ _____ _____
  2. // __| | __| | | | JSON for Modern C++ (supporting code)
  3. // | | |__ | | | | | | version 3.11.2
  4. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  5. //
  6. // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
  7. // SPDX-License-Identifier: MIT
  8. #include "doctest_compatibility.h"
  9. #include <nlohmann/json.hpp>
  10. using nlohmann::json;
  11. #include <climits> // SIZE_MAX
  12. #include <limits> // numeric_limits
  13. template <typename OfType, typename T, bool MinInRange, bool MaxInRange>
  14. struct trait_test_arg
  15. {
  16. using of_type = OfType;
  17. using type = T;
  18. static constexpr bool min_in_range = MinInRange;
  19. static constexpr bool max_in_range = MaxInRange;
  20. };
  21. TEST_CASE_TEMPLATE_DEFINE("value_in_range_of trait", T, value_in_range_of_test)
  22. {
  23. using nlohmann::detail::value_in_range_of;
  24. using of_type = typename T::of_type;
  25. using type = typename T::type;
  26. constexpr bool min_in_range = T::min_in_range;
  27. constexpr bool max_in_range = T::max_in_range;
  28. type const val_min = std::numeric_limits<type>::min();
  29. type const val_min2 = val_min + 1;
  30. type const val_max = std::numeric_limits<type>::max();
  31. type const val_max2 = val_max - 1;
  32. REQUIRE(CHAR_BIT == 8);
  33. std::string of_type_str;
  34. if (std::is_unsigned<of_type>::value)
  35. {
  36. of_type_str += "u";
  37. }
  38. of_type_str += "int";
  39. of_type_str += std::to_string(sizeof(of_type) * 8);
  40. INFO("of_type := ", of_type_str);
  41. std::string type_str;
  42. if (std::is_unsigned<type>::value)
  43. {
  44. type_str += "u";
  45. }
  46. type_str += "int";
  47. type_str += std::to_string(sizeof(type) * 8);
  48. INFO("type := ", type_str);
  49. CAPTURE(val_min);
  50. CAPTURE(min_in_range);
  51. CAPTURE(val_max);
  52. CAPTURE(max_in_range);
  53. if (min_in_range)
  54. {
  55. CHECK(value_in_range_of<of_type>(val_min));
  56. CHECK(value_in_range_of<of_type>(val_min2));
  57. }
  58. else
  59. {
  60. CHECK_FALSE(value_in_range_of<of_type>(val_min));
  61. CHECK_FALSE(value_in_range_of<of_type>(val_min2));
  62. }
  63. if (max_in_range)
  64. {
  65. CHECK(value_in_range_of<of_type>(val_max));
  66. CHECK(value_in_range_of<of_type>(val_max2));
  67. }
  68. else
  69. {
  70. CHECK_FALSE(value_in_range_of<of_type>(val_max));
  71. CHECK_FALSE(value_in_range_of<of_type>(val_max2));
  72. }
  73. }
  74. TEST_CASE("32bit")
  75. {
  76. REQUIRE(SIZE_MAX == 0xffffffff);
  77. }
  78. TEST_CASE_TEMPLATE_INVOKE(value_in_range_of_test, \
  79. trait_test_arg<std::size_t, std::int32_t, false, true>, \
  80. trait_test_arg<std::size_t, std::uint32_t, true, true>, \
  81. trait_test_arg<std::size_t, std::int64_t, false, false>, \
  82. trait_test_arg<std::size_t, std::uint64_t, true, false>);
  83. TEST_CASE("BJData")
  84. {
  85. SECTION("parse errors")
  86. {
  87. SECTION("array")
  88. {
  89. SECTION("optimized array: negative size")
  90. {
  91. std::vector<uint8_t> const vM = {'[', '$', 'M', '#', '[', 'I', 0x00, 0x20, 'M', 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0xFF, ']'};
  92. std::vector<uint8_t> const vMX = {'[', '$', 'U', '#', '[', 'M', 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 'U', 0x01, ']'};
  93. json _;
  94. CHECK_THROWS_WITH_AS(_ = json::from_bjdata(vM), "[json.exception.out_of_range.408] syntax error while parsing BJData size: integer value overflow", json::out_of_range&);
  95. CHECK(json::from_bjdata(vM, true, false).is_discarded());
  96. CHECK_THROWS_WITH_AS(_ = json::from_bjdata(vMX), "[json.exception.out_of_range.408] syntax error while parsing BJData size: integer value overflow", json::out_of_range&);
  97. CHECK(json::from_bjdata(vMX, true, false).is_discarded());
  98. }
  99. SECTION("optimized array: integer value overflow")
  100. {
  101. std::vector<uint8_t> const vL = {'[', '#', 'L', 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7F};
  102. std::vector<uint8_t> const vM = {'[', '$', 'M', '#', '[', 'I', 0x00, 0x20, 'M', 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0xFF, ']'};
  103. json _;
  104. CHECK_THROWS_WITH_AS(_ = json::from_bjdata(vL), "[json.exception.out_of_range.408] syntax error while parsing BJData size: integer value overflow", json::out_of_range&);
  105. CHECK(json::from_bjdata(vL, true, false).is_discarded());
  106. CHECK_THROWS_WITH_AS(_ = json::from_bjdata(vM), "[json.exception.out_of_range.408] syntax error while parsing BJData size: integer value overflow", json::out_of_range&);
  107. CHECK(json::from_bjdata(vM, true, false).is_discarded());
  108. }
  109. }
  110. }
  111. }