extract_int.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*=============================================================================
  2. Copyright (c) 2018 Nikita Kniazev
  3. Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. =============================================================================*/
  6. #include <boost/detail/lightweight_test.hpp>
  7. #include <boost/spirit/home/x3/support/numeric_utils/extract_int.hpp>
  8. #include <cmath> // for std::pow
  9. #include <cstdio>
  10. #include <iosfwd>
  11. #include <limits>
  12. #ifdef _MSC_VER
  13. # pragma warning(disable: 4127) // conditional expression is constant
  14. #endif
  15. template <int Min, int Max>
  16. struct custom_int
  17. {
  18. custom_int() = default;
  19. constexpr custom_int(int value) : value_{value} {}
  20. custom_int operator+(custom_int x) const { return value_ + x.value_; }
  21. custom_int operator-(custom_int x) const { return value_ - x.value_; }
  22. custom_int operator*(custom_int x) const { return value_ * x.value_; }
  23. custom_int operator/(custom_int x) const { return value_ / x.value_; }
  24. custom_int& operator+=(custom_int x) { value_ += x.value_; return *this; }
  25. custom_int& operator-=(custom_int x) { value_ -= x.value_; return *this; }
  26. custom_int& operator*=(custom_int x) { value_ *= x.value_; return *this; }
  27. custom_int& operator/=(custom_int x) { value_ /= x.value_; return *this; }
  28. custom_int& operator++() { ++value_; return *this; }
  29. custom_int& operator--() { --value_; return *this; }
  30. custom_int operator++(int) { return value_++; }
  31. custom_int operator--(int) { return value_--; }
  32. custom_int operator+() { return +value_; }
  33. custom_int operator-() { return -value_; }
  34. bool operator< (custom_int x) const { return value_ < x.value_; }
  35. bool operator> (custom_int x) const { return value_ > x.value_; }
  36. bool operator<=(custom_int x) const { return value_ <= x.value_; }
  37. bool operator>=(custom_int x) const { return value_ >= x.value_; }
  38. bool operator==(custom_int x) const { return value_ == x.value_; }
  39. bool operator!=(custom_int x) const { return value_ != x.value_; }
  40. template <typename Char, typename Traits>
  41. friend std::basic_ostream<Char, Traits>&
  42. operator<<(std::basic_ostream<Char, Traits>& os, custom_int x) {
  43. return os << x.value_;
  44. }
  45. static constexpr int max = Max;
  46. static constexpr int min = Min;
  47. private:
  48. int value_;
  49. };
  50. namespace utils {
  51. template <int Min, int Max> struct digits;
  52. template <> struct digits<-9, 9> { static constexpr int r2 = 3, r10 = 1; };
  53. template <> struct digits<-10, 10> { static constexpr int r2 = 3, r10 = 1; };
  54. template <> struct digits<-15, 15> { static constexpr int r2 = 3, r10 = 1; };
  55. }
  56. namespace std {
  57. template <int Min, int Max>
  58. class numeric_limits<custom_int<Min, Max>> : public numeric_limits<int>
  59. {
  60. public:
  61. static constexpr custom_int<Min, Max> max() noexcept { return Max; }
  62. static constexpr custom_int<Min, Max> min() noexcept { return Min; }
  63. static constexpr custom_int<Min, Max> lowest() noexcept { return min(); }
  64. static_assert(numeric_limits<int>::radix == 2, "hardcoded for digits of radix 2");
  65. static constexpr int digits = utils::digits<Min, Max>::r2;
  66. static constexpr int digits10 = utils::digits<Min, Max>::r10;
  67. };
  68. }
  69. namespace x3 = boost::spirit::x3;
  70. template <typename T, int Base, int MaxDigits>
  71. void test_overflow_handling(char const* begin, char const* end, int i)
  72. {
  73. // Check that parser fails on overflow
  74. static_assert(std::numeric_limits<T>::is_bounded, "tests prerequest");
  75. BOOST_ASSERT_MSG(MaxDigits == -1 || static_cast<int>(std::pow(float(Base), MaxDigits)) > T::max,
  76. "test prerequest");
  77. int initial = Base - i % Base; // just a 'random' non-equal to i number
  78. T x { initial };
  79. char const* it = begin;
  80. bool r = x3::extract_int<T, Base, 1, MaxDigits>::call(it, end, x);
  81. if (T::min <= i && i <= T::max) {
  82. BOOST_TEST(r);
  83. BOOST_TEST(it == end);
  84. BOOST_TEST_EQ(x, i);
  85. }
  86. else
  87. if (MaxDigits == -1) // TODO: Looks like a regression. See #430
  88. {
  89. BOOST_TEST(!r);
  90. BOOST_TEST(it == begin);
  91. }
  92. }
  93. template <typename T, int Base>
  94. void test_unparsed_digits_are_not_consumed(char const* it, char const* end, int i)
  95. {
  96. // Check that unparsed digits are not consumed
  97. static_assert(T::min <= -Base+1, "test prerequest");
  98. static_assert(T::max >= Base-1, "test prerequest");
  99. bool has_sign = *it == '+' || *it == '-';
  100. auto len = end - it;
  101. int initial = Base - i % Base; // just a 'random' non-equal to i number
  102. T x { initial };
  103. bool r = x3::extract_int<T, Base, 1, 1>::call(it, end, x);
  104. BOOST_TEST(r);
  105. if (-Base < i && i < Base) {
  106. BOOST_TEST(it == end);
  107. BOOST_TEST_EQ(x, i);
  108. }
  109. else {
  110. BOOST_TEST_EQ(end - it, len - 1 - has_sign);
  111. BOOST_TEST_EQ(x, i / Base);
  112. }
  113. }
  114. template <typename T, int Base>
  115. void run_tests(char const* begin, char const* end, int i)
  116. {
  117. // Check that parser fails on overflow
  118. test_overflow_handling<T, Base, -1>(begin, end, i);
  119. // Check that MaxDigits > digits10 behave like MaxDigits=-1
  120. test_overflow_handling<T, Base, 2>(begin, end, i);
  121. // Check that unparsed digits are not consumed
  122. test_unparsed_digits_are_not_consumed<T, Base>(begin, end, i);
  123. }
  124. int main()
  125. {
  126. for (int i = -30; i <= 30; ++i) {
  127. char s[4];
  128. std::snprintf(s, 4, "%d", i);
  129. auto begin = &s[0], end = begin + std::strlen(begin);
  130. // log(Base, abs(MinOrMax) + 1) == digits
  131. run_tests<custom_int<-9, 9>, 10>(begin, end, i);
  132. // (MinOrMax % Base) == 0
  133. run_tests<custom_int<-10, 10>, 10>(begin, end, i);
  134. // (MinOrMax % Base) != 0
  135. run_tests<custom_int<-15, 15>, 10>(begin, end, i);
  136. }
  137. return boost::report_errors();
  138. }