test262/harness/compareArray.js

51 lines
1.4 KiB
JavaScript

// 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]
---*/
function compareArray(a, b) {
if (b.length !== a.length) {
return false;
}
for (var i = 0; i < a.length; i++) {
if (!compareArray.isSameValue(b[i], a[i])) {
return false;
}
}
return true;
}
compareArray.isSameValue = function(a, b) {
if (a === 0 && b === 0) return 1 / a === 1 / b;
if (a !== a && b !== b) return true;
return a === b;
};
compareArray.format = function(arrayLike) {
return `[${[].map.call(arrayLike, String).join(', ')}]`;
};
assert.compareArray = function(actual, expected, message) {
message = message === undefined ? '' : message;
if (typeof message === 'symbol') {
message = message.toString();
}
assert(actual != null, `First argument shouldn't be nullish. ${message}`);
assert(expected != null, `Second argument shouldn't be nullish. ${message}`);
var format = compareArray.format;
var result = compareArray(actual, expected);
// The following prevents actual and expected from being iterated and evaluated
// more than once unless absolutely necessary.
if (!result) {
assert(false, `Expected ${format(actual)} and ${format(expected)} to have the same contents. ${message}`);
}
};