From 353889486051f51c62956c2399002b7ab8bc1bd5 Mon Sep 17 00:00:00 2001 From: Yogi Xun Date: Tue, 5 Jun 2018 10:56:18 +0800 Subject: [PATCH] nvm: add date.js test cases --- nf/nvm/engine_v8_test.go | 1 + nf/nvm/test/test_date_1.0.1.js | 116 +++++++++++++++++++++++++++++++++ nf/nvm/v8/libjs/1.0.1/date.js | 63 +++++++++++++++++- util/uint128_test.go | 14 +++- 4 files changed, 192 insertions(+), 2 deletions(-) create mode 100644 nf/nvm/test/test_date_1.0.1.js diff --git a/nf/nvm/engine_v8_test.go b/nf/nvm/engine_v8_test.go index 14e266ef7..0b6fae04f 100644 --- a/nf/nvm/engine_v8_test.go +++ b/nf/nvm/engine_v8_test.go @@ -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 { diff --git a/nf/nvm/test/test_date_1.0.1.js b/nf/nvm/test/test_date_1.0.1.js new file mode 100644 index 000000000..b2c886356 --- /dev/null +++ b/nf/nvm/test/test_date_1.0.1.js @@ -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 . +// + +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); \ No newline at end of file diff --git a/nf/nvm/v8/libjs/1.0.1/date.js b/nf/nvm/v8/libjs/1.0.1/date.js index 336440080..663d48c53 100644 --- a/nf/nvm/v8/libjs/1.0.1/date.js +++ b/nf/nvm/v8/libjs/1.0.1/date.js @@ -16,4 +16,65 @@ // along with the go-nebulas library. If not, see . // -module.exports = Date; \ No newline at end of file + +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; \ No newline at end of file diff --git a/util/uint128_test.go b/util/uint128_test.go index 4bc6eabac..1f09606d0 100644 --- a/util/uint128_test.go +++ b/util/uint128_test.go @@ -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) {