Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[LAB1] 512558012 #164

Closed
wants to merge 22 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 61 additions & 13 deletions lab1/main_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
});
// 添加更多测试用例以覆盖你预期的所有场景和边界情况
52 changes: 49 additions & 3 deletions lab3/main_test.js
Original file line number Diff line number Diff line change
@@ -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
Loading