Skip to content

Commit 5531512

Browse files
authored
Merge pull request #235 from MinKuanIsHere/lab3
[LAB3] 313552009
2 parents 7c57c8e + c3c1124 commit 5531512

2 files changed

Lines changed: 66 additions & 2 deletions

File tree

lab0/lab0.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log("Hello world!");

lab3/main_test.js

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,68 @@
1-
const {describe, it} = require('node:test');
1+
const { describe, it } = require('node:test');
22
const assert = require('assert');
33
const { Calculator } = require('./main');
44

5-
// TODO: write your tests here
5+
const calculator = new Calculator();
6+
7+
// exp() Error Cases
8+
describe('Calculator.exp() Error Cases', () => {
9+
const errorCases = [
10+
{ param: [NaN], error: 'unsupported operand type' },
11+
{ param: [Infinity], error: 'unsupported operand type' },
12+
{ param: [-Infinity], error: 'unsupported operand type' },
13+
{ param: [1000], error: 'overflow' }
14+
];
15+
16+
for (const tc of errorCases) {
17+
it(`should throw "${tc.error}" for exp(${tc.param})`, () => {
18+
assert.throws(() => calculator.exp.apply(calculator, tc.param), { message: tc.error });
19+
});
20+
}
21+
});
22+
23+
// exp() Success Cases
24+
describe('Calculator.exp() Success Cases', () => {
25+
const successCases = [
26+
{ param: [0], expected: 1 },
27+
{ param: [1], expected: Math.exp(1) },
28+
{ param: [-1], expected: Math.exp(-1) }
29+
];
30+
31+
for (const tc of successCases) {
32+
it(`should return ${tc.expected} for exp(${tc.param})`, () => {
33+
assert.strictEqual(calculator.exp.apply(calculator, tc.param), tc.expected);
34+
});
35+
}
36+
});
37+
38+
// log() Error Cases
39+
describe('Calculator.log() Error Cases', () => {
40+
const errorCases = [
41+
{ param: [NaN], error: 'unsupported operand type' },
42+
{ param: [Infinity], error: 'unsupported operand type' },
43+
{ param: [-Infinity], error: 'unsupported operand type' },
44+
{ param: [0], error: 'math domain error (1)' },
45+
{ param: [-1], error: 'math domain error (2)' }
46+
];
47+
48+
for (const tc of errorCases) {
49+
it(`should throw "${tc.error}" for log(${tc.param})`, () => {
50+
assert.throws(() => calculator.log.apply(calculator, tc.param), { message: tc.error });
51+
});
52+
}
53+
});
54+
55+
// log() Success Cases
56+
describe('Calculator.log() Success Cases', () => {
57+
const successCases = [
58+
{ param: [1], expected: 0 },
59+
{ param: [Math.E], expected: 1 },
60+
{ param: [10], expected: Math.log(10) }
61+
];
62+
63+
for (const tc of successCases) {
64+
it(`should return ${tc.expected} for log(${tc.param})`, () => {
65+
assert.strictEqual(calculator.log.apply(calculator, tc.param), tc.expected);
66+
});
67+
}
68+
});

0 commit comments

Comments
 (0)