proconlib

This documentation is automatically generated by competitive-verifier/competitive-verifier

View the Project on GitHub anqooqie/proconlib

:heavy_check_mark: tests/safe_int.test.cpp

Depends on

Code

// competitive-verifier: STANDALONE

#include <cstdlib>
#include <iostream>
#include "tools/assert_that.hpp"
#include "tools/safe_int.hpp"

void test_signed_int() {
  const tools::safe_int<int> POS_INF = tools::safe_int<int>::infinity();
  const tools::safe_int<int> NEG_INF = -tools::safe_int<int>::infinity();
  const int INT_MAX = std::numeric_limits<int>::max();
  const int INT_MIN = std::numeric_limits<int>::min();
  using s = tools::safe_int<int>;

  // basic arithmetic operations
  assert_that(s(1) + s(2) == s(3));
  assert_that(s(1) - s(2) == s(-1));
  assert_that(s(1) * s(2) == s(2));
  assert_that(s(1) / s(2) == s(0));
  assert_that(s(1) % s(2) == s(1));

  // operator+ should detect an overflow.
  assert_that(s(INT_MAX - 2) + s(1) == s(INT_MAX - 1));
  assert_that(s(INT_MAX - 2) + s(2) == s(INT_MAX));
  assert_that((s(INT_MAX - 2) + s(3)).is_nan());
  assert_that(s(1) + s(INT_MAX - 2) == s(INT_MAX - 1));
  assert_that(s(2) + s(INT_MAX - 2) == s(INT_MAX));
  assert_that((s(3) + s(INT_MAX - 2)).is_nan());
  assert_that(s(INT_MIN + 2) + s(-1) == s(INT_MIN + 1));
  assert_that(s(INT_MIN + 2) + s(-2) == s(INT_MIN));
  assert_that((s(INT_MIN + 2) + s(-3)).is_nan());
  assert_that(s(-1) + s(INT_MIN + 2) == s(INT_MIN + 1));
  assert_that(s(-2) + s(INT_MIN + 2) == s(INT_MIN));
  assert_that((s(-3) + s(INT_MIN + 2)).is_nan());

  // infinite + finite should be infinite.
  assert_that(POS_INF + s(-1) == POS_INF);
  assert_that(s(-1) + POS_INF == POS_INF);
  assert_that(POS_INF + POS_INF == POS_INF);
  assert_that(NEG_INF + s(1) == NEG_INF);
  assert_that(s(1) + NEG_INF == NEG_INF);
  assert_that(NEG_INF + NEG_INF == NEG_INF);

  // operator- should detect an overflow.
  assert_that(s(INT_MAX - 2) - s(-1) == s(INT_MAX - 1));
  assert_that(s(INT_MAX - 2) - s(-2) == s(INT_MAX));
  assert_that(s((INT_MAX - 2) - s(-3)).is_nan());
  assert_that(s(INT_MIN + 2) - s(1) == s(INT_MIN + 1));
  assert_that(s(INT_MIN + 2) - s(2) == s(INT_MIN));
  assert_that((s(INT_MIN + 2) - s(3)).is_nan());

  // infinite - finite should be finite.
  assert_that(POS_INF - s(1) == POS_INF);
  assert_that(POS_INF - NEG_INF == POS_INF);
  assert_that(NEG_INF - s(-1) == NEG_INF);
  assert_that(NEG_INF - POS_INF == NEG_INF);

  // operator* should detect an overflow.
  assert_that(NEG_INF * NEG_INF == POS_INF);
  assert_that(NEG_INF * s(INT_MIN) == POS_INF);
  assert_that((NEG_INF * s(0)).is_nan());
  assert_that(NEG_INF * s(INT_MAX) == NEG_INF);
  assert_that(NEG_INF * POS_INF == NEG_INF);
  assert_that(s(INT_MIN) * NEG_INF == POS_INF);
  assert_that((s(INT_MIN) * s(INT_MIN)).is_nan());
  assert_that(s(INT_MIN) * s(0) == s(0));
  assert_that((s(INT_MIN) * s(INT_MAX)).is_nan());
  assert_that(s(INT_MIN) * POS_INF == NEG_INF);
  assert_that((s(0) * NEG_INF).is_nan());
  assert_that(s(0) * s(INT_MIN) == s(0));
  assert_that(s(0) * s(0) == s(0));
  assert_that(s(0) * s(INT_MAX) == s(0));
  assert_that((s(0) * POS_INF).is_nan());
  assert_that(s(INT_MAX) * NEG_INF == NEG_INF);
  assert_that((s(INT_MAX) * s(INT_MIN)).is_nan());
  assert_that(s(INT_MAX) * s(0) == s(0));
  assert_that((s(INT_MAX) * s(INT_MAX)).is_nan());
  assert_that(s(INT_MAX) * POS_INF == POS_INF);
  assert_that(POS_INF * NEG_INF == NEG_INF);
  assert_that(POS_INF * s(INT_MIN) == NEG_INF);
  assert_that((POS_INF * s(0)).is_nan());
  assert_that(POS_INF * s(INT_MAX) == POS_INF);
  assert_that(POS_INF * POS_INF == POS_INF);
}

void test_unsigned_int() {
  const tools::safe_int<unsigned int> POS_INF = tools::safe_int<unsigned int>::infinity();
  const unsigned int UINT_MAX = std::numeric_limits<unsigned int>::max();
  using u = tools::safe_int<unsigned int>;

  // basic arithmetic operations
  assert_that(u(3) + u(2) == u(5));
  assert_that(u(3) - u(2) == u(1));
  assert_that(u(3) * u(2) == u(6));
  assert_that(u(3) / u(2) == u(1));
  assert_that(u(3) % u(2) == u(1));

  // operator+ should detect an overflow.
  assert_that(u(UINT_MAX - 2) + u(1) == u(UINT_MAX - 1));
  assert_that(u(UINT_MAX - 2) + u(2) == u(UINT_MAX));
  assert_that((u(UINT_MAX - 2) + u(3)).is_nan());
  assert_that(u(1) + u(UINT_MAX - 2) == u(UINT_MAX - 1));
  assert_that(u(2) + u(UINT_MAX - 2) == u(UINT_MAX));
  assert_that((u(3) + u(UINT_MAX - 2)).is_nan());

  // infinite + finite should be infinite.
  assert_that(POS_INF + u(1) == POS_INF);
  assert_that(u(1) + POS_INF == POS_INF);
  assert_that(POS_INF + POS_INF == POS_INF);

  // operator- should detect an overflow.
  assert_that(u(2) - u(1) == u(1));
  assert_that(u(2) - u(2) == u(0));
  assert_that((u(2) - u(3)).is_nan());

  // infinite - finite should be finite.
  assert_that(POS_INF - u(1) == POS_INF);

  // operator* should detect an overflow.
  assert_that(u(0) * u(0) == u(0));
  assert_that(u(0) * u(UINT_MAX) == u(0));
  assert_that((u(0) * POS_INF).is_nan());
  assert_that(u(UINT_MAX) * u(0) == u(0));
  assert_that((u(UINT_MAX) * u(UINT_MAX)).is_nan());
  assert_that(u(UINT_MAX) * POS_INF == POS_INF);
  assert_that((POS_INF * u(0)).is_nan());
  assert_that(POS_INF * u(UINT_MAX) == POS_INF);
  assert_that(POS_INF * POS_INF == POS_INF);
}

int main() {
  std::cin.tie(nullptr);
  std::ios_base::sync_with_stdio(false);

  test_signed_int();
  test_unsigned_int();

  return 0;
}
#line 1 "tests/safe_int.test.cpp"
// competitive-verifier: STANDALONE

#include <cstdlib>
#include <iostream>
#line 1 "tools/assert_that.hpp"



#line 6 "tools/assert_that.hpp"

#define assert_that_impl(cond, file, line, func) do {\
  if (!cond) {\
    ::std::cerr << file << ':' << line << ": " << func << ": Assertion `" << #cond << "' failed." << '\n';\
    ::std::exit(EXIT_FAILURE);\
  }\
} while (false)
#define assert_that(...) assert_that_impl((__VA_ARGS__), __FILE__, __LINE__, __func__)


#line 1 "tools/safe_int.hpp"



#include <type_traits>
#include <cstddef>
#include <cassert>
#include <limits>
#include <array>
#include <optional>
#line 11 "tools/safe_int.hpp"

namespace tools {
  template <typename T, typename = void>
  class safe_int;

  template <typename T>
  class safe_int<T, ::std::enable_if_t<::std::is_signed_v<T>>> {
  private:
    enum class type {
      finite,
      pos_inf,
      neg_inf,
      nan
    };
    typename ::tools::safe_int<T>::type m_type;
    T m_value;

    constexpr safe_int(const typename ::tools::safe_int<T>::type type) :
      m_type(type), m_value(T()) {
    }

  public:
    constexpr safe_int() :
      m_type(::tools::safe_int<T>::type::finite), m_value(T()) {
    }
    explicit constexpr safe_int(const T value) :
      m_type(::tools::safe_int<T>::type::finite), m_value(value) {
    }
    constexpr safe_int(const ::tools::safe_int<T>& other) :
      m_type(other.m_type), m_value(other.m_value) {
    }
    ~safe_int() = default;
    constexpr ::tools::safe_int<T>& operator=(const ::tools::safe_int<T>& other) {
      this->m_type = other.m_type;
      this->m_value = other.m_value;
      return *this;
    }

    static constexpr ::tools::safe_int<T> infinity() {
      return tools::safe_int<T>(::tools::safe_int<T>::type::pos_inf);
    }
    static constexpr ::tools::safe_int<T> nan() {
      return tools::safe_int<T>(::tools::safe_int<T>::type::nan);
    }

  private:
    static constexpr int f1(const ::tools::safe_int<T>& n) {
      switch (n.m_type) {
      case ::tools::safe_int<T>::type::neg_inf:
        return 0;
      case ::tools::safe_int<T>::type::finite:
        return 1;
      case ::tools::safe_int<T>::type::pos_inf:
        return 2;
      default: // nan
        return 3;
      }
    };
    static constexpr int f2(const ::tools::safe_int<T>& n) {
      switch (n.m_type) {
      case ::tools::safe_int<T>::type::neg_inf:
        return 0;
      case ::tools::safe_int<T>::type::finite:
        if (n.m_value < 0) {
          return 1;
        } else if (n.m_value == 0) {
          return 2;
        } else {
          return 3;
        }
      case ::tools::safe_int<T>::type::pos_inf:
        return 4;
      default: // nan
        return 5;
      }
    };
    static constexpr ::std::optional<::tools::safe_int<T>> Q() {
      return ::std::nullopt;
    }
    static constexpr ::std::optional<::tools::safe_int<T>> Z() {
      return ::std::optional<::tools::safe_int<T>>(::tools::safe_int<T>(0));
    }
    static constexpr ::std::optional<::tools::safe_int<T>> N() {
      return ::std::optional<::tools::safe_int<T>>(::tools::safe_int<T>(::tools::safe_int<T>::type::neg_inf));
    }
    static constexpr ::std::optional<::tools::safe_int<T>> P() {
      return ::std::optional<::tools::safe_int<T>>(::tools::safe_int<T>(::tools::safe_int<T>::type::pos_inf));
    }
    static constexpr ::std::optional<::tools::safe_int<T>> U() {
      return ::std::optional<::tools::safe_int<T>>(::tools::safe_int<T>(::tools::safe_int<T>::type::nan));
    }
    static constexpr ::std::optional<bool> BQ() {
      return ::std::nullopt;
    }
    static constexpr ::std::optional<bool> BF() {
      return ::std::optional<bool>(false);
    }
    static constexpr ::std::optional<bool> BT() {
      return ::std::optional<bool>(true);
    }

  public:
    constexpr bool is_finite() const {
      return this->m_type == ::tools::safe_int<T>::type::finite;
    }

    constexpr bool is_nan() const {
      return this->m_type == ::tools::safe_int<T>::type::nan;
    }

    constexpr T val() const {
      assert(this->is_finite());
      return this->m_value;
    }

    friend constexpr bool operator==(const ::tools::safe_int<T>& x, const ::tools::safe_int<T>& y) {
      constexpr auto table = ::std::array<::std::array<::std::optional<bool>, 4>, 4>({{
        {BT(), BF(), BF(), BF()},
        {BF(), BQ(), BF(), BF()},
        {BF(), BF(), BT(), BF()},
        {BF(), BF(), BF(), BF()}
      }});
      if (const auto r = table[f1(x)][f1(y)]; r) return *r;

      return x.m_value == y.m_value;
    }
    friend constexpr bool operator!=(const ::tools::safe_int<T>& x, const ::tools::safe_int<T>& y) {
      return !(x == y);
    }

    constexpr ::tools::safe_int<T> operator+() const {
      return *this;
    }
    constexpr ::tools::safe_int<T> operator-() const {
      constexpr auto table = ::std::array<::std::optional<::tools::safe_int<T>>, 4>({
        {P(), Q(), N(), U()}
      });
      if (const auto r = table[f1(*this)]; r) return *r;

      if (this->m_value == ::std::numeric_limits<T>::min()) return *U();
      return ::tools::safe_int<T>(-this->m_value);
    }

    friend constexpr ::tools::safe_int<T> operator+(const ::tools::safe_int<T>& x, const ::tools::safe_int<T>& y) {
      constexpr auto table = ::std::array<::std::array<::std::optional<::tools::safe_int<T>>, 4>, 4>({{
        {N(), N(), U(), U()},
        {N(), Q(), P(), U()},
        {U(), P(), P(), U()},
        {U(), U(), U(), U()}
      }});
      if (const auto r = table[f1(x)][f1(y)]; r) return *r;

      if (y.m_value > 0 && x.m_value > ::std::numeric_limits<T>::max() - y.m_value) return *U();
      if (y.m_value < 0 && x.m_value < ::std::numeric_limits<T>::min() - y.m_value) return *U();
      return ::tools::safe_int<T>(x.m_value + y.m_value);
    }
    friend constexpr ::tools::safe_int<T> operator+(const ::tools::safe_int<T>& x, const T& y) {
      return x + tools::safe_int<T>(y);
    }
    friend constexpr ::tools::safe_int<T> operator+(const T& x, const ::tools::safe_int<T>& y) {
      return tools::safe_int<T>(x) + y;
    }

    friend constexpr ::tools::safe_int<T> operator-(const ::tools::safe_int<T>& x, const ::tools::safe_int<T>& y) {
      constexpr auto table = ::std::array<::std::array<::std::optional<::tools::safe_int<T>>, 4>, 4>({{
        {U(), N(), N(), U()},
        {P(), Q(), N(), U()},
        {P(), P(), U(), U()},
        {U(), U(), U(), U()}
      }});
      if (const auto r = table[f1(x)][f1(y)]; r) return *r;

      if (y.m_value < 0 && x.m_value > ::std::numeric_limits<T>::max() + y.m_value) return *U();
      if (y.m_value > 0 && x.m_value < ::std::numeric_limits<T>::min() + y.m_value) return *U();
      return ::tools::safe_int<T>(x.m_value - y.m_value);
    }
    friend constexpr ::tools::safe_int<T> operator-(const ::tools::safe_int<T>& x, const T& y) {
      return x - tools::safe_int<T>(y);
    }
    friend constexpr ::tools::safe_int<T> operator-(const T& x, const ::tools::safe_int<T>& y) {
      return tools::safe_int<T>(x) - y;
    }

    friend constexpr ::tools::safe_int<T> operator*(const ::tools::safe_int<T>& x, const ::tools::safe_int<T>& y) {
      constexpr auto table = ::std::array<::std::array<::std::optional<::tools::safe_int<T>>, 6>, 6>({{
        {P(), P(), U(), N(), N(), U()},
        {P(), Q(), Z(), Q(), N(), U()},
        {U(), Z(), Z(), Z(), U(), U()},
        {N(), Q(), Z(), Q(), P(), U()},
        {N(), N(), U(), P(), P(), U()},
        {U(), U(), U(), U(), U(), U()}
      }});
      if (const auto r = table[f2(x)][f2(y)]; r) return *r;

      if (x.m_value > 0) {
        if (y.m_value > 0) {
          if (x.m_value > ::std::numeric_limits<T>::max() / y.m_value) {
            return *U();
          }
        } else {
          if (y.m_value < ::std::numeric_limits<T>::min() / x.m_value) {
            return *U();
          }
        }
      } else {
        if (y.m_value > 0) {
          if (x.m_value < ::std::numeric_limits<T>::min() / y.m_value) {
            return *U();
          }
        } else {
          if (x.m_value != 0 && y.m_value < ::std::numeric_limits<T>::max() / x.m_value) {
            return *U();
          }
        }
      }
      return ::tools::safe_int<T>(x.m_value * y.m_value);
    }
    friend constexpr ::tools::safe_int<T> operator*(const ::tools::safe_int<T>& x, const T& y) {
      return x * tools::safe_int<T>(y);
    }
    friend constexpr ::tools::safe_int<T> operator*(const T& x, const ::tools::safe_int<T>& y) {
      return tools::safe_int<T>(x) * y;
    }

    friend constexpr ::tools::safe_int<T> operator/(const ::tools::safe_int<T>& x, const ::tools::safe_int<T>& y) {
      constexpr auto table = ::std::array<::std::array<::std::optional<::tools::safe_int<T>>, 6>, 6>({{
        {U(), P(), U(), N(), U(), U()},
        {Z(), Q(), U(), Q(), Z(), U()},
        {Z(), Z(), U(), Z(), Z(), U()},
        {Z(), Q(), U(), Q(), Z(), U()},
        {U(), N(), U(), P(), U(), U()},
        {U(), U(), U(), U(), U(), U()}
      }});
      if (const auto r = table[f2(x)][f2(y)]; r) return *r;

      if (x.m_value == ::std::numeric_limits<T>::min() && y.m_value == -1) return *U();
      return ::tools::safe_int<T>(x.m_value / y.m_value);
    }
    friend constexpr ::tools::safe_int<T> operator/(const ::tools::safe_int<T>& x, const T& y) {
      return x / tools::safe_int<T>(y);
    }
    friend constexpr ::tools::safe_int<T> operator/(const T& x, const ::tools::safe_int<T>& y) {
      return tools::safe_int<T>(x) / y;
    }

    friend constexpr ::tools::safe_int<T> operator%(const ::tools::safe_int<T>& x, const ::tools::safe_int<T>& y) {
      constexpr auto table = ::std::array<::std::array<::std::optional<::tools::safe_int<T>>, 6>, 6>({{
        {U(), U(), U(), U(), U(), U()},
        {U(), Q(), U(), Q(), U(), U()},
        {U(), Z(), U(), Z(), U(), U()},
        {U(), Q(), U(), Q(), U(), U()},
        {U(), U(), U(), U(), U(), U()},
        {U(), U(), U(), U(), U(), U()}
      }});
      if (const auto r = table[f2(x)][f2(y)]; r) return *r;

      if (x.m_value == ::std::numeric_limits<T>::min() && y.m_value == -1) return *U();
      return ::tools::safe_int<T>(x.m_value % y.m_value);
    }
    friend constexpr ::tools::safe_int<T> operator%(const ::tools::safe_int<T>& x, const T& y) {
      return x % tools::safe_int<T>(y);
    }
    friend constexpr ::tools::safe_int<T> operator%(const T& x, const ::tools::safe_int<T>& y) {
      return tools::safe_int<T>(x) % y;
    }

    constexpr ::tools::safe_int<T>& operator+=(const ::tools::safe_int<T>& other) {
      return *this = *this + other;
    }
    constexpr ::tools::safe_int<T>& operator+=(const T& other) {
      return *this = *this + ::tools::safe_int<T>(other);
    }
    constexpr ::tools::safe_int<T>& operator-=(const ::tools::safe_int<T>& other) {
      return *this = *this - other;
    }
    constexpr ::tools::safe_int<T>& operator-=(const T& other) {
      return *this = *this - ::tools::safe_int<T>(other);
    }
    constexpr ::tools::safe_int<T>& operator*=(const ::tools::safe_int<T>& other) {
      return *this = *this * other;
    }
    constexpr ::tools::safe_int<T>& operator*=(const T& other) {
      return *this = *this * ::tools::safe_int<T>(other);
    }
    constexpr ::tools::safe_int<T>& operator/=(const ::tools::safe_int<T>& other) {
      return *this = *this / other;
    }
    constexpr ::tools::safe_int<T>& operator/=(const T& other) {
      return *this = *this / ::tools::safe_int<T>(other);
    }
    constexpr ::tools::safe_int<T>& operator%=(const ::tools::safe_int<T>& other) {
      return *this = *this % other;
    }
    constexpr ::tools::safe_int<T>& operator%=(const T& other) {
      return *this = *this % ::tools::safe_int<T>(other);
    }

    constexpr ::tools::safe_int<T>& operator++() {
      return *this += ::tools::safe_int<T>(T(1));
    }
    constexpr ::tools::safe_int<T> operator++(int) {
      const auto r = *this;
      ++(*this);
      return r;
    }
    constexpr ::tools::safe_int<T>& operator--() {
      return *this -= ::tools::safe_int<T>(T(1));
    }
    constexpr ::tools::safe_int<T> operator--(int) {
      const auto r = *this;
      --(*this);
      return r;
    }

    friend constexpr bool operator<(const ::tools::safe_int<T>& x, const ::tools::safe_int<T>& y) {
      constexpr auto table = ::std::array<::std::array<::std::optional<bool>, 4>, 4>({{
        {BF(), BT(), BT(), BF()},
        {BF(), BQ(), BT(), BF()},
        {BF(), BF(), BF(), BF()},
        {BF(), BF(), BF(), BF()}
      }});
      if (const auto r = table[f1(x)][f1(y)]; r) return *r;

      return x.m_value < y.m_value;
    }
    friend constexpr bool operator>(const ::tools::safe_int<T>& x, const ::tools::safe_int<T>& y) {
      constexpr auto table = ::std::array<::std::array<::std::optional<bool>, 4>, 4>({{
        {BF(), BF(), BF(), BF()},
        {BT(), BQ(), BF(), BF()},
        {BT(), BT(), BF(), BF()},
        {BF(), BF(), BF(), BF()}
      }});
      if (const auto r = table[f1(x)][f1(y)]; r) return *r;

      return x.m_value > y.m_value;
    }
    friend constexpr bool operator<=(const ::tools::safe_int<T>& x, const ::tools::safe_int<T>& y) {
      return x < y || x == y;
    }
    friend constexpr bool operator>=(const ::tools::safe_int<T>& x, const ::tools::safe_int<T>& y) {
      return x > y || x == y;
    }

    friend ::std::istream& operator>>(::std::istream& is, ::tools::safe_int<T>& self) {
      self.m_type = ::tools::safe_int<T>::type::finite;
      return is >> self.m_value;
    }
    friend ::std::ostream& operator<<(::std::ostream& os, const ::tools::safe_int<T>& self) {
      switch (self.m_type) {
      case ::tools::safe_int<T>::type::neg_inf:
        return os << "-inf";
      case ::tools::safe_int<T>::type::finite:
        return os << self.m_value;
      case ::tools::safe_int<T>::type::pos_inf:
        return os << "inf";
      default: // nan
        return os << "nan";
      }
    }
  };

  template <typename T>
  class safe_int<T, ::std::enable_if_t<::std::is_unsigned_v<T>>> {
  private:
    enum class type {
      finite,
      pos_inf,
      nan
    };
    typename ::tools::safe_int<T>::type m_type;
    T m_value;

    constexpr safe_int(const typename ::tools::safe_int<T>::type type) :
      m_type(type), m_value(T()) {
    }

  public:
    constexpr safe_int() :
      m_type(::tools::safe_int<T>::type::finite), m_value(T()) {
    }
    explicit constexpr safe_int(const T value) :
      m_type(::tools::safe_int<T>::type::finite), m_value(value) {
    }
    constexpr safe_int(const ::tools::safe_int<T>& other) :
      m_type(other.m_type), m_value(other.m_value) {
    }
    ~safe_int() = default;
    constexpr ::tools::safe_int<T>& operator=(const ::tools::safe_int<T>& other) {
      this->m_type = other.m_type;
      this->m_value = other.m_value;
      return *this;
    }

    static constexpr ::tools::safe_int<T> infinity() {
      return tools::safe_int<T>(::tools::safe_int<T>::type::pos_inf);
    }
    static constexpr ::tools::safe_int<T> nan() {
      return tools::safe_int<T>(::tools::safe_int<T>::type::nan);
    }

  private:
    static constexpr int f1(const ::tools::safe_int<T>& n) {
      switch (n.m_type) {
      case ::tools::safe_int<T>::type::finite:
        return 0;
      case ::tools::safe_int<T>::type::pos_inf:
        return 1;
      default: // nan
        return 2;
      }
    };
    static constexpr int f2(const ::tools::safe_int<T>& n) {
      switch (n.m_type) {
      case ::tools::safe_int<T>::type::finite:
        if (n.m_value == 0) {
          return 0;
        } else {
          return 1;
        }
      case ::tools::safe_int<T>::type::pos_inf:
        return 2;
      default: // nan
        return 3;
      }
    };
    static constexpr ::std::optional<::tools::safe_int<T>> Q() {
      return ::std::nullopt;
    }
    static constexpr ::std::optional<::tools::safe_int<T>> Z() {
      return ::std::optional<::tools::safe_int<T>>(::tools::safe_int<T>(0));
    }
    static constexpr ::std::optional<::tools::safe_int<T>> P() {
      return ::std::optional<::tools::safe_int<T>>(::tools::safe_int<T>(::tools::safe_int<T>::type::pos_inf));
    }
    static constexpr ::std::optional<::tools::safe_int<T>> U() {
      return ::std::optional<::tools::safe_int<T>>(::tools::safe_int<T>(::tools::safe_int<T>::type::nan));
    }
    static constexpr ::std::optional<bool> BQ() {
      return ::std::nullopt;
    }
    static constexpr ::std::optional<bool> BF() {
      return ::std::optional<bool>(false);
    }
    static constexpr ::std::optional<bool> BT() {
      return ::std::optional<bool>(true);
    }

  public:
    constexpr bool is_finite() const {
      return this->m_type == ::tools::safe_int<T>::type::finite;
    }

    constexpr bool is_nan() const {
      return this->m_type == ::tools::safe_int<T>::type::nan;
    }

    constexpr T val() const {
      assert(this->is_finite());
      return this->m_value;
    }

    friend constexpr bool operator==(const ::tools::safe_int<T>& x, const ::tools::safe_int<T>& y) {
      constexpr auto table = ::std::array<::std::array<::std::optional<bool>, 3>, 3>({{
        {BQ(), BF(), BF()},
        {BF(), BT(), BF()},
        {BF(), BF(), BF()}
      }});
      if (const auto r = table[f1(x)][f1(y)]; r) return *r;

      return x.m_value == y.m_value;
    }
    friend constexpr bool operator!=(const ::tools::safe_int<T>& x, const ::tools::safe_int<T>& y) {
      return !(x == y);
    }

    constexpr ::tools::safe_int<T> operator+() const {
      return *this;
    }
    constexpr ::tools::safe_int<T> operator-() const {
      constexpr auto table = ::std::array<::std::optional<::tools::safe_int<T>>, 3>({
        {Q(), U(), U()}
      });
      if (const auto r = table[f1(*this)]; r) return *r;

      if (this->m_value > 0) return *U();
      return ::tools::safe_int<T>(0);
    }

    friend constexpr ::tools::safe_int<T> operator+(const ::tools::safe_int<T>& x, const ::tools::safe_int<T>& y) {
      constexpr auto table = ::std::array<::std::array<::std::optional<::tools::safe_int<T>>, 3>, 3>({{
        {Q(), P(), U()},
        {P(), P(), U()},
        {U(), U(), U()}
      }});
      if (const auto r = table[f1(x)][f1(y)]; r) return *r;

      if (y.m_value > 0 && x.m_value > ::std::numeric_limits<T>::max() - y.m_value) return *U();
      return ::tools::safe_int<T>(x.m_value + y.m_value);
    }
    friend constexpr ::tools::safe_int<T> operator+(const ::tools::safe_int<T>& x, const T& y) {
      return x + tools::safe_int<T>(y);
    }
    friend constexpr ::tools::safe_int<T> operator+(const T& x, const ::tools::safe_int<T>& y) {
      return tools::safe_int<T>(x) + y;
    }

    friend constexpr ::tools::safe_int<T> operator-(const ::tools::safe_int<T>& x, const ::tools::safe_int<T>& y) {
      constexpr auto table = ::std::array<::std::array<::std::optional<::tools::safe_int<T>>, 3>, 3>({{
        {Q(), U(), U()},
        {P(), U(), U()},
        {U(), U(), U()}
      }});
      if (const auto r = table[f1(x)][f1(y)]; r) return *r;

      if (x.m_value < y.m_value) return *U();
      return ::tools::safe_int<T>(x.m_value - y.m_value);
    }
    friend constexpr ::tools::safe_int<T> operator-(const ::tools::safe_int<T>& x, const T& y) {
      return x - tools::safe_int<T>(y);
    }
    friend constexpr ::tools::safe_int<T> operator-(const T& x, const ::tools::safe_int<T>& y) {
      return tools::safe_int<T>(x) - y;
    }

    friend constexpr ::tools::safe_int<T> operator*(const ::tools::safe_int<T>& x, const ::tools::safe_int<T>& y) {
      constexpr auto table = ::std::array<::std::array<::std::optional<::tools::safe_int<T>>, 4>, 4>({{
        {Z(), Z(), U(), U()},
        {Z(), Q(), P(), U()},
        {U(), P(), P(), U()},
        {U(), U(), U(), U()}
      }});
      if (const auto r = table[f2(x)][f2(y)]; r) return *r;

      if (x.m_value > ::std::numeric_limits<T>::max() / y.m_value) {
        return *U();
      }
      return ::tools::safe_int<T>(x.m_value * y.m_value);
    }
    friend constexpr ::tools::safe_int<T> operator*(const ::tools::safe_int<T>& x, const T& y) {
      return x * tools::safe_int<T>(y);
    }
    friend constexpr ::tools::safe_int<T> operator*(const T& x, const ::tools::safe_int<T>& y) {
      return tools::safe_int<T>(x) * y;
    }

    friend constexpr ::tools::safe_int<T> operator/(const ::tools::safe_int<T>& x, const ::tools::safe_int<T>& y) {
      constexpr auto table = ::std::array<::std::array<::std::optional<::tools::safe_int<T>>, 4>, 4>({{
        {U(), Z(), Z(), U()},
        {U(), Q(), Z(), U()},
        {U(), P(), U(), U()},
        {U(), U(), U(), U()}
      }});
      if (const auto r = table[f2(x)][f2(y)]; r) return *r;

      return ::tools::safe_int<T>(x.m_value / y.m_value);
    }
    friend constexpr ::tools::safe_int<T> operator/(const ::tools::safe_int<T>& x, const T& y) {
      return x / tools::safe_int<T>(y);
    }
    friend constexpr ::tools::safe_int<T> operator/(const T& x, const ::tools::safe_int<T>& y) {
      return tools::safe_int<T>(x) / y;
    }

    friend constexpr ::tools::safe_int<T> operator%(const ::tools::safe_int<T>& x, const ::tools::safe_int<T>& y) {
      constexpr auto table = ::std::array<::std::array<::std::optional<::tools::safe_int<T>>, 4>, 4>({{
        {U(), Z(), U(), U()},
        {U(), Q(), U(), U()},
        {U(), U(), U(), U()},
        {U(), U(), U(), U()}
      }});
      if (const auto r = table[f2(x)][f2(y)]; r) return *r;

      return ::tools::safe_int<T>(x.m_value % y.m_value);
    }
    friend constexpr ::tools::safe_int<T> operator%(const ::tools::safe_int<T>& x, const T& y) {
      return x % tools::safe_int<T>(y);
    }
    friend constexpr ::tools::safe_int<T> operator%(const T& x, const ::tools::safe_int<T>& y) {
      return tools::safe_int<T>(x) % y;
    }

    constexpr ::tools::safe_int<T>& operator+=(const ::tools::safe_int<T>& other) {
      return *this = *this + other;
    }
    constexpr ::tools::safe_int<T>& operator+=(const T& other) {
      return *this = *this + ::tools::safe_int<T>(other);
    }
    constexpr ::tools::safe_int<T>& operator-=(const ::tools::safe_int<T>& other) {
      return *this = *this - other;
    }
    constexpr ::tools::safe_int<T>& operator-=(const T& other) {
      return *this = *this - ::tools::safe_int<T>(other);
    }
    constexpr ::tools::safe_int<T>& operator*=(const ::tools::safe_int<T>& other) {
      return *this = *this * other;
    }
    constexpr ::tools::safe_int<T>& operator*=(const T& other) {
      return *this = *this * ::tools::safe_int<T>(other);
    }
    constexpr ::tools::safe_int<T>& operator/=(const ::tools::safe_int<T>& other) {
      return *this = *this / other;
    }
    constexpr ::tools::safe_int<T>& operator/=(const T& other) {
      return *this = *this / ::tools::safe_int<T>(other);
    }
    constexpr ::tools::safe_int<T>& operator%=(const ::tools::safe_int<T>& other) {
      return *this = *this % other;
    }
    constexpr ::tools::safe_int<T>& operator%=(const T& other) {
      return *this = *this % ::tools::safe_int<T>(other);
    }

    constexpr ::tools::safe_int<T>& operator++() {
      return *this += ::tools::safe_int<T>(T(1));
    }
    constexpr ::tools::safe_int<T> operator++(int) {
      const auto r = *this;
      ++(*this);
      return r;
    }
    constexpr ::tools::safe_int<T>& operator--() {
      return *this -= ::tools::safe_int<T>(T(1));
    }
    constexpr ::tools::safe_int<T> operator--(int) {
      const auto r = *this;
      --(*this);
      return r;
    }

    friend constexpr bool operator<(const ::tools::safe_int<T>& x, const ::tools::safe_int<T>& y) {
      constexpr auto table = ::std::array<::std::array<::std::optional<bool>, 3>, 3>({{
        {BQ(), BT(), BF()},
        {BF(), BF(), BF()},
        {BF(), BF(), BF()}
      }});
      if (const auto r = table[f1(x)][f1(y)]; r) return *r;

      return x.m_value < y.m_value;
    }
    friend constexpr bool operator>(const ::tools::safe_int<T>& x, const ::tools::safe_int<T>& y) {
      constexpr auto table = ::std::array<::std::array<::std::optional<bool>, 3>, 3>({{
        {BQ(), BF(), BF()},
        {BT(), BF(), BF()},
        {BF(), BF(), BF()}
      }});
      if (const auto r = table[f1(x)][f1(y)]; r) return *r;

      return x.m_value > y.m_value;
    }
    friend constexpr bool operator<=(const ::tools::safe_int<T>& x, const ::tools::safe_int<T>& y) {
      return x < y || x == y;
    }
    friend constexpr bool operator>=(const ::tools::safe_int<T>& x, const ::tools::safe_int<T>& y) {
      return x > y || x == y;
    }

    friend ::std::istream& operator>>(::std::istream& is, ::tools::safe_int<T>& self) {
      self.m_type = ::tools::safe_int<T>::type::finite;
      return is >> self.m_value;
    }
    friend ::std::ostream& operator<<(::std::ostream& os, const ::tools::safe_int<T>& self) {
      switch (self.m_type) {
      case ::tools::safe_int<T>::type::finite:
        return os << self.m_value;
      case ::tools::safe_int<T>::type::pos_inf:
        return os << "inf";
      default: // nan
        return os << "nan";
      }
    }
  };
}


#line 7 "tests/safe_int.test.cpp"

void test_signed_int() {
  const tools::safe_int<int> POS_INF = tools::safe_int<int>::infinity();
  const tools::safe_int<int> NEG_INF = -tools::safe_int<int>::infinity();
  const int INT_MAX = std::numeric_limits<int>::max();
  const int INT_MIN = std::numeric_limits<int>::min();
  using s = tools::safe_int<int>;

  // basic arithmetic operations
  assert_that(s(1) + s(2) == s(3));
  assert_that(s(1) - s(2) == s(-1));
  assert_that(s(1) * s(2) == s(2));
  assert_that(s(1) / s(2) == s(0));
  assert_that(s(1) % s(2) == s(1));

  // operator+ should detect an overflow.
  assert_that(s(INT_MAX - 2) + s(1) == s(INT_MAX - 1));
  assert_that(s(INT_MAX - 2) + s(2) == s(INT_MAX));
  assert_that((s(INT_MAX - 2) + s(3)).is_nan());
  assert_that(s(1) + s(INT_MAX - 2) == s(INT_MAX - 1));
  assert_that(s(2) + s(INT_MAX - 2) == s(INT_MAX));
  assert_that((s(3) + s(INT_MAX - 2)).is_nan());
  assert_that(s(INT_MIN + 2) + s(-1) == s(INT_MIN + 1));
  assert_that(s(INT_MIN + 2) + s(-2) == s(INT_MIN));
  assert_that((s(INT_MIN + 2) + s(-3)).is_nan());
  assert_that(s(-1) + s(INT_MIN + 2) == s(INT_MIN + 1));
  assert_that(s(-2) + s(INT_MIN + 2) == s(INT_MIN));
  assert_that((s(-3) + s(INT_MIN + 2)).is_nan());

  // infinite + finite should be infinite.
  assert_that(POS_INF + s(-1) == POS_INF);
  assert_that(s(-1) + POS_INF == POS_INF);
  assert_that(POS_INF + POS_INF == POS_INF);
  assert_that(NEG_INF + s(1) == NEG_INF);
  assert_that(s(1) + NEG_INF == NEG_INF);
  assert_that(NEG_INF + NEG_INF == NEG_INF);

  // operator- should detect an overflow.
  assert_that(s(INT_MAX - 2) - s(-1) == s(INT_MAX - 1));
  assert_that(s(INT_MAX - 2) - s(-2) == s(INT_MAX));
  assert_that(s((INT_MAX - 2) - s(-3)).is_nan());
  assert_that(s(INT_MIN + 2) - s(1) == s(INT_MIN + 1));
  assert_that(s(INT_MIN + 2) - s(2) == s(INT_MIN));
  assert_that((s(INT_MIN + 2) - s(3)).is_nan());

  // infinite - finite should be finite.
  assert_that(POS_INF - s(1) == POS_INF);
  assert_that(POS_INF - NEG_INF == POS_INF);
  assert_that(NEG_INF - s(-1) == NEG_INF);
  assert_that(NEG_INF - POS_INF == NEG_INF);

  // operator* should detect an overflow.
  assert_that(NEG_INF * NEG_INF == POS_INF);
  assert_that(NEG_INF * s(INT_MIN) == POS_INF);
  assert_that((NEG_INF * s(0)).is_nan());
  assert_that(NEG_INF * s(INT_MAX) == NEG_INF);
  assert_that(NEG_INF * POS_INF == NEG_INF);
  assert_that(s(INT_MIN) * NEG_INF == POS_INF);
  assert_that((s(INT_MIN) * s(INT_MIN)).is_nan());
  assert_that(s(INT_MIN) * s(0) == s(0));
  assert_that((s(INT_MIN) * s(INT_MAX)).is_nan());
  assert_that(s(INT_MIN) * POS_INF == NEG_INF);
  assert_that((s(0) * NEG_INF).is_nan());
  assert_that(s(0) * s(INT_MIN) == s(0));
  assert_that(s(0) * s(0) == s(0));
  assert_that(s(0) * s(INT_MAX) == s(0));
  assert_that((s(0) * POS_INF).is_nan());
  assert_that(s(INT_MAX) * NEG_INF == NEG_INF);
  assert_that((s(INT_MAX) * s(INT_MIN)).is_nan());
  assert_that(s(INT_MAX) * s(0) == s(0));
  assert_that((s(INT_MAX) * s(INT_MAX)).is_nan());
  assert_that(s(INT_MAX) * POS_INF == POS_INF);
  assert_that(POS_INF * NEG_INF == NEG_INF);
  assert_that(POS_INF * s(INT_MIN) == NEG_INF);
  assert_that((POS_INF * s(0)).is_nan());
  assert_that(POS_INF * s(INT_MAX) == POS_INF);
  assert_that(POS_INF * POS_INF == POS_INF);
}

void test_unsigned_int() {
  const tools::safe_int<unsigned int> POS_INF = tools::safe_int<unsigned int>::infinity();
  const unsigned int UINT_MAX = std::numeric_limits<unsigned int>::max();
  using u = tools::safe_int<unsigned int>;

  // basic arithmetic operations
  assert_that(u(3) + u(2) == u(5));
  assert_that(u(3) - u(2) == u(1));
  assert_that(u(3) * u(2) == u(6));
  assert_that(u(3) / u(2) == u(1));
  assert_that(u(3) % u(2) == u(1));

  // operator+ should detect an overflow.
  assert_that(u(UINT_MAX - 2) + u(1) == u(UINT_MAX - 1));
  assert_that(u(UINT_MAX - 2) + u(2) == u(UINT_MAX));
  assert_that((u(UINT_MAX - 2) + u(3)).is_nan());
  assert_that(u(1) + u(UINT_MAX - 2) == u(UINT_MAX - 1));
  assert_that(u(2) + u(UINT_MAX - 2) == u(UINT_MAX));
  assert_that((u(3) + u(UINT_MAX - 2)).is_nan());

  // infinite + finite should be infinite.
  assert_that(POS_INF + u(1) == POS_INF);
  assert_that(u(1) + POS_INF == POS_INF);
  assert_that(POS_INF + POS_INF == POS_INF);

  // operator- should detect an overflow.
  assert_that(u(2) - u(1) == u(1));
  assert_that(u(2) - u(2) == u(0));
  assert_that((u(2) - u(3)).is_nan());

  // infinite - finite should be finite.
  assert_that(POS_INF - u(1) == POS_INF);

  // operator* should detect an overflow.
  assert_that(u(0) * u(0) == u(0));
  assert_that(u(0) * u(UINT_MAX) == u(0));
  assert_that((u(0) * POS_INF).is_nan());
  assert_that(u(UINT_MAX) * u(0) == u(0));
  assert_that((u(UINT_MAX) * u(UINT_MAX)).is_nan());
  assert_that(u(UINT_MAX) * POS_INF == POS_INF);
  assert_that((POS_INF * u(0)).is_nan());
  assert_that(POS_INF * u(UINT_MAX) == POS_INF);
  assert_that(POS_INF * POS_INF == POS_INF);
}

int main() {
  std::cin.tie(nullptr);
  std::ios_base::sync_with_stdio(false);

  test_signed_int();
  test_unsigned_int();

  return 0;
}
Back to top page