shogo-library

競技プログラミング用の C++ ライブラリ

View the Project on GitHub shogo314/shogo-library

:heavy_check_mark: test/unittest/utility/repr.test.cpp

Depends on

Code

#define PROBLEM "https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ITP1_1_A"

// repr.hpp の文字列化を検証する
// CHECK_REPR(値, 式) は repr(値) が「式」の文字列と一致し、さらにその式を評価すると値と等しくなることを確かめる
// (式はこのファイルで C++ としてコンパイルされるので、repr の出力が C++ の式として正しいことも確かめられる)
// 問題の入力は使わず、すべて一致したら Hello World を出力する

#include "../../../src/utility/repr.hpp"

#include <array>
#include <iostream>
#include <map>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>

#include "../../../src/utility/range.hpp"

bool ok = true;
void check(bool cond, const std::string& what) {
    if (!cond) {
        if (ok) std::cerr << "failed: " << what << std::endl;
        ok = false;
    }
}

#define CHECK_REPR(value, ...)                                                                                  \
    do {                                                                                                        \
        const auto& value_ = (value);                                                                           \
        check(repr(value_) == #__VA_ARGS__, "repr(" #value ") == " #__VA_ARGS__ " (got " + repr(value_) + ")"); \
        check(value_ == (__VA_ARGS__), "evaluating " #__VA_ARGS__);                                             \
    } while (0)

// repr などのメンバ関数を持たない型
struct Plain {};
// initializer_str だけを持つ型
struct OnlyInit {
    int x;
    std::string initializer_str() const { return "{" + std::to_string(x) + "}"; }
};

// char の期待値 (愚直に作る)
std::string naive_char_repr(char c) {
    unsigned char u = static_cast<unsigned char>(c);
    switch (c) {
        case '\0':
            return "'\\0'";
        case '\a':
            return "'\\a'";
        case '\b':
            return "'\\b'";
        case '\f':
            return "'\\f'";
        case '\r':
            return "'\\r'";
        case '\n':
            return "'\\n'";
        case '\v':
            return "'\\v'";
        case '\'':
            return "'\\''";
        case '\\':
            return "'\\\\'";
    }
    if (32 <= u && u <= 126) return std::string("'") + c + "'";
    return "'\\" + std::to_string(u / 64) + std::to_string(u / 8 % 8) + std::to_string(u % 8) + "'";
}

int main() {
    std::mt19937 rng(314);

    // 整数・浮動小数点数
    CHECK_REPR(5, 5);
    CHECK_REPR(-3, -3);
    CHECK_REPR(5L, 5l);
    CHECK_REPR(-7LL, -7ll);
    CHECK_REPR(5U, 5u);
    CHECK_REPR(5UL, 5ul);
    CHECK_REPR(18446744073709551615ULL, 18446744073709551615ull);
    CHECK_REPR(1.5f, 1.500000f);
    CHECK_REPR(-0.25, -0.250000);
    CHECK_REPR(2.5L, 2.500000l);
    check(type_str(5) == "int" && type_str(5L) == "long" && type_str(5LL) == "long long", "type_str (signed)");
    check(type_str(5U) == "unsigned int" && type_str(5UL) == "unsigned long" && type_str(5ULL) == "unsigned long long", "type_str (unsigned)");
    check(type_str(1.0f) == "float" && type_str(1.0) == "double" && type_str(1.0L) == "long double", "type_str (floating)");
    check(initializer_str(5LL) == "5ll" && initializer_str(1.5f) == "1.500000f", "initializer_str (number)");

    // 128 bit 整数: リテラルがないので 10^18 ごとに区切った式にする
    {
        const __uint128_t umax = ~static_cast<__uint128_t>(0);
        const __int128_t imax = static_cast<__int128_t>(umax >> 1), imin = -imax - 1;
        CHECK_REPR(static_cast<__int128_t>(0), __int128_t{0});
        CHECK_REPR(static_cast<__int128_t>(-42), __int128_t{-42});
        CHECK_REPR(static_cast<__int128_t>(1000000000000000000LL), __int128_t{1} * 1000000000000000000 + 0);
        CHECK_REPR(imax, (__int128_t{170} * 1000000000000000000 + 141183460469231731) * 1000000000000000000 + 687303715884105727);
        CHECK_REPR(imin, (__int128_t{-170} * 1000000000000000000 - 141183460469231731) * 1000000000000000000 - 687303715884105728);
        CHECK_REPR(umax, (__uint128_t{340} * 1000000000000000000 + 282366920938463463) * 1000000000000000000 + 374607431768211455);
        CHECK_REPR(static_cast<__uint128_t>(7), __uint128_t{7});
        CHECK_REPR((std::vector<__int128_t>{1, -2}), std::vector<__int128_t>{__int128_t{1}, __int128_t{-2}});
        check(type_str(imax) == "__int128_t" && type_str(umax) == "__uint128_t", "type_str (128 bit)");
        // 区切りの下の桁が 0 から始まっても 8 進数のリテラルにならない
        CHECK_REPR(static_cast<__int128_t>(1000000000000000007LL), __int128_t{1} * 1000000000000000000 + 7);
        // ランダム: 全範囲の値で、式を左から計算し直すと元の値になる
        for (int rep = 0; rep < 20000 && ok; rep++) {
            __uint128_t u = 0;
            for (int k = 0; k < 4; k++) u = u << 32 | rng();
            u >>= rng() % 128;
            const __int128_t x = static_cast<__int128_t>(u);
            const std::string s = repr(x);
            // "__int128_t{a}" のあとに " * 1000000000000000000 ± c" が続く (括弧は飛ばす)
            std::size_t i = s.find('{') + 1;
            auto number = [&](std::size_t& j) {
                bool neg = s[j] == '-';
                if (neg) j++;
                __int128_t v = 0;
                for (; j < s.size() && '0' <= s[j] && s[j] <= '9'; j++) v = v * 10 + (s[j] - '0');
                return neg ? -v : v;
            };
            __int128_t v = number(i);
            const std::string sep = " * 1000000000000000000 ";
            for (; (i = s.find(sep, i)) != std::string::npos;) {
                i += sep.size();
                const bool minus = s[i] == '-';
                i += 2;
                const __int128_t c = number(i);
                v = v * 1000000000000000000 + (minus ? -c : c);
            }
            check(v == x, "repr(__int128_t) (random)");
        }
    }

    // char
    CHECK_REPR('a', 'a');
    CHECK_REPR('\n', '\n');
    CHECK_REPR('\0', '\0');
    CHECK_REPR('\'', '\'');
    CHECK_REPR('\\', '\\');
    CHECK_REPR(static_cast<char>(1), '\001');
    CHECK_REPR(static_cast<char>(127), '\177');
    CHECK_REPR(static_cast<char>(-61), '\303');  // UTF-8 の文字のバイトなど、負の値の char
    check(type_str('a') == "char" && initializer_str('a') == "'a'", "type_str / initializer_str (char)");
    for (int i = -128; i <= 127; i++) {
        char c = static_cast<char>(i);
        check(repr(c) == naive_char_repr(c), "repr(char) for all values");
    }

    // 文字列
    {
        const char* s = "abc";
        check(repr(s) == "\"abc\"" && type_str(s) == "char*" && initializer_str(s) == "\"abc\"", "const char*");
    }
    CHECK_REPR(std::string("abc"), std::string{"abc"});
    CHECK_REPR(std::string(), std::string{""});
    check(type_str(std::string()) == "std::string" && initializer_str(std::string("x")) == "\"x\"", "type_str / initializer_str (string)");

    // コンテナ
    CHECK_REPR((std::vector<int>{1, 2, 3}), std::vector<int>{1, 2, 3});
    CHECK_REPR(std::vector<int>(), std::vector<int>{});
    CHECK_REPR((std::vector<std::vector<long long>>{{1, 2}, {}}), std::vector<std::vector<long long>>{{1ll, 2ll}, {}});
    CHECK_REPR((std::set<int>{3, 1, 2}), std::set<int>{1, 2, 3});
    CHECK_REPR((std::pair<int, std::string>{1, "a"}), std::pair<int, std::string>{1, "a"});
    CHECK_REPR((std::pair<char, double>{'x', 0.5}), std::pair<char, double>{'x', 0.500000});
    CHECK_REPR((std::map<int, char>{{2, 'b'}, {1, 'a'}}), std::map<int, char>{{1, 'a'}, {2, 'b'}});
    CHECK_REPR((std::map<std::string, std::vector<int>>{{"k", {1}}}), std::map<std::string, std::vector<int>>{{"k", {1}}});
    CHECK_REPR((std::array<int, 3>{1, 2, 3}), std::array<int, 3>{1, 2, 3});
    CHECK_REPR((std::vector<std::pair<int, unsigned>>{{1, 2u}}), std::vector<std::pair<int, unsigned int>>{{1, 2u}});
    check(type_str(std::vector<std::set<long long>>()) == "std::vector<std::set<long long>>", "type_str (nested)");
    check(initializer_str(std::vector<int>{1, 2}) == "{1, 2}", "initializer_str (vector)");

    // repr / type_str / initializer_str をメンバ関数に持つ型 (Range)
    CHECK_REPR(Range(0, 10, 2), Range(0, 10, 2));
    CHECK_REPR((std::vector<Range>{Range(0, 3, 1)}), std::vector<Range>{{0, 3, 1}});
    check(type_str(Range(5)) == "Range" && initializer_str(Range(1, 5, 2)) == "{1, 5, 2}", "Range");

    // メンバ関数を持たない型は "T" と "{}"
    check(type_str(Plain{}) == "T" && initializer_str(Plain{}) == "{}" && repr(Plain{}) == "T{}", "Plain");
    // initializer_str だけを持つ型
    check(repr(OnlyInit{7}) == "T{7}" && initializer_str(std::vector<OnlyInit>{{1}, {2}}) == "{{1}, {2}}", "OnlyInit");

    // ランダム: 整数の vector
    for (int rep = 0; rep < 1000 && ok; rep++) {
        int n = rng() % 5;
        std::vector<long long> v(n);
        std::string expect = "std::vector<long long>{";
        for (int i = 0; i < n; i++) {
            v[i] = static_cast<long long>(rng() % 2001) - 1000;
            expect += (i ? ", " : "") + std::to_string(v[i]) + "ll";
        }
        expect += "}";
        check(repr(v) == expect, "repr(vector<long long>) (random)");
    }

    // DEBUG(x) は std::cerr に "x = repr(x)" を出力する
    {
        std::ostringstream buf;
        std::streambuf* old = std::cerr.rdbuf(buf.rdbuf());
        std::vector<int> xs{1, 2};
        DEBUG(xs);
        std::cerr.rdbuf(old);
        check(buf.str() == "xs = std::vector<int>{1, 2}\n", "DEBUG");
    }

    if (ok) std::cout << "Hello World" << std::endl;
}
#line 1 "test/unittest/utility/repr.test.cpp"
#define PROBLEM "https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ITP1_1_A"

// repr.hpp の文字列化を検証する
// CHECK_REPR(値, 式) は repr(値) が「式」の文字列と一致し、さらにその式を評価すると値と等しくなることを確かめる
// (式はこのファイルで C++ としてコンパイルされるので、repr の出力が C++ の式として正しいことも確かめられる)
// 問題の入力は使わず、すべて一致したら Hello World を出力する

#line 2 "src/utility/repr.hpp"

/**
 * @file repr.hpp
 * @brief 出力をそのままコンストラクタに使える文字列化 (repr)
 */

#include <array>
#include <cstddef>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <type_traits>
#include <utility>
#include <vector>

#line 2 "src/utility/traits.hpp"

/**
 * @file traits.hpp
 * @brief 型特性
 */

#line 9 "src/utility/traits.hpp"
#include <functional>
#include <iterator>
#line 14 "src/utility/traits.hpp"

#if __cplusplus >= 202002L
#include <concepts>
#include <ranges>
#endif

namespace internal_traits {
using std::begin, std::end;

#if __cplusplus >= 202002L
template <class T>
    struct is_range_impl : std::bool_constant < requires(T& t) {
    begin(t);
    end(t);
}>{};
#else
template <class T, class = void>
struct is_range_impl : std::false_type {};
template <class T>
struct is_range_impl<T, std::void_t<decltype(begin(std::declval<T&>()), end(std::declval<T&>()))>> : std::true_type {};
#endif

// サイズ付き範囲から要素を直接構築するコンストラクタで共有する。
#if __cplusplus >= 202002L
template <class R, class S>
concept sized_constructible_range = std::ranges::input_range<R> && std::ranges::sized_range<R> &&
                                    std::is_constructible_v<S, std::ranges::range_reference_t<R>>;

template <class R>
auto range_size(R& r) {
    return std::ranges::size(r);
}
template <class R>
auto range_begin(R& r) {
    return std::ranges::begin(r);
}
template <class R>
using range_value_t = std::ranges::range_value_t<R>;
#else
using std::size;
template <class R>
auto range_size(R& r) -> decltype(size(r)) {
    return size(r);
}
template <class R>
auto range_begin(R& r) -> decltype(begin(r)) {
    return begin(r);
}
template <class R>
using range_value_t = typename std::iterator_traits<decltype(range_begin(std::declval<R&>()))>::value_type;

template <class R, class S, class = void>
struct is_sized_constructible_range : std::false_type {};
template <class R, class S>
struct is_sized_constructible_range<R, S, std::void_t<decltype(range_size(std::declval<R&>())), decltype(++std::declval<decltype(begin(std::declval<R&>()))&>()), decltype(begin(std::declval<R&>()) != end(std::declval<R&>())), decltype(*begin(std::declval<R&>()))>>
    : std::is_constructible<S, decltype(*begin(std::declval<R&>()))> {};
#endif
}  // namespace internal_traits

template <class T>
struct is_range : internal_traits::is_range_impl<T>::type {};
template <class T>
inline constexpr bool is_range_v = is_range<T>::value;

#if __cplusplus >= 202002L
#define HAS_METHOD(func_name)                                                                   \
    namespace internal_traits {                                                                 \
    template <class T>                                                                          \
    concept has_##func_name##_impl = requires { std::declval<T>().func_name(); };               \
    }                                                                                           \
    template <class T>                                                                          \
    struct has_##func_name : std::bool_constant<internal_traits::has_##func_name##_impl<T>> {}; \
    template <class T>                                                                          \
    inline constexpr bool has_##func_name##_v = has_##func_name<T>::value;

#define HAS_METHOD_ARG(func_name)                                                                  \
    namespace internal_traits {                                                                    \
    template <class T, class U>                                                                    \
    concept has_##func_name##_impl = requires { std::declval<T>().func_name(std::declval<U>()); }; \
    }                                                                                              \
    template <class T, class U>                                                                    \
    struct has_##func_name : std::bool_constant<internal_traits::has_##func_name##_impl<T, U>> {}; \
    template <class T, class U>                                                                    \
    inline constexpr bool has_##func_name##_v = has_##func_name<T, U>::value;
#else
#define HAS_METHOD(func_name)                                                              \
    namespace internal_traits {                                                            \
    template <class T, class = void>                                                       \
    struct has_##func_name##_impl : std::false_type {};                                    \
    template <class T>                                                                     \
    struct has_##func_name##_impl<T, std::void_t<decltype(std::declval<T>().func_name())>> \
        : std::true_type {};                                                               \
    }                                                                                      \
    template <class T>                                                                     \
    struct has_##func_name : internal_traits::has_##func_name##_impl<T>::type {};          \
    template <class T>                                                                     \
    inline constexpr bool has_##func_name##_v = has_##func_name<T>::value;

#define HAS_METHOD_ARG(func_name)                                                                              \
    namespace internal_traits {                                                                                \
    template <class T, typename U, class = void>                                                               \
    struct has_##func_name##_impl : std::false_type {};                                                        \
    template <class T, typename U>                                                                             \
    struct has_##func_name##_impl<T, U, std::void_t<decltype(std::declval<T>().func_name(std::declval<U>()))>> \
        : std::true_type {};                                                                                   \
    }                                                                                                          \
    template <class T, typename U>                                                                             \
    struct has_##func_name : internal_traits::has_##func_name##_impl<T, U>::type {};                           \
    template <class T, typename U>                                                                             \
    inline constexpr bool has_##func_name##_v = has_##func_name<T, U>::value;
#endif

HAS_METHOD(repr)
HAS_METHOD(type_str)
HAS_METHOD(initializer_str)
HAS_METHOD(max)
HAS_METHOD(min)
HAS_METHOD(reversed)
HAS_METHOD(sorted)
HAS_METHOD(sum)
HAS_METHOD(product)
HAS_METHOD(product_xor)
HAS_METHOD_ARG(count)
HAS_METHOD_ARG(find)
HAS_METHOD_ARG(lower_bound)
HAS_METHOD_ARG(upper_bound)

#define ENABLE_IF_T_IMPL(expr) std::enable_if_t<expr, std::nullptr_t> = nullptr
#define ENABLE_IF_T(...) ENABLE_IF_T_IMPL((__VA_ARGS__))

namespace internal_traits {
// 射影 (proj) の既定値
#if __cplusplus >= 202002L
using identity = std::identity;
#else
struct identity {
    using is_transparent = void;
    template <class T>
    constexpr T&& operator()(T&& x) const noexcept {
        return std::forward<T>(x);
    }
};
#endif
}  // namespace internal_traits

template <class C>
using mem_value_type = typename C::value_type;
template <class C>
using mem_difference_type = typename C::difference_type;

namespace internal_traits {
template <class T, class = void>
struct is_sorted_container_impl : std::false_type {};
template <class T>
struct is_sorted_container_impl<std::set<T>> : std::true_type {};
template <class T>
struct is_sorted_container_impl<std::multiset<T>> : std::true_type {};
}  // namespace internal_traits
template <class T>
struct is_sorted_container : internal_traits::is_sorted_container_impl<T>::type {};
template <class T>
inline constexpr bool is_sorted_container_v = is_sorted_container<T>::value;
#line 19 "src/utility/repr.hpp"

namespace internal_repr {

// declaration

inline std::string type_str(const char*);
inline std::string initializer_str(const char*);
inline std::string repr(const char*);

inline std::string type_str(const char&);
inline std::string initializer_str(const char&);
inline std::string repr(const char&);

inline std::string type_str(const int&);
inline std::string initializer_str(const int&);
inline std::string repr(const int&);

inline std::string type_str(const long&);
inline std::string initializer_str(const long&);
inline std::string repr(const long&);

inline std::string type_str(const long long&);
inline std::string initializer_str(const long long&);
inline std::string repr(const long long&);

inline std::string type_str(const unsigned int&);
inline std::string initializer_str(const unsigned int&);
inline std::string repr(const unsigned int&);

inline std::string type_str(const unsigned long&);
inline std::string initializer_str(const unsigned long&);
inline std::string repr(const unsigned long&);

inline std::string type_str(const unsigned long long&);
inline std::string initializer_str(const unsigned long long&);
inline std::string repr(const unsigned long long&);

inline std::string type_str(const __int128_t&);
inline std::string initializer_str(const __int128_t&);
inline std::string repr(const __int128_t&);

inline std::string type_str(const __uint128_t&);
inline std::string initializer_str(const __uint128_t&);
inline std::string repr(const __uint128_t&);

inline std::string type_str(const std::string&);
inline std::string initializer_str(const std::string&);
inline std::string repr(const std::string&);

inline std::string type_str(const float&);
inline std::string initializer_str(const float&);
inline std::string repr(const float&);

inline std::string type_str(const double&);
inline std::string initializer_str(const double&);
inline std::string repr(const double&);

inline std::string type_str(const long double&);
inline std::string initializer_str(const long double&);
inline std::string repr(const long double&);

template <typename T>
inline std::string type_str(const std::vector<T>&);
template <typename T>
inline std::string initializer_str(const std::vector<T>&);
template <typename T>
inline std::string repr(const std::vector<T>&);

template <typename T>
inline std::string type_str(const std::set<T>&);
template <typename T>
inline std::string initializer_str(const std::set<T>&);
template <typename T>
inline std::string repr(const std::set<T>&);

template <typename T1, typename T2>
inline std::string type_str(const std::pair<T1, T2>&);
template <typename T1, typename T2>
inline std::string initializer_str(const std::pair<T1, T2>&);
template <typename T1, typename T2>
inline std::string repr(const std::pair<T1, T2>&);

template <typename T1, typename T2>
inline std::string type_str(const std::map<T1, T2>&);
template <typename T1, typename T2>
inline std::string initializer_str(const std::map<T1, T2>&);
template <typename T1, typename T2>
inline std::string repr(const std::map<T1, T2>&);

template <typename T, std::size_t N>
inline std::string type_str(const std::array<T, N>&);
template <typename T, std::size_t N>
inline std::string initializer_str(const std::array<T, N>&);
template <typename T, std::size_t N>
inline std::string repr(const std::array<T, N>&);

// definition

#if __cplusplus >= 202002L
template <typename T>
    requires(!has_type_str_v<T>)
#else
template <typename T, ENABLE_IF_T(!has_type_str_v<T>)>
#endif
inline std::string type_str(const T&) {
    return "T";
}
#if __cplusplus >= 202002L
template <typename T>
    requires(has_type_str_v<T>)
#else
template <typename T, ENABLE_IF_T(has_type_str_v<T>)>
#endif
inline std::string type_str(const T& t) {
    return t.type_str();
}
#if __cplusplus >= 202002L
template <typename T>
    requires(!has_initializer_str_v<T>)
#else
template <typename T, ENABLE_IF_T(!has_initializer_str_v<T>)>
#endif
inline std::string initializer_str(const T&) {
    return "{}";
}
#if __cplusplus >= 202002L
template <typename T>
    requires(has_initializer_str_v<T>)
#else
template <typename T, ENABLE_IF_T(has_initializer_str_v<T>)>
#endif
inline std::string initializer_str(const T& t) {
    return t.initializer_str();
}
#if __cplusplus >= 202002L
template <typename T>
    requires(!has_repr_v<T>)
#else
template <typename T, ENABLE_IF_T(!has_repr_v<T>)>
#endif
inline std::string repr(const T& t) {
    return internal_repr::type_str(t) + internal_repr::initializer_str(t);
}
#if __cplusplus >= 202002L
template <typename T>
    requires(has_repr_v<T>)
#else
template <typename T, ENABLE_IF_T(has_repr_v<T>)>
#endif
inline std::string repr(const T& t) {
    return t.repr();
}

inline std::string type_str(const char*) {
    return "char*";
}
inline std::string initializer_str(const char* t) {
    return "\"" + std::string(t) + "\"";
}
inline std::string repr(const char* t) {
    return internal_repr::initializer_str(t);
}

inline std::string type_str(const char&) {
    return "char";
}
inline std::string initializer_str(const char& t) {
    // char が符号付きでも添字が負にならないように unsigned char で扱う (UTF-8 のバイトなど)
    const unsigned char u = static_cast<unsigned char>(t);
    std::vector<std::string> v(128, "");
    v['\0'] = "\\0";
    v['\a'] = "\\a";
    v['\b'] = "\\b";
    v['\f'] = "\\f";
    v['\r'] = "\\r";
    v['\n'] = "\\n";
    v['\v'] = "\\v";
    v['\''] = "\\'";
    v['\\'] = "\\\\";
    if (u < 128 && !v[u].empty())
        return "'" + v[u] + "'";
    if (32 <= u && u <= 126)
        return "'" + std::string{t} + "'";
    return "'\\" + std::to_string(u / 64) + std::to_string(u / 8 % 8) + std::to_string(u % 8) + "'";
}
inline std::string repr(const char& t) {
    return internal_repr::initializer_str(t);
}

inline std::string type_str(const int&) {
    return "int";
}
inline std::string initializer_str(const int& t) {
    return std::to_string(t);
}
inline std::string repr(const int& t) {
    return internal_repr::initializer_str(t);
}

inline std::string type_str(const long&) {
    return "long";
}
inline std::string initializer_str(const long& t) {
    return std::to_string(t) + "l";
}
inline std::string repr(const long& t) {
    return internal_repr::initializer_str(t);
}

inline std::string type_str(const long long&) {
    return "long long";
}
inline std::string initializer_str(const long long& t) {
    return std::to_string(t) + "ll";
}
inline std::string repr(const long long& t) {
    return internal_repr::initializer_str(t);
}

inline std::string type_str(const unsigned int&) {
    return "unsigned int";
}
inline std::string initializer_str(const unsigned int& t) {
    return std::to_string(t) + "u";
}
inline std::string repr(const unsigned int& t) {
    return internal_repr::initializer_str(t);
}

inline std::string type_str(const unsigned long&) {
    return "unsigned long";
}
inline std::string initializer_str(const unsigned long& t) {
    return std::to_string(t) + "ul";
}
inline std::string repr(const unsigned long& t) {
    return internal_repr::initializer_str(t);
}

inline std::string type_str(const unsigned long long&) {
    return "unsigned long long";
}
inline std::string initializer_str(const unsigned long long& t) {
    return std::to_string(t) + "ull";
}
inline std::string repr(const unsigned long long& t) {
    return internal_repr::initializer_str(t);
}

// 128 bit 整数のリテラルはないので、10^18 ごとに区切った式にする (各区切りに符号を付けるので、途中であふれない)
// 例: (__int128_t{-170} * 1000000000000000000 - 141183460469231731) * 1000000000000000000 - 687303715884105728
inline std::string int128_str(const std::string& type, unsigned __int128 u, bool neg) {
    constexpr unsigned long long B = 1000000000000000000ULL;
    std::vector<unsigned long long> c;
    do {
        c.push_back(static_cast<unsigned long long>(u % B));
        u /= B;
    } while (u > 0);
    const std::string sign = neg ? "-" : "";
    std::string ret = type + "{" + sign + std::to_string(c.back()) + "}";
    for (int i = static_cast<int>(c.size()) - 2; i >= 0; i--) {
        if (i + 2 < static_cast<int>(c.size())) ret = "(" + ret + ")";
        ret += " * 1000000000000000000 " + std::string(neg ? "-" : "+") + " " + std::to_string(c[i]);
    }
    return ret;
}

inline std::string type_str(const __int128_t&) {
    return "__int128_t";
}
inline std::string initializer_str(const __int128_t& t) {
    return internal_repr::int128_str("__int128_t", t < 0 ? -static_cast<unsigned __int128>(t) : static_cast<unsigned __int128>(t), t < 0);
}
inline std::string repr(const __int128_t& t) {
    return internal_repr::initializer_str(t);
}

inline std::string type_str(const __uint128_t&) {
    return "__uint128_t";
}
inline std::string initializer_str(const __uint128_t& t) {
    return internal_repr::int128_str("__uint128_t", t, false);
}
inline std::string repr(const __uint128_t& t) {
    return internal_repr::initializer_str(t);
}

inline std::string type_str(const std::string&) {
    return "std::string";
}
inline std::string initializer_str(const std::string& t) {
    return "\"" + t + "\"";
}
inline std::string repr(const std::string& t) {
    return internal_repr::type_str(t) + "{" + internal_repr::initializer_str(t) + "}";
}

inline std::string type_str(const float&) {
    return "float";
}
inline std::string initializer_str(const float& t) {
    return std::to_string(t) + "f";
}
inline std::string repr(const float& t) {
    return internal_repr::initializer_str(t);
}

inline std::string type_str(const double&) {
    return "double";
}
inline std::string initializer_str(const double& t) {
    return std::to_string(t);
}
inline std::string repr(const double& t) {
    return internal_repr::initializer_str(t);
}

inline std::string type_str(const long double&) {
    return "long double";
}
inline std::string initializer_str(const long double& t) {
    return std::to_string(t) + "l";
}
inline std::string repr(const long double& t) {
    return internal_repr::initializer_str(t);
}

template <typename T>
inline std::string type_str(const std::vector<T>&) {
    T t;
    return "std::vector<" + internal_repr::type_str(t) + ">";
}
template <typename T>
inline std::string initializer_str(const std::vector<T>& t) {
    std::string ret;
    ret += "{";
    for (auto itr = t.begin(); itr != t.end(); itr++) {
        if (itr != t.begin()) ret += ", ";
        ret += internal_repr::initializer_str(*itr);
    }
    ret += "}";
    return ret;
}
template <typename T>
inline std::string repr(const std::vector<T>& t) {
    return internal_repr::type_str(t) + internal_repr::initializer_str(t);
}

template <typename T>
inline std::string type_str(const std::set<T>&) {
    T t;
    return "std::set<" + internal_repr::type_str(t) + ">";
}
template <typename T>
inline std::string initializer_str(const std::set<T>& t) {
    std::string ret;
    ret += "{";
    for (auto itr = t.begin(); itr != t.end(); itr++) {
        if (itr != t.begin()) ret += ", ";
        ret += internal_repr::initializer_str(*itr);
    }
    ret += "}";
    return ret;
}
template <typename T>
inline std::string repr(const std::set<T>& t) {
    return internal_repr::type_str(t) + internal_repr::initializer_str(t);
}

template <typename T1, typename T2>
inline std::string type_str(const std::pair<T1, T2>&) {
    T1 t1;
    T2 t2;
    return "std::pair<" + internal_repr::type_str(t1) + ", " + internal_repr::type_str(t2) + ">";
}
template <typename T1, typename T2>
inline std::string initializer_str(const std::pair<T1, T2>& t) {
    return "{" + internal_repr::initializer_str(t.first) + ", " + internal_repr::initializer_str(t.second) + "}";
}
template <typename T1, typename T2>
inline std::string repr(const std::pair<T1, T2>& t) {
    return internal_repr::type_str(t) + internal_repr::initializer_str(t);
}

template <typename T1, typename T2>
inline std::string type_str(const std::map<T1, T2>&) {
    T1 t1;
    T2 t2;
    return "std::map<" + internal_repr::type_str(t1) + ", " + internal_repr::type_str(t2) + ">";
}
template <typename T1, typename T2>
inline std::string initializer_str(const std::map<T1, T2>& t) {
    std::string ret;
    ret += "{";
    for (auto itr = t.begin(); itr != t.end(); itr++) {
        if (itr != t.begin()) ret += ", ";
        ret += "{";
        ret += internal_repr::initializer_str(itr->first);
        ret += ", ";
        ret += internal_repr::initializer_str(itr->second);
        ret += "}";
    }
    ret += "}";
    return ret;
}
template <typename T1, typename T2>
inline std::string repr(const std::map<T1, T2>& t) {
    return internal_repr::type_str(t) + internal_repr::initializer_str(t);
}

template <typename T, std::size_t N>
inline std::string type_str(const std::array<T, N>&) {
    T t;
    return "std::array<" + internal_repr::type_str(t) + ", " + std::to_string(N) + ">";
}
template <typename T, std::size_t N>
inline std::string initializer_str(const std::array<T, N>& t) {
    std::string ret;
    ret += "{";
    for (auto itr = t.begin(); itr != t.end(); itr++) {
        if (itr != t.begin()) ret += ", ";
        ret += internal_repr::initializer_str(*itr);
    }
    ret += "}";
    return ret;
}
template <typename T, std::size_t N>
inline std::string repr(const std::array<T, N>& t) {
    return internal_repr::type_str(t) + internal_repr::initializer_str(t);
}

}  // namespace internal_repr

template <typename T>
inline std::string type_str(const T& t) {
    return internal_repr::type_str(t);
}
template <typename T>
inline std::string initializer_str(const T& t) {
    return internal_repr::initializer_str(t);
}
template <typename T>
inline std::string repr(const T& t) {
    return internal_repr::repr(t);
}

// clang-format の版によって 1 行のままか展開するかが変わるので整形しない
// clang-format off
#define DEBUG(x) \
    { std::cerr << #x << " = " << repr(x) << std::endl; }
// clang-format on
#line 9 "test/unittest/utility/repr.test.cpp"

#line 13 "test/unittest/utility/repr.test.cpp"
#include <random>
#line 15 "test/unittest/utility/repr.test.cpp"
#include <sstream>
#line 19 "test/unittest/utility/repr.test.cpp"

#line 2 "src/utility/range.hpp"

/**
 * @file range.hpp
 * @brief Python の range のような整数列と、その直積の Grid
 */

#include <cassert>
#line 15 "src/utility/range.hpp"

struct Range {
   public:
    using value_type = long long;
    using const_reference = value_type;
    using size_type = long long;
    using difference_type = std::ptrdiff_t;

    const value_type start;
    const value_type stop;
    const value_type step;

   private:
    const value_type norm_stop;

    constexpr static value_type normalize(value_type _start, value_type _stop, value_type _step) {
        assert(_step != 0);
        if (_step > 0) {
            if (_start >= _stop) {
                return _start;
            } else {
                return _start + (_stop - _start + _step - 1) / _step * _step;
            }
        } else {
            if (_start <= _stop) {
                return _start;
            } else {
                return _start - (_start - _stop - _step - 1) / _step * _step;
            }
        }
    }

    constexpr Range(value_type _start, value_type _stop, value_type _step, value_type _norm_stop)
        : start(_start), stop(_stop), step(_step), norm_stop(_norm_stop) {
    }

   public:
    /**
     * @brief start から stop の手前まで step ずつ進む整数列
     * @pre step != 0
     */
    constexpr Range(value_type _start, value_type _stop, value_type _step)
        : start(_start), stop(_stop), step(_step), norm_stop(normalize(_start, _stop, _step)) {
    }
    constexpr Range(value_type _start, value_type _stop)
        : start(_start), stop(_stop), step(1), norm_stop(normalize(_start, _stop, 1)) {}
    explicit constexpr Range(value_type _stop)
        : start(0), stop(_stop), step(1), norm_stop(normalize(0, _stop, 1)) {}
    constexpr Range()
        : start(0), stop(0), step(1), norm_stop(0) {
    }
    constexpr Range(const Range& o) = default;
    Range& operator=(const Range& o) = delete;

    struct const_iterator {
       public:
        using difference_type = Range::difference_type;
        using value_type = Range::value_type;
        using pointer = void;
        using reference = Range::const_reference;
        using iterator_category = std::random_access_iterator_tag;

       private:
        value_type first = 0, stop = 0, step = 1, value = 0;

        constexpr bool same_range(const const_iterator& o) const noexcept {
            return first == o.first && stop == o.stop && step == o.step;
        }

       public:
        constexpr const_iterator() noexcept = default;
        constexpr const_iterator(const Range& range, value_type value) noexcept
            : first(range.start), stop(range.stop), step(range.step), value(value) {}
        constexpr reference base() const noexcept {
            return value;
        }
        constexpr const_iterator& operator++() noexcept {
            value += step;
            return *this;
        }
        constexpr const_iterator operator++(int) noexcept {
            const_iterator ret(*this);
            value += step;
            return ret;
        }
        constexpr const_iterator& operator+=(difference_type _n) noexcept {
            value += _n * step;
            return *this;
        }
        constexpr friend const_iterator operator+(const const_iterator& i, difference_type _n) noexcept {
            const_iterator ret(i);
            ret += _n;
            return ret;
        }
        constexpr friend const_iterator operator+(difference_type _n, const const_iterator& i) noexcept {
            const_iterator ret(i);
            ret += _n;
            return ret;
        }
        constexpr const_iterator& operator--() noexcept {
            value -= step;
            return *this;
        }
        constexpr const_iterator operator--(int) noexcept {
            const_iterator ret(*this);
            value -= step;
            return ret;
        }
        constexpr const_iterator& operator-=(difference_type _n) noexcept {
            value -= _n * step;
            return *this;
        }
        constexpr friend const_iterator operator-(const const_iterator& i, difference_type _n) noexcept {
            const_iterator ret(i);
            ret -= _n;
            return ret;
        }
        constexpr reference operator[](difference_type i) const noexcept { return value + i * step; }
        constexpr reference operator*() const noexcept {
            return value;
        }
        constexpr friend bool operator!=(const const_iterator& a, const const_iterator& b) noexcept {
            return !a.same_range(b) || a.value != b.value;
        }
        constexpr friend bool operator==(const const_iterator& a, const const_iterator& b) noexcept {
            return !(a != b);
        }
        constexpr friend bool operator<(const const_iterator& a, const const_iterator& b) noexcept {
            assert(a.same_range(b));
            return a.step > 0 ? a.value < b.value : a.value > b.value;
        }
        constexpr friend bool operator>(const const_iterator& a, const const_iterator& b) noexcept { return b < a; }
        constexpr friend bool operator<=(const const_iterator& a, const const_iterator& b) noexcept { return !(b < a); }
        constexpr friend bool operator>=(const const_iterator& a, const const_iterator& b) noexcept { return !(a < b); }
        constexpr friend difference_type operator-(const const_iterator& a, const const_iterator& b) {
            assert(a.same_range(b));
            if (a.step > 0) {
                return (a.value - b.value) / a.step;
            } else {
                return (b.value - a.value) / (-a.step);
            }
        }
    };

    constexpr const_iterator begin() const noexcept {
        return {*this, start};
    }
    constexpr const_iterator end() const noexcept {
        return {*this, norm_stop};
    }
    /**
     * @brief x の個数 (0 か 1)
     */
    constexpr size_type count(value_type x) const noexcept {
        // step がコンパイル時に分からないときも、よく使う step = 1 では割り算をしない
        if (step == 1) return start <= x && x < stop;
        if (step > 0) {
            if (start <= x && x < stop) {
                if ((x - start) % step == 0) {
                    return 1;
                }
            }
        } else {
            if (stop < x && x <= start) {
                if ((start - x) % (-step) == 0) {
                    return 1;
                }
            }
        }
        return 0;
    }
    /**
     * @brief x が含まれるか
     */
    constexpr bool contains(value_type x) const noexcept {
        return count(x) != 0;
    }
    /**
     * @brief x のインデックス
     * x が含まれないとエラー
     */
    constexpr size_type index(value_type x) const {
        assert(contains(x));
        if (step == 1) return x - start;
        return (x - start) / step;
    }
    /**
     * @brief 要素が x のイテレータ
     * x が含まれないと end() を返す
     */
    constexpr const_iterator find(value_type x) const noexcept {
        if (contains(x)) {
            return {*this, x};
        } else {
            return {*this, norm_stop};
        }
    }
    /**
     * @brief 要素が x 以上の最小のイテレータ
     * step > 0 じゃないとエラー
     */
    constexpr const_iterator lower_bound(value_type x) const {
        assert(step > 0);
        if (norm_stop <= x) {
            return end();
        } else if (x < start) {
            return begin();
        } else {
            return {*this, start + (x - start + step - 1) / step * step};
        }
    }
    /**
     * @brief 要素が x より大きい最小のイテレータ
     * step > 0 じゃないとエラー
     */
    constexpr const_iterator upper_bound(value_type x) const {
        assert(step > 0);
        if (norm_stop <= x) {
            return end();
        } else if (x < start) {
            return begin();
        } else {
            return {*this, start + (x - start + step) / step * step};
        }
    }
    /**
     * @brief コンテナの要素数
     */
    constexpr size_type size() const noexcept {
        if (step > 0) {
            return (norm_stop - start) / step;
        } else {
            return (start - norm_stop) / (-step);
        }
    }
    constexpr value_type at(size_type i) const {
        assert(0 <= i && i < size());
        return start + i * step;
    }
    constexpr value_type operator[](size_type i) const {
        return at(i);
    }
    constexpr bool empty() const noexcept {
        return start == norm_stop;
    }
    constexpr value_type sum() const noexcept {
        return size() * (norm_stop - step + start) / 2;
    }
    /**
     * @brief 最小値
     * 空だとエラー
     */
    constexpr value_type min() const {
        assert(!empty());
        if (step > 0) {
            return start;
        } else {
            return norm_stop - step;
        }
    }
    /**
     * @brief 最大値
     * 空だとエラー
     */
    constexpr value_type max() const {
        assert(!empty());
        if (step > 0) {
            return norm_stop - step;
        } else {
            return start;
        }
    }
    /**
     * @brief 総 xor
     * 負を含むとエラー
     */
    constexpr value_type product_xor() const noexcept {
        if (empty()) return 0;
        assert(min() >= 0);
        Range sorted_range(sorted());
        value_type res = 0;
        std::make_unsigned_t<value_type> t = 1;
        while (t <= static_cast<std::make_unsigned_t<value_type>>(max())) {
            std::make_unsigned_t<value_type> tmp = 0, n = size(), m = t, a = sorted_range.step, b = sorted_range.start;
            while (true) {
                if (a >= m) {
                    tmp += n * (n - 1) / 2 * (a / m);
                    a %= m;
                }
                if (b >= m) {
                    tmp += n * (b / m);
                    b %= m;
                }
                std::make_unsigned_t<value_type> y_max = a * n + b;
                if (y_max < m) break;
                n = y_max / m;
                b = y_max % m;
                std::swap(m, a);
            }
            if (tmp % 2 == 1) {
                res += t;
            }
            t <<= 1;
        }
        return res;
    }
    /**
     * @brief 逆順にしたもの
     */
    constexpr Range reversed() const noexcept {
        return {norm_stop - step, start - step, -step, start - step};
    }
    /**
     * @brief step > 0 だとそのまま step < 0 だと逆順にしたもの
     */
    constexpr Range sorted() const noexcept {
        if (step > 0) {
            return {start, norm_stop, step, norm_stop};
        } else {
            return {norm_stop - step, start - step, -step, start - step};
        }
    }
    /**
     * @brief 要素が [l, r) である範囲を返す
     */
    constexpr Range clipped(value_type l, value_type r) const {
        if (l >= r) return {};
        r--;  // 以下では r を含む [l, r] として扱う
        if (step > 0) {
            value_type _start = [&] {
                if (norm_stop <= l) {
                    return norm_stop;
                } else if (l < start) {
                    return start;
                } else {
                    return start + (l - start + step - 1) / step * step;
                }
            }();
            value_type _stop = [&] {
                if (norm_stop <= r) {
                    return norm_stop;
                } else if (r < start) {
                    return start;
                } else {
                    return start + (r - start + step) / step * step;
                }
            }();
            return {_start, _stop, step, _stop};
        } else {
            value_type _start = [&] {
                if (start - step <= r) {
                    return start;
                } else if (r < norm_stop - step) {
                    return norm_stop;
                } else {
                    return norm_stop + (r - norm_stop) / step * step;
                }
            }();
            value_type _stop = [&] {
                if (start - step <= l) {
                    return start;
                } else if (l < norm_stop - step) {
                    return norm_stop;
                } else {
                    return norm_stop + (l - norm_stop - 1) / step * step;
                }
            }();
            return {_start, _stop, step, _stop};
        }
    }
    /**
     * @brief 要素を Range で指定したコンテナ
     */
    constexpr Range slice(const Range& r) const {
        if (r.empty()) return {};
        if (r.max() < 0) return {};
        if (r.min() >= size()) return {};
        Range trimmed_range(r.clipped(0, size()));
        if (trimmed_range.empty()) return {};
        value_type _step = step * trimmed_range.step;
        value_type _start = at(trimmed_range.start);
        value_type _stop = _start + _step * trimmed_range.size();
        return {_start, _stop, _step, _stop};
    }

    static std::string type_str() {
        return "Range";
    }
    std::string initializer_str() const {
        return "{" + std::to_string(start) + ", " + std::to_string(stop) + ", " + std::to_string(step) + "}";
    }
    std::string repr() const {
        return "Range(" + std::to_string(start) + ", " + std::to_string(stop) + ", " + std::to_string(step) + ")";
    }

    std::vector<value_type> to_vector() const {
        std::vector<value_type> ret;
        ret.reserve(size());
        if (step > 0) {
            for (value_type x = start; x < stop; x += step) {
                ret.push_back(x);
            }
        } else {
            for (value_type x = start; x > stop; x += step) {
                ret.push_back(x);
            }
        }
        return ret;
    }
    constexpr friend bool operator==(const Range& a, const Range& b) noexcept {
        if (a.empty() && b.empty()) return true;
        if (a.size() != b.size()) return false;
        if (a.start != b.start) return false;
        if (a.size() == 1) return true;
        if (a.step != b.step) return false;
        return true;
    }
    constexpr friend bool operator!=(const Range& a, const Range& b) noexcept {
        return !(a == b);
    }
};

/**
 * @brief 2 つの Range の直積 rows × cols
 * (i, j) を i の昇順、同じ i では j の昇順 (行優先) に並べたものとして扱う
 */
struct Grid {
   public:
    using value_type = std::pair<long long, long long>;
    using size_type = long long;
    using difference_type = std::ptrdiff_t;

    const Range rows;
    const Range cols;

   private:
    const long long width;  // cols.size() (index や at で毎回割り算しないように持っておく)

   public:
    /**
     * @brief [0, h) × [0, w)
     */
    constexpr Grid(long long h, long long w) : rows(h), cols(w), width(cols.size()) {}
    constexpr Grid(const Range& _rows, const Range& _cols) : rows(_rows), cols(_cols), width(cols.size()) {}
    constexpr Grid(const Grid& o) = default;
    Grid& operator=(const Grid& o) = delete;

    struct const_iterator {
       public:
        using difference_type = Grid::difference_type;
        using value_type = Grid::value_type;
        using pointer = void;
        using reference = value_type;
        using iterator_category = std::forward_iterator_tag;

       private:
        const Grid* rect = nullptr;
        long long w = 0;   // cols.size()
        long long ic = 0;  // rows の何番目か
        long long jc = 0;  // cols の何番目か

       public:
        constexpr const_iterator() noexcept = default;
        constexpr const_iterator(const Grid* _rect, long long _w, long long _ic, long long _jc) noexcept
            : rect(_rect), w(_w), ic(_ic), jc(_jc) {}
        constexpr value_type operator*() const noexcept {
            return {rect->rows.start + ic * rect->rows.step, rect->cols.start + jc * rect->cols.step};
        }
        constexpr const_iterator& operator++() noexcept {
            if (++jc == w) {
                jc = 0;
                ++ic;
            }
            return *this;
        }
        constexpr const_iterator operator++(int) noexcept {
            const_iterator ret(*this);
            ++*this;
            return ret;
        }
        constexpr friend bool operator==(const const_iterator& a, const const_iterator& b) noexcept {
            return a.ic == b.ic && a.jc == b.jc;
        }
        constexpr friend bool operator!=(const const_iterator& a, const const_iterator& b) noexcept {
            return !(a == b);
        }
    };

    constexpr const_iterator begin() const noexcept {
        return {this, width, width == 0 ? rows.size() : 0, 0};
    }
    constexpr const_iterator end() const noexcept {
        return {this, width, rows.size(), 0};
    }
    /**
     * @brief 要素数 rows.size() * cols.size()
     */
    constexpr size_type size() const noexcept {
        return rows.size() * width;
    }
    constexpr bool empty() const noexcept {
        return rows.empty() || cols.empty();
    }
    /**
     * @brief (i, j) が含まれるか
     */
    constexpr bool contains(long long i, long long j) const noexcept {
        return rows.contains(i) && cols.contains(j);
    }
    constexpr bool contains(const value_type& p) const noexcept {
        return contains(p.first, p.second);
    }
    /**
     * @brief (i, j) の行優先での位置 (グリッドの頂点番号に使う)
     * (i, j) が含まれないとエラー
     */
    constexpr size_type index(long long i, long long j) const {
        assert(contains(i, j));
        return rows.index(i) * width + cols.index(j);
    }
    constexpr size_type index(const value_type& p) const {
        return index(p.first, p.second);
    }
    /**
     * @brief 行優先で k 番目の要素 (index の逆)
     */
    constexpr value_type at(size_type k) const {
        assert(0 <= k && k < size());
        return {rows[k / width], cols[k % width]};
    }
    constexpr value_type operator[](size_type k) const {
        return at(k);
    }

   private:
    // 近傍の方向 (rows・cols の中での位置の差) を Grid の順に並べたもの。先頭 4 つは上下左右だけを並べた別の表
    // 4 近傍と 8 近傍の境目が分かるように 2 行に分けているので整形しない
    // clang-format off
    static constexpr int neighbor_dirs[12][2] = {{-1, 0}, {0, -1}, {0, 1}, {1, 0},
                                                 {-1, -1}, {-1, 0}, {-1, 1}, {0, -1}, {0, 1}, {1, -1}, {1, 0}, {1, 1}};
    // clang-format on

   public:
    /**
     * @brief neighbors4 / neighbors8 の戻り値 (範囲 for で使う)
     * 作るときに Grid に含まれる方向をビットで持っておき、立っているビットを順に返す
     */
    struct Neighbors {
        long long i, j, row_step, col_step;
        int first;      // neighbor_dirs[first + k] がビット k の方向
        unsigned mask;  // Grid に含まれる方向

        struct iterator {
            using difference_type = std::ptrdiff_t;
            using value_type = Grid::value_type;
            using pointer = void;
            using reference = value_type;
            using iterator_category = std::forward_iterator_tag;

            const Neighbors* nb = nullptr;
            unsigned rest = 0;  // まだ返していない方向

            constexpr value_type operator*() const noexcept {
                const int k = nb->first + __builtin_ctz(rest);
                return {nb->i + neighbor_dirs[k][0] * nb->row_step, nb->j + neighbor_dirs[k][1] * nb->col_step};
            }
            constexpr iterator& operator++() noexcept {
                rest &= rest - 1;
                return *this;
            }
            constexpr iterator operator++(int) noexcept {
                iterator ret(*this);
                ++*this;
                return ret;
            }
            constexpr friend bool operator==(const iterator& a, const iterator& b) noexcept { return a.rest == b.rest; }
            constexpr friend bool operator!=(const iterator& a, const iterator& b) noexcept { return a.rest != b.rest; }
        };
        constexpr iterator begin() const noexcept { return {this, mask}; }
        constexpr iterator end() const noexcept { return {this, 0}; }
        constexpr long long size() const noexcept { return __builtin_popcount(mask); }
        constexpr bool empty() const noexcept { return mask == 0; }
    };

   private:
    constexpr Neighbors neighbors(long long i, long long j, int first, int last) const {
        assert(contains(i, j));
        unsigned mask = 0;
        for (int k = first; k < last; k++) {
            if (contains(i + neighbor_dirs[k][0] * rows.step, j + neighbor_dirs[k][1] * cols.step)) mask |= 1u << (k - first);
        }
        return {i, j, rows.step, cols.step, first, mask};
    }

   public:
    /**
     * @brief (i, j) の上下左右のうち Grid に含まれるものを、Grid の順に返す
     * 隣は rows・cols の中で隣り合う要素 (i ± rows.step など)。(i, j) が含まれないとエラー
     */
    constexpr Neighbors neighbors4(long long i, long long j) const {
        return neighbors(i, j, 0, 4);
    }
    constexpr Neighbors neighbors4(const value_type& p) const {
        return neighbors4(p.first, p.second);
    }
    /**
     * @brief (i, j) を囲む 8 つのうち Grid に含まれるものを、Grid の順に返す
     */
    constexpr Neighbors neighbors8(long long i, long long j) const {
        return neighbors(i, j, 4, 12);
    }
    constexpr Neighbors neighbors8(const value_type& p) const {
        return neighbors8(p.first, p.second);
    }

    static std::string type_str() {
        return "Grid";
    }
    std::string initializer_str() const {
        return "{" + rows.initializer_str() + ", " + cols.initializer_str() + "}";
    }
    std::string repr() const {
        return "Grid(" + rows.repr() + ", " + cols.repr() + ")";
    }

    constexpr friend bool operator==(const Grid& a, const Grid& b) noexcept {
        if (a.empty() && b.empty()) return true;
        return a.rows == b.rows && a.cols == b.cols;
    }
    constexpr friend bool operator!=(const Grid& a, const Grid& b) noexcept {
        return !(a == b);
    }
};
#line 21 "test/unittest/utility/repr.test.cpp"

bool ok = true;
void check(bool cond, const std::string& what) {
    if (!cond) {
        if (ok) std::cerr << "failed: " << what << std::endl;
        ok = false;
    }
}

#define CHECK_REPR(value, ...)                                                                                  \
    do {                                                                                                        \
        const auto& value_ = (value);                                                                           \
        check(repr(value_) == #__VA_ARGS__, "repr(" #value ") == " #__VA_ARGS__ " (got " + repr(value_) + ")"); \
        check(value_ == (__VA_ARGS__), "evaluating " #__VA_ARGS__);                                             \
    } while (0)

// repr などのメンバ関数を持たない型
struct Plain {};
// initializer_str だけを持つ型
struct OnlyInit {
    int x;
    std::string initializer_str() const { return "{" + std::to_string(x) + "}"; }
};

// char の期待値 (愚直に作る)
std::string naive_char_repr(char c) {
    unsigned char u = static_cast<unsigned char>(c);
    switch (c) {
        case '\0':
            return "'\\0'";
        case '\a':
            return "'\\a'";
        case '\b':
            return "'\\b'";
        case '\f':
            return "'\\f'";
        case '\r':
            return "'\\r'";
        case '\n':
            return "'\\n'";
        case '\v':
            return "'\\v'";
        case '\'':
            return "'\\''";
        case '\\':
            return "'\\\\'";
    }
    if (32 <= u && u <= 126) return std::string("'") + c + "'";
    return "'\\" + std::to_string(u / 64) + std::to_string(u / 8 % 8) + std::to_string(u % 8) + "'";
}

int main() {
    std::mt19937 rng(314);

    // 整数・浮動小数点数
    CHECK_REPR(5, 5);
    CHECK_REPR(-3, -3);
    CHECK_REPR(5L, 5l);
    CHECK_REPR(-7LL, -7ll);
    CHECK_REPR(5U, 5u);
    CHECK_REPR(5UL, 5ul);
    CHECK_REPR(18446744073709551615ULL, 18446744073709551615ull);
    CHECK_REPR(1.5f, 1.500000f);
    CHECK_REPR(-0.25, -0.250000);
    CHECK_REPR(2.5L, 2.500000l);
    check(type_str(5) == "int" && type_str(5L) == "long" && type_str(5LL) == "long long", "type_str (signed)");
    check(type_str(5U) == "unsigned int" && type_str(5UL) == "unsigned long" && type_str(5ULL) == "unsigned long long", "type_str (unsigned)");
    check(type_str(1.0f) == "float" && type_str(1.0) == "double" && type_str(1.0L) == "long double", "type_str (floating)");
    check(initializer_str(5LL) == "5ll" && initializer_str(1.5f) == "1.500000f", "initializer_str (number)");

    // 128 bit 整数: リテラルがないので 10^18 ごとに区切った式にする
    {
        const __uint128_t umax = ~static_cast<__uint128_t>(0);
        const __int128_t imax = static_cast<__int128_t>(umax >> 1), imin = -imax - 1;
        CHECK_REPR(static_cast<__int128_t>(0), __int128_t{0});
        CHECK_REPR(static_cast<__int128_t>(-42), __int128_t{-42});
        CHECK_REPR(static_cast<__int128_t>(1000000000000000000LL), __int128_t{1} * 1000000000000000000 + 0);
        CHECK_REPR(imax, (__int128_t{170} * 1000000000000000000 + 141183460469231731) * 1000000000000000000 + 687303715884105727);
        CHECK_REPR(imin, (__int128_t{-170} * 1000000000000000000 - 141183460469231731) * 1000000000000000000 - 687303715884105728);
        CHECK_REPR(umax, (__uint128_t{340} * 1000000000000000000 + 282366920938463463) * 1000000000000000000 + 374607431768211455);
        CHECK_REPR(static_cast<__uint128_t>(7), __uint128_t{7});
        CHECK_REPR((std::vector<__int128_t>{1, -2}), std::vector<__int128_t>{__int128_t{1}, __int128_t{-2}});
        check(type_str(imax) == "__int128_t" && type_str(umax) == "__uint128_t", "type_str (128 bit)");
        // 区切りの下の桁が 0 から始まっても 8 進数のリテラルにならない
        CHECK_REPR(static_cast<__int128_t>(1000000000000000007LL), __int128_t{1} * 1000000000000000000 + 7);
        // ランダム: 全範囲の値で、式を左から計算し直すと元の値になる
        for (int rep = 0; rep < 20000 && ok; rep++) {
            __uint128_t u = 0;
            for (int k = 0; k < 4; k++) u = u << 32 | rng();
            u >>= rng() % 128;
            const __int128_t x = static_cast<__int128_t>(u);
            const std::string s = repr(x);
            // "__int128_t{a}" のあとに " * 1000000000000000000 ± c" が続く (括弧は飛ばす)
            std::size_t i = s.find('{') + 1;
            auto number = [&](std::size_t& j) {
                bool neg = s[j] == '-';
                if (neg) j++;
                __int128_t v = 0;
                for (; j < s.size() && '0' <= s[j] && s[j] <= '9'; j++) v = v * 10 + (s[j] - '0');
                return neg ? -v : v;
            };
            __int128_t v = number(i);
            const std::string sep = " * 1000000000000000000 ";
            for (; (i = s.find(sep, i)) != std::string::npos;) {
                i += sep.size();
                const bool minus = s[i] == '-';
                i += 2;
                const __int128_t c = number(i);
                v = v * 1000000000000000000 + (minus ? -c : c);
            }
            check(v == x, "repr(__int128_t) (random)");
        }
    }

    // char
    CHECK_REPR('a', 'a');
    CHECK_REPR('\n', '\n');
    CHECK_REPR('\0', '\0');
    CHECK_REPR('\'', '\'');
    CHECK_REPR('\\', '\\');
    CHECK_REPR(static_cast<char>(1), '\001');
    CHECK_REPR(static_cast<char>(127), '\177');
    CHECK_REPR(static_cast<char>(-61), '\303');  // UTF-8 の文字のバイトなど、負の値の char
    check(type_str('a') == "char" && initializer_str('a') == "'a'", "type_str / initializer_str (char)");
    for (int i = -128; i <= 127; i++) {
        char c = static_cast<char>(i);
        check(repr(c) == naive_char_repr(c), "repr(char) for all values");
    }

    // 文字列
    {
        const char* s = "abc";
        check(repr(s) == "\"abc\"" && type_str(s) == "char*" && initializer_str(s) == "\"abc\"", "const char*");
    }
    CHECK_REPR(std::string("abc"), std::string{"abc"});
    CHECK_REPR(std::string(), std::string{""});
    check(type_str(std::string()) == "std::string" && initializer_str(std::string("x")) == "\"x\"", "type_str / initializer_str (string)");

    // コンテナ
    CHECK_REPR((std::vector<int>{1, 2, 3}), std::vector<int>{1, 2, 3});
    CHECK_REPR(std::vector<int>(), std::vector<int>{});
    CHECK_REPR((std::vector<std::vector<long long>>{{1, 2}, {}}), std::vector<std::vector<long long>>{{1ll, 2ll}, {}});
    CHECK_REPR((std::set<int>{3, 1, 2}), std::set<int>{1, 2, 3});
    CHECK_REPR((std::pair<int, std::string>{1, "a"}), std::pair<int, std::string>{1, "a"});
    CHECK_REPR((std::pair<char, double>{'x', 0.5}), std::pair<char, double>{'x', 0.500000});
    CHECK_REPR((std::map<int, char>{{2, 'b'}, {1, 'a'}}), std::map<int, char>{{1, 'a'}, {2, 'b'}});
    CHECK_REPR((std::map<std::string, std::vector<int>>{{"k", {1}}}), std::map<std::string, std::vector<int>>{{"k", {1}}});
    CHECK_REPR((std::array<int, 3>{1, 2, 3}), std::array<int, 3>{1, 2, 3});
    CHECK_REPR((std::vector<std::pair<int, unsigned>>{{1, 2u}}), std::vector<std::pair<int, unsigned int>>{{1, 2u}});
    check(type_str(std::vector<std::set<long long>>()) == "std::vector<std::set<long long>>", "type_str (nested)");
    check(initializer_str(std::vector<int>{1, 2}) == "{1, 2}", "initializer_str (vector)");

    // repr / type_str / initializer_str をメンバ関数に持つ型 (Range)
    CHECK_REPR(Range(0, 10, 2), Range(0, 10, 2));
    CHECK_REPR((std::vector<Range>{Range(0, 3, 1)}), std::vector<Range>{{0, 3, 1}});
    check(type_str(Range(5)) == "Range" && initializer_str(Range(1, 5, 2)) == "{1, 5, 2}", "Range");

    // メンバ関数を持たない型は "T" と "{}"
    check(type_str(Plain{}) == "T" && initializer_str(Plain{}) == "{}" && repr(Plain{}) == "T{}", "Plain");
    // initializer_str だけを持つ型
    check(repr(OnlyInit{7}) == "T{7}" && initializer_str(std::vector<OnlyInit>{{1}, {2}}) == "{{1}, {2}}", "OnlyInit");

    // ランダム: 整数の vector
    for (int rep = 0; rep < 1000 && ok; rep++) {
        int n = rng() % 5;
        std::vector<long long> v(n);
        std::string expect = "std::vector<long long>{";
        for (int i = 0; i < n; i++) {
            v[i] = static_cast<long long>(rng() % 2001) - 1000;
            expect += (i ? ", " : "") + std::to_string(v[i]) + "ll";
        }
        expect += "}";
        check(repr(v) == expect, "repr(vector<long long>) (random)");
    }

    // DEBUG(x) は std::cerr に "x = repr(x)" を出力する
    {
        std::ostringstream buf;
        std::streambuf* old = std::cerr.rdbuf(buf.rdbuf());
        std::vector<int> xs{1, 2};
        DEBUG(xs);
        std::cerr.rdbuf(old);
        check(buf.str() == "xs = std::vector<int>{1, 2}\n", "DEBUG");
    }

    if (ok) std::cout << "Hello World" << std::endl;
}
Back to top page