Project

General

Profile

« Previous | Next » 

Revision c7e0510c

Added by David Sorber over 4 years ago

Fix minor bug in int128.h. Also add initial unit tests.

View differences:

software/fixed/fixed_test.cc
#include <cmath>
#include <cstdint>
#include <limits>
#include <iomanip>
#include <iostream>
#include <stdexcept>
#define BOOST_TEST_MAIN 1
#include <boost/test/unit_test.hpp>
#include "fixed.h"
#include "int128.h"
// g++ fixed_test.cc -o fixed_test
int main(int argc, char** argv)
BOOST_AUTO_TEST_CASE(t0)
{
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 << std::endl;
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";
uint128_t u128Val = strtoull_128(strVal, nullptr, 0);
std::cout << "String value: " << strVal << std::endl;
std::cout << "Parsed value: " << u128Val << std::endl;
return 0;
// Check overall type sizes
fixed_8_8 f_8_8;
fixed_8_32 f_8_32;
fixed_8_16 f_8_16;
fixed_8_64 f_8_64;
BOOST_CHECK(sizeof(f_8_8) == 2);
BOOST_CHECK(sizeof(f_8_16) == 4);
BOOST_CHECK(sizeof(f_8_32) == 8);
BOOST_CHECK(sizeof(f_8_64) == 16);
fixed_16_8 f_16_8;
fixed_16_16 f_16_16;
fixed_16_32 f_16_32;
fixed_16_64 f_16_64;
BOOST_CHECK(sizeof(f_16_8) == 4);
BOOST_CHECK(sizeof(f_16_16) == 4);
BOOST_CHECK(sizeof(f_16_32) == 8);
BOOST_CHECK(sizeof(f_16_64) == 16);
fixed_32_8 f_32_8;
fixed_32_16 f_32_16;
fixed_32_32 f_32_32;
fixed_32_64 f_32_64;
BOOST_CHECK(sizeof(f_32_8) == 8);
BOOST_CHECK(sizeof(f_32_16) == 8);
BOOST_CHECK(sizeof(f_32_32) == 8);
BOOST_CHECK(sizeof(f_32_64) == 16);
fixed_64_8 f_64_8;
fixed_64_16 f_64_16;
fixed_64_32 f_64_32;
fixed_64_64 f_64_64;
BOOST_CHECK(sizeof(f_64_8) == 16);
BOOST_CHECK(sizeof(f_64_16) == 16);
BOOST_CHECK(sizeof(f_64_32) == 16);
BOOST_CHECK(sizeof(f_64_64) == 16);
}

Also available in: Unified diff