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.
decky/packages/practice_engine/test/weighted_selector_test.dart

139 lines
3.6 KiB

import 'package:test/test.dart';
import 'package:practice_engine/models/question.dart';
import 'package:practice_engine/algorithms/weighted_selector.dart';
void main() {
group('WeightedSelector', () {
test('selects correct number of questions', () {
final selector = WeightedSelector(seed: 42);
final questions = List.generate(10, (i) {
return Question(
id: 'q$i',
prompt: 'Question $i',
answers: ['A', 'B'],
correctAnswerIndices: [0],
priorityPoints: i,
);
});
final selected = selector.selectQuestions(
candidates: questions,
count: 5,
currentAttemptIndex: 0,
);
expect(selected.length, equals(5));
});
test('returns all questions if count >= candidates length', () {
final selector = WeightedSelector(seed: 42);
final questions = List.generate(5, (i) {
return Question(
id: 'q$i',
prompt: 'Question $i',
answers: ['A', 'B'],
correctAnswerIndices: [0],
);
});
final selected = selector.selectQuestions(
candidates: questions,
count: 10,
currentAttemptIndex: 0,
);
expect(selected.length, equals(5));
});
test('returns empty list for empty candidates', () {
final selector = WeightedSelector(seed: 42);
final selected = selector.selectQuestions(
candidates: [],
count: 5,
currentAttemptIndex: 0,
);
expect(selected, isEmpty);
});
test('returns empty list for count <= 0', () {
final selector = WeightedSelector(seed: 42);
final questions = List.generate(5, (i) {
return Question(
id: 'q$i',
prompt: 'Question $i',
answers: ['A', 'B'],
correctAnswerIndices: [0],
);
});
final selected = selector.selectQuestions(
candidates: questions,
count: 0,
currentAttemptIndex: 0,
);
expect(selected, isEmpty);
});
test('no duplicates in selection', () {
final selector = WeightedSelector(seed: 42);
final questions = List.generate(10, (i) {
return Question(
id: 'q$i',
prompt: 'Question $i',
answers: ['A', 'B'],
correctAnswerIndices: [0],
priorityPoints: 5,
);
});
final selected = selector.selectQuestions(
candidates: questions,
count: 5,
currentAttemptIndex: 0,
);
final ids = selected.map((q) => q.id).toSet();
expect(ids.length, equals(selected.length));
});
test('higher priority questions are more likely to be selected', () {
final selector = WeightedSelector(seed: 42);
final questions = [
Question(
id: 'low',
prompt: 'Low priority',
answers: ['A', 'B'],
correctAnswerIndices: [0],
priorityPoints: 1,
),
Question(
id: 'high',
prompt: 'High priority',
answers: ['A', 'B'],
correctAnswerIndices: [0],
priorityPoints: 100,
),
];
// Run multiple selections and count occurrences
int highSelected = 0;
for (int i = 0; i < 100; i++) {
final selected = selector.selectQuestions(
candidates: questions,
count: 1,
currentAttemptIndex: 0,
);
if (selected.first.id == 'high') {
highSelected++;
}
}
// High priority should be selected more often
expect(highSelected, greaterThan(50));
});
});
}

Powered by TurnKey Linux.