Skip to content

Commit

Permalink
nvm: add date.js test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
bibibong committed Jun 5, 2018
1 parent 2fb2999 commit 3538894
Show file tree
Hide file tree
Showing 4 changed files with 192 additions and 2 deletions.
1 change: 1 addition & 0 deletions nf/nvm/engine_v8_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1758,6 +1758,7 @@ func TestMultiLibVersion(t *testing.T) {
}{
{"test/test_multi_lib_version_require.js", nil, "\"\""},
{"test/test_uint.js", nil, "\"\""},
{"test/test_date_1.0.1.js", nil, "\"\""},
}

for _, tt := range tests {
Expand Down
116 changes: 116 additions & 0 deletions nf/nvm/test/test_date_1.0.1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
// 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/>.
//

function eq(a, b) {
if (a !== b) {
throw new Error("Not equal: " + a + " <--> " + b);
}
}

Blockchain.blockParse("{\"timestamp\":20000000000,\"seed\":\"\"}");

var date = new Date();
var options = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' };

eq(date.toDateString(), "Tue Oct 11 2603");
eq(Date.UTC(2603, 9, 11, 11, 33, 20), 20000000000000);
eq(date.getTimezoneOffset(), 0);
eq(date.toTimeString(), "11:33:20 GMT+0000 (UTC)");
eq(date.toString(), "Tue Oct 11 2603 11:33:20 GMT+0000 (UTC)");
eq(date.toGMTString(), "Tue, 11 Oct 2603 11:33:20 GMT");
eq(date.toUTCString(), "Tue, 11 Oct 2603 11:33:20 GMT");
eq(date.toISOString(), "2603-10-11T11:33:20.000Z");
eq(date.toJSON(), "2603-10-11T11:33:20.000Z");
eq(date.valueOf(), 20000000000000);

eq(Object.prototype.toLocaleString.call(date), "Tue Oct 11 2603 11:33:20 GMT+0000 (UTC)");
eq(Object.prototype.toLocaleString.call(date, 'ko-KR', { timeZone: 'UTC' }), "Tue Oct 11 2603 11:33:20 GMT+0000 (UTC)");
eq(date.toLocaleString(), "10/11/2603, 11:33:20 AM");
eq(date.toLocaleString('ko-KR', { timeZone: 'UTC' }), "2603. 10. 11. 오전 11:33:20");

eq(date.toLocaleDateString(), "10/11/2603");
eq(date.toLocaleDateString('de-DE', options), "Dienstag, 11. Oktober 2603");

eq(date.toLocaleTimeString(), "11:33:20 AM");
eq(date.toLocaleTimeString('ar-EG'), "١١:٣٣:٢٠ ص");


eq(date.getDate(), date.getUTCDate());
eq(date.getDay(), date.getUTCDay());
eq(date.getFullYear(), date.getUTCFullYear());
eq(date.getHours(), date.getUTCHours());
eq(date.getMilliseconds(), date.getUTCMilliseconds());
eq(date.getMinutes(), date.getUTCMinutes());
eq(date.getMonth(), date.getUTCMonth());
eq(date.getSeconds(), date.getUTCSeconds());

try {
date.getYear();
throw new Error("should not be here.");
} catch(err) {
if (err != "Error: Deprecated!") {
throw err;
}
}
try {
date.setYear(1999);
throw new Error("should not be here.");
} catch(err) {
if (err != "Error: Deprecated!") {
throw err;
}
}

date = new Date('August 19, 2017 23:15:30');
var date2 = new Date('August 19, 2017 23:15:30');

date.setDate(12);
date2.setUTCDate(12);
eq(date - date2, 0);

date.setMonth(1);
date2.setUTCMonth(1);
eq(date - date2, 0);

date.setFullYear(1999);
date2.setUTCFullYear(1999);
eq(date - date2, 0);

date.setHours(22);
date2.setUTCHours(22);
eq(date - date2, 0);

date.setMilliseconds(420);
date2.setUTCMilliseconds(420);
eq(date - date2, 0);

date.setMinutes(12);
date2.setUTCMinutes(12);
eq(date - date2, 0);

date.setSeconds(12);
date2.setUTCSeconds(12);
eq(date - date2, 0);



var d1 = new Date('December 17, 1995 00:00:00');
var d2 = new Date(d1.getUTCFullYear(), d1.getUTCMonth(), d1.getUTCDate());
eq(d1.getTime(), d2.getTime());

eq(Date.parse(Date()), 20000000000000);
63 changes: 62 additions & 1 deletion nf/nvm/v8/libjs/1.0.1/date.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,65 @@
// along with the go-nebulas library. If not, see <http://www.gnu.org/licenses/>.
//

module.exports = Date;

var NebDate = (function(ProtoDate) {

function NebDate() {
if (!Blockchain) {
throw new Error("'Blockchain' is not defined.");
}
if (!Blockchain.block) {
throw new Error("'Blockchain.block' is not defined.");
}

var date = new(Function.prototype.bind.apply(ProtoDate, [ProtoDate].concat(Array.prototype.slice.call(arguments))))();
if (arguments.length == 0) {
// unit of timestamp is second
date.setTime(Blockchain.block.timestamp * 1000);
}
Object.setPrototypeOf(date, NebDate.prototype);
return date;
}
NebDate.now = function() {
return new NebDate().getTime();
}
NebDate.UTC = function() {
return ProtoDate.UTC.apply(null, Array.prototype.slice.call(arguments));
}
NebDate.parse = function(dateString) {
return ProtoDate.parse(dateString);
}

NebDate.prototype.getYear = function() {
throw new Error("Deprecated!");
}
NebDate.prototype.setYear = function() {
throw new Error("Deprecated!");
}

NebDate.prototype.toLocaleDateString = function() {
var tmp = new ProtoDate.prototype.constructor(this.getTime());
return ProtoDate.prototype.toLocaleDateString.apply(tmp, Array.prototype.slice.call(arguments));
}

NebDate.prototype.toLocaleTimeString = function() {
var tmp = new ProtoDate.prototype.constructor(this.getTime());
return ProtoDate.prototype.toLocaleTimeString.apply(tmp, Array.prototype.slice.call(arguments));
}

NebDate.prototype.toLocaleString = function() {
var tmp = new ProtoDate.prototype.constructor(this.getTime());
return ProtoDate.prototype.toLocaleString.apply(tmp, Array.prototype.slice.call(arguments));
}

NebDate.prototype = new Proxy(NebDate.prototype, {
getPrototypeOf: function(target) {
throw new Error("Unsupported method!");
},
});

Object.setPrototypeOf(NebDate.prototype, ProtoDate.prototype);
return NebDate;
})(Date);

module.exports = NebDate;
14 changes: 13 additions & 1 deletion util/uint128_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,21 @@ const (
)

func TestMax(t *testing.T) {
bigMaxUint64 := &big.Int{}
bigMaxUint64.SetString(strings.Repeat("f", 16), 16)
fmt.Println(strings.Repeat("f", 16), bigMaxUint64.String())

bigMaxUint128 := &big.Int{}
bigMaxUint128.SetString(strings.Repeat("f", 32), 16)
fmt.Println(bigMaxUint128.String())
fmt.Println(strings.Repeat("f", 32), bigMaxUint128.String())

bigMaxUint256 := &big.Int{}
bigMaxUint256.SetString(strings.Repeat("f", 64), 16)
fmt.Println(strings.Repeat("f", 64), bigMaxUint256.String())

bigMaxUint512 := &big.Int{}
bigMaxUint512.SetString(strings.Repeat("f", 128), 16)
fmt.Println(strings.Repeat("f", 128), bigMaxUint512.String())
}

func TestUint128(t *testing.T) {
Expand Down

0 comments on commit 3538894

Please sign in to comment.