Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
YottaChain
YTBP
提交
e730d29b
Y
YTBP
项目概览
YottaChain
/
YTBP
通知
0
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
Y
YTBP
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
e730d29b
编写于
11月 01, 2017
作者:
D
Dhanesh Valappil
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
STAT-110,87 - fixed_point and real class
上级
3449a9b7
变更
7
展开全部
隐藏空白更改
内联
并排
Showing
7 changed file
with
491 addition
and
138 deletion
+491
-138
contracts/eoslib/fixedpoint.hpp
contracts/eoslib/fixedpoint.hpp
+147
-138
contracts/eoslib/real.hpp
contracts/eoslib/real.hpp
+113
-0
contracts/test_api/test_api.cpp
contracts/test_api/test_api.cpp
+15
-0
contracts/test_api/test_api.hpp
contracts/test_api/test_api.hpp
+15
-0
contracts/test_api/test_fixedpoint.cpp
contracts/test_api/test_fixedpoint.cpp
+137
-0
contracts/test_api/test_real.cpp
contracts/test_api/test_real.cpp
+51
-0
tests/api_tests/api_tests.cpp
tests/api_tests/api_tests.cpp
+13
-0
未找到文件。
contracts/eoslib/fixedpoint.hpp
浏览文件 @
e730d29b
此差异已折叠。
点击以展开。
contracts/eoslib/real.hpp
0 → 100644
浏览文件 @
e730d29b
#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
);
}
}
contracts/test_api/test_api.cpp
浏览文件 @
e730d29b
...
...
@@ -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"
...
...
@@ -110,6 +112,19 @@ extern "C" {
WASM_TEST_HANDLER
(
test_string
,
print_non_null_terminated
);
WASM_TEST_HANDLER
(
test_string
,
print_unicode
);
// 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
;
}
...
...
contracts/test_api/test_api.hpp
浏览文件 @
e730d29b
...
...
@@ -148,3 +148,18 @@ struct test_string {
static
unsigned
int
print_non_null_terminated
();
static
unsigned
int
print_unicode
();
};
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
contracts/test_api/test_fixedpoint.cpp
0 → 100644
浏览文件 @
e730d29b
#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
;
}
contracts/test_api/test_real.cpp
0 → 100644
浏览文件 @
e730d29b
#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
;
}
tests/api_tests/api_tests.cpp
浏览文件 @
e730d29b
...
...
@@ -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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录