/*
 * Copyright (C) 2020-2021 Aloysius Indrayanto
 *
 * This file is part of the eCxx library, see LICENSE file for the license details.
 */


#include "TestUtility.h"


//
// Test application
//
int main()
{
#ifndef ECXX_FIXED_POINT_USE_HIGH_PRECISION
    #define DTD_FMT "%c%d.%02d"
    #define DTD_TCS int
#else
    #define DTD_FMT "%c%zd.%04zd"
    #define DTD_TCS eCxx::ssize_t
#endif

    bool                                     n; // Negative number flag
    eCxx::FixedPoint::UnsignedHalfValueType  i; // Integer part
    eCxx::FixedPoint::UnsignedQuartValueType f; // Fractional part scaled by eCxx::FixedPoint::FractionalDSF



    _____________USERSTR_NL2("FixedPoint : multiplication");

    eCxx::FixedPoint a(3.045_fp);
    eCxx::FixedPoint b(3.0_fp  );
    eCxx::FixedPoint c = a * b;
    c.decomposeToDecimal(n, i, f);
    _____________CODERES_NL2(DTD_FMT, (n ? '-' : '+'), (DTD_TCS) i, (DTD_TCS) f);

    a = -3.045_fp;
    b =  3.0_fp;
    c = a * b;
    c.decomposeToDecimal(n, i, f);
    _____________CODERES_NL2(DTD_FMT, (n ? '-' : '+'), (DTD_TCS) i, (DTD_TCS) f);

    a = -3.045_fp;
    b = -3.0_fp;
    c = a * b;
    c.decomposeToDecimal(n, i, f);
    _____________CODERES_NL2(DTD_FMT, (n ? '-' : '+'), (DTD_TCS) i, (DTD_TCS) f);

    _DIVIDERLINE_DASHSTR_NL2();



    _____________USERSTR_NL2("FixedPoint : user-defined literal");

    c = 625.0_fp;
    c.decomposeToDecimal(n, i, f);
    _____________CODERES_NL2(DTD_FMT, (n ? '-' : '+'), (DTD_TCS) i, (DTD_TCS) f);

    c = 625_fp;
    c.decomposeToDecimal(n, i, f);
    _____________CODERES_NL2(DTD_FMT, (n ? '-' : '+'), (DTD_TCS) i, (DTD_TCS) f);

    c = 625_dfp;
    c.decomposeToDecimal(n, i, f);
    _____________CODERES_NL2(DTD_FMT, (n ? '-' : '+'), (DTD_TCS) i, (DTD_TCS) f);

    c = 625_hfp;
    c.decomposeToDecimal(n, i, f);
    _____________CODERES_NL2(DTD_FMT, (n ? '-' : '+'), (DTD_TCS) i, (DTD_TCS) f);

    c = -625.0_fp;
    c.decomposeToDecimal(n, i, f);
    _____________CODERES_NL2(DTD_FMT, (n ? '-' : '+'), (DTD_TCS) i, (DTD_TCS) f);

    c = -625_fp;
    c.decomposeToDecimal(n, i, f);
    _____________CODERES_NL2(DTD_FMT, (n ? '-' : '+'), (DTD_TCS) i, (DTD_TCS) f);

    c = -625_dfp;
    c.decomposeToDecimal(n, i, f);
    _____________CODERES_NL2(DTD_FMT, (n ? '-' : '+'), (DTD_TCS) i, (DTD_TCS) f);

    c = -625_hfp;
    c.decomposeToDecimal(n, i, f);
    _____________CODERES_NL2(DTD_FMT, (n ? '-' : '+'), (DTD_TCS) i, (DTD_TCS) f);

    _DIVIDERLINE_DASHSTR_NL2();



    _____________USERSTR_NL2("FixedPoint : number ranges");

    c = eCxx::FixedPoint::S_POSITIVE;
    c.decomposeToDecimal(n, i, f);
    _____________CODERES_NL2(DTD_FMT, (n ? '-' : '+'), (DTD_TCS) i, (DTD_TCS) f);

    c = eCxx::FixedPoint::L_POSITIVE;
    c.decomposeToDecimal(n, i, f);
    _____________CODERES_NL2(DTD_FMT, (n ? '-' : '+'), (DTD_TCS) i, (DTD_TCS) f);

    c = eCxx::FixedPoint::S_NEGATIVE;
    c.decomposeToDecimal(n, i, f);
    _____________CODERES_NL2(DTD_FMT, (n ? '-' : '+'), (DTD_TCS) i, (DTD_TCS) f);

    c = eCxx::FixedPoint::L_NEGATIVE;
    c.decomposeToDecimal(n, i, f);
    _____________CODERES_NL2(DTD_FMT, (n ? '-' : '+'), (DTD_TCS) i, (DTD_TCS) f);

    _DIVIDERLINE_DASHSTR_NL2();



    _____________USERSTR_NL2("FixedPoint : sqrt()");

    c = 100.0_fp;
    c.sqrt();
    c.decomposeToDecimal(n, i, f);
    _____________CODERES_NL2(DTD_FMT, (n ? '-' : '+'), (DTD_TCS) i, (DTD_TCS) f);

    c = 6.25_fp;
    c.sqrt();
    c.decomposeToDecimal(n, i, f);
    _____________CODERES_NL2(DTD_FMT, (n ? '-' : '+'), (DTD_TCS) i, (DTD_TCS) f);

    c = 5.0_fp;
    c.sqrt();
    c.decomposeToDecimal(n, i, f);
    _____________CODERES_NL2(DTD_FMT, (n ? '-' : '+'), (DTD_TCS) i, (DTD_TCS) f);

    c = 3.0_fp;
    c.sqrt();
    c.decomposeToDecimal(n, i, f);
    _____________CODERES_NL2(DTD_FMT, (n ? '-' : '+'), (DTD_TCS) i, (DTD_TCS) f);

    c = 0.04_fp;
    c.sqrt();
    c.decomposeToDecimal(n, i, f);
    _____________CODERES_NL2(DTD_FMT, (n ? '-' : '+'), (DTD_TCS) i, (DTD_TCS) f);

    _DIVIDERLINE_DASHSTR_NL2();



    _____________USERSTR_NL2("FixedPoint : exp()");

    c = 1.0_fp;
    c.exp();
    c.decomposeToDecimal(n, i, f);
    _____________CODERES_NL2(DTD_FMT, (n ? '-' : '+'), (DTD_TCS) i, (DTD_TCS) f);

    c = 2.0_fp;
    c.exp();
    c.decomposeToDecimal(n, i, f);
    _____________CODERES_NL2(DTD_FMT, (n ? '-' : '+'), (DTD_TCS) i, (DTD_TCS) f);

    _DIVIDERLINE_DASHSTR_NL2();



    _____________USERSTR_NL2("FixedPoint : ln()");

    c = eCxx::FixedPoint::E;
    c.ln();
    c.decomposeToDecimal(n, i, f);
    _____________CODERES_NL2(DTD_FMT, (n ? '-' : '+'), (DTD_TCS) i, (DTD_TCS) f);

    c = 2.0_fp;
    c.ln();
    c.decomposeToDecimal(n, i, f);
    _____________CODERES_NL2(DTD_FMT, (n ? '-' : '+'), (DTD_TCS) i, (DTD_TCS) f);

    c = 10.0_fp;
    c.ln();
    c.decomposeToDecimal(n, i, f);
    _____________CODERES_NL2(DTD_FMT, (n ? '-' : '+'), (DTD_TCS) i, (DTD_TCS) f);

    _DIVIDERLINE_DASHSTR_NL2();



    _____________USERSTR_NL2("FixedPoint : log()");

    c = 10000.0_fp;
    c.log();
    c.decomposeToDecimal(n, i, f);
    _____________CODERES_NL2(DTD_FMT, (n ? '-' : '+'), (DTD_TCS) i, (DTD_TCS) f);

    c = 100.0_fp;
    c.log();
    c.decomposeToDecimal(n, i, f);
    _____________CODERES_NL2(DTD_FMT, (n ? '-' : '+'), (DTD_TCS) i, (DTD_TCS) f);

    c = eCxx::FixedPoint::E;
    c.log();
    c.decomposeToDecimal(n, i, f);
    _____________CODERES_NL2(DTD_FMT, (n ? '-' : '+'), (DTD_TCS) i, (DTD_TCS) f);

    c = eCxx::FixedPoint::TWO;
    c.log();
    c.decomposeToDecimal(n, i, f);
    _____________CODERES_NL2(DTD_FMT, (n ? '-' : '+'), (DTD_TCS) i, (DTD_TCS) f);

    _DIVIDERLINE_DASHSTR_NL2();



    _____________USERSTR_NL2("FixedPoint : pow()");

    c = eCxx::FixedPoint::TWO;
    c.pow(2);
    c.decomposeToDecimal(n, i, f);
    _____________CODERES_NL2(DTD_FMT, (n ? '-' : '+'), (DTD_TCS) i, (DTD_TCS) f);

    c = eCxx::FixedPoint::TWO;
    c.pow(3);
    c.decomposeToDecimal(n, i, f);
    _____________CODERES_NL2(DTD_FMT, (n ? '-' : '+'), (DTD_TCS) i, (DTD_TCS) f);

    c = eCxx::FixedPoint::TWO;
    c.pow(4);
    c.decomposeToDecimal(n, i, f);
    _____________CODERES_NL2(DTD_FMT, (n ? '-' : '+'), (DTD_TCS) i, (DTD_TCS) f);

    c = -eCxx::FixedPoint::TWO;
    c.pow(2);
    c.decomposeToDecimal(n, i, f);
    _____________CODERES_NL2(DTD_FMT, (n ? '-' : '+'), (DTD_TCS) i, (DTD_TCS) f);

    c = -eCxx::FixedPoint::TWO;
    c.pow(3);
    c.decomposeToDecimal(n, i, f);
    _____________CODERES_NL2(DTD_FMT, (n ? '-' : '+'), (DTD_TCS) i, (DTD_TCS) f);

    c = -eCxx::FixedPoint::TWO;
    c.pow(4);
    c.decomposeToDecimal(n, i, f);
    _____________CODERES_NL2(DTD_FMT, (n ? '-' : '+'), (DTD_TCS) i, (DTD_TCS) f);



    _____________USERSTR_NL2("FixedPoint : sin(), cos(), and tan()");

    c = eCxx::FixedPoint::DEG_TO_RAD * 30.0f;
    c.sin();
    c.decomposeToDecimal(n, i, f);
    _____________CODERES_NL2(DTD_FMT, (n ? '-' : '+'), (DTD_TCS) i, (DTD_TCS) f);

    c = eCxx::FixedPoint::DEG_TO_RAD * 60.0f;
    c.cos();
    c.decomposeToDecimal(n, i, f);
    _____________CODERES_NL2(DTD_FMT, (n ? '-' : '+'), (DTD_TCS) i, (DTD_TCS) f);

    c = eCxx::FixedPoint::DEG_TO_RAD * 45.0f;
    c.tan();
    c.decomposeToDecimal(n, i, f);
    _____________CODERES_NL2(DTD_FMT, (n ? '-' : '+'), (DTD_TCS) i, (DTD_TCS) f);

    c = eCxx::FixedPoint::DEG_TO_RAD * (30.0f + 180.0f);
    c.sin();
    c.decomposeToDecimal(n, i, f);
    _____________CODERES_NL2(DTD_FMT, (n ? '-' : '+'), (DTD_TCS) i, (DTD_TCS) f);

    c = eCxx::FixedPoint::DEG_TO_RAD * (60.0f + 180.0f);
    c.cos();
    c.decomposeToDecimal(n, i, f);
    _____________CODERES_NL2(DTD_FMT, (n ? '-' : '+'), (DTD_TCS) i, (DTD_TCS) f);

    c = eCxx::FixedPoint::DEG_TO_RAD * (45.0f + 90.0f);
    c.tan();
    c.decomposeToDecimal(n, i, f);
    _____________CODERES_NL2(DTD_FMT, (n ? '-' : '+'), (DTD_TCS) i, (DTD_TCS) f);



    _____________USERSTR_NL2("FixedPoint : abs()");

    c = 10.3_fp;
    c.abs();
    c.decomposeToDecimal(n, i, f);
    _____________CODERES_NL2(DTD_FMT, (n ? '-' : '+'), (DTD_TCS) i, (DTD_TCS) f);

    c = 10.7_fp;
    c.abs();
    c.decomposeToDecimal(n, i, f);
    _____________CODERES_NL2(DTD_FMT, (n ? '-' : '+'), (DTD_TCS) i, (DTD_TCS) f);

    c = -10.3_fp;
    c.abs();
    c.decomposeToDecimal(n, i, f);
    _____________CODERES_NL2(DTD_FMT, (n ? '-' : '+'), (DTD_TCS) i, (DTD_TCS) f);

    c = -10.7_fp;
    c.abs();
    c.decomposeToDecimal(n, i, f);
    _____________CODERES_NL2(DTD_FMT, (n ? '-' : '+'), (DTD_TCS) i, (DTD_TCS) f);



    _____________USERSTR_NL2("FixedPoint : floor()");

    c = 10.3_fp;
    c.floor();
    c.decomposeToDecimal(n, i, f);
    _____________CODERES_NL2(DTD_FMT, (n ? '-' : '+'), (DTD_TCS) i, (DTD_TCS) f);

    c = 10.7_fp;
    c.floor();
    c.decomposeToDecimal(n, i, f);
    _____________CODERES_NL2(DTD_FMT, (n ? '-' : '+'), (DTD_TCS) i, (DTD_TCS) f);

    c = -10.3_fp;
    c.floor();
    c.decomposeToDecimal(n, i, f);
    _____________CODERES_NL2(DTD_FMT, (n ? '-' : '+'), (DTD_TCS) i, (DTD_TCS) f);

    c = -10.7_fp;
    c.floor();
    c.decomposeToDecimal(n, i, f);
    _____________CODERES_NL2(DTD_FMT, (n ? '-' : '+'), (DTD_TCS) i, (DTD_TCS) f);



    _____________USERSTR_NL2("FixedPoint : ceil()");

    c = 10.3_fp;
    c.ceil();
    c.decomposeToDecimal(n, i, f);
    _____________CODERES_NL2(DTD_FMT, (n ? '-' : '+'), (DTD_TCS) i, (DTD_TCS) f);

    c = 10.7_fp;
    c.ceil();
    c.decomposeToDecimal(n, i, f);
    _____________CODERES_NL2(DTD_FMT, (n ? '-' : '+'), (DTD_TCS) i, (DTD_TCS) f);

    c = -10.3_fp;
    c.ceil();
    c.decomposeToDecimal(n, i, f);
    _____________CODERES_NL2(DTD_FMT, (n ? '-' : '+'), (DTD_TCS) i, (DTD_TCS) f);

    c = -10.7_fp;
    c.ceil();
    c.decomposeToDecimal(n, i, f);
    _____________CODERES_NL2(DTD_FMT, (n ? '-' : '+'), (DTD_TCS) i, (DTD_TCS) f);



    _____________USERSTR_NL2("FixedPoint : round()");

    c = 10.3_fp;
    c.round();
    c.decomposeToDecimal(n, i, f);
    _____________CODERES_NL2(DTD_FMT, (n ? '-' : '+'), (DTD_TCS) i, (DTD_TCS) f);

    c = 10.7_fp;
    c.round();
    c.decomposeToDecimal(n, i, f);
    _____________CODERES_NL2(DTD_FMT, (n ? '-' : '+'), (DTD_TCS) i, (DTD_TCS) f);

    c = -10.3_fp;
    c.round();
    c.decomposeToDecimal(n, i, f);
    _____________CODERES_NL2(DTD_FMT, (n ? '-' : '+'), (DTD_TCS) i, (DTD_TCS) f);

    c = -10.7_fp;
    c.round();
    c.decomposeToDecimal(n, i, f);
    _____________CODERES_NL2(DTD_FMT, (n ? '-' : '+'), (DTD_TCS) i, (DTD_TCS) f);



    return 0;
}
