feat: some array tests

This commit is contained in:
TopchetoEU 2024-01-06 17:49:36 +02:00
parent 0251c4689d
commit 4572db5c46
Signed by: topchetoeu
GPG Key ID: 6531B8583E5F6ED4
14 changed files with 195 additions and 80 deletions

View File

@ -3,8 +3,8 @@ return new UnitTest('counters')
.add('postfix decrement', function () { var i = 10; i-- === 10; })
.add('prefix decrement', function () { var i = 10; --i === 9; })
.add('prefix increment', function () { var i = 10; ++i === 11; })
.add('ostfix increment of non-number', function () { var i = 'hi mom'; isNaN(i++); })
.add('ostfix decrement of non-number', function () { var i = 'hi mom'; isNaN(i--); })
.add('postfix increment of non-number', function () { var i = 'hi mom'; isNaN(i++); })
.add('postfix decrement of non-number', function () { var i = 'hi mom'; isNaN(i--); })
.add('prefix increment of non-number', function () { var i = 'hi mom'; isNaN(++i); })
.add('prefix decrement of non-number', function () { var i = 'hi mom'; isNaN(--i); })
.add('postfix increment of convertible to number', function () { var i = '10'; i++; i === 11; })

View File

@ -1,2 +1,2 @@
return new UnitTest('Arithmetics')
.add(include('counters.js'))
.add(require('counters.js'))

View File

@ -0,0 +1,12 @@
return new UnitTest('constructor', function() { return typeof Array === 'function'; })
.add('no args', function () { return match(new Array(), []); })
.add('length', function () {
var res = new Array(3);
return res.length === 3 &&
!(0 in res) &&
!(1 in res) &&
!(2 in res);
})
.add('elements', function () {
return match(new Array(1, 2, 3), [1, 2, 3]);
})

19
tests/array/fill.js Normal file
View File

@ -0,0 +1,19 @@
return new UnitTest('fill', function() { return typeof Array.prototype.push === 'function'; })
.add('simple fill', function() {
return match([5, 5, 5, 5, 5], [1, 2, 3, 4, 5].fill(5))
})
.add('fill empty', function() {
return match([], [].fill(5))
})
.add('fill from', function() {
return match([1, 'a', 'a', 'a', 'a'], [1, 2, 3, 4, 5].fill('a', 1))
})
.add('fill range', function() {
return match([1, 'a', 'a', 'a', 5], [1, 2, 3, 4, 5].fill('a', 1, 4))
})
.add('fill wrap', function() {
return match([1, 'a', 'a', 4, 5], [1, 2, 3, 4, 5].fill('a', 1, -2))
})
.add('fill out of range', function() {
return match([1, 2, 'a', 'a', 'a'], [1, 2, 3, 4, 5].fill('a', 2, 8))
})

19
tests/array/find.js Normal file
View File

@ -0,0 +1,19 @@
var a = { id: 10, name: 'test1' };
var b = { id: 15, name: 'test2' };
var c = { id: 20, name: 'test3' };
return new UnitTest('find', function() { return typeof Array.prototype.find === 'function'; })
.add('simple', function() {
return [ a, b, c ].find(function (v) { return v.id === 15; }) === b;
})
.add('sparse', function() {
var n = 0;
[ a, b,,,, c ].find(function (v) { n++; return v === undefined; });
return n === 3;
})
.add('no occurence', function() {
return [ a, b, c ].find(function (v) { return v.id === 30 }) === undefined;
})
.add('pass this', function() {
return [ a, b ].find(function (v) { return this === c; }, c) === a;
})

View File

@ -1,22 +1,12 @@
function runIterator(arr, method, func, n) {
var res = [];
var j = 1;
var args = [ function() {
var pushed = [];
for (var i = 0; i < n; i++) pushed[i] = arguments[i];
res[j++] = pushed;
return func.apply(this, arguments);
} ];
for (var i = 4; i < arguments.length; i++) args[i - 3] = arguments[i];
res[0] = method.apply(arr, args);
return res;
}
return new UnitTest('Array', function() { []; })
.add(include('length.js'))
.add(include('reduce.js'))
.add(include('sparse.js'))
.add(include('concat.js'))
.add(require('constructor.js'))
.add(require('length.js'))
.add(require('reduce.js')('reduce'))
.add(require('reduce.js')('reduceRight'))
.add(require('sparse.js'))
.add(require('concat.js'))
.add(require('sort.js'))
.add(require('push.js'))
.add(require('pop.js'))
.add(require('fill.js'))
.add(require('find.js'))

19
tests/array/indexOf.js Normal file
View File

@ -0,0 +1,19 @@
return new UnitTest('fill', function() { return typeof Array.prototype.push === 'function'; })
.add('simple fill', function() {
return match([5, 5, 5, 5, 5], [1, 2, 3, 4, 5].fill(5))
})
.add('fill empty', function() {
return match([], [].fill(5))
})
.add('fill from', function() {
return match([1, 'a', 'a', 'a', 'a'], [1, 2, 3, 4, 5].fill('a', 1))
})
.add('fill range', function() {
return match([1, 'a', 'a', 'a', 5], [1, 2, 3, 4, 5].fill('a', 1, 4))
})
.add('fill wrap', function() {
return match([1, 'a', 'a', 4, 5], [1, 2, 3, 4, 5].fill('a', 1, -2))
})
.add('fill out of range', function() {
return match([1, 2, 'a', 'a', 'a'], [1, 2, 3, 4, 5].fill('a', 2, 8))
})

View File

@ -11,7 +11,7 @@ return new UnitTest('length & capacity', function() { return 'length' in Array.p
a[5] = 5;
return a.length === 6;
})
.add('length after set (big', function() {
.add('length after set (big)', function() {
var a = [1, 2];
a[5000] = 5;
return a.length === 5001;

8
tests/array/pop.js Normal file
View File

@ -0,0 +1,8 @@
return new UnitTest('pop', function() { return typeof Array.prototype.pop === 'function'; })
.add('simple pop', function() {
var arr = [1, 2, 3];
return match(3, arr.pop())
})
.add('pop from empty', function() {
return match(undefined, [].pop())
})

16
tests/array/push.js Normal file
View File

@ -0,0 +1,16 @@
return new UnitTest('push', function() { return typeof Array.prototype.push === 'function'; })
.add('simple push', function() {
var arr = [];
arr.push(1, 2, 3);
return match(arr, [1, 2, 3])
})
.add('push array', function() {
var arr = [];
arr.push([1, 2, 3]);
return match(arr, [[1, 2, 3]])
})
.add('push as concat', function() {
var arr = [1, 2, 3];
arr.push(4, 5, 6);
return match(arr, [1, 2, 3, 4, 5, 6])
})

View File

@ -1,44 +1,62 @@
function runIterator(arr, method, func, n) {
var res = [];
var j = 1;
var args = [ function() {
var pushed = [];
for (var i = 0; i < n; i++) pushed[i] = arguments[i];
res[j++] = pushed;
return func.apply(this, arguments);
} ];
return new UnitTest('reduceRight', function () { return typeof Array.prototype.reduceRight === 'function' })
for (var i = 4; i < arguments.length; i++) args[i - 3] = arguments[i];
res[0] = method.apply(arr, args);
return res;
}
return function(name) {
var func = Array.prototype[name];
return new UnitTest(name, function () { return typeof func === 'function' })
.add('empty function', function () {match(
[ undefined, [4, 3, 2], [undefined, 2, 1], [undefined, 1, 0], ],
runIterator([1, 2, 3, 4], Array.prototype.reduceRight, function() { }, 3), 1
runIterator([1, 2, 3, 4], func, function() { }, 3), 1
)})
.add('adder', function () {match(
[ 10, [4, 3, 2], [7, 2, 1], [9, 1, 0], ],
runIterator([1, 2, 3, 4], Array.prototype.reduceRight, function(a, b) { return a + b; }, 3), 1
runIterator([1, 2, 3, 4], func, function(a, b) { return a + b; }, 3), 1
)})
.add('sparse array', function () {match(
[ 10, [4, 3, 11], [7, 2, 7], [9, 1, 3], ],
runIterator([,,,1,,,, 2,,,, 3,,,, 4,,,,], Array.prototype.reduceRight, function(a, b) { return a + b }, 3), 1
runIterator([,,,1,,,, 2,,,, 3,,,, 4,,,,], func, function(a, b) { return a + b }, 3), 1
)})
.add('sparse array with one element', function () {match(
[ 1 ],
runIterator([,,,1,,,,], Array.prototype.reduceRight, function(v) { return v; }, 3), 1
runIterator([,,,1,,,,], func, function(v) { return v; }, 3), 1
)})
.add('sparse array with no elements', function () {match(
[ undefined ],
runIterator([,,,,,,,], Array.prototype.reduceRight, function(v) { return v; }, 3), 1
runIterator([,,,,,,,], func, function(v) { return v; }, 3), 1
)})
.add('initial value and empty function', function () {match(
[ undefined, [0, 4, 3], [undefined, 3, 2], [undefined, 2, 1], [undefined, 1, 0] ],
runIterator([1, 2, 3, 4], Array.prototype.reduceRight, function() { }, 3, 0), 1
runIterator([1, 2, 3, 4], func, function() { }, 3, 0), 1
)})
.add('initial value and adder', function () {match(
[ 15, [5, 4, 3], [9, 3, 2], [12, 2, 1], [14, 1, 0] ],
runIterator([1, 2, 3, 4], Array.prototype.reduceRight, function(a, b) { return a + b; }, 3, 5), 1
runIterator([1, 2, 3, 4], func, function(a, b) { return a + b; }, 3, 5), 1
)})
.add('initial value, sparce array and adder', function () {match(
[ 15, [5, 4, 15], [9, 3, 11], [12, 2, 7], [14, 1, 3] ],
runIterator([,,,1,,,, 2,,,, 3,,,, 4,,,,], Array.prototype.reduceRight, function(a, b) { return a + b; }, 3, 5), 1
runIterator([,,,1,,,, 2,,,, 3,,,, 4,,,,], func, function(a, b) { return a + b; }, 3, 5), 1
)})
.add('initial value and sparse array with one element', function () {match(
[ 6, [5, 1, 3] ],
runIterator([,,,1,,,,], Array.prototype.reduceRight, function(a, b) { return a + b; }, 3, 5), 1
runIterator([,,,1,,,,], func, function(a, b) { return a + b; }, 3, 5), 1
)})
.add('initial value and sparse array with no elements', function () {match(
[ 5 ],
runIterator([,,,,,,,], Array.prototype.reduceRight, function(v) { return v; }, 3, 5), 1
runIterator([,,,,,,,], func, function(v) { return v; }, 3, 5), 1
)});
}

View File

@ -1,4 +1,6 @@
return new UnitTest('concat', function() { return typeof Array.prototype.concat === 'function'; })
.add('two arrays', function() { return match([1, 2, 3], [1].concat([2], [3])) })
.add('simple spread', function() { return match([1, 2, 3, 4, 5], [1].concat([2], 3, [4, 5])) })
.add('sparse concat', function() { return match([1,, 2,,, 3,,, 4, 5], [1,,2].concat([,,3,,,4], 5)) })
return new UnitTest('sort', function() { return typeof Array.prototype.sort === 'function'; })
.add('simple', function() { return match([4, 6, 2].sort(), [2, 4, 6]) })
.add('stringify', function() { return match([4, 6, 2, 10].sort(), [10, 2, 4, 6]) })
.add('undefined and empty', function() { return match([4, undefined, 6, , 2].sort(), [2, 4, 6, undefined,,]) })
.add('function ascend', function() { return match([3, 1, 2].sort(function (a, b) { return a - b; }), [1, 2, 3]) })
.add('function descend', function() { return match([3, 1, 2].sort(function (a, b) { return b - a; }), [3, 2, 1]) })

1
tests/entry.js Normal file
View File

@ -0,0 +1 @@
require('tests/index.js')();

View File

@ -2,19 +2,24 @@ function assert(cond, msg, locDepth) {
if (locDepth < 0 || locDepth === undefined) locDepth = 0;
if (!cond) {
log('Assert failed', (typeof locDepth === 'string' ? locDepth : Error().stack[locDepth + 1]) + ': ', msg);
return false;
}
return true;
}
function assertMatch(expected, actual, depth, msg) {
if (!match(expected, actual, depth)) {
log('Assert failed', Error().stack[1] + ': ', msg);
log('Expected:', expected);
log('Actual:', actual);
return false;
}
return true;
}
function match(expected, actual, depth) {
if (!Array.isArray(expected) || !Array.isArray(actual)) return expected === actual;
else if (expected.length !== actual.length) return false;
else if (depth === undefined || depth < 0) depth = 0;
else if (depth === undefined) depth = Infinity;
else if (depth < 0) depth = 0;
for (var i = 0; i < expected.length; i++) {
if (!(i in expected) || !(i in actual)) return !(i in expected) && !(i in actual);
@ -43,6 +48,7 @@ UnitTest.prototype.run = function(path) {
if (path === undefined) path = [];
path.push(this.name);
var res = true;
if (typeof this.exec === 'function') {
var res = true, err = 'exec() returned false.';
@ -50,13 +56,15 @@ UnitTest.prototype.run = function(path) {
if (this.exec() === false) res = false;
}
catch (e) { res = false; err = e; }
assert(res, path.join('/') + ': ' + err, this.exec.location());
res &= assert(res, path.join('/') + ': ' + err, this.exec.location());
}
for (var i = 0; i < this.subtests.length; i++) {
this.subtests[i].run(path);
res &= this.subtests[i].run(path);
}
path.pop();
return res;
}
UnitTest.prototype.add = function(test, exec) {
if (test instanceof UnitTest) this.subtests.push(test);
@ -64,7 +72,10 @@ UnitTest.prototype.add = function(test, exec) {
return this;
}
include('arithmetics/index.js').run();
include('array/index.js').run();
log('Tests complete.');
return function() {
if (
require('arithmetics/index.js').run() &&
require('array/index.js').run()
) log('All tests passed!');
exit();
}