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;
|
|
|
|
};
|
|
|
|
|
2021-10-01 18:52:15 +02:00
|
|
|
compareArray.format = function(arrayLike) {
|
|
|
|
return `[${[].map.call(arrayLike, String).join(', ')}]`;
|
2019-10-09 16:57:20 +02:00
|
|
|
};
|
|
|
|
|
2021-07-02 15:04:13 +02:00
|
|
|
assert.compareArray = function(actual, expected, message) {
|
|
|
|
message = message === undefined ? '' : message;
|
2021-10-05 18:30:06 +02:00
|
|
|
|
|
|
|
if (typeof message === 'symbol') {
|
|
|
|
message = message.toString();
|
|
|
|
}
|
|
|
|
|
2020-08-31 20:45:36 +02:00
|
|
|
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;
|
2021-10-01 18:52:15 +02:00
|
|
|
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}`);
|
|
|
|
}
|
2017-07-14 17:37:24 +02:00
|
|
|
};
|