Skip to content

Commit 4653cec

Browse files
🧪 test(sample): Simplify.
1 parent 53fd779 commit 4653cec

File tree

3 files changed

+45
-41
lines changed

3 files changed

+45
-41
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
"dependencies": {},
6666
"devDependencies": {
6767
"@aureooms/js-array": "4.0.0",
68+
"@aureooms/js-compare": "^2.0.1",
6869
"@aureooms/js-functools": "2.0.3",
6970
"@aureooms/js-itertools": "5.0.1",
7071
"@aureooms/js-memory": "4.0.0",

test/src/sample.js

Lines changed: 39 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,50 @@
11
import test from 'ava';
2-
import * as random from '../../src/index.js';
2+
import {sample, _fisheryates, randint} from '../../src/index.js';
33

4-
import * as mem from '@aureooms/js-memory';
5-
import * as array from '@aureooms/js-array';
6-
import operator from '@aureooms/js-operator';
4+
import {_calloc} from '@aureooms/js-memory';
5+
import {iota, copy} from '@aureooms/js-array';
6+
import {increasing} from '@aureooms/js-compare';
77

8-
function one(type, sample_name, sample) {
9-
const type_name = type.toString().split(' ')[1].slice(0, -2);
10-
11-
const calloc = mem._calloc(type);
12-
13-
const n = 100;
8+
const macro = (t, type, _sample_name, sample, n, k, i, j) => {
9+
const calloc = _calloc(type);
1410

1511
const a = calloc(n);
1612
const b = calloc(n);
1713

18-
array.iota(a, 0, n, 0);
19-
20-
const range = function (k, i, j) {
21-
const name = `sample ( ${type_name}, ${sample_name}, ${k}, ${i}, ${j} )`;
22-
23-
test(name, (t) => {
24-
array.copy(a, 0, n, b, 0);
25-
sample(k, b, i, j);
14+
iota(a, 0, n, 0);
2615

27-
for (let it = 0; it < i; ++it) {
28-
const msg = `b[${it}] === a[${it}]`;
29-
t.deepEqual(b[it], a[it], msg);
30-
}
16+
copy(a, 0, n, b, 0);
17+
sample(k, b, i, j);
3118

32-
const _a = Array.prototype.slice.call(a, i, j).sort(operator.sub);
33-
const _b = Array.prototype.slice.call(b, i, j).sort(operator.sub);
19+
for (let it = 0; it < i; ++it) {
20+
const msg = `b[${it}] === a[${it}]`;
21+
t.deepEqual(b[it], a[it], msg);
22+
}
3423

35-
const msg = 'shuffled region contains same elements as original';
24+
const _a = Array.prototype.slice.call(a, i, j).sort(increasing);
25+
const _b = Array.prototype.slice.call(b, i, j).sort(increasing);
3626

37-
t.deepEqual(_b, _a, msg);
27+
const msg = 'shuffled region contains same elements as original';
3828

39-
for (let it = j; it < n; ++it) {
40-
const msg = `b[${it}] === a[${it}]`;
41-
t.deepEqual(b[it], a[it], msg);
42-
}
43-
});
44-
};
29+
t.deepEqual(_b, _a, msg);
4530

46-
range(n, 0, n);
47-
range(n - 20, 20, n);
48-
range(n - 20, 0, n - 20);
49-
range(n - 20, 10, n - 10);
50-
range(n - 30, 10, n - 10);
51-
}
31+
for (let it = j; it < n; ++it) {
32+
const msg = `b[${it}] === a[${it}]`;
33+
t.deepEqual(b[it], a[it], msg);
34+
}
35+
};
36+
37+
macro.title = (title, type, sample_name, _sample, n, k, i, j) =>
38+
title || `[${n}] sample ( ${type.name}, ${sample_name}, ${k}, ${i}, ${j} )`;
39+
40+
const n = 100;
41+
const params = [
42+
[n, n, 0, n],
43+
[n, n - 20, 20, n],
44+
[n, n - 20, 0, n - 20],
45+
[n, n - 20, 10, n - 10],
46+
[n, n - 30, 10, n - 10],
47+
];
5248

5349
const types = [
5450
Array,
@@ -64,12 +60,14 @@ const types = [
6460
];
6561

6662
const algorithms = [
67-
['Fisher-Yates', random._fisheryates(random.randint)],
68-
['API', random.sample],
63+
['Fisher-Yates', _fisheryates(randint)],
64+
['API', sample],
6965
];
7066

7167
for (const type of types) {
7268
for (const [name, algorithm] of algorithms) {
73-
one(type, name, algorithm);
69+
for (const [n, k, i, j] of params) {
70+
test(macro, type, name, algorithm, n, k, i, j);
71+
}
7472
}
7573
}

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414
dependencies:
1515
"@aureooms/js-error" "^5.0.2"
1616

17+
"@aureooms/js-compare@^2.0.1":
18+
version "2.0.1"
19+
resolved "https://registry.yarnpkg.com/@aureooms/js-compare/-/js-compare-2.0.1.tgz#4636ea0736945abbcca38d5bd56a1f405034b2f9"
20+
integrity sha512-J+gG1wlwF401ySv0mOYhIIVUuoukvZaRkIwYNH9xZTLhRyI4VxoaFi3k8GA0ebjHqndLGYJS9uEeG3cuQcg+7w==
21+
1722
"@aureooms/js-error@^5.0.2":
1823
version "5.0.2"
1924
resolved "https://registry.yarnpkg.com/@aureooms/js-error/-/js-error-5.0.2.tgz#dc2ad2fc2b10365581c32c28222daccc5c3c9600"

0 commit comments

Comments
 (0)