extract_int.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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/qi/numeric/numeric_utils.hpp>
  8. #include <boost/static_assert.hpp>
  9. #include <cmath> // for std::pow
  10. #include <iosfwd>
  11. #include <limits>
  12. #include <sstream>
  13. #ifdef _MSC_VER
  14. # pragma warning(disable: 4127) // conditional expression is constant
  15. #endif
  16. template <int Min, int Max>
  17. struct custom_int
  18. {
  19. BOOST_DEFAULTED_FUNCTION(custom_int(), {})
  20. BOOST_CONSTEXPR custom_int(int value) : value_(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) const { return value_ / x.value_; }
  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/=(custom_int x) { value_ /= x.value_; return *this; }
  29. custom_int& operator++() { ++value_; return *this; }
  30. custom_int& operator--() { --value_; return *this; }
  31. custom_int operator++(int) { return value_++; }
  32. custom_int operator--(int) { return value_--; }
  33. custom_int operator+() { return +value_; }
  34. custom_int operator-() { return -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. bool operator!=(custom_int x) const { return value_ != x.value_; }
  41. template <typename Char, typename Traits>
  42. friend std::basic_ostream<Char, Traits>&
  43. operator<<(std::basic_ostream<Char, Traits>& os, custom_int x) {
  44. return os << x.value_;
  45. }
  46. BOOST_STATIC_CONSTEXPR int max = Max;
  47. BOOST_STATIC_CONSTEXPR int min = Min;
  48. private:
  49. int value_;
  50. };
  51. namespace utils {
  52. template <int Min, int Max> struct digits;
  53. template <> struct digits<-9, 9> { BOOST_STATIC_CONSTEXPR int r2 = 3, r10 = 1; };
  54. template <> struct digits<-10, 10> { BOOST_STATIC_CONSTEXPR int r2 = 3, r10 = 1; };
  55. template <> struct digits<-15, 15> { BOOST_STATIC_CONSTEXPR int r2 = 3, r10 = 1; };
  56. }
  57. namespace std {
  58. template <int Min, int Max>
  59. class numeric_limits<custom_int<Min, Max> > : public numeric_limits<int>
  60. {
  61. public:
  62. static BOOST_CONSTEXPR custom_int<Min, Max> max() BOOST_NOEXCEPT_OR_NOTHROW { return Max; }
  63. static BOOST_CONSTEXPR custom_int<Min, Max> min() BOOST_NOEXCEPT_OR_NOTHROW { return Min; }
  64. static BOOST_CONSTEXPR custom_int<Min, Max> lowest() BOOST_NOEXCEPT_OR_NOTHROW { return min(); }
  65. BOOST_STATIC_ASSERT_MSG(numeric_limits<int>::radix == 2, "hardcoded for digits of radix 2");
  66. BOOST_STATIC_CONSTEXPR int digits = utils::digits<Min, Max>::r2;
  67. BOOST_STATIC_CONSTEXPR int digits10 = utils::digits<Min, Max>::r10;
  68. };
  69. }
  70. namespace qi = boost::spirit::qi;
  71. template <typename T, int Base, int MaxDigits>
  72. void test_overflow_handling(char const* begin, char const* end, int i)
  73. {
  74. // Check that parser fails on overflow
  75. BOOST_STATIC_ASSERT_MSG(std::numeric_limits<T>::is_bounded, "tests prerequest");
  76. BOOST_ASSERT_MSG(MaxDigits == -1 || static_cast<int>(std::pow(float(Base), MaxDigits)) > T::max,
  77. "test prerequest");
  78. int initial = Base - i % Base; // just a 'random' non-equal to i number
  79. T x(initial);
  80. char const* it = begin;
  81. bool r = qi::extract_int<T, Base, 1, MaxDigits>::call(it, end, x);
  82. if (T::min <= i && i <= T::max) {
  83. BOOST_TEST(r);
  84. BOOST_TEST(it == end);
  85. BOOST_TEST_EQ(x, i);
  86. }
  87. else {
  88. BOOST_TEST(!r);
  89. BOOST_TEST(it == begin);
  90. }
  91. }
  92. template <typename T, int Base>
  93. void test_unparsed_digits_are_not_consumed(char const* it, char const* end, int i)
  94. {
  95. // Check that unparsed digits are not consumed
  96. BOOST_STATIC_ASSERT_MSG(T::min <= -Base+1, "test prerequest");
  97. BOOST_STATIC_ASSERT_MSG(T::max >= Base-1, "test prerequest");
  98. bool has_sign = *it == '+' || *it == '-';
  99. char const* begin = it;
  100. int initial = Base - i % Base; // just a 'random' non-equal to i number
  101. T x(initial);
  102. bool r = qi::extract_int<T, Base, 1, 1>::call(it, end, x);
  103. BOOST_TEST(r);
  104. if (-Base < i && i < Base) {
  105. BOOST_TEST(it == end);
  106. BOOST_TEST_EQ(x, i);
  107. }
  108. else {
  109. BOOST_TEST_EQ(end - it, (end - begin) - 1 - has_sign);
  110. BOOST_TEST_EQ(x, i / Base);
  111. }
  112. }
  113. template <typename T, int Base>
  114. void test_ignore_overflow_digits(char const* it, char const* end, int i)
  115. {
  116. // TODO: Check accumulating too?
  117. if (i < 0) return; // extract_int does not support IgnoreOverflowDigits
  118. bool has_sign = *it == '+' || *it == '-';
  119. char const* begin = it;
  120. int initial = Base - i % Base; // just a 'random' non-equal to i number
  121. T x(initial);
  122. BOOST_TEST((qi::extract_uint<T, Base, 1, -1, false, true>::call(it, end, x)));
  123. if (T::min <= i && i <= T::max) {
  124. BOOST_TEST(it == end);
  125. BOOST_TEST_EQ(x, i);
  126. }
  127. else {
  128. BOOST_TEST_EQ(it - begin, (qi::detail::digits_traits<T, Base>::value) + has_sign);
  129. if (Base == std::numeric_limits<T>::radix)
  130. BOOST_TEST_EQ(it - begin, std::numeric_limits<T>::digits + has_sign);
  131. if (Base == 10)
  132. BOOST_TEST_EQ(it - begin, std::numeric_limits<T>::digits10 + has_sign);
  133. int expected = i;
  134. for (char const* p = it; p < end; ++p) expected /= Base;
  135. BOOST_TEST_EQ(x, expected);
  136. }
  137. }
  138. template <typename T, int Base>
  139. void run_tests(char const* begin, char const* end, int i)
  140. {
  141. // Check that parser fails on overflow
  142. test_overflow_handling<T, Base, -1>(begin, end, i);
  143. // Check that MaxDigits > digits10 behave like MaxDigits=-1
  144. test_overflow_handling<T, Base, 2>(begin, end, i);
  145. // Check that unparsed digits are not consumed
  146. test_unparsed_digits_are_not_consumed<T, Base>(begin, end, i);
  147. // Check that IgnoreOverflowDigits does what we expect
  148. test_ignore_overflow_digits<T, Base>(begin, end, i);
  149. }
  150. int main()
  151. {
  152. for (int i = -30; i <= 30; ++i) {
  153. std::ostringstream oss;
  154. oss << i;
  155. std::string s = oss.str();
  156. char const* begin = s.data(), *const end = begin + s.size();
  157. // log(Base, abs(MinOrMax) + 1) == digits
  158. run_tests<custom_int<-9, 9>, 10>(begin, end, i);
  159. // (MinOrMax % Base) == 0
  160. run_tests<custom_int<-10, 10>, 10>(begin, end, i);
  161. // (MinOrMax % Base) != 0
  162. run_tests<custom_int<-15, 15>, 10>(begin, end, i);
  163. }
  164. return boost::report_errors();
  165. }