forked from 99techteam/code-challenge
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.ts
More file actions
62 lines (55 loc) · 1.4 KB
/
Copy pathtest.ts
File metadata and controls
62 lines (55 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// Test file for problem4.ts
import { sum_to_n_a, sum_to_n_b, sum_to_n_c } from "./problem4";
function testSumFunction(func: (n: number) => number, name: string) {
const testCases = [
{ input: 0, expected: 0 },
{ input: 1, expected: 1 },
{ input: 5, expected: 15 },
{ input: 10, expected: 55 },
{ input: 100, expected: 5050 },
];
console.log(`Testing ${name}:`);
for (const { input, expected } of testCases) {
try {
const result = func(input);
const pass = result === expected;
console.log(
` sum_to_n(${input}) = ${result} ${
pass ? "✓" : "✗"
} (expected ${expected})`
);
} catch (error) {
console.log(` sum_to_n(${input}) threw error: ${error}`);
}
}
console.log("");
}
testSumFunction(sum_to_n_a, "sum_to_n_a (iterative)");
testSumFunction(sum_to_n_b, "sum_to_n_b (formula)");
testSumFunction(sum_to_n_c, "sum_to_n_c (recursive)");
// Test edge cases
console.log("Testing edge cases:");
try {
sum_to_n_a(-1);
} catch (error) {
console.log(
"sum_to_n_a(-1) correctly threw error:",
error instanceof Error ? error.message : String(error)
);
}
try {
sum_to_n_b(-1);
} catch (error) {
console.log(
"sum_to_n_b(-1) correctly threw error:",
error instanceof Error ? error.message : String(error)
);
}
try {
sum_to_n_c(-1);
} catch (error) {
console.log(
"sum_to_n_c(-1) correctly threw error:",
error instanceof Error ? error.message : String(error)
);
}