#include "tile/base/chrono.h"

#include "gtest/gtest.h"

namespace tile {

static constexpr auto one_ms = std::chrono::milliseconds(1);
static constexpr auto kTestN = 1000;

long
AvageTime(std::function<long()> f, std::size_t n = kTestN)
{
    long double total = 0;
    for (std::size_t i = 0; i != n; ++i) {
        total += 1.0f / n * f();
        std::this_thread::sleep_for(std::chrono::milliseconds(1));
    }
    return static_cast<long>(total);
}

TEST(SystemClock, Compare)
{
    auto diff = AvageTime([] { return (ReadSystemClock() - std::chrono::system_clock::now()) / one_ms; });
    ASSERT_NEAR(diff, 0, 5);
}

TEST(SteadyClock, Compare)
{
    auto diff = AvageTime([] { return (ReadSteadyClock() - std::chrono::steady_clock::now()) / one_ms; });
    ASSERT_NEAR(diff, 0, 5);
}

TEST(CoarseSystemClock, Compare)
{
    auto diff = AvageTime([] { return (ReadCoarseSystemClock() - std::chrono::system_clock::now()) / one_ms; });
    ASSERT_NEAR(diff, 0, kTestN);
}

TEST(CoarseSteadyClock, Compare)
{
    auto diff = AvageTime([] { return (ReadCoarseSteadyClock() - std::chrono::steady_clock::now()) / one_ms; });
    ASSERT_NEAR(diff, 0, kTestN);
}
}// namespace tile
