From f888b79a9e30591719a7f83b5edc7c56a87c9430 Mon Sep 17 00:00:00 2001 From: Frank Tang Date: Mon, 19 Jul 2021 14:59:43 -0700 Subject: [PATCH 1/2] Add tests for Temporal.Duration.p*.negated --- .../Duration/prototype/negated/simple.js | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 test/built-ins/Temporal/Duration/prototype/negated/simple.js diff --git a/test/built-ins/Temporal/Duration/prototype/negated/simple.js b/test/built-ins/Temporal/Duration/prototype/negated/simple.js new file mode 100644 index 00000000000..ae484b4ddf1 --- /dev/null +++ b/test/built-ins/Temporal/Duration/prototype/negated/simple.js @@ -0,0 +1,54 @@ +// Copyright (C) 2021 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-temporal.duration.prototype.negated +description: Temporal.Duration.prototype.negated will return negated value of the input duration. +info: | + 1. Let duration be the this value. + 2. Perform ? RequireInternalSlot(duration, [[InitializedTemporalDuration]]). + 3. Return ? CreateTemporalDuration(abs(duration.[[Years]]), abs(duration.[[Months]]), abs(duration.[[Weeks]]), abs(duration.[[Days]]), abs(duration.[[Hours]]), abs(duration.[[Minutes]]), abs(duration.[[Seconds]]), abs(duration.[[Milliseconds]]), abs(duration.[[Microseconds]]), abs(duration.[[Nanoseconds]])). +features: [Temporal] +---*/ + +function assertDuration(duration, years, months, weeks, days, hours, + minutes, seconds, milliseconds, microseconds, nanoseconds, sign, blank) { + assert.sameValue(years, duration.years, duration); + assert.sameValue(months, duration.months, duration); + assert.sameValue(weeks, duration.weeks, duration); + assert.sameValue(days, duration.days, duration); + assert.sameValue(hours, duration.hours, duration); + assert.sameValue(minutes, duration.minutes, duration); + assert.sameValue(seconds, duration.seconds, duration); + assert.sameValue(milliseconds, duration.milliseconds, duration); + assert.sameValue(microseconds, duration.microseconds, duration); + assert.sameValue(nanoseconds, duration.nanoseconds, duration); + assert.sameValue(sign, duration.sign, duration); + assert.sameValue(blank, duration.blank, duration); +} +let d1 = new Temporal.Duration(); +assertDuration(d1.negated(), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, true); + +let d2 = new Temporal.Duration(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); +assertDuration(d2.negated(), -1, -2, -3, -4, -5, -6, -7, -8, -9, -10, -1, false); + +// Test large number +let d3 = new Temporal.Duration(1e5, 2e5, 3e5, 4e5, 5e5, 6e5, 7e5, 8e5, 9e5, 10e5); +assertDuration(d3.negated(), -1e5, -2e5, -3e5, -4e5, -5e5, -6e5, -7e5, -8e5, -9e5, -10e5, -1, false); + + +// Test negative values +let d4 = new Temporal.Duration(-1, -2, -3, -4, -5, -6, -7, -8, -9, -10); +assertDuration(d4.negated(), 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, false); + +let d5 = new Temporal.Duration(-1e5, -2e5, -3e5, -4e5, -5e5, -6e5, -7e5, -8e5, -9e5, -10e5); +assertDuration(d5.negated(), 1e5, 2e5, 3e5, 4e5, 5e5, 6e5, 7e5, 8e5, 9e5, 10e5, 1, false); + +let d6 = new Temporal.Duration(1, 0, 3, 0, 5, 0, 7, 0, 9, 0); +assertDuration(d6.negated(), -1, 0, -3, 0, -5, 0, -7, 0, -9, 0, -1, false); + +let d7 = new Temporal.Duration(-1, 0, -3, 0, -5, 0, -7, 0, -9, 0); +assertDuration(d7.negated(), 1, 0, 3, 0, 5, 0, 7, 0, 9, 0, 1, false); + +let d8 = new Temporal.Duration(0, -2, 0, -4, 0, -6, 0, -8, 0, -10); +assertDuration(d8.negated(), 0, 2, 0, 4, 0, 6, 0, 8, 0, 10, 1, false); From 01cfde766ea35367db9f733699f6f37dc193e6ad Mon Sep 17 00:00:00 2001 From: Frank Tang Date: Thu, 5 Aug 2021 19:02:18 -0700 Subject: [PATCH 2/2] Add test --- .../Duration/prototype/negated/simple.js | 46 ++++++++----------- .../throw-type-error-RequireInternalSlot.js | 15 ++++++ 2 files changed, 34 insertions(+), 27 deletions(-) create mode 100644 test/built-ins/Temporal/Duration/prototype/negated/throw-type-error-RequireInternalSlot.js diff --git a/test/built-ins/Temporal/Duration/prototype/negated/simple.js b/test/built-ins/Temporal/Duration/prototype/negated/simple.js index ae484b4ddf1..5ed477f3ad3 100644 --- a/test/built-ins/Temporal/Duration/prototype/negated/simple.js +++ b/test/built-ins/Temporal/Duration/prototype/negated/simple.js @@ -1,54 +1,46 @@ // Copyright (C) 2021 the V8 project authors. All rights reserved. // This code is governed by the BSD license found in the LICENSE file. - /*--- esid: sec-temporal.duration.prototype.negated description: Temporal.Duration.prototype.negated will return negated value of the input duration. info: | - 1. Let duration be the this value. - 2. Perform ? RequireInternalSlot(duration, [[InitializedTemporalDuration]]). 3. Return ? CreateTemporalDuration(abs(duration.[[Years]]), abs(duration.[[Months]]), abs(duration.[[Weeks]]), abs(duration.[[Days]]), abs(duration.[[Hours]]), abs(duration.[[Minutes]]), abs(duration.[[Seconds]]), abs(duration.[[Milliseconds]]), abs(duration.[[Microseconds]]), abs(duration.[[Nanoseconds]])). features: [Temporal] +includes: [temporalHelpers.js] ---*/ - -function assertDuration(duration, years, months, weeks, days, hours, - minutes, seconds, milliseconds, microseconds, nanoseconds, sign, blank) { - assert.sameValue(years, duration.years, duration); - assert.sameValue(months, duration.months, duration); - assert.sameValue(weeks, duration.weeks, duration); - assert.sameValue(days, duration.days, duration); - assert.sameValue(hours, duration.hours, duration); - assert.sameValue(minutes, duration.minutes, duration); - assert.sameValue(seconds, duration.seconds, duration); - assert.sameValue(milliseconds, duration.milliseconds, duration); - assert.sameValue(microseconds, duration.microseconds, duration); - assert.sameValue(nanoseconds, duration.nanoseconds, duration); - assert.sameValue(sign, duration.sign, duration); - assert.sameValue(blank, duration.blank, duration); -} let d1 = new Temporal.Duration(); -assertDuration(d1.negated(), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, true); +TemporalHelpers.assertDuration( + d1.negated(), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "zeros"); let d2 = new Temporal.Duration(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); -assertDuration(d2.negated(), -1, -2, -3, -4, -5, -6, -7, -8, -9, -10, -1, false); +TemporalHelpers.assertDuration( + d2.negated(), -1, -2, -3, -4, -5, -6, -7, -8, -9, -10, "positive values"); // Test large number let d3 = new Temporal.Duration(1e5, 2e5, 3e5, 4e5, 5e5, 6e5, 7e5, 8e5, 9e5, 10e5); -assertDuration(d3.negated(), -1e5, -2e5, -3e5, -4e5, -5e5, -6e5, -7e5, -8e5, -9e5, -10e5, -1, false); +TemporalHelpers.assertDuration( + d3.negated(), -1e5, -2e5, -3e5, -4e5, -5e5, -6e5, -7e5, -8e5, -9e5, -10e5, + "large positive values"); // Test negative values let d4 = new Temporal.Duration(-1, -2, -3, -4, -5, -6, -7, -8, -9, -10); -assertDuration(d4.negated(), 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, false); +TemporalHelpers.assertDuration( + d4.negated(), 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, "negative values"); let d5 = new Temporal.Duration(-1e5, -2e5, -3e5, -4e5, -5e5, -6e5, -7e5, -8e5, -9e5, -10e5); -assertDuration(d5.negated(), 1e5, 2e5, 3e5, 4e5, 5e5, 6e5, 7e5, 8e5, 9e5, 10e5, 1, false); +TemporalHelpers.assertDuration( + d5.negated(), 1e5, 2e5, 3e5, 4e5, 5e5, 6e5, 7e5, 8e5, 9e5, 10e5, + "large negative values"); let d6 = new Temporal.Duration(1, 0, 3, 0, 5, 0, 7, 0, 9, 0); -assertDuration(d6.negated(), -1, 0, -3, 0, -5, 0, -7, 0, -9, 0, -1, false); +TemporalHelpers.assertDuration( + d6.negated(), -1, 0, -3, 0, -5, 0, -7, 0, -9, 0, "some zeros"); let d7 = new Temporal.Duration(-1, 0, -3, 0, -5, 0, -7, 0, -9, 0); -assertDuration(d7.negated(), 1, 0, 3, 0, 5, 0, 7, 0, 9, 0, 1, false); +TemporalHelpers.assertDuration( + d7.negated(), 1, 0, 3, 0, 5, 0, 7, 0, 9, 0, "some zeros with negative values"); let d8 = new Temporal.Duration(0, -2, 0, -4, 0, -6, 0, -8, 0, -10); -assertDuration(d8.negated(), 0, 2, 0, 4, 0, 6, 0, 8, 0, 10, 1, false); +TemporalHelpers.assertDuration( + d8.negated(), 0, 2, 0, 4, 0, 6, 0, 8, 0, 10, "other zeros with negative values"); diff --git a/test/built-ins/Temporal/Duration/prototype/negated/throw-type-error-RequireInternalSlot.js b/test/built-ins/Temporal/Duration/prototype/negated/throw-type-error-RequireInternalSlot.js new file mode 100644 index 00000000000..7c395ed110f --- /dev/null +++ b/test/built-ins/Temporal/Duration/prototype/negated/throw-type-error-RequireInternalSlot.js @@ -0,0 +1,15 @@ +// Copyright (C) 2021 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-temporal.duration.prototype.negated +description: Temporal.Duration.prototype.negated throws TypeError on + RequireInternalSlot if object has no internal slot. +info: | + 2. Perform ? RequireInternalSlot(duration, [[InitializedTemporalDuration]]). +features: [Temporal] +---*/ +let dur = new Temporal.Duration(1,2,3,4,5); + +let badDur = { negated: dur.negated } +assert.throws(TypeError, () => badDur.negated(), + "Throw TypeError if there are no internal slot");