Skip to content

Commit

Permalink
refactor: update all tests (packages T-Z)
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Nov 8, 2023
1 parent e3085e4 commit 020ef6c
Show file tree
Hide file tree
Showing 55 changed files with 4,015 additions and 4,285 deletions.
2 changes: 1 addition & 1 deletion packages/tangle/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"doc:ae": "mkdir -p .ae/doc .ae/temp && api-extractor run --local --verbose",
"doc:readme": "bun ../../tools/src/module-stats.ts && bun ../../tools/src/readme.ts",
"pub": "yarn npm publish --access public",
"test": "bun test"
"test": "testament test"
},
"dependencies": {
"@thi.ng/api": "^8.9.6",
Expand Down
File renamed without changes.
51 changes: 24 additions & 27 deletions packages/timestep/test/main.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { eqDelta } from "@thi.ng/math";
import { group } from "@thi.ng/testament";
import {
eqDelta2,
eqDelta3,
Expand All @@ -8,39 +7,37 @@ import {
maddN3,
maddN4,
} from "@thi.ng/vectors";
import * as assert from "assert";
import {
defNumeric,
defTimeStep,
defVector2,
defVector3,
defVector4,
} from "../src/index.js";
import { expect, test } from "bun:test";

group("timestep", {
numeric: () => {
const sim = defTimeStep({ maxFrameTime: 10 });
const states = [
defNumeric(100, (x, dt) => x - 0.1 * dt),
defNumeric(0, (x, dt) => (100 - x) * 0.1 * dt),
];
sim.update(61 * (1000 / 60), states);
assert.ok(eqDelta(states[0].deref(), 99.9, 0.001));
assert.ok(eqDelta(states[1].deref(), 0.166, 0.001));
},
test("numeric", () => {
const sim = defTimeStep({ maxFrameTime: 10 });
const states = [
defNumeric(100, (x, dt) => x - 0.1 * dt),
defNumeric(0, (x, dt) => (100 - x) * 0.1 * dt),
];
sim.update(61 * (1000 / 60), states);
expect(eqDelta(states[0].deref(), 99.9, 0.001)).toBeTrue();
expect(eqDelta(states[1].deref(), 0.166, 0.001)).toBeTrue();
});

vector: () => {
const sim = defTimeStep({ maxFrameTime: 10 });
const states = [
defVector2([0, 0], (v, dt) => maddN2(v, [-10, 20], dt, v)),
defVector3([0, 0, 0], (v, dt) => maddN3(v, [-10, 20, -30], dt, v)),
defVector4([0, 0, 0, 0], (v, dt) =>
maddN4(v, [-10, 20, -30, 40], dt, v)
),
];
sim.update(61 * (1000 / 60), states);
assert.ok(eqDelta2(states[0].deref(), [-10, 20], 0.001));
assert.ok(eqDelta3(states[1].deref(), [-10, 20, -30], 0.001));
assert.ok(eqDelta4(states[2].deref(), [-10, 20, -30, 40], 0.001));
},
test("vector", () => {
const sim = defTimeStep({ maxFrameTime: 10 });
const states = [
defVector2([0, 0], (v, dt) => maddN2(v, [-10, 20], dt, v)),
defVector3([0, 0, 0], (v, dt) => maddN3(v, [-10, 20, -30], dt, v)),
defVector4([0, 0, 0, 0], (v, dt) =>
maddN4(v, [-10, 20, -30, 40], dt, v)
),
];
sim.update(61 * (1000 / 60), states);
expect(eqDelta2(states[0].deref(), [-10, 20], 0.001)).toBeTrue();
expect(eqDelta3(states[1].deref(), [-10, 20, -30], 0.001)).toBeTrue();
expect(eqDelta4(states[2].deref(), [-10, 20, -30, 40], 0.001)).toBeTrue();
});
2 changes: 1 addition & 1 deletion packages/transclude/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"doc:ae": "mkdir -p .ae/doc .ae/temp && api-extractor run --local --verbose",
"doc:readme": "bun ../../tools/src/module-stats.ts && bun ../../tools/src/readme.ts",
"pub": "yarn npm publish --access public",
"test": "bun test"
"test": "testament test"
},
"dependencies": {
"@thi.ng/api": "^8.9.6",
Expand Down
File renamed without changes.
4 changes: 0 additions & 4 deletions packages/transducers-binary/test/main.test.ts

This file was deleted.

21 changes: 7 additions & 14 deletions packages/transducers-binary/test/partition-bits.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { radix } from "@thi.ng/strings";
import { group } from "@thi.ng/testament";
import {
comp,
iterator,
Expand All @@ -9,7 +8,7 @@ import {
range,
run,
} from "@thi.ng/transducers";
import * as assert from "assert";
import { expect, test } from "bun:test";
import { bits, partitionBits } from "../src/index.js";

const src = [0xff, 0xa5, 0xfe, 0xf7];
Expand All @@ -25,16 +24,10 @@ const xformB = (n: number) =>
);

const check = (n: number) =>
assert.deepStrictEqual(
[...iterator(xform(n), src)],
[...iterator(xformB(n), src)],
`bits=${n}`
);
expect([...iterator(xform(n), src)]).toEqual([...iterator(xformB(n), src)]);

group("partitionBits", {
"all sizes": () =>
run(
map((n: number) => check(n)),
range(1, 33)
),
});
test("all sizes", () =>
run(
map((n: number) => check(n)),
range(1, 33)
));
94 changes: 43 additions & 51 deletions packages/transducers-fsm/test/main.test.ts
Original file line number Diff line number Diff line change
@@ -1,59 +1,51 @@
import { group } from "@thi.ng/testament";
import { comp, iterator, map, range, takeNth } from "@thi.ng/transducers";
import * as assert from "assert";
import { expect, test } from "bun:test";
import { fsm } from "../src/index.js";

group("transducers-fsm", {
"readme example": () => {
const testFSM = fsm<any, number, number>({
states: {
skip: (state, x) => {
if (x < 20) {
if (++state.count > 5) {
state.state = "take";
state.count = 1;
return [x];
}
} else {
state.state = "done";
test("readme example", () => {
const testFSM = fsm<any, number, number>({
states: {
skip: (state, x) => {
if (x < 20) {
if (++state.count > 5) {
state.state = "take";
state.count = 1;
return [x];
}
},
take: (state, x) => {
if (x < 20) {
if (++state.count > 5) {
state.state = "skip";
state.count = 1;
} else {
return [x];
}
} else {
state.state = "done";
}
},
take: (state, x) => {
if (x < 20) {
if (++state.count > 5) {
state.state = "skip";
state.count = 1;
} else {
state.state = "done";
return [x];
}
},
done: () => {},
} else {
state.state = "done";
}
},
terminate: "done",
init: () => ({ state: "skip", count: 0 }),
});
assert.deepStrictEqual(
[...iterator(testFSM, range(100))],
[5, 6, 7, 8, 9, 15, 16, 17, 18, 19]
);
assert.deepStrictEqual(
[...iterator(comp(takeNth(2), testFSM), range(100))],
[10, 12, 14, 16, 18]
);
assert.deepStrictEqual(
[
...iterator(
comp(
testFSM,
map((x: number) => x * 10)
),
range(100)
),
],
[50, 60, 70, 80, 90, 150, 160, 170, 180, 190]
);
},
done: () => {},
},
terminate: "done",
init: () => ({ state: "skip", count: 0 }),
});
expect([...iterator(testFSM, range(100))]).toEqual([
5, 6, 7, 8, 9, 15, 16, 17, 18, 19,
]);
expect([...iterator(comp(takeNth(2), testFSM), range(100))]).toEqual([
10, 12, 14, 16, 18,
]);
expect([
...iterator(
comp(
testFSM,
map((x: number) => x * 10)
),
range(100)
),
]).toEqual([50, 60, 70, 80, 90, 150, 160, 170, 180, 190]);
});
73 changes: 34 additions & 39 deletions packages/transducers-patch/test/main.test.ts
Original file line number Diff line number Diff line change
@@ -1,48 +1,43 @@
import { group } from "@thi.ng/testament";
import { expect, test } from "bun:test";
import { reduce, reductions } from "@thi.ng/transducers";
import * as assert from "assert";
import { patchArray, patchObj } from "../src/index.js";

group("transducers-patch", {
patchArray: () => {
assert.deepStrictEqual(
reduce(
reductions(patchArray<number>()),
[[1, 2, 3]],
[
["insert", 0, [10, 11]],
["update", 1, (x, n) => x * n, 10],
["delete", 3],
["set", 2, 200],
]
),
test("patchArray", () => {
expect(
reduce(
reductions(patchArray<number>()),
[[1, 2, 3]],
[
[1, 2, 3],
[10, 11, 1, 2, 3],
[10, 110, 1, 2, 3],
[10, 110, 1, 3],
[10, 110, 200, 3],
["insert", 0, [10, 11]],
["update", 1, (x, n) => x * n, 10],
["delete", 3],
["set", 2, 200],
]
);
},
)
).toEqual([
[1, 2, 3],
[10, 11, 1, 2, 3],
[10, 110, 1, 2, 3],
[10, 110, 1, 3],
[10, 110, 200, 3],
]);
});

patchObj: () => {
assert.deepStrictEqual(
reduce(
reductions(patchObj()),
[{ x: 23 }],
[
["set", ["a", "b"], 1],
["update", "a.b", (x, n) => x + n, 10],
["delete", "x"],
]
),
test("patchObj", () => {
expect(
reduce(
reductions(patchObj()),
[{ x: 23 }],
[
{ x: 23 },
{ x: 23, a: { b: 1 } },
{ x: 23, a: { b: 11 } },
{ a: { b: 11 } },
["set", ["a", "b"], 1],
["update", "a.b", (x, n) => x + n, 10],
["delete", "x"],
]
);
},
)
).toEqual([
{ x: 23 },
{ x: 23, a: { b: 1 } },
{ x: 23, a: { b: 11 } },
{ a: { b: 11 } },
]);
});
33 changes: 15 additions & 18 deletions packages/transducers/test/drop.test.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
import { group } from "@thi.ng/testament";
import * as assert from "assert";
import { expect, test } from "bun:test";
import { drop, range } from "../src/index.js";

group("drop", {
"starts iterating after N items": () => {
assert.deepStrictEqual([...drop(0, [true, false])], [true, false]);
assert.deepStrictEqual([...drop(1, [true, false])], [false]);
assert.deepStrictEqual([...drop(2, [true, false])], []);
assert.deepStrictEqual([...drop(3, [true, false])], []);
assert.deepStrictEqual([...drop(2, range(0, 4))], [2, 3]);
assert.deepStrictEqual([...drop(0, ["", "ab", "c"])], ["", "ab", "c"]);
assert.deepStrictEqual([...drop(1, ["", "ab", "c"])], ["ab", "c"]);
assert.deepStrictEqual([...drop(2, ["", "ab", "c"])], ["c"]);
assert.deepStrictEqual([...drop(0, "")], []);
assert.deepStrictEqual([...drop(1, "")], []);
assert.deepStrictEqual([...drop(0, "abc")], ["a", "b", "c"]);
assert.deepStrictEqual([...drop(1, "abc")], ["b", "c"]);
assert.deepStrictEqual([...drop(2, "abc")], ["c"]);
},
test("starts iterating after N items", () => {
expect([...drop(0, [true, false])]).toEqual([true, false]);
expect([...drop(1, [true, false])]).toEqual([false]);
expect([...drop(2, [true, false])]).toEqual([]);
expect([...drop(3, [true, false])]).toEqual([]);
expect([...drop(2, range(0, 4))]).toEqual([2, 3]);
expect([...drop(0, ["", "ab", "c"])]).toEqual(["", "ab", "c"]);
expect([...drop(1, ["", "ab", "c"])]).toEqual(["ab", "c"]);
expect([...drop(2, ["", "ab", "c"])]).toEqual(["c"]);
expect([...drop(0, "")]).toEqual([]);
expect([...drop(1, "")]).toEqual([]);
expect([...drop(0, "abc")]).toEqual(["a", "b", "c"]);
expect([...drop(1, "abc")]).toEqual(["b", "c"]);
expect([...drop(2, "abc")]).toEqual(["c"]);
});
Loading

0 comments on commit 020ef6c

Please sign in to comment.