#! /usr/bin/env qore
# -*- mode: qore; indent-tabs-mode: nil -*-

%new-style
%enable-all-warnings
%require-types
%strict-args

%requires ../../../../qlib/QUnit.qm

%exec-class FloorTest

public class FloorTest inherits QUnit::Test {
    constructor() : Test("FloorTest", "1.1") {
        addTestCase("Floor without precision test", \testZeroPrecision(), NOTHING);
        addTestCase("Positive precision test", \testPositivePrecision(), NOTHING);
        addTestCase("Negative precision test", \testNegativePrecision(), NOTHING);

        # Return for compatibility with test harness that checks return value.
        set_return_value(main());
    }

    testZeroPrecision() {
        testAssertion("test floor(        6538738)",  \floor(), (        6538738),         6538738);
        testAssertion("test floor(-27457456938463)",  \floor(), (-27457456938463), -27457456938463);
        testAssertion("test floor( -2745745693846n)", \floor(), ( -2745745693846n), -2745745693846n);

        testAssertion("test floor( 27.57456       )", \floor(), ( 27.57456       ),  27.0);
        testAssertion("test floor(-27.5           )", \floor(), (-27.5           ), -28.0);
        testAssertion("test floor( -3.00000001    )", \floor(), ( -3.00000001    ),  -4.0);
        testAssertion("test floor( 27.99999999999n)", \floor(), ( 27.99999999999n),  27n);

        testAssertion("test floor( 0)",   \floor(), ( 0),   0);
        testAssertion("test floor( 0n)",  \floor(), ( 0n),  0n);
        testAssertion("test floor(-0.0)", \floor(), (-0.0), 0.0);
    }

    testPositivePrecision() {
        assertEq      ( 2345.34,        floor( 2345.34544,       2));
        assertEq      (-2345.346,       floor(-2345.34544,       3));
        assertNumberEq( 2345.345447n,   floor( 2345.3454475543n, 6), 10e-36);
        assertNumberEq( 2345.34544755n, floor( 2345.3454475543n, 8), 10e-36);
        assertNumberEq(-2345.34544756n, floor(-2345.3454475543n, 8), 10e-36);
    }

    testNegativePrecision() {
        testAssertion("test floor( 54174,    -2)", \floor(), ( 54174,    -2),       54100);
        testAssertion("test floor( 54174.24, -3)", \floor(), ( 54174.24, -3),       54000.0);
        testAssertion("test floor( 54174.54, -4)", \floor(), ( 54174.54, -4),       50000.0);
        testAssertion("test floor(-54174.23, -4)", \floor(), (-54174.23, -4),      -60000.0);
        testAssertion("test floor( 54174,    -5)", \floor(), ( 54174,    -5),           0);
        testAssertion("test floor( 54174.0,  -6)", \floor(), ( 54174.0,  -6),           0.0);
        testAssertion("test floor(-54174,    -9)", \floor(), (-54174,    -9), -1000000000);

        testAssertion("test floor(        5262654174.23n, -5)", \floor(), (        5262654174.23n, -5),         5262600000n);
        testAssertion("test floor(     -262654174983,     -7)", \floor(), (     -262654174983,     -7),      -262660000000);
        testAssertion("test floor(-26265417498326484n,   -11)", \floor(), (-26265417498326484n,   -11), -26265500000000000n);
    }
}
