summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorKai Stevenson <kai@kaistevenson.com>2025-11-09 21:08:12 -0800
committerKai Stevenson <kai@kaistevenson.com>2025-11-09 21:08:30 -0800
commit413eaa284e164143c5416cdce5a1de0f9f992409 (patch)
treed999e8cbaddefcce9df3265c594083177427b6cb /tests
parent93992029bd349185d15de02e0f633e95c62695a9 (diff)
map + reduce
Diffstat (limited to 'tests')
-rw-r--r--tests/type-consistency/spec/index.ts4
-rw-r--r--tests/type-consistency/spec/mapReduce.ts34
-rw-r--r--tests/type-consistency/spec/recursion.ts25
3 files changed, 62 insertions, 1 deletions
diff --git a/tests/type-consistency/spec/index.ts b/tests/type-consistency/spec/index.ts
index b2da682..780c20c 100644
--- a/tests/type-consistency/spec/index.ts
+++ b/tests/type-consistency/spec/index.ts
@@ -2,5 +2,7 @@ import array from "./array";
import functions from "./function";
import types from "./types";
import tostring from "./tostring";
+import mapreduce from "./mapReduce";
+import recursion from "./recursion";
-export default [array, functions, types, tostring];
+export default [array, functions, types, tostring, mapreduce, recursion];
diff --git a/tests/type-consistency/spec/mapReduce.ts b/tests/type-consistency/spec/mapReduce.ts
new file mode 100644
index 0000000..b5077ff
--- /dev/null
+++ b/tests/type-consistency/spec/mapReduce.ts
@@ -0,0 +1,34 @@
+import path from "path";
+import { createTestHarness } from "../harness";
+
+export default createTestHarness({
+ harnessName: "Map reduce",
+ generatedPath: path.join(__dirname, "..", "generated"),
+})
+ .createFunctionTest({
+ name: "Map: numbers to string",
+ program: "fn(a, map(a, fn(n, tostring(n))))",
+ cases: [
+ { input: [1, 2, 3], output: ["1", "2", "3"] },
+ { input: [50], output: ["50"] },
+ { input: [], output: [] },
+ ],
+ })
+ .createFunctionTest({
+ name: "Reduce: array length",
+ program: "fn(a, reduce(a, fn(acc, add(acc, 1)), 0))",
+ cases: [
+ { input: [1, 2, 3], output: 3 },
+ { input: ["hello", ["hello", "world"]], output: 2 },
+ { input: [], output: 0 },
+ ],
+ })
+ .createFunctionTest({
+ name: "Reduce: sum of numbers times index",
+ program: "fn(a, reduce(a, fn(acc, cur, idx, add(acc, mul(cur, idx))), 0))",
+ cases: [
+ { input: [1, 2, 3], output: 8 },
+ { input: [], output: 0 },
+ { input: [50, 10, 0], output: 10 },
+ ],
+ });
diff --git a/tests/type-consistency/spec/recursion.ts b/tests/type-consistency/spec/recursion.ts
new file mode 100644
index 0000000..c5de83f
--- /dev/null
+++ b/tests/type-consistency/spec/recursion.ts
@@ -0,0 +1,25 @@
+import path from "path";
+import { createTestHarness } from "../harness";
+
+export default createTestHarness({
+ harnessName: "Recursive functions",
+ generatedPath: path.join(__dirname, "..", "generated"),
+})
+ .createFunctionTest({
+ name: "n!",
+ program: "bind(fac,fn(n,?(eq(n, 1),n,mul(n,call(fac,sub(n,1))))))",
+ cases: [
+ { input: 3, output: 6 },
+ { input: 1, output: 1 },
+ { input: 5, output: 120 },
+ ],
+ })
+ .createFunctionTest({
+ name: "Sum of natural numbers on [0, n]",
+ program: "bind(sumnn,fn(n,?(eq(n, 1),n,add(n,call(sumnn,sub(n,1))))))",
+ cases: [
+ { input: 5, output: 15 },
+ { input: 1, output: 1 },
+ { input: 2, output: 3 },
+ ],
+ });