#include <cmath>
#include <cstdint>
#include <limits>
#include <iomanip>
#include <iostream>
#include <stdexcept>

#include "fixed.h"
#include "int128.h"

// g++ fixed_test.cc -o fixed_test
int main(int argc, char** argv)
{
    std::cout << "Fixed Type Test Util\n" << std::endl;

    //-------------------------------------------------------------------------
    fixed_64_64 foobar;
    std::cout << "Type size: " << sizeof(fixed_64_64) << std::endl;
    
    std::cout << "Integer decimal digits: " << fixed_64_64::integer_decimal_digits << std::endl;
    std::cout << "Fractional decimal digits: " << fixed_64_64::fractional_decimal_digits << std::endl;
    
    std::cout << "Max integer value: " << fixed_64_64::MAX_INTEGER_VALUE << std::endl;
    
    std::cout << "Max fractional value: " << fixed_64_64::MAX_FRACTIONAL_VALUE << std::endl;
    
    std::cout << std::endl;
    std::cout << "Value: " << foobar << "\n\n" << std::endl;
    //-------------------------------------------------------------------------
#ifdef USE_INT128

    fixed_128_128 barfoo;
    std::cout << "Type size: " << sizeof(barfoo) << std::endl;
    
    std::cout << "Integer decimal digits: " << fixed_128_128::integer_decimal_digits << std::endl;
    std::cout << "Fractional decimal digits: " << fixed_128_128::fractional_decimal_digits << std::endl;
    
    std::cout << "Max integer value: " << fixed_128_128::MAX_INTEGER_VALUE << std::endl;
    
    std::cout << "Max fractional value: " << fixed_128_128::MAX_FRACTIONAL_VALUE << std::endl;
    
    std::cout << std::endl;
    std::cout << "Value: " << barfoo << "\n\n" << std::endl;

#endif    
    //-------------------------------------------------------------------------
    
    fixed_64_64 quxbar(3, 1415926353);
    std::cout << "Value: " << quxbar << std::endl;
    
    fixed_64_64 intMax(999999999999999999ULL);
    std::cout << "Value: " << intMax << std::endl;
    
    try
    {
        fixed_64_64 intOverflow(1999999999999999999ULL);
        std::cout << "Value: " << intMax << std::endl;
    }
    catch (const std::out_of_range& excep)
    {
        std::cout << "Integer overflow caught" << std::endl;
    }
    
    fixed_64_64 fracMax(0, 9999999999999999999ULL);
    std::cout << "Value: " << fracMax << std::endl;
    
    try
    {
        fixed_64_64 fracOverflow(0, 10000000000000000001ULL);
    }
    catch (const std::out_of_range& excep)
    {
        std::cout << "Fractional overflow caught" << std::endl;
    }
    
    
    fixed_64_64 value1;
    fixed_64_64 value2;
    const char* strVal = "100.1";
    value1.parse(strVal);
    std::cout << "String value: " << strVal << std::endl;
    std::cout << "Parsed value: " << value1 << std::endl;
    
    strVal = "100";
    value2.parse(strVal);
    std::cout << "String value: " << strVal << std::endl;
    std::cout << "Parsed value: " << value2 << std::endl;
    
    std::cout << value1 << " == " << value2 << " --> " << (value1 == value2) << std::endl;
    std::cout << value1 << " != " << value2 << " --> " << (value1 != value2) << std::endl;
    std::cout << value1 << " > " << value2 << " --> " << (value1 > value2) << std::endl;
    std::cout << value1 << " < " << value2 << " --> " << (value1 < value2) << std::endl;    
    std::cout << value1 << " >= " << value2 << " --> " << (value1 >= value2) << std::endl;
    std::cout << value1 << " <= " << value2 << " --> " << (value1 <= value2) << std::endl;
    
    //~ double foo = std::pow(10, 18);
    //~ std::cout << "Foo: " << std::fixed << std::setprecision(0) << foo << std::endl;
    //~ std::cout << "Foo: " << std::fixed << std::setprecision(0) << (static_cast<uint64_t>(foo) - 1) << std::endl;
    
    uint128_t big = 1098298217598174ULL;
    std::cout << "BIG INT: " << big << std::endl;
    big *= 1098982694765;
    std::cout << "BIGGER INT: " << big << std::endl;
    
    std::cout << "INT128T MAX: " << std::numeric_limits<int128_t>::max() << std::endl;
    std::cout << "UINT128T MAX: " << std::numeric_limits<uint128_t>::max() << std::endl;
    
    strVal = "-1207010734831637608463359110";
    int128_t s128Val = strtoll_128(strVal, nullptr, 0);
    std::cout << "String value: " << strVal << std::endl;
    std::cout << "Parsed value: " << s128Val << std::endl;
    
    //~ strVal = "991207010734831637608463359110888";
    strVal = "340282366920938463463374607431768211456";
    uint128_t u128Val = strtoull_128(strVal, nullptr, 0);
    std::cout << "String value: " << strVal << std::endl;
    std::cout << "Parsed value: " << u128Val << std::endl;
    
    return 0;
}
