diff --git a/lab1/main_test.js b/lab1/main_test.js index 74a716b4..2827bfba 100644 --- a/lab1/main_test.js +++ b/lab1/main_test.js @@ -2,22 +2,70 @@ const test = require('node:test'); const assert = require('assert'); const { MyClass, Student } = require('./main'); -test("Test MyClass's addStudent", () => { - // TODO - throw new Error("Test not implemented"); +// 测试 MyClass 的 addStudent 方法 +test("Test MyClass's addStudent", async () => { + const myClass = new MyClass(); + const student = new Student(); + student.setName('John Doe'); + const index = myClass.addStudent(student); + assert.strictEqual(index, 0); // 第一个学生的索引应该是 0 }); -test("Test MyClass's getStudentById", () => { - // TODO - throw new Error("Test not implemented"); +// 测试 MyClass 的 getStudentById 方法 +test("Test MyClass's getStudentById", async () => { + const myClass = new MyClass(); + const student = new Student(); + student.setName('John Doe'); + const index = myClass.addStudent(student); + + // 有效的 ID + const retrievedStudent = myClass.getStudentById(index); + assert.strictEqual(retrievedStudent, student); + + // 无效的 ID + const invalidRetrieval = myClass.getStudentById(-1); + assert.strictEqual(invalidRetrieval, null); +}); + +// 测试 Student 的 setName 方法 +test("Test Student's setName", async () => { + const student = new Student(); + + // 有效的名称 + student.setName('Jane Doe'); + assert.strictEqual(student.getName(), 'Jane Doe'); + + // 无效的名称(非字符串) + student.setName(123); + assert.notStrictEqual(student.getName(), 123); // 名称不应该设置为一个数字 }); -test("Test Student's setName", () => { - // TODO - throw new Error("Test not implemented"); +// 测试 Student 的 getName 方法 +test("Test Student's getName", async () => { + const student = new Student(); + + // 名称未设置 + assert.strictEqual(student.getName(), ''); // 应该返回一个空字符串 + + // 名称已设置 + student.setName('Jane Doe'); + assert.strictEqual(student.getName(), 'Jane Doe'); +}); + +// 测试 MyClass 的 removeStudent 方法 +test("Test MyClass's removeStudent", async () => { + const myClass = new MyClass(); + const student = new Student(); + student.setName('John Doe'); + const index = myClass.addStudent(student); + + // 移除学生 + const removedStudent = myClass.removeStudent(index); + assert.strictEqual(removedStudent, student); + + // 尝试移除不存在的学生 + const result = myClass.removeStudent(999); + assert.strictEqual(result, null); }); -test("Test Student's getName", () => { - // TODO - throw new Error("Test not implemented"); -}); \ No newline at end of file +// 添加更多测试用例以覆盖你预期的所有场景和边界情况 diff --git a/lab3/main_test.js b/lab3/main_test.js index 096fd421..260343a8 100644 --- a/lab3/main_test.js +++ b/lab3/main_test.js @@ -1,5 +1,51 @@ -const { describe, it } = require('node:test'); -const assert = require('assert'); const { Calculator } = require('./main'); +const Jasmine = require('jasmine'); +const jasmine = new Jasmine(); + +describe('Calculator', () => { + let calculator; + + beforeEach(() => { + calculator = new Calculator(); + }); + + describe('exp function', () => { + it('should correctly calculate the exponential of valid numbers', () => { + expect(calculator.exp(1)).toBeCloseTo(Math.exp(1)); + expect(calculator.exp(0)).toBeCloseTo(1); + expect(calculator.exp(-1)).toBeCloseTo(Math.exp(-1)); + }); + + it('should throw an error for non-finite inputs', () => { + expect(() => calculator.exp(Infinity)).toThrowError('unsupported operand type'); + expect(() => calculator.exp(-Infinity)).toThrowError('unsupported operand type'); + expect(() => calculator.exp(NaN)).toThrowError('unsupported operand type'); + }); + + it('should throw an overflow error for large inputs', () => { + expect(() => calculator.exp(1000)).toThrowError('overflow'); + }); + }); + + describe('log function', () => { + it('should correctly calculate the logarithm of valid numbers', () => { + expect(calculator.log(1)).toBeCloseTo(Math.log(1)); + expect(calculator.log(Math.E)).toBeCloseTo(1); + expect(calculator.log(10)).toBeCloseTo(Math.log(10)); + }); + + it('should throw an error for non-finite inputs', () => { + expect(() => calculator.log(Infinity)).toThrowError('unsupported operand type'); + expect(() => calculator.log(-Infinity)).toThrowError('unsupported operand type'); + expect(() => calculator.log(NaN)).toThrowError('unsupported operand type'); + }); + + it('should throw a math domain error for invalid domain inputs', () => { + expect(() => calculator.log(-1)).toThrowError('math domain error (2)'); + expect(() => calculator.log(0)).toThrowError('math domain error (1)'); + }); + }); +}); + +jasmine.execute(); -// TODO: write your tests here