2017-07-14 17:37:24 +02:00
|
|
|
// 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
|
2019-09-25 02:22:26 +02:00
|
|
|
defines: [compareArray]
|
2017-07-14 17:37:24 +02:00
|
|
|
---*/
|
2014-07-19 00:22:37 +02:00
|
|
|
|
2015-03-04 16:30:59 +01:00
|
|
|
function compareArray(a, b) {
|
|
|
|
if (b.length !== a.length) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (var i = 0; i < a.length; i++) {
|
2019-10-09 16:57:20 +02:00
|
|
|
if (!compareArray.isSameValue(b[i], a[i])) {
|
2015-03-04 16:30:59 +01:00
|
|
|
return false;
|
2014-07-19 00:22:37 +02:00
|
|
|
}
|
2015-03-04 16:30:59 +01:00
|
|
|
}
|
|
|
|
return true;
|
2014-07-19 00:22:37 +02:00
|
|
|
}
|
|
|
|
|
2019-10-09 16:57:20 +02:00
|
|
|
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(array) {
|
|
|
|
return `[${array.map(String).join(', ')}]`;
|
|
|
|
};
|
|
|
|
|
2020-08-31 20:45:36 +02:00
|
|
|
assert.compareArray = function(actual, expected, message = '') {
|
|
|
|
assert(actual != null, `First argument shouldn't be nullish. ${message}`);
|
|
|
|
assert(expected != null, `Second argument shouldn't be nullish. ${message}`);
|
2019-10-09 16:57:20 +02:00
|
|
|
var format = compareArray.format;
|
|
|
|
assert(
|
|
|
|
compareArray(actual, expected),
|
2020-08-31 20:45:36 +02:00
|
|
|
`Expected ${format(actual)} and ${format(expected)} to have the same contents. ${message}`
|
2019-10-09 16:57:20 +02:00
|
|
|
);
|
2017-07-14 17:37:24 +02:00
|
|
|
};
|