Commit 7d729e85 authored by Frank Tang's avatar Frank Tang Committed by V8 LUCI CQ

[Temporal] Add some tests for Instant

Land some of the tests for Temporal.Instant
All marked as FAIL at this stage.

Bug: v8:11544
Change-Id: I79d14df47248c708e5d73a0e00e3f7973c521d16
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3086903
Commit-Queue: Frank Tang <ftang@chromium.org>
Reviewed-by: 's avatarShu-yu Guo <syg@chromium.org>
Cr-Commit-Position: refs/heads/main@{#76550}
parent 52488575
// Copyright 2021 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Flags: --harmony-temporal
let i1 = new Temporal.Instant(50000n);
assertEquals(3052001n,
i1.add(new Temporal.Duration(0,0,0,0,0,0,0,3,2,1)).epochNanoseconds);
assertEquals(BigInt(4 * 1e9) + 3052001n,
i1.add(new Temporal.Duration(0,0,0,0,0,0,4,3,2,1)).epochNanoseconds);
assertEquals(BigInt(5 * 60 + 4) * 1000000000n + 3052001n,
i1.add(new Temporal.Duration(0,0,0,0,0,5,4,3,2,1)).epochNanoseconds);
assertEquals(BigInt(6 * 3600 + 5 * 60 + 4) * 1000000000n + 3052001n,
i1.add(new Temporal.Duration(0,0,0,0,6,5,4,3,2,1)).epochNanoseconds);
assertEquals(-2952001n,
i1.add(new Temporal.Duration(0,0,0,0,0,0,0,-3,-2,-1)).epochNanoseconds);
assertEquals(BigInt(-4 * 1e9) - 2952001n,
i1.add(new Temporal.Duration(0,0,0,0,0,0,-4,-3,-2,-1)).epochNanoseconds);
assertEquals(BigInt(5 * 60 + 4) * -1000000000n - 2952001n,
i1.add(new Temporal.Duration(0,0,0,0,0,-5,-4,-3,-2,-1)).epochNanoseconds);
assertEquals(BigInt(6 * 3600 + 5 * 60 + 4) * -1000000000n - 2952001n,
i1.add(new Temporal.Duration(0,0,0,0,-6,-5,-4,-3,-2,-1)).epochNanoseconds);
// Test RequireInternalSlot Throw TypeError
let badInstant = { add: i1.add };
assertThrows(() => badInstant.add(new Temporal.Duration(0, 0, 0, 0, 5)), TypeError);
// Test ToLimitedTemporalDuration Throw RangeError
assertThrows(() => i1.add(new Temporal.Duration(1)), RangeError);
assertThrows(() => i1.add(new Temporal.Duration(0, 2)), RangeError);
assertThrows(() => i1.add(new Temporal.Duration(0, 0, 3)), RangeError);
assertThrows(() => i1.add(new Temporal.Duration(0, 0, 0, 4)), RangeError);
assertThrows(() => i1.add(new Temporal.Duration(-1)), RangeError);
assertThrows(() => i1.add(new Temporal.Duration(0, -2)), RangeError);
assertThrows(() => i1.add(new Temporal.Duration(0, 0, -3)), RangeError);
assertThrows(() => i1.add(new Temporal.Duration(0, 0, 0, -4)), RangeError);
// Test AddInstant Throw RangeError
let i2 = new Temporal.Instant(86400n * 99999999999999999n);
assertThrows(() => i2.add(new Temporal.Duration(0, 0, 0, 0, 999999999)), RangeError);
// Copyright 2021 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Flags: --harmony-temporal
let inst1 = new Temporal.Instant(1234567890123456789n);
let inst2 = new Temporal.Instant(1234567890123456000n);
let inst3 = new Temporal.Instant(1234567890123456000n);
assertEquals(Temporal.Instant.compare(inst2, inst3), 0);
assertEquals(Temporal.Instant.compare(inst1, inst2), 1);
assertEquals(Temporal.Instant.compare(inst3, inst1), -1);
assertThrows(() => Temporal.Instant.compare(inst1, "invalid iso8601 string"),
RangeError);
assertThrows(() => Temporal.Instant.compare("invalid iso8601 string", inst1),
RangeError);
// TODO Test Temporal.compare with Temporal.ZonedDateTime object
// TODO Test Temporal.compare with TemporalInstantString
// Copyright 2021 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Flags: --harmony-temporal
let inst1 = new Temporal.Instant(1234567890123456789n);
assertEquals(1234567890123456789n , inst1.epochNanoseconds);
assertEquals(1234567890123456n , inst1.epochMicroseconds);
assertEquals(1234567890123n , inst1.epochMilliseconds);
assertEquals(1234567890n , inst1.epochSeconds);
let inst2 = new Temporal.Instant(-1234567890123456789n);
assertEquals(-1234567890123456789n , inst2.epochNanoseconds);
assertEquals(-1234567890123456n , inst2.epochMicroseconds);
assertEquals(-1234567890123n , inst2.epochMilliseconds);
assertEquals(-1234567890n , inst2.epochSeconds);
// 1. If NewTarget is undefined, then
// a. Throw a TypeError exception.
assertThrows(() => Temporal.Instant(1234567890123456789n), TypeError);
// 2. Let epochNanoseconds be ? ToBigInt(epochNanoseconds).
assertThrows(() => {let inst = new Temporal.Instant(undefined)},
TypeError);
assertThrows(() => {let inst = new Temporal.Instant(null)}, TypeError);
assertEquals(1n, (new Temporal.Instant(true)).epochNanoseconds);
assertEquals(0n, (new Temporal.Instant(false)).epochNanoseconds);
assertThrows(() => {let inst = Temporal.Instant(12345)}, TypeError);
assertEquals(1234567890123456789n,
(new Temporal.Instant("1234567890123456789")).epochNanoseconds);
assertThrows(() => {let inst = new Temporal.Instant(Symbol(12345n))},
TypeError);
// 3. If ! IsValidEpochNanoseconds(epochNanoseconds) is false,
// throw a RangeError exception.
assertThrows(() => {let inst = new Temporal.Instant(8640000000000000000001n)},
RangeError);
assertThrows(() => {let inst = new Temporal.Instant(-8640000000000000000001n)},
RangeError);
assertEquals(8640000000000000000000n,
(new Temporal.Instant(8640000000000000000000n)).epochNanoseconds);
assertEquals(-8640000000000000000000n,
(new Temporal.Instant(-8640000000000000000000n)).epochNanoseconds);
// Copyright 2021 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Flags: --harmony-temporal
let inst1 = new Temporal.Instant(1234567890123456789n);
let inst2 = new Temporal.Instant(1234567890123456000n);
let inst3 = new Temporal.Instant(1234567890123456000n);
assertEquals(inst1.equals(inst2), false);
assertEquals(inst2.equals(inst3), true);
let badInst = {equals: inst1.equals};
assertThrows(() => badInst.equals(inst1), TypeError);
// TODO Test Temporal.compare with Temporal.ZonedDateTime object
// TODO Test Temporal.compare with TemporalInstantString
// Copyright 2021 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Flags: --harmony-temporal
let bigint_nano = 1234567890123456789000n;
let bigint_micro = 1234567890123456789n;
let inst1 = new Temporal.Instant(bigint_nano);
let inst2 = Temporal.Instant.fromEpochMicroseconds(bigint_micro);
assertEquals(inst1, inst2);
let just_fit_neg_bigint = -8640000000000000000n;
let just_fit_pos_bigint = 8640000000000000000n;
let too_big_bigint = 8640000000000000001n;
let too_small_bigint = -8640000000000000001n;
assertThrows(() =>
{let inst = Temporal.Instant.fromEpochMicroseconds(too_small_bigint)},
RangeError);
assertThrows(() =>
{let inst = Temporal.Instant.fromEpochMicroseconds(too_big_bigint)},
RangeError);
assertEquals(just_fit_neg_bigint,
(Temporal.Instant.fromEpochMicroseconds(
just_fit_neg_bigint)).epochMicroseconds);
assertEquals(just_fit_pos_bigint,
(Temporal.Instant.fromEpochMicroseconds(
just_fit_pos_bigint)).epochMicroseconds);
// Copyright 2021 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Flags: --harmony-temporal
let bigint_nano = 567890123456789000000n;
let bigint_milli = 567890123456789n;
let inst1 = new Temporal.Instant(bigint_nano);
let inst2 = Temporal.Instant.fromEpochMilliseconds(bigint_milli);
assertEquals(inst1, inst2);
let just_fit_neg_bigint = -8640000000000000n;
let just_fit_pos_bigint = 8640000000000000n;
let too_big_bigint = 8640000000000001n;
let too_small_bigint = -8640000000000001n;
assertThrows(() =>
{let inst = Temporal.Instant.fromEpochMilliseconds(too_small_bigint)},
RangeError);
assertThrows(() =>
{let inst = Temporal.Instant.fromEpochMilliseconds(too_big_bigint)},
RangeError);
assertEquals(just_fit_neg_bigint,
(Temporal.Instant.fromEpochMilliseconds(
just_fit_neg_bigint)).epochMilliseconds);
assertEquals(just_fit_pos_bigint,
(Temporal.Instant.fromEpochMilliseconds(
just_fit_pos_bigint)).epochMilliseconds);
// Copyright 2021 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Flags: --harmony-temporal
let bigint1 = 1234567890123456789n;
let inst1 = new Temporal.Instant(bigint1);
let inst2 = Temporal.Instant.fromEpochNanoseconds(bigint1);
assertEquals(inst1, inst2);
let just_fit_neg_bigint = -8640000000000000000000n;
let just_fit_pos_bigint = 8640000000000000000000n;
let too_big_bigint = 8640000000000000000001n;
let too_small_bigint = -8640000000000000000001n;
assertThrows(() =>
{let inst = Temporal.Instant.fromEpochNanoseconds(too_small_bigint)},
RangeError);
assertThrows(() =>
{let inst = Temporal.Instant.fromEpochNanoseconds(too_big_bigint)},
RangeError);
assertEquals(just_fit_neg_bigint,
(Temporal.Instant.fromEpochNanoseconds(
just_fit_neg_bigint)).epochNanoseconds);
assertEquals(just_fit_pos_bigint,
(Temporal.Instant.fromEpochNanoseconds(
just_fit_pos_bigint)).epochNanoseconds);
// Copyright 2021 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Flags: --harmony-temporal
let bigint_nano = 7890123456789000000000n;
let bigint_sec = 7890123456789n;
let inst1 = new Temporal.Instant(bigint_nano);
let inst2 = Temporal.Instant.fromEpochSeconds(bigint_sec);
assertEquals(inst1, inst2);
let just_fit_neg_bigint = -8640000000000n;
let just_fit_pos_bigint = 8640000000000n;
let too_big_bigint = 8640000000001n;
let too_small_bigint = -8640000000001n;
assertThrows(() =>
{let inst = Temporal.Instant.fromEpochSeconds(too_small_bigint)},
RangeError)
assertThrows(() =>
{let inst = Temporal.Instant.fromEpochSeconds(too_big_bigint)},
RangeError)
assertEquals(just_fit_neg_bigint,
(Temporal.Instant.fromEpochSeconds(just_fit_neg_bigint)).epochSeconds);
assertEquals(just_fit_pos_bigint,
(Temporal.Instant.fromEpochSeconds(just_fit_pos_bigint)).epochSeconds);
// Copyright 2021 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Flags: --harmony-temporal
let i1 = new Temporal.Instant(50000n);
assertEquals(3052001n,
i1.subtract(new Temporal.Duration(
0,0,0,0,0,0,0,-3,-2,-1)).epochNanoseconds);
assertEquals(BigInt(4 * 1e9) + 3052001n,
i1.subtract(new Temporal.Duration(
0,0,0,0,0,0,-4,-3,-2,-1)).epochNanoseconds);
assertEquals(BigInt(5 * 60 + 4) * 1000000000n + 3052001n,
i1.subtract(new Temporal.Duration(
0,0,0,0,0,-5,-4,-3,-2,-1)).epochNanoseconds);
assertEquals(BigInt(6 * 3600 + 5 * 60 + 4) * 1000000000n + 3052001n,
i1.subtract(new Temporal.Duration(
0,0,0,0,-6,-5,-4,-3,-2,-1)).epochNanoseconds);
assertEquals(-2952001n,
i1.subtract(new Temporal.Duration(0,0,0,0,0,0,0,3,2,1)).epochNanoseconds);
assertEquals(BigInt(-4 * 1e9) - 2952001n,
i1.subtract(new Temporal.Duration(0,0,0,0,0,0,4,3,2,1)).epochNanoseconds);
assertEquals(BigInt(5 * 60 + 4) * -1000000000n - 2952001n,
i1.subtract(new Temporal.Duration(0,0,0,0,0,5,4,3,2,1)).epochNanoseconds);
assertEquals(BigInt(6 * 3600 + 5 * 60 + 4) * -1000000000n - 2952001n,
i1.subtract(new Temporal.Duration(0,0,0,0,6,5,4,3,2,1)).epochNanoseconds);
// Test RequireInternalSlot Throw TypeError
let badInstant = { subtract: i1.subtract };
assertThrows(() => badInstant.subtract(
new Temporal.Duration(0, 0, 0, 0, 5)), TypeError);
// Test ToLimitedTemporalDuration Throw RangeError
assertThrows(() => i1.subtract(new Temporal.Duration(1)), RangeError);
assertThrows(() => i1.subtract(new Temporal.Duration(0, 2)), RangeError);
assertThrows(() => i1.subtract(new Temporal.Duration(0, 0, 3)), RangeError);
assertThrows(() => i1.subtract(new Temporal.Duration(0, 0, 0, 4)), RangeError);
assertThrows(() => i1.subtract(new Temporal.Duration(-1)), RangeError);
assertThrows(() => i1.subtract(new Temporal.Duration(0, -2)), RangeError);
assertThrows(() => i1.subtract(new Temporal.Duration(0, 0, -3)), RangeError);
assertThrows(() => i1.subtract(new Temporal.Duration(0, 0, 0, -4)), RangeError);
// Test AddInstant Throw RangeError
let i2 = new Temporal.Instant(-86400n * 99999999999999999n);
assertThrows(() => i2.subtract(new Temporal.Duration(0, 0, 0, 0, 999999999)),
RangeError);
// Copyright 2021 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Flags: --harmony-temporal
assertEquals("1970-01-01T00:00:00Z",
(Temporal.Instant.fromEpochSeconds(0n)).toJSON());
let days_in_sec = 24n * 60n * 60n;
assertEquals("1970-12-31T23:59:59Z",
Temporal.Instant.fromEpochSeconds((365n * days_in_sec) - 1n).toJSON());
assertEquals("1971-01-01T00:00:00Z",
Temporal.Instant.fromEpochSeconds((365n * days_in_sec)).toJSON());
assertEquals("1971-12-31T23:59:59Z",
Temporal.Instant.fromEpochSeconds((2n *365n * days_in_sec - 1n)).toJSON());
assertEquals("1972-01-01T00:00:00Z",
Temporal.Instant.fromEpochSeconds((2n *365n * days_in_sec)).toJSON());
// 1972 is a leap year
assertEquals("1972-02-28T00:00:00Z",
Temporal.Instant.fromEpochSeconds(((2n *365n + 58n) * days_in_sec)).toJSON());
assertEquals("1972-02-29T00:00:00Z",
Temporal.Instant.fromEpochSeconds(((2n *365n + 59n) * days_in_sec)).toJSON());
assertEquals("1985-01-01T00:00:00Z",
Temporal.Instant.fromEpochSeconds(((15n *365n + 4n) * days_in_sec)).toJSON());
// Test with Date
const year_in_sec = 24*60*60*365;
const number_of_random_test = 500;
for (i = 0; i < number_of_random_test ; i++) {
// bertween -5000 years and +5000 years
let ms = Math.floor(Math.random() * year_in_sec * 1000 * 10000) - year_in_sec * 1000 * 5000;
// Temporal auto precision will remove trailing zeros in milliseconds so we only
// compare the first 19 char- to second.
let d = new Date(ms)
let bigd = BigInt(d)
dateout = d.toJSON().substr(0,19);
temporalout = Temporal.Instant.fromEpochMilliseconds(bigd).toJSON().substr(0, 19);
if (dateout[0] != '0') {
assertEquals(dateout, temporalout, ms);
}
}
// Copyright 2021 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Flags: --harmony-temporal
let d = new Temporal.Instant(0n);
assertEquals("1970-01-01T00:00:00Z", d.toJSON());
d = new Temporal.Instant(1n);
assertEquals("1970-01-01T00:00:00.000000001Z", d.toJSON());
d = new Temporal.Instant(12n);
assertEquals("1970-01-01T00:00:00.000000012Z", d.toJSON());
d = new Temporal.Instant(123n);
assertEquals("1970-01-01T00:00:00.000000123Z", d.toJSON());
d = new Temporal.Instant(1234n);
assertEquals("1970-01-01T00:00:00.000001234Z", d.toJSON());
d = new Temporal.Instant(12345n);
assertEquals("1970-01-01T00:00:00.000012345Z", d.toJSON());
d = new Temporal.Instant(123456n);
assertEquals("1970-01-01T00:00:00.000123456Z", d.toJSON());
d = new Temporal.Instant(1234567n);
assertEquals("1970-01-01T00:00:00.001234567Z", d.toJSON());
d = new Temporal.Instant(12345678n);
assertEquals("1970-01-01T00:00:00.012345678Z", d.toJSON());
d = new Temporal.Instant(123456789n);
assertEquals("1970-01-01T00:00:00.123456789Z", d.toJSON());
d = new Temporal.Instant(1234567891n);
assertEquals("1970-01-01T00:00:01.234567891Z", d.toJSON());
d = new Temporal.Instant(12345678912n);
assertEquals("1970-01-01T00:00:12.345678912Z", d.toJSON());
d = new Temporal.Instant(-1n);
assertEquals("1969-12-31T23:59:59.999999999Z", d.toJSON());
d = new Temporal.Instant(-12n);
assertEquals("1969-12-31T23:59:59.999999988Z", d.toJSON());
d = new Temporal.Instant(-123n);
assertEquals("1969-12-31T23:59:59.999999877Z", d.toJSON());
d = new Temporal.Instant(-1234n);
assertEquals("1969-12-31T23:59:59.999998766Z", d.toJSON());
d = new Temporal.Instant(-12345n);
assertEquals("1969-12-31T23:59:59.999987655Z", d.toJSON());
d = new Temporal.Instant(-123456n);
assertEquals("1969-12-31T23:59:59.999876544Z", d.toJSON());
d = new Temporal.Instant(-1234567n);
assertEquals("1969-12-31T23:59:59.998765433Z", d.toJSON());
d = new Temporal.Instant(-12345678n);
assertEquals("1969-12-31T23:59:59.987654322Z", d.toJSON());
d = new Temporal.Instant(-123456789n);
assertEquals("1969-12-31T23:59:59.876543211Z", d.toJSON());
d = new Temporal.Instant(-1234567891n);
assertEquals("1969-12-31T23:59:58.765432109Z", d.toJSON());
d = new Temporal.Instant(-12345678912n);
assertEquals("1969-12-31T23:59:47.654321088Z", d.toJSON());
// Copyright 2021 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Flags: --harmony-temporal
let d1 = Temporal.Now.instant( );
assertThrows(() => d1.valueOf(), TypeError);
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment