test262/harness/compareArray.js

46 lines
988 B
JavaScript
Raw Normal View History

// Copyright (C) 2017 Ecma International. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: |
Compare the contents of two arrays
defines: [compareArray]
---*/
// @ts-check
2019-08-24 12:24:33 +02:00
function isSameValue(a, b) {
if (a === 0 && b === 0) return 1 / a === 1 / b;
if (a !== a && b !== b) return true;
return a === b;
}
/**
* @template T
* @param {T[]} a
* @param {T[]} b
*/
function compareArray(a, b) {
if (b.length !== a.length) {
return false;
}
for (var i = 0; i < a.length; i++) {
2019-08-24 12:24:33 +02:00
if (!isSameValue(b[i], a[i])) {
return false;
}
}
return true;
}
/**
* @template T
* @param {T[]} actual
* @param {T[]} expected
* @param {string} [message]
*/
assert.compareArray = function(actual, expected, message) {
2017-04-26 14:05:31 +02:00
assert(compareArray(actual, expected),
'Expected ' + assert._formatValue(actual) + ' and ' + assert._formatValue(expected) + ' to have the same contents. ' + message);
};