Link Search Menu Expand Document
あるまかんライブラリ

:heavy_check_mark: test/helloworld/fix-point.test.cpp

Depends on

Code

#define PROBLEM "http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ITP1_1_A"
#include <iostream>
#include <cassert>

#include "../../Util/fix-point.hpp"

void FixPoint_test() {
    const auto factorial = FixPoint([](auto f, int n) -> int {
        if (n <= 1) return 1;
        return n * f(n - 1);
    });

    assert(factorial(5) == 120);

    std::clog << __func__ << " : OK" << std::endl;
}

int main() {
    std::cout << "Hello World" << std::endl;

    FixPoint_test();

    return 0;
}
#line 1 "test/helloworld/fix-point.test.cpp"
#define PROBLEM "http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ITP1_1_A"
#include <iostream>
#include <cassert>

#line 2 "Util/fix-point.hpp"
#include <functional>

/**
 * @brief FixPoint (ラムダ式の再帰)
 *
 * (Ex) FixPoint([&](auto func, int n) -> int {...})(10);
 */
template <class F>
struct FixPoint : F {
    FixPoint(F&& f)
        : F(std::forward<F>(f)) {}

    template <class... Args>
    decltype(auto) operator()(Args&&... args) const {
        return F::operator()(*this, std::forward<Args>(args)...);
    }
};
#line 6 "test/helloworld/fix-point.test.cpp"

void FixPoint_test() {
    const auto factorial = FixPoint([](auto f, int n) -> int {
        if (n <= 1) return 1;
        return n * f(n - 1);
    });

    assert(factorial(5) == 120);

    std::clog << __func__ << " : OK" << std::endl;
}

int main() {
    std::cout << "Hello World" << std::endl;

    FixPoint_test();

    return 0;
}
Back to top page