You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

161 lines
5.3 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

describe('课前抽点功能测试', () => {
// 测试1检查是否能正确抽取学生且数量在30%-50%之间
test('应在30%-50%范围内抽取学生进行点名', () => {
const students = [
{ name: '学生1', score: 10 },
{ name: '学生2', score: 5 },
{ name: '学生3', score: 8 },
{ name: '学生4', score: 3 },
{ name: '学生5', score: 12 },
];
const selectedStudents = performMultipleWeightedRandom(students);
// 检查选中的学生数量是否在总人数的30%-50%之间
const totalStudents = students.length;
expect(selectedStudents.length).toBeGreaterThanOrEqual(Math.floor(0.3 * totalStudents));
expect(selectedStudents.length).toBeLessThanOrEqual(Math.floor(0.5 * totalStudents));
});
// 测试2确保不会重复选择同一个学生
test('学生不应被重复点名', () => {
const students = [
{ name: '学生1', score: 10 },
{ name: '学生2', score: 5 },
{ name: '学生3', score: 8 },
{ name: '学生4', score: 3 },
{ name: '学生5', score: 12 },
];
const selectedStudents = performMultipleWeightedRandom(students);
// 检查是否有重复的学生
const uniqueStudents = new Set(selectedStudents.map(student => student.name));
expect(uniqueStudents.size).toBe(selectedStudents.length); // 如果有重复,集合大小会小于数组长度
});
// 测试3低分学生应有较高概率被抽中
test('低分学生应有更高概率被选中', () => {
const students = [
{ name: '学生1', score: 5 }, // 低分
{ name: '学生2', score: 50 },
{ name: '学生3', score: 100 }
];
let lowScoreSelected = 0;
const iterations = 1000;
// 进行多次测试,统计低分学生被选中的次数
for (let i = 0; i < iterations; i++) {
const selectedStudents = performMultipleWeightedRandom(students);
if (selectedStudents.some(student => student.name === '学生1')) {
lowScoreSelected++;
}
}
// 检查低分学生被选中的次数较高
expect(lowScoreSelected / iterations).toBeGreaterThan(0.4); // 期望低分学生被选中的概率较高
});
// 测试4确保返回的学生总是有效的学生对象
test('随机选择应返回有效的学生对象', () => {
const students = [
{ name: '学生1', score: 30 },
{ name: '学生2', score: 20 },
{ name: '学生3', score: 10 }
];
const selectedStudents = performMultipleWeightedRandom(students);
// 检查返回的学生对象是否包含name和score
selectedStudents.forEach(student => {
expect(student).toHaveProperty('name');
expect(student).toHaveProperty('score');
});
});
});
// 导入函数
const performWeightedRandom = require('./path-to-your-function');
describe('随机点名功能测试', () => {
// 测试1所有学生积分都相等的情况
test('所有学生积分都相等时,应该随机选择任何一个学生', () => {
const students = [
{ name: '学生1', score: 10 },
{ name: '学生2', score: 10 },
{ name: '学生3', score: 10 },
];
const selectedStudent = performWeightedRandom(students);
// 检查返回值是否在给定的学生名单中
expect(students).toContainEqual(selectedStudent);
});
// 测试2学生的积分范围不同时低分学生应有较高概率被选中
test('低分学生应该有较高的概率被选中', () => {
const students = [
{ name: '学生1', score: 5 }, // 低分
{ name: '学生2', score: 50 },
{ name: '学生3', score: 100 }
];
let lowScoreSelected = 0;
const iterations = 10000;
// 进行多次测试,统计被选中的学生
for (let i = 0; i < iterations; i++) {
const selectedStudent = performWeightedRandom(students);
if (selectedStudent.name === '学生1') {
lowScoreSelected++;
}
}
// 检查低分学生是否被选择较多次
expect(lowScoreSelected / iterations).toBeGreaterThan(0.4); // 期望低分学生被选中的概率较高
});
// 测试3边界条件单个学生的情况
test('只有一个学生时,应该总是选择该学生', () => {
const students = [{ name: '唯一学生', score: 10 }];
const selectedStudent = performWeightedRandom(students);
// 检查返回的学生是否就是唯一的学生
expect(selectedStudent.name).toBe('唯一学生');
});
// 测试4负分、零分和正分学生的情况
test('包含负分、零分和正分的学生,仍能正常选择学生', () => {
const students = [
{ name: '学生1', score: -10 },
{ name: '学生2', score: 0 },
{ name: '学生3', score: 10 }
];
const selectedStudent = performWeightedRandom(students);
// 检查返回值是否在给定的学生名单中
expect(students).toContainEqual(selectedStudent);
});
// 测试5检查返回的学生不为空
test('随机选择应返回有效的学生', () => {
const students = [
{ name: '学生1', score: 30 },
{ name: '学生2', score: 20 },
{ name: '学生3', score: 10 }
];
const selectedStudent = performWeightedRandom(students);
// 检查返回的学生是否是定义好的对象之一
expect(selectedStudent).toHaveProperty('name');
expect(selectedStudent).toHaveProperty('score');
});
});