| 1 |
// |reftest| shell-option(--enable-math-sumprecise) skip-if(!Math.sumPrecise||!xulRuntime.shell) -- Math.sumPrecise is not enabled unconditionally, requires shell-options |
| 2 |
// Copyright (C) 2024 Kevin Gibbons. All rights reserved. |
| 3 |
// This code is governed by the BSD license found in the LICENSE file. |
| 4 |
|
| 5 |
/*--- |
| 6 |
esid: sec-math.sumprecise |
| 7 |
description: Math.sumPrecise throws and closes the iterator if any element is not a Number |
| 8 |
features: [Math.sumPrecise] |
| 9 |
---*/ |
| 10 |
|
| 11 |
assert.throws(TypeError, function () { |
| 12 |
Math.sumPrecise([{}]); |
| 13 |
}); |
| 14 |
|
| 15 |
assert.throws(TypeError, function () { |
| 16 |
Math.sumPrecise([0n]); |
| 17 |
}); |
| 18 |
|
| 19 |
|
| 20 |
var coercions = 0; |
| 21 |
var objectWithValueOf = { |
| 22 |
valueOf: function() { |
| 23 |
++coercions; |
| 24 |
throw new Test262Error("valueOf should not be called"); |
| 25 |
}, |
| 26 |
toString: function() { |
| 27 |
++coercions; |
| 28 |
throw new Test262Error("toString should not be called"); |
| 29 |
} |
| 30 |
}; |
| 31 |
|
| 32 |
assert.throws(TypeError, function () { |
| 33 |
Math.sumPrecise([objectWithValueOf]); |
| 34 |
}); |
| 35 |
assert.sameValue(coercions, 0); |
| 36 |
|
| 37 |
assert.throws(TypeError, function () { |
| 38 |
Math.sumPrecise([objectWithValueOf, NaN]); |
| 39 |
}); |
| 40 |
assert.sameValue(coercions, 0); |
| 41 |
|
| 42 |
assert.throws(TypeError, function () { |
| 43 |
Math.sumPrecise([NaN, objectWithValueOf]); |
| 44 |
}); |
| 45 |
assert.sameValue(coercions, 0); |
| 46 |
|
| 47 |
assert.throws(TypeError, function () { |
| 48 |
Math.sumPrecise([-Infinity, Infinity, objectWithValueOf]); |
| 49 |
}); |
| 50 |
assert.sameValue(coercions, 0); |
| 51 |
|
| 52 |
var nextCalls = 0; |
| 53 |
var closed = false; |
| 54 |
var iterator = { |
| 55 |
next: function () { |
| 56 |
++nextCalls; |
| 57 |
return { done: false, value: objectWithValueOf }; |
| 58 |
}, |
| 59 |
return: function () { |
| 60 |
closed = true; |
| 61 |
return {}; |
| 62 |
} |
| 63 |
}; |
| 64 |
var iterable = { |
| 65 |
[Symbol.iterator]: function () { |
| 66 |
return iterator; |
| 67 |
} |
| 68 |
}; |
| 69 |
|
| 70 |
assert.throws(TypeError, function () { |
| 71 |
Math.sumPrecise(iterable); |
| 72 |
}); |
| 73 |
assert.sameValue(coercions, 0); |
| 74 |
assert.sameValue(nextCalls, 1); |
| 75 |
assert.sameValue(closed, true); |
| 76 |
|
| 77 |
reportCompare(0, 0); |