Test and bugfix real128

This commit is contained in:
Nathan Hourt 2014-10-15 17:53:15 -04:00
parent b55ae3431a
commit c3a6b40188
4 changed files with 27 additions and 4 deletions

View file

@ -235,6 +235,9 @@ target_link_libraries( ntp_test fc )
add_executable( task_cancel_test tests/task_cancel.cpp )
target_link_libraries( task_cancel_test fc )
add_executable( real128_test tests/real128_test.cpp )
target_link_libraries( real128_test fc )
#include_directories( vendor/udt4/src )
add_executable( udt_server tests/udts.cpp )

View file

@ -10,7 +10,7 @@ namespace fc {
class real128
{
public:
real128( uint64_t integer = 0):fixed(integer){}
real128( uint64_t integer = 0):fixed(integer,0){}
real128( const std::string& str );
operator std::string()const;

View file

@ -55,7 +55,7 @@ namespace fc
++c;
digit = *c - '0';
}
fixed = fc::uint128(int_part);
fixed = fc::uint128(int_part, 0);
}
else
{
@ -92,8 +92,7 @@ namespace fc
std::stringstream ss;
ss << to_uint64();
ss << '.';
auto frac = *this * real128( uint128(-1,0) );
frac += real128(1);
real128 frac(fixed.low_bits());
ss << std::string( frac.fixed ).substr(1);

21
tests/real128_test.cpp Normal file
View file

@ -0,0 +1,21 @@
#include <fc/real128.hpp>
#define BOOST_TEST_MODULE Real128Test
#include <boost/test/unit_test.hpp>
using fc::real128;
using std::string;
BOOST_AUTO_TEST_CASE(real128_test)
{
BOOST_CHECK_EQUAL(string(real128(0)), string("0."));
BOOST_CHECK_EQUAL(real128(8).to_uint64(), 8);
BOOST_CHECK_EQUAL(real128(6789).to_uint64(), 6789);
BOOST_CHECK_EQUAL(real128(10000).to_uint64(), 10000);
BOOST_CHECK_EQUAL(string(real128(1)), string("1."));
BOOST_CHECK_EQUAL(string(real128(5)), string("5."));
BOOST_CHECK_EQUAL(string(real128(12345)), string("12345."));
BOOST_CHECK_EQUAL(string(real128(0)), string(real128("0")));
BOOST_CHECK_EQUAL(real128("12345.6789").to_uint64(), 12345);
BOOST_CHECK_EQUAL(string(real128("12345.6789")), string("12345.6789"));
}