shogo-library

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

View the Project on GitHub shogo314/shogo-library

:heavy_check_mark: test/unittest/base/io.test.cpp

Depends on

Code

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

// io.hpp の入出力を std::istringstream / std::ostringstream で検証する
// print / err は std::cout / std::cerr の出力先を一時的に差し替えて検証する
// 問題の入力は使わず、すべて一致したら Hello World を出力する

#include "../../../src/base/io.hpp"

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

// std::cout (または std::cerr) に書かれた内容を文字列で受け取る
template <class F>
std::string capture(std::ostream& stream, F f) {
    std::ostringstream buf;
    std::streambuf* old = stream.rdbuf(buf.rdbuf());
    f();
    stream.rdbuf(old);
    return buf.str();
}

template <class T>
std::string to_str(const T& x) {
    std::ostringstream os;
    os << x;
    return os.str();
}

int main() {
    std::mt19937 rng(314);
    bool ok = true;
    auto check = [&](bool cond, const char* what) {
        if (!cond) {
            if (ok) std::cerr << "failed: " << what << std::endl;
            ok = false;
        }
    };

    // 出力: 空白区切り
    check(to_str(std::make_pair(1, std::string("ab"))) == "1 ab", "<< pair");
    check(to_str(std::make_tuple(1, 'x', std::string("yz"), 2.5)) == "1 x yz 2.5", "<< tuple");
    check(to_str(std::make_tuple(7)) == "7", "<< tuple (1 要素)");
    check(to_str(std::array<int, 3>{4, 5, 6}) == "4 5 6", "<< array");
    check(to_str(std::array<int, 0>{}) == "", "<< array (空)");
    check(to_str(std::vector<int>{3, 1, 2}) == "3 1 2", "<< vector");
    check(to_str(std::vector<int>{}) == "", "<< vector (空)");
    check(to_str(std::set<int>{3, 1, 2}) == "1 2 3", "<< set");
    check(to_str(std::set<int>{}) == "", "<< set (空)");
    // ネストした型
    check(to_str(std::vector<std::pair<int, int>>{{1, 2}, {3, 4}}) == "1 2 3 4", "<< vector<pair>");
    check(to_str(std::make_pair(std::vector<int>{1, 2}, std::array<int, 2>{3, 4})) == "1 2 3 4", "<< pair<vector, array>");

    // 入力
    {
        std::istringstream is("1 ab  x 2 yz 3.5\n 4 5 6 7 8 9 10");
        std::pair<int, std::string> p;
        std::tuple<char, int, std::string, double> t;
        std::array<int, 3> a;
        std::vector<int> v(4);
        is >> p >> t >> a >> v;
        check(p == std::make_pair(1, std::string("ab")), ">> pair");
        check(t == std::make_tuple('x', 2, std::string("yz"), 3.5), ">> tuple");
        check(a == std::array<int, 3>{4, 5, 6}, ">> array");
        check(v == std::vector<int>{7, 8, 9, 10}, ">> vector");
        check(static_cast<bool>(is), "stream state");
    }
    {
        // ネストした型
        std::istringstream is("1 2 3 4 5 6");
        std::pair<std::vector<int>, std::array<int, 2>> p;
        p.first.resize(2);
        std::vector<std::pair<int, int>> vp(1);
        is >> p >> vp;
        check(p.first == std::vector<int>{1, 2} && p.second == std::array<int, 2>{3, 4}, ">> pair<vector, array>");
        check(vp[0] == std::make_pair(5, 6), ">> vector<pair>");
    }

    // ランダム: 書いて読み戻すと一致し、出力は愚直に空白でつないだものと一致する
    for (int rep = 0; rep < 2000 && ok; rep++) {
        int n = rng() % 6;
        std::vector<int> v(n);
        for (int& x : v) x = static_cast<int>(rng() % 2001) - 1000;
        std::string expect;
        for (int i = 0; i < n; i++) expect += (i ? " " : "") + std::to_string(v[i]);
        check(to_str(v) == expect, "<< vector (random)");
        std::set<int> s(v.begin(), v.end());
        std::string expect_s;
        for (auto it = s.begin(); it != s.end(); ++it) expect_s += (it == s.begin() ? "" : " ") + std::to_string(*it);
        check(to_str(s) == expect_s, "<< set (random)");
        std::istringstream is(expect);
        std::vector<int> w(n);
        is >> w;
        check(w == v, ">> vector (random)");
        std::array<int, 4> a;
        for (int& x : a) x = static_cast<int>(rng() % 2001) - 1000;
        std::istringstream is2(to_str(a));
        std::array<int, 4> b;
        is2 >> b;
        check(a == b, "array round trip");
        auto t = std::make_tuple(static_cast<int>(rng() % 100), static_cast<long long>(rng()), std::string(1 + rng() % 3, 'a' + rng() % 26));
        std::istringstream is3(to_str(t));
        decltype(t) u;
        is3 >> u;
        check(t == u, "tuple round trip");
    }

    // 128 bit 整数
    {
        const __uint128_t umax = ~static_cast<__uint128_t>(0);
        const __int128_t imax = static_cast<__int128_t>(umax >> 1), imin = -imax - 1;
        check(to_str(static_cast<__int128_t>(0)) == "0", "<< int128 (0)");
        check(to_str(static_cast<__int128_t>(-42)) == "-42", "<< int128 (負)");
        check(to_str(imax) == "170141183460469231731687303715884105727", "<< int128 (最大値)");
        check(to_str(imin) == "-170141183460469231731687303715884105728", "<< int128 (最小値)");
        check(to_str(umax) == "340282366920938463463374607431768211455", "<< uint128 (最大値)");
        check(to_str(std::vector<__int128_t>{1, -2}) == "1 -2", "<< vector<int128>");
        std::ostringstream w;
        w << std::setw(5) << static_cast<__int128_t>(-7);
        check(w.str() == "   -7", "<< int128 (setw)");

        std::istringstream is("170141183460469231731687303715884105727 -170141183460469231731687303715884105728 +5 340282366920938463463374607431768211455 3 -4");
        __int128_t a, b, c;
        __uint128_t d;
        std::pair<__int128_t, __int128_t> p;
        is >> a >> b >> c >> d >> p;
        check(a == imax && b == imin && c == 5 && d == umax && p == std::make_pair<__int128_t, __int128_t>(3, -4), ">> int128");
        check(static_cast<bool>(is), ">> int128 (stream state)");
        // 範囲外や数でないものは failbit
        for (std::string bad : {"170141183460469231731687303715884105728", "-170141183460469231731687303715884105729", "12a", "-", "x"}) {
            std::istringstream bs(bad);
            __int128_t x;
            bs >> x;
            check(bs.fail(), ">> int128 (不正)");
        }
        for (std::string bad : {"340282366920938463463374607431768211456", "-1"}) {
            std::istringstream bs(bad);
            __uint128_t x;
            bs >> x;
            check(bs.fail(), ">> uint128 (不正)");
        }
        std::istringstream zs("-0");
        __uint128_t z = 1;
        zs >> z;
        check(!zs.fail() && z == 0, ">> uint128 (-0)");
        // ランダム: long long の積で作った値を書いて読み戻す
        for (int rep = 0; rep < 2000; rep++) {
            __int128_t x = static_cast<__int128_t>(static_cast<long long>(rng())) * static_cast<long long>(rng() * 1000003ULL);
            if (rng() % 2) x = -x;
            std::istringstream rs(to_str(x));
            __int128_t y = 0;
            rs >> y;
            check(x == y, "int128 round trip");
            // long long に収まるなら long long と同じ表記
            if (-1000000000000000000 <= x && x <= 1000000000000000000) check(to_str(x) == std::to_string(static_cast<long long>(x)), "<< int128 (long long と比べる)");
        }
        // 先頭の 0 は桁数に数えない
        std::istringstream lz("000000000000000000000000000000000000000000000123 -0000 +00340282366920938463463374607431768211455");
        __int128_t l1, l2;
        __uint128_t l3;
        lz >> l1 >> l2 >> l3;
        check(!lz.fail() && l1 == 123 && l2 == 0 && l3 == umax, ">> int128 (先頭の 0)");
        // ランダム: 全範囲の値を 1 桁ずつ愚直に変換したものと比べる (18 桁ずつ分ける境目を通る)
        for (int rep = 0; rep < 20000; rep++) {
            __uint128_t u = 0;
            for (int k = 0; k < 4; k++) u = u << 32 | rng();
            u >>= rng() % 128;
            std::string expect;
            for (__uint128_t y = u; expect.empty() || y > 0; y /= 10) expect.insert(expect.begin(), static_cast<char>('0' + static_cast<int>(y % 10)));
            check(to_str(u) == expect, "<< uint128 (random)");
            std::istringstream us(expect);
            __uint128_t v = 0;
            us >> v;
            check(!us.fail() && u == v, ">> uint128 (random)");
            const __int128_t x = static_cast<__int128_t>(u >> 1), nx = -x;
            check(to_str(nx) == (x == 0 ? "" : "-") + to_str(static_cast<__uint128_t>(x)), "<< int128 (random 負)");
            std::istringstream ns(to_str(nx));
            __int128_t y = 0;
            ns >> y;
            check(!ns.fail() && y == nx, ">> int128 (random 負)");
            // 最大値の付近: imax - 10 + r は r <= 10 なら読めて、そうでなければ範囲外
            const int r = rng() % 21;
            std::istringstream near(to_str(static_cast<__uint128_t>(imax) - 10 + r));
            __int128_t a = 0;
            near >> a;
            check(near.fail() == (r > 10) && (r > 10 || a == imax - 10 + r), ">> int128 (最大値の付近)");
            std::istringstream near_neg("-" + to_str(static_cast<__uint128_t>(imax) - 9 + r));
            near_neg >> a;
            check(near_neg.fail() == (r > 10) && (r > 10 || a == -imax + 9 - r), ">> int128 (最小値の付近)");
        }
        check(capture(std::cout, [&] { print(imin, umax); }) == "-170141183460469231731687303715884105728 340282366920938463463374607431768211455\n", "print(int128)");
    }

    // print: std::cout に空白区切りで出力して改行
    check(capture(std::cout, [] { print(); }) == "\n", "print()");
    check(capture(std::cout, [] { print(42); }) == "42\n", "print(x)");
    check(capture(std::cout, [] { print(1, "a", 2.5, 'c'); }) == "1 a 2.5 c\n", "print(x, ...)");
    check(capture(std::cout, [] { print(std::vector<int>{1, 2}, std::make_pair(3, 4)); }) == "1 2 3 4\n", "print(vector, pair)");

    // err: std::cerr に空白区切りで出力して改行
    check(capture(std::cerr, [] { err(); }) == "\n", "err()");
    check(capture(std::cerr, [] { err(42); }) == "42\n", "err(x)");
    check(capture(std::cerr, [] { err(1, "a", 2.5, 'c'); }) == "1 a 2.5 c\n", "err(x, ...)");
    check(capture(std::cerr, [] { err(std::set<int>{2, 1}); }) == "1 2\n", "err(set)");

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

// io.hpp の入出力を std::istringstream / std::ostringstream で検証する
// print / err は std::cout / std::cerr の出力先を一時的に差し替えて検証する
// 問題の入力は使わず、すべて一致したら Hello World を出力する

#line 2 "src/base/io.hpp"
/**
 * @file io.hpp
 * @brief 空白区切り出力、iostreamのオーバーロード
 */

#include <algorithm>
#include <array>
#include <cstddef>
#include <iostream>
#include <set>
#include <string>
#include <tuple>
#include <utility>
#include <vector>

namespace internal_io {
// 128 bit の割り算は遅いので、18 桁ずつ 64 bit で扱う
constexpr unsigned long long POW10_18 = 1000000000000000000ULL;
// 空白で区切られた 1 語を 10 進の整数として読む。数でない、または lim を超えるなら false
template <typename CharT, typename Traits>
bool read_u128(std::basic_istream<CharT, Traits>& is, unsigned __int128& x, bool& neg, unsigned __int128 pos_max, unsigned __int128 neg_max) {
    std::basic_string<CharT, Traits> s;
    if (!(is >> s)) return false;
    std::size_t i = 0;
    neg = false;
    if (s[0] == CharT('-') || s[0] == CharT('+')) neg = s[i++] == CharT('-');
    if (i == s.size()) return false;
    for (std::size_t j = i; j < s.size(); j++) {
        if (s[j] < CharT('0') || CharT('9') < s[j]) return false;
    }
    while (i + 1 < s.size() && s[i] == CharT('0')) i++;
    // 39 桁なら 2^128 を超えることがあるので、最後の 1 桁の前で比べる。40 桁以上は必ず範囲外
    const std::size_t len = s.size() - i;
    if (len > 39) return false;
    const unsigned __int128 lim = neg ? neg_max : pos_max;
    const std::size_t safe = len == 39 ? s.size() - 1 : s.size();
    x = 0;
    while (i < safe) {
        const std::size_t k = std::min<std::size_t>(18, safe - i);
        unsigned long long c = 0, p = 1;
        for (std::size_t j = 0; j < k; j++, i++) c = c * 10 + (s[i] - CharT('0')), p *= 10;
        x = x * p + c;
    }
    if (len == 39) {
        const unsigned d = s[i] - CharT('0');
        if (x > lim / 10 || x * 10 > lim - d) return false;
        x = x * 10 + d;
    }
    return x <= lim;
}
// x < 10^18 を書く。pad なら 18 桁になるよう 0 で埋める。末尾から書いて先頭を返す
inline char* write_u64(char* end, unsigned long long x, bool pad) {
    char* p = end;
    do {
        *--p = static_cast<char>('0' + x % 10);
        x /= 10;
    } while (x > 0);
    if (pad) {
        while (p > end - 18) *--p = '0';
    }
    return p;
}
template <typename CharT, typename Traits>
std::basic_ostream<CharT, Traits>& write_u128(std::basic_ostream<CharT, Traits>& os, unsigned __int128 x, bool neg) {
    char buf[41];
    char* p = buf + 40;
    *p = '\0';
    if (x < POW10_18) {
        p = write_u64(p, static_cast<unsigned long long>(x), false);
    } else {
        const unsigned __int128 q = x / POW10_18;
        p = write_u64(p, static_cast<unsigned long long>(x - q * POW10_18), true);
        if (q < POW10_18) {
            p = write_u64(p, static_cast<unsigned long long>(q), false);
        } else {
            const unsigned long long hi = static_cast<unsigned long long>(q / POW10_18);
            p = write_u64(p, static_cast<unsigned long long>(q - static_cast<unsigned __int128>(hi) * POW10_18), true);
            p = write_u64(p, hi, false);
        }
    }
    if (neg) *--p = '-';
    return os << p;
}
}  // namespace internal_io

// 128 bit 整数の入出力 (GNU 拡張)。std::basic_istream / std::basic_ostream には用意されていない
template <typename CharT, typename Traits>
std::basic_istream<CharT, Traits>& operator>>(std::basic_istream<CharT, Traits>& is, __int128_t& x) {
    const unsigned __int128 pos_max = static_cast<unsigned __int128>(-1) >> 1;
    unsigned __int128 u;
    bool neg;
    if (internal_io::read_u128(is, u, neg, pos_max, pos_max + 1)) {
        x = neg ? static_cast<__int128_t>(-u) : static_cast<__int128_t>(u);
    } else {
        x = 0;
        is.setstate(std::ios_base::failbit);
    }
    return is;
}
template <typename CharT, typename Traits>
std::basic_istream<CharT, Traits>& operator>>(std::basic_istream<CharT, Traits>& is, __uint128_t& x) {
    unsigned __int128 u;
    bool neg;
    if (internal_io::read_u128(is, u, neg, static_cast<unsigned __int128>(-1), 0)) {
        x = u;
    } else {
        x = 0;
        is.setstate(std::ios_base::failbit);
    }
    return is;
}
template <typename CharT, typename Traits>
std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os, __int128_t x) {
    // -x は最小値であふれるので、符号なしで符号を反転する
    return internal_io::write_u128(os, x < 0 ? -static_cast<unsigned __int128>(x) : static_cast<unsigned __int128>(x), x < 0);
}
template <typename CharT, typename Traits>
std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os, __uint128_t x) {
    return internal_io::write_u128(os, x, false);
}

// ネストした型 (pair<vector<int>, int> など) を扱えるように先に宣言しておく
template <typename T1, typename T2, typename CharT, typename Traits>
std::basic_istream<CharT, Traits>& operator>>(std::basic_istream<CharT, Traits>& is, std::pair<T1, T2>& p);
template <typename... Types, typename CharT, typename Traits>
std::basic_istream<CharT, Traits>& operator>>(std::basic_istream<CharT, Traits>& is, std::tuple<Types...>& p);
template <typename T, std::size_t N, typename CharT, typename Traits>
std::basic_istream<CharT, Traits>& operator>>(std::basic_istream<CharT, Traits>& is, std::array<T, N>& a);
template <typename T, typename CharT, typename Traits>
std::basic_istream<CharT, Traits>& operator>>(std::basic_istream<CharT, Traits>& is, std::vector<T>& v);
template <typename T1, typename T2, typename CharT, typename Traits>
std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os, const std::pair<T1, T2>& p);
template <typename... Types, typename CharT, typename Traits>
std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os, const std::tuple<Types...>& p);
template <typename T, std::size_t N, typename CharT, typename Traits>
std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os, const std::array<T, N>& a);
template <typename T, typename CharT, typename Traits>
std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os, const std::vector<T>& v);
template <typename T, typename CharT, typename Traits>
std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os, const std::set<T>& s);

namespace internal_io {
template <typename Tuple, std::size_t I, typename CharT, typename Traits>
std::basic_istream<CharT, Traits>& read_tuple(std::basic_istream<CharT, Traits>& is, Tuple& t) {
    is >> std::get<I>(t);
    if constexpr (I + 1 < std::tuple_size_v<Tuple>) {
        return read_tuple<Tuple, I + 1>(is, t);
    }
    return is;
}
template <typename Tuple, std::size_t I, typename CharT, typename Traits>
std::basic_ostream<CharT, Traits>& write_tuple(std::basic_ostream<CharT, Traits>& os, const Tuple& t) {
    os << std::get<I>(t);
    if constexpr (I + 1 < std::tuple_size_v<Tuple>) {
        os << CharT(' ');
        return write_tuple<Tuple, I + 1>(os, t);
    }
    return os;
}
};  // namespace internal_io

template <typename T1, typename T2, typename CharT, typename Traits>
std::basic_istream<CharT, Traits>& operator>>(std::basic_istream<CharT, Traits>& is, std::pair<T1, T2>& p) {
    is >> p.first >> p.second;
    return is;
}
template <typename... Types, typename CharT, typename Traits>
std::basic_istream<CharT, Traits>& operator>>(std::basic_istream<CharT, Traits>& is, std::tuple<Types...>& p) {
    return internal_io::read_tuple<std::tuple<Types...>, 0>(is, p);
}
template <typename T, std::size_t N, typename CharT, typename Traits>
std::basic_istream<CharT, Traits>& operator>>(std::basic_istream<CharT, Traits>& is, std::array<T, N>& a) {
    for (auto& e : a) is >> e;
    return is;
}
template <typename T, typename CharT, typename Traits>
std::basic_istream<CharT, Traits>& operator>>(std::basic_istream<CharT, Traits>& is, std::vector<T>& v) {
    for (auto& e : v) is >> e;
    return is;
}

template <typename T1, typename T2, typename CharT, typename Traits>
std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os, const std::pair<T1, T2>& p) {
    os << p.first << CharT(' ') << p.second;
    return os;
}
template <typename... Types, typename CharT, typename Traits>
std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os, const std::tuple<Types...>& p) {
    return internal_io::write_tuple<std::tuple<Types...>, 0>(os, p);
}
template <typename T, std::size_t N, typename CharT, typename Traits>
std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os, const std::array<T, N>& a) {
    for (std::size_t i = 0; i < N; ++i) {
        if (i) os << CharT(' ');
        os << a[i];
    }
    return os;
}
template <typename T, typename CharT, typename Traits>
std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os, const std::vector<T>& v) {
    for (std::size_t i = 0; i < v.size(); ++i) {
        if (i) os << CharT(' ');
        os << v[i];
    }
    return os;
}
template <typename T, typename CharT, typename Traits>
std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os, const std::set<T>& s) {
    for (auto itr = s.begin(); itr != s.end(); ++itr) {
        if (itr != s.begin()) os << CharT(' ');
        os << *itr;
    }
    return os;
}

/**
 * @brief 空行出力
 */
inline void print() { std::cout << '\n'; }
/**
 * @brief 出力して改行
 *
 * @tparam T 型
 * @param x 出力する値
 */
template <typename T>
void print(const T& x) { std::cout << x << '\n'; }
/**
 * @brief 空白区切りで出力して改行
 *
 * @tparam T 1つ目の要素の型
 * @tparam Tail 2つ目以降の要素の型
 * @param x 1つ目の要素
 * @param tail 2つ目以降の要素
 */
template <typename T, typename... Tail>
void print(const T& x, const Tail&... tail) {
    std::cout << x << ' ';
    print(tail...);
}

/**
 * @brief 標準エラー出力に空行出力
 */
inline void err() { std::cerr << std::endl; }
/**
 * @brief 標準エラー出力に出力して改行
 *
 * @tparam T 型
 * @param x 出力する値
 */
template <typename T>
void err(const T& x) { std::cerr << x << std::endl; }
/**
 * @brief 標準エラー出力に空白区切りで出力して改行
 *
 * @tparam T 1つ目の要素の型
 * @tparam Tail 2つ目以降の要素の型
 * @param x 1つ目の要素
 * @param tail 2つ目以降の要素
 */
template <typename T, typename... Tail>
void err(const T& x, const Tail&... tail) {
    std::cerr << x << ' ';
    err(tail...);
}
#line 8 "test/unittest/base/io.test.cpp"

#line 10 "test/unittest/base/io.test.cpp"
#include <iomanip>
#line 12 "test/unittest/base/io.test.cpp"
#include <random>
#line 14 "test/unittest/base/io.test.cpp"
#include <sstream>
#line 19 "test/unittest/base/io.test.cpp"

// std::cout (または std::cerr) に書かれた内容を文字列で受け取る
template <class F>
std::string capture(std::ostream& stream, F f) {
    std::ostringstream buf;
    std::streambuf* old = stream.rdbuf(buf.rdbuf());
    f();
    stream.rdbuf(old);
    return buf.str();
}

template <class T>
std::string to_str(const T& x) {
    std::ostringstream os;
    os << x;
    return os.str();
}

int main() {
    std::mt19937 rng(314);
    bool ok = true;
    auto check = [&](bool cond, const char* what) {
        if (!cond) {
            if (ok) std::cerr << "failed: " << what << std::endl;
            ok = false;
        }
    };

    // 出力: 空白区切り
    check(to_str(std::make_pair(1, std::string("ab"))) == "1 ab", "<< pair");
    check(to_str(std::make_tuple(1, 'x', std::string("yz"), 2.5)) == "1 x yz 2.5", "<< tuple");
    check(to_str(std::make_tuple(7)) == "7", "<< tuple (1 要素)");
    check(to_str(std::array<int, 3>{4, 5, 6}) == "4 5 6", "<< array");
    check(to_str(std::array<int, 0>{}) == "", "<< array (空)");
    check(to_str(std::vector<int>{3, 1, 2}) == "3 1 2", "<< vector");
    check(to_str(std::vector<int>{}) == "", "<< vector (空)");
    check(to_str(std::set<int>{3, 1, 2}) == "1 2 3", "<< set");
    check(to_str(std::set<int>{}) == "", "<< set (空)");
    // ネストした型
    check(to_str(std::vector<std::pair<int, int>>{{1, 2}, {3, 4}}) == "1 2 3 4", "<< vector<pair>");
    check(to_str(std::make_pair(std::vector<int>{1, 2}, std::array<int, 2>{3, 4})) == "1 2 3 4", "<< pair<vector, array>");

    // 入力
    {
        std::istringstream is("1 ab  x 2 yz 3.5\n 4 5 6 7 8 9 10");
        std::pair<int, std::string> p;
        std::tuple<char, int, std::string, double> t;
        std::array<int, 3> a;
        std::vector<int> v(4);
        is >> p >> t >> a >> v;
        check(p == std::make_pair(1, std::string("ab")), ">> pair");
        check(t == std::make_tuple('x', 2, std::string("yz"), 3.5), ">> tuple");
        check(a == std::array<int, 3>{4, 5, 6}, ">> array");
        check(v == std::vector<int>{7, 8, 9, 10}, ">> vector");
        check(static_cast<bool>(is), "stream state");
    }
    {
        // ネストした型
        std::istringstream is("1 2 3 4 5 6");
        std::pair<std::vector<int>, std::array<int, 2>> p;
        p.first.resize(2);
        std::vector<std::pair<int, int>> vp(1);
        is >> p >> vp;
        check(p.first == std::vector<int>{1, 2} && p.second == std::array<int, 2>{3, 4}, ">> pair<vector, array>");
        check(vp[0] == std::make_pair(5, 6), ">> vector<pair>");
    }

    // ランダム: 書いて読み戻すと一致し、出力は愚直に空白でつないだものと一致する
    for (int rep = 0; rep < 2000 && ok; rep++) {
        int n = rng() % 6;
        std::vector<int> v(n);
        for (int& x : v) x = static_cast<int>(rng() % 2001) - 1000;
        std::string expect;
        for (int i = 0; i < n; i++) expect += (i ? " " : "") + std::to_string(v[i]);
        check(to_str(v) == expect, "<< vector (random)");
        std::set<int> s(v.begin(), v.end());
        std::string expect_s;
        for (auto it = s.begin(); it != s.end(); ++it) expect_s += (it == s.begin() ? "" : " ") + std::to_string(*it);
        check(to_str(s) == expect_s, "<< set (random)");
        std::istringstream is(expect);
        std::vector<int> w(n);
        is >> w;
        check(w == v, ">> vector (random)");
        std::array<int, 4> a;
        for (int& x : a) x = static_cast<int>(rng() % 2001) - 1000;
        std::istringstream is2(to_str(a));
        std::array<int, 4> b;
        is2 >> b;
        check(a == b, "array round trip");
        auto t = std::make_tuple(static_cast<int>(rng() % 100), static_cast<long long>(rng()), std::string(1 + rng() % 3, 'a' + rng() % 26));
        std::istringstream is3(to_str(t));
        decltype(t) u;
        is3 >> u;
        check(t == u, "tuple round trip");
    }

    // 128 bit 整数
    {
        const __uint128_t umax = ~static_cast<__uint128_t>(0);
        const __int128_t imax = static_cast<__int128_t>(umax >> 1), imin = -imax - 1;
        check(to_str(static_cast<__int128_t>(0)) == "0", "<< int128 (0)");
        check(to_str(static_cast<__int128_t>(-42)) == "-42", "<< int128 (負)");
        check(to_str(imax) == "170141183460469231731687303715884105727", "<< int128 (最大値)");
        check(to_str(imin) == "-170141183460469231731687303715884105728", "<< int128 (最小値)");
        check(to_str(umax) == "340282366920938463463374607431768211455", "<< uint128 (最大値)");
        check(to_str(std::vector<__int128_t>{1, -2}) == "1 -2", "<< vector<int128>");
        std::ostringstream w;
        w << std::setw(5) << static_cast<__int128_t>(-7);
        check(w.str() == "   -7", "<< int128 (setw)");

        std::istringstream is("170141183460469231731687303715884105727 -170141183460469231731687303715884105728 +5 340282366920938463463374607431768211455 3 -4");
        __int128_t a, b, c;
        __uint128_t d;
        std::pair<__int128_t, __int128_t> p;
        is >> a >> b >> c >> d >> p;
        check(a == imax && b == imin && c == 5 && d == umax && p == std::make_pair<__int128_t, __int128_t>(3, -4), ">> int128");
        check(static_cast<bool>(is), ">> int128 (stream state)");
        // 範囲外や数でないものは failbit
        for (std::string bad : {"170141183460469231731687303715884105728", "-170141183460469231731687303715884105729", "12a", "-", "x"}) {
            std::istringstream bs(bad);
            __int128_t x;
            bs >> x;
            check(bs.fail(), ">> int128 (不正)");
        }
        for (std::string bad : {"340282366920938463463374607431768211456", "-1"}) {
            std::istringstream bs(bad);
            __uint128_t x;
            bs >> x;
            check(bs.fail(), ">> uint128 (不正)");
        }
        std::istringstream zs("-0");
        __uint128_t z = 1;
        zs >> z;
        check(!zs.fail() && z == 0, ">> uint128 (-0)");
        // ランダム: long long の積で作った値を書いて読み戻す
        for (int rep = 0; rep < 2000; rep++) {
            __int128_t x = static_cast<__int128_t>(static_cast<long long>(rng())) * static_cast<long long>(rng() * 1000003ULL);
            if (rng() % 2) x = -x;
            std::istringstream rs(to_str(x));
            __int128_t y = 0;
            rs >> y;
            check(x == y, "int128 round trip");
            // long long に収まるなら long long と同じ表記
            if (-1000000000000000000 <= x && x <= 1000000000000000000) check(to_str(x) == std::to_string(static_cast<long long>(x)), "<< int128 (long long と比べる)");
        }
        // 先頭の 0 は桁数に数えない
        std::istringstream lz("000000000000000000000000000000000000000000000123 -0000 +00340282366920938463463374607431768211455");
        __int128_t l1, l2;
        __uint128_t l3;
        lz >> l1 >> l2 >> l3;
        check(!lz.fail() && l1 == 123 && l2 == 0 && l3 == umax, ">> int128 (先頭の 0)");
        // ランダム: 全範囲の値を 1 桁ずつ愚直に変換したものと比べる (18 桁ずつ分ける境目を通る)
        for (int rep = 0; rep < 20000; rep++) {
            __uint128_t u = 0;
            for (int k = 0; k < 4; k++) u = u << 32 | rng();
            u >>= rng() % 128;
            std::string expect;
            for (__uint128_t y = u; expect.empty() || y > 0; y /= 10) expect.insert(expect.begin(), static_cast<char>('0' + static_cast<int>(y % 10)));
            check(to_str(u) == expect, "<< uint128 (random)");
            std::istringstream us(expect);
            __uint128_t v = 0;
            us >> v;
            check(!us.fail() && u == v, ">> uint128 (random)");
            const __int128_t x = static_cast<__int128_t>(u >> 1), nx = -x;
            check(to_str(nx) == (x == 0 ? "" : "-") + to_str(static_cast<__uint128_t>(x)), "<< int128 (random 負)");
            std::istringstream ns(to_str(nx));
            __int128_t y = 0;
            ns >> y;
            check(!ns.fail() && y == nx, ">> int128 (random 負)");
            // 最大値の付近: imax - 10 + r は r <= 10 なら読めて、そうでなければ範囲外
            const int r = rng() % 21;
            std::istringstream near(to_str(static_cast<__uint128_t>(imax) - 10 + r));
            __int128_t a = 0;
            near >> a;
            check(near.fail() == (r > 10) && (r > 10 || a == imax - 10 + r), ">> int128 (最大値の付近)");
            std::istringstream near_neg("-" + to_str(static_cast<__uint128_t>(imax) - 9 + r));
            near_neg >> a;
            check(near_neg.fail() == (r > 10) && (r > 10 || a == -imax + 9 - r), ">> int128 (最小値の付近)");
        }
        check(capture(std::cout, [&] { print(imin, umax); }) == "-170141183460469231731687303715884105728 340282366920938463463374607431768211455\n", "print(int128)");
    }

    // print: std::cout に空白区切りで出力して改行
    check(capture(std::cout, [] { print(); }) == "\n", "print()");
    check(capture(std::cout, [] { print(42); }) == "42\n", "print(x)");
    check(capture(std::cout, [] { print(1, "a", 2.5, 'c'); }) == "1 a 2.5 c\n", "print(x, ...)");
    check(capture(std::cout, [] { print(std::vector<int>{1, 2}, std::make_pair(3, 4)); }) == "1 2 3 4\n", "print(vector, pair)");

    // err: std::cerr に空白区切りで出力して改行
    check(capture(std::cerr, [] { err(); }) == "\n", "err()");
    check(capture(std::cerr, [] { err(42); }) == "42\n", "err(x)");
    check(capture(std::cerr, [] { err(1, "a", 2.5, 'c'); }) == "1 a 2.5 c\n", "err(x, ...)");
    check(capture(std::cerr, [] { err(std::set<int>{2, 1}); }) == "1 2\n", "err(set)");

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