Repository: microsoft/terminal
File: src/til/ut_til/MathTests.cpp (C++)

Code before:

    template<class TilMath, typename TG, typename TX, int N>
    static void _RunCases(TilMath, const std::array<TestCase<TG, TX>, N>& cases)
    {
        for (const auto& tc : cases)
        {
            VERIFY_ARE_EQUAL(tc.expected, TilMath::template cast<decltype(tc.expected)>(tc.given));
        }
    }

    TEST_METHOD(Truncating)
    {
        std::array<TestCase<long double, ptrdiff_t>, 8> cases{
            TestCase<long double, ptrdiff_t>{ 1., 1 },
            { 1.9, 1 },
            { -7.1, -7 },
            { -8.5, -8 },
            { PTRDIFF_MAX + 0.5, PTRDIFF_MAX },
            { PTRDIFF_MIN - 0.5, PTRDIFF_MIN },
            { INFINITY, PTRDIFF_MAX },
            { -INFINITY, PTRDIFF_MIN },
        };

        _RunCases(til::math::truncating, cases);

        const auto fn = []() {
            const auto v = til::math::details::truncating_t::cast<ptrdiff_t>(NAN);
        };
        VERIFY_THROWS_SPECIFIC(fn(), wil::ResultException, [](wil::ResultException& e) { return e.GetErrorCode() == E_ABORT; });
    }

    TEST_METHOD(Ceiling)
    {
        std::array<TestCase<long double, ptrdiff_t>, 8> cases{
            TestCase<long double, ptrdiff_t>{ 1., 1 },
            { 1.9, 2 },
            { -7.1, -7 },
            { -8.5, -8 },
            { PTRDIFF_MAX + 0.5, PTRDIFF_MAX },
            { PTRDIFF_MIN - 0.5, PTRDIFF_MIN },
            { INFINITY, PTRDIFF_MAX },
            { -INFINITY, PTRDIFF_MIN },
        };

        _RunCases(til::math::ceiling, cases);

        const auto fn = []() {
            const auto v = til::math::details::ceiling_t::cast<ptrdiff_t>(NAN);
        };
        VERIFY_THROWS_SPECIFIC(fn(), wil::ResultException, [](wil::ResultException& e) { return e.GetErrorCode() == E_ABORT; });
    }

Review comment (suggestion):
In these cases `v` was flagged as an unused variable.

Code after:

    template<class TilMath, typename TG, typename TX, int N>
    static void _RunCases(TilMath, const std::array<TestCase<TG, TX>, N>& cases)
    {
        for (const auto& tc : cases)
        {
            VERIFY_ARE_EQUAL(tc.expected, TilMath::template cast<decltype(tc.expected)>(tc.given));
        }
    }

    TEST_METHOD(Truncating)
    {
        std::array<TestCase<long double, ptrdiff_t>, 8> cases{
            TestCase<long double, ptrdiff_t>{ 1., 1 },
            { 1.9, 1 },
            { -7.1, -7 },
            { -8.5, -8 },
            { PTRDIFF_MAX + 0.5, PTRDIFF_MAX },
            { PTRDIFF_MIN - 0.5, PTRDIFF_MIN },
            { INFINITY, PTRDIFF_MAX },
            { -INFINITY, PTRDIFF_MIN },
        };

        _RunCases(til::math::truncating, cases);

        VERIFY_THROWS_SPECIFIC(til::math::details::truncating_t::cast<ptrdiff_t>(NAN), wil::ResultException, [](wil::ResultException& e) { return e.GetErrorCode() == E_ABORT; });
    }

    TEST_METHOD(Ceiling)
    {
        std::array<TestCase<long double, ptrdiff_t>, 8> cases{
            TestCase<long double, ptrdiff_t>{ 1., 1 },
            { 1.9, 2 },
            { -7.1, -7 },
            { -8.5, -8 },
            { PTRDIFF_MAX + 0.5, PTRDIFF_MAX },
            { PTRDIFF_MIN - 0.5, PTRDIFF_MIN },
            { INFINITY, PTRDIFF_MAX },
            { -INFINITY, PTRDIFF_MIN },
        };

        _RunCases(til::math::ceiling, cases);

        VERIFY_THROWS_SPECIFIC(til::math::details::ceiling_t::cast<ptrdiff_t>(NAN), wil::ResultException, [](wil::ResultException& e) { return e.GetErrorCode() == E_ABORT; });
    }

    TEST_METHOD(Flooring)
    {
        std::array<TestCase<long double, ptrdiff_t>, 8> cases{
            TestCase<long double, ptrdiff_t>{ 1., 1 },
            { 1.9, 1 },