Skip to content

Commit

Permalink
nvm: add uint.js test cases;
Browse files Browse the repository at this point in the history
  • Loading branch information
bibibong committed Jun 4, 2018
1 parent 5338b32 commit 2fb2999
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 7 deletions.
13 changes: 10 additions & 3 deletions nf/nvm/engine_v8_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ func newUint128FromIntWrapper(a int64) *util.Uint128 {
}

type testBlock struct {
height uint64
}

// Coinbase mock
Expand All @@ -73,7 +74,7 @@ func (block *testBlock) Hash() byteutils.Hash {

// Height mock
func (block *testBlock) Height() uint64 {
return core.NvmMemoryLimitWithoutInjectHeight
return block.height
}

// RandomSeed mock
Expand Down Expand Up @@ -106,7 +107,12 @@ func (block *testBlock) Timestamp() int64 {
}

func mockBlock() Block {
block := &testBlock{}
block := &testBlock{core.NvmMemoryLimitWithoutInjectHeight}
return block
}

func mockBlockForLib(height uint64) Block {
block := &testBlock{height}
return block
}

Expand Down Expand Up @@ -1751,6 +1757,7 @@ func TestMultiLibVersion(t *testing.T) {
expectedResult string
}{
{"test/test_multi_lib_version_require.js", nil, "\"\""},
{"test/test_uint.js", nil, "\"\""},
}

for _, tt := range tests {
Expand All @@ -1764,7 +1771,7 @@ func TestMultiLibVersion(t *testing.T) {
assert.Nil(t, err)
owner.AddBalance(newUint128FromIntWrapper(1000000000))
contract, _ := context.CreateContractAccount([]byte("account2"), nil, &corepb.ContractMeta{Version: "1.0.1"})
ctx, err := NewContext(mockBlock(), mockTransaction(), contract, context)
ctx, err := NewContext(mockBlockForLib(2000000), mockTransaction(), contract, context)

engine := NewV8Engine(ctx)
engine.SetExecutionLimits(900000, 10000000)
Expand Down
126 changes: 126 additions & 0 deletions nf/nvm/test/test_uint.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
// Copyright (C) 2018 go-nebulas authors
//
// This file is part of the go-nebulas library.
//
// the go-nebulas library is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// the go-nebulas library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with the go-nebulas library. If not, see <http://www.gnu.org/licenses/>.
//

'use strict';

var uint = require('uint.js');
var Uint64 = uint.Uint64;
var Uint128 = uint.Uint128;
var Uint256 = uint.Uint256;
var Uint512 = uint.Uint512;

var err1 = "[Uint64 Error] overflow";
var err2 = '[Uint128 Error] incompatible type';
var err3 = "[Uint64 Error] underflow";
var err4 = '[Uint512 Error] incompatible type';
var err5 = '[Uint256 Error] overflow';
var err6 = '[Uint64 Error] NaN';
var err7 = '[Uint64 Error] not an integer';

var a = new Uint64(100000000000000);
var a1 = new Uint64(100000000000001);
var b = new Uint128(100000000000000);
var c = new Uint256(100000000000000);
var d = new Uint512(100000000000000);


// overflow
try {
a.pow(new Uint64(2));
} catch (e) {
if (e.message !== err1) {
throw e;
}
}
a.pow(new Uint64(1));

try {
c.mul(c).mul(c).mul(c).mul(c).mul(c).mul(c);
} catch (e) {
if (e.message !== err5) {
throw e;
}
}

var bpow2 = b.pow(new Uint128(2));
if (bpow2.toString(10) !== "10000000000000000000000000000") {
throw new Error("b.pow(2) not equal");
}

// incompatible
try {
b.plus(c);
} catch (e) {
if (e.message !== err2) {
throw e;
}
}
b.plus(b);

try {
d.minus(1);
} catch (e) {
if (e.message !== err4) {
throw e;
}
}

// underflow
try {
a.minus(a1);
} catch (e) {
if (e.message !== err3) {
throw e;
}
}
if (a.minus(a).toString(10) !== "0") {
throw new Error("a.minus(a) not 0");
}

// NaN
try {
a.div(null);
} catch (e) {
if (e.message !== err6) {
throw e;
}
}

if (a.div(a).toString(10) !== "1") {
throw new Error("a.div(a) not 1");
}

if (a.mod(a).toString(10) !== "0") {
throw new Error("a.mod(a) not 0");
}

// not an integer
try {
new Uint64(1.2);
} catch (e) {
if (e.message !== err7) {
throw e;
}
}
try {
a.div(new Uint64(0));
} catch (e) {
if (e.message !== err7) {
throw e;
}
}
8 changes: 4 additions & 4 deletions nf/nvm/v8/libjs/1.0.1/uint.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class Uint {
_validate() {
// check integer
if (!this._inner.isInteger()) {
throw new Error('[Uint Error] not a integer');
throw new Error('[Uint' + this._size + ' Error] not an integer');
}

// check negative
Expand All @@ -72,18 +72,18 @@ class Uint {

_checkRightOperand(right) {
if (typeof right === 'undefined' || right == null) {
throw new Error('[Uint Error] NaN');
throw new Error('[Uint' + this._size + ' Error] NaN');
}

if (!right instanceof Uint || this.constructor !== right.constructor) {
throw new Error('[Uint Error] incompatible Uint type');
throw new Error('[Uint' + this._size + ' Error] incompatible type');
}
right._validate();
}

div(o) {
this._checkRightOperand(o);
var r = this._inner.idiv(o._inner);
var r = this._inner.divToInt(o._inner);
return new this.constructor(r, null, this._size);
}

Expand Down

0 comments on commit 2fb2999

Please sign in to comment.