未验证 提交 3439ca26 编写于 作者: D Daniel Larimer 提交者: GitHub

Merge pull request #592 from dhaneshvb/fixedpoint

Initial version of fixedpoint template for 32,64,128 bits
此差异已折叠。
#pragma once
#include <eoslib/math.hpp>
#include <eoslib/print.hpp>
namespace eos {
/**
* @defgroup real data type representation for eos
* @brief real data type with basic operators. Contract developers need not use the math APIs
* @ingroup mathapi
*
* Example:
* @code
* real a(100);
* real b(10);
* real c = a+b
* real d = a / b
* real e = a*b
* if(a == b) {}
* if(a > b) {}
* if(a < b) {}
* auto val = d.value();
*/
class real {
private:
uint64_t val;
public:
/**
* Constructor to double object from uint64 value
* @param val data
*/
real(const uint64_t &_val) : val(_val) {}
uint64_t value() const { return val; }
// Arithmetic operations
real operator+(const real &rhs) const;
real operator*(const real &rhs) const;
real operator/(const real &rhs) const;
// Comparison operators
friend bool operator==(const real &c1, const real &c2);
friend bool operator>(const real &c1, const real &c2);
friend bool operator<(const real &c1, const real &c2);
};
/**
* Add two real variables
* @param rhs double variable to be added with this
* @return the sum of this and rhs
*/
real real::operator+(const real &rhs) const {
auto _result = double_add(value(), rhs.value());
return real(_result);
}
/**
* Multiply two real variables
* @param rhs double variable to be multiplied with this
* @return the result after multiplication
*/
real real::operator*(const real &rhs) const {
auto _result = double_mult(value(), rhs.value());
return real(_result);
}
/**
* Division between two real varialbles
* @param rhs double variable to be multiplied with this
* @return the result after division
*/
real real::operator/(const real &rhs) const {
auto _result = double_div(i64_to_double(value()), i64_to_double(rhs.value()));
return real(_result);
}
/**
* Compares two double variables c1 and c2
* @param c1
* @param c2
* @return if c1 == c2, return true, otherwise false
*/
bool operator==(const real &c1, const real &c2) {
auto res = double_eq(c1.value(), c2.value());
return (res == 1);
}
/**
* Compares two double variables c1 and c2
* @param c1
* @param c2
* @return if c1 > c2, return true, otherwise false
*/
bool operator>(const real &c1, const real &c2) {
auto res = double_gt(c1.value(), c2.value());
return (res == 1);
}
/**
* Compares two double variables c1 and c2
* @param c1
* @param c2
* @return if c1 < c2, return true, otherwise false
*/
bool operator<(const real &c1, const real &c2) {
auto res = double_lt(c1.value(), c2.value());
return (res == 1);
}
}
......@@ -12,6 +12,8 @@
#include "test_message.cpp"
#include "test_print.cpp"
#include "test_string.cpp"
#include "test_fixedpoint.cpp"
#include "test_real.cpp"
#include "test_transaction.cpp"
#include "test_types.cpp"
......@@ -113,6 +115,19 @@ extern "C" {
WASM_TEST_HANDLER(test_string, invalid_utf8);
WASM_TEST_HANDLER(test_string, string_literal);
// test fixed_point
WASM_TEST_HANDLER(test_fixedpoint, create_instances);
WASM_TEST_HANDLER(test_fixedpoint, test_addition);
WASM_TEST_HANDLER(test_fixedpoint, test_subtraction);
WASM_TEST_HANDLER(test_fixedpoint, test_multiplication);
WASM_TEST_HANDLER(test_fixedpoint, test_division);
// test double
WASM_TEST_HANDLER(test_real, create_instances);
WASM_TEST_HANDLER(test_real, test_addition);
WASM_TEST_HANDLER(test_real, test_multiplication);
WASM_TEST_HANDLER(test_real, test_division);
//unhandled test call
WASM_TEST_ERROR_CODE = WASM_TEST_FAIL;
}
......
......@@ -151,3 +151,18 @@ struct test_string {
static unsigned int invalid_utf8();
static unsigned int string_literal();
};
struct test_fixedpoint {
static unsigned int create_instances();
static unsigned int test_addition();
static unsigned int test_subtraction();
static unsigned int test_multiplication();
static unsigned int test_division();
};
struct test_real {
static unsigned int create_instances();
static unsigned int test_addition();
static unsigned int test_multiplication();
static unsigned int test_division();
};
\ No newline at end of file
#include <eoslib/fixedpoint.hpp>
#include <eoslib/eos.hpp>
#include "test_api.hpp"
using namespace eos;
unsigned int test_fixedpoint::create_instances()
{
{
// Various ways to create fixed_point128
fixed_point128<18> a(12345667);
fixed_point128<18> b(12345667);
fixed_point128<16> c(12345667);
WASM_ASSERT(b == a, "fixed_point128 instances comparison with same number of decimals");
WASM_ASSERT(c == a, "fixed_point128 instances with different number of decimals");
}
{
// Various ways to create fixed_point64
fixed_point64<5> a(12345667);
fixed_point64<5> b(12345667);
fixed_point64<5> c(12345667);
WASM_ASSERT(b == a, "fixed_point64 instances comparison with same number of decimals");
WASM_ASSERT(c == a, "fixed_point64 instances with different number of decimals");
}
{
// Various ways to create fixed_point32
fixed_point32<18> a(12345667);
fixed_point32<18> b(12345667);
fixed_point32<16> c(12345667);
WASM_ASSERT(b == a, "fixed_point32 instances comparison with same number of decimals");
WASM_ASSERT(c == a, "fixed_point32 instances with different number of decimals");
}
return WASM_TEST_PASS;
}
unsigned int test_fixedpoint::test_addition()
{
{
// Various ways to create fixed_point32
fixed_point32<0> a(100);
fixed_point32<0> b(100);
fixed_point32<0> c = a + b;
fixed_point32<0> d = 200;
WASM_ASSERT(c == d, "fixed_point32 instances addition with zero decmimals");
}
{
// Various ways to create fixed_point64
fixed_point64<0> a(100);
fixed_point64<0> b(100);
fixed_point64<0> c = a + b;
fixed_point64<0> d = 200;
WASM_ASSERT(c == d, "fixed_point64 instances addition with zero decmimals");
}
return WASM_TEST_PASS;
};
unsigned int test_fixedpoint::test_subtraction()
{
{
// Various ways to create fixed_point64
fixed_point64<0> a(100);
fixed_point64<0> b(100);
fixed_point64<0> c = a - b;
fixed_point64<0> d = 0;
WASM_ASSERT(c == d, "fixed_point64 instances subtraction with zero decmimals");
}
{
// Various ways to create fixed_point32
fixed_point32<0> a(100);
fixed_point32<0> b(100);
fixed_point32<0> c = a - b;
fixed_point32<0> d = 0;
WASM_ASSERT(c == d, "fixed_point32 instances subtraction with zero decmimals");
}
return WASM_TEST_PASS;
};
unsigned int test_fixedpoint::test_multiplication()
{
{
// Various ways to create fixed_point64
fixed_point64<0> a(100);
fixed_point64<0> b(200);
fixed_point128<0> c = a * b;
fixed_point128<0> d(200*100);
WASM_ASSERT(c == d, "fixed_point64 instances multiplication result in fixed_point128");
}
{
// Various ways to create fixed_point32
fixed_point32<0> a(100);
fixed_point32<0> b(200);
fixed_point64<0> c = a * b;
fixed_point64<0> d(200*100);
WASM_ASSERT(c == d, "fixed_point32 instances multiplication result in fixed_point64");
}
return WASM_TEST_PASS;
}
unsigned int test_fixedpoint::test_division()
{
{
uint64_t lhs = 10000000;
uint64_t rhs = 333;
fixed_point64<0> a(lhs);
fixed_point64<0> b(rhs);
fixed_point128<5> c = a / b;
fixed_point128<5> e = fixed_divide<5>(lhs, rhs);
WASM_ASSERT(c == e, "fixed_point64 instances division result from operator and function and compare in fixed_point128");
}
{
uint32_t lhs = 100000;
uint32_t rhs = 33;
fixed_point32<0> a(lhs);
fixed_point32<0> b(rhs);
fixed_point64<5> c = a / b;
fixed_point64<5> e = fixed_divide<5>(lhs, rhs);
WASM_ASSERT(c == e, "fixed_point64 instances division result from operator and function and compare in fixed_point128");
}
return WASM_TEST_PASS;
}
#include <eoslib/real.hpp>
#include <eoslib/eos.hpp>
#include "test_api.hpp"
using namespace eos;
unsigned int test_real::create_instances() {
real lhs1(5);
WASM_ASSERT(lhs1.value() == 5, "real instance value is wrong");
return WASM_TEST_PASS;
}
unsigned int test_real::test_division() {
real lhs1(5);
real rhs1(10);
real result1 = lhs1 / rhs1;
uint64_t a = double_div(i64_to_double(5), i64_to_double(10));
WASM_ASSERT(a == result1.value(), "real division result is wrong");
return WASM_TEST_PASS;
}
unsigned int test_real::test_multiplication() {
real lhs1(5);
real rhs1(10);
real result1 = lhs1 * rhs1;
uint64_t res = double_mult( 5, 10 );
WASM_ASSERT(res == result1.value(), "real multiplication result is wrong");
return WASM_TEST_PASS;
}
unsigned int test_real::test_addition()
{
real lhs1(5);
real rhs1(10);
real result1 = lhs1 / rhs1;
uint64_t a = double_div(i64_to_double(5), i64_to_double(10));
real lhs2(5);
real rhs2(2);
real result2 = lhs2 / rhs2;
uint64_t b = double_div(i64_to_double(5), i64_to_double(2));
real sum = result1+result2;
uint64_t c = double_add( a, b );
WASM_ASSERT(sum.value() == c, "real addition operation result is wrong");
return WASM_TEST_PASS;
}
......@@ -447,7 +447,20 @@ BOOST_FIXTURE_TEST_CASE(test_all, testing_fixture)
BOOST_CHECK_MESSAGE( CALL_TEST_FUNCTION( TEST_METHOD("test_string", "concatenation_non_null_terminated"), {}, {} ) == WASM_TEST_PASS, "test_string::concatenation_non_null_terminated()" );
BOOST_CHECK_MESSAGE( CALL_TEST_FUNCTION( TEST_METHOD("test_string", "assign"), {}, {} ) == WASM_TEST_PASS, "test_string::assign()" );
BOOST_CHECK_MESSAGE( CALL_TEST_FUNCTION( TEST_METHOD("test_string", "comparison_operator"), {}, {} ) == WASM_TEST_PASS, "test_string::comparison_operator()" );
BOOST_CHECK_MESSAGE( CALL_TEST_FUNCTION( TEST_METHOD("test_fixedpoint", "create_instances"), {}, {} ) == WASM_TEST_PASS, "test_fixedpoint::create_instances()" );
BOOST_CHECK_MESSAGE( CALL_TEST_FUNCTION( TEST_METHOD("test_fixedpoint", "test_addition"), {}, {} ) == WASM_TEST_PASS, "test_fixedpoint::test_addition()" );
BOOST_CHECK_MESSAGE( CALL_TEST_FUNCTION( TEST_METHOD("test_fixedpoint", "test_subtraction"), {}, {} ) == WASM_TEST_PASS, "test_fixedpoint::test_subtraction()" );
BOOST_CHECK_MESSAGE( CALL_TEST_FUNCTION( TEST_METHOD("test_fixedpoint", "test_multiplication"), {}, {} ) == WASM_TEST_PASS, "test_fixedpoint::test_multiplication()" );
BOOST_CHECK_MESSAGE( CALL_TEST_FUNCTION( TEST_METHOD("test_fixedpoint", "test_division"), {}, {} ) == WASM_TEST_PASS, "test_fixedpoint::test_division()" );
BOOST_CHECK_MESSAGE( CALL_TEST_FUNCTION( TEST_METHOD("test_real", "create_instances"), {}, {} ) == WASM_TEST_PASS, "test_real::create_instances()" );
BOOST_CHECK_MESSAGE( CALL_TEST_FUNCTION( TEST_METHOD("test_real", "test_addition"), {}, {} ) == WASM_TEST_PASS, "test_real::test_addition()" );
BOOST_CHECK_MESSAGE( CALL_TEST_FUNCTION( TEST_METHOD("test_real", "test_multiplication"), {}, {} ) == WASM_TEST_PASS, "test_real::test_multiplication()" );
BOOST_CHECK_MESSAGE( CALL_TEST_FUNCTION( TEST_METHOD("test_real", "test_division"), {}, {} ) == WASM_TEST_PASS, "test_real::test_division()" );
CAPTURE(cerr, CALL_TEST_FUNCTION( TEST_METHOD("test_real", "create_instances"), {}, {}) );
CAPTURE(cerr, CALL_TEST_FUNCTION( TEST_METHOD("test_string", "print_null_terminated"), {}, {}) );
BOOST_CHECK_EQUAL( capture.size() , 1);
BOOST_CHECK_EQUAL( capture[0], "Hello World!");
CAPTURE(cerr, CALL_TEST_FUNCTION( TEST_METHOD("test_string", "print_non_null_terminated"), {}, {}) );
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册