DayByDayTests/RoundTests.swift
239 lines · 10227 bytes
25 symbols in this file
RoundTestschoicesIncludeAnswerAndAreDistinctchoiceCountClampsToItemCountneverRepeatsPreviousAnswerpromptUsesClosurewrongAnswerRetriesSameQuestiontapsWhileAwaitingNextAreIgnoredfinishesAfterFiveCorrecttapsDuringCorrectionAreIgnoredfinalAnswerCelebratesBeforeCompletingchoicesGrowWithinRounddistractorsComeFromTheirPoolsmallAnswerPoolStillVariessoundComesFromClosurecorrectionUsesFactWhenPresentoddOneOutShapeoddOneOutNeverRepeatsPreviousoddOneOutFallsBackToThreechoiceCountGrowsnewRoundAvoidsPreviousAnswerweightedPickFavorsLearningallLearningStillVariesweightsKeepPreviousRuleemptyWeightsMatchOldPicksoddOneOutUsesWeights
1import Testing
2@testable import DayByDay
3
4struct RoundTests {
5 let days = Category.days.quizItems
6 var picker: QuestionPicker { QuestionPicker(answers: days) { "Can you find \($0.name)?" } }
7
8 @Test func choicesIncludeAnswerAndAreDistinct() {
9 var rng = SeededGenerator(seed: 1)
10 for _ in 0..<50 {
11 let q = picker.next(correctCount: 4, excluding: nil, using: &rng)
12 #expect(q.choices.count == 4)
13 #expect(q.choices.contains(q.answer))
14 #expect(Set(q.choices).count == 4)
15 }
16 }
17
18 @Test func choiceCountClampsToItemCount() {
19 var rng = SeededGenerator(seed: 2)
20 let small = QuestionPicker(answers: Array(Category.seasons.quizItems.prefix(2))) { $0.name }
21 let q = small.next(correctCount: 4, excluding: nil, using: &rng)
22 #expect(q.choices.count == 2)
23 }
24
25 @Test func neverRepeatsPreviousAnswer() {
26 var rng = SeededGenerator(seed: 3)
27 let previous = days[0]
28 for _ in 0..<200 {
29 #expect(picker.next(correctCount: 0, excluding: previous, using: &rng).answer != previous)
30 }
31 }
32
33 @Test func promptUsesClosure() {
34 var rng = SeededGenerator(seed: 4)
35 let q = picker.next(correctCount: 0, excluding: nil, using: &rng)
36 #expect(q.prompt == "Can you find \(q.answer.name)?")
37 }
38
39 @Test func wrongAnswerRetriesSameQuestion() {
40 var round = Round(source: picker, rng: SeededGenerator(seed: 5))
41 let question = round.current
42 let wrong = question.choices.first { $0 != question.answer }!
43 #expect(round.answer(wrong) == .incorrect(correctName: question.answer.name))
44 #expect(round.current == question)
45 #expect(round.correctCount == 0)
46 #expect(!round.isAwaitingNext)
47 }
48
49 @Test func tapsWhileAwaitingNextAreIgnored() {
50 var round = Round(source: picker, rng: SeededGenerator(seed: 6))
51 let answer = round.current.answer
52 #expect(round.answer(answer) == .correct)
53 #expect(round.isAwaitingNext)
54 #expect(round.answer(answer) == .ignored)
55 #expect(round.answer(round.current.choices.first { $0 != answer }!) == .ignored)
56 #expect(round.correctCount == 1)
57 }
58
59 @Test func finishesAfterFiveCorrect() {
60 var round = Round(source: picker, rng: SeededGenerator(seed: 7))
61 for _ in 0..<Round.length {
62 #expect(!round.isFinished)
63 _ = round.answer(round.current.answer)
64 round.advance()
65 }
66 #expect(round.isFinished)
67 #expect(round.correctCount == 5)
68 }
69
70 @Test func tapsDuringCorrectionAreIgnored() {
71 var round = Round(source: picker, rng: SeededGenerator(seed: 8))
72 let question = round.current
73 let wrong = question.choices.filter { $0 != question.answer }
74 #expect(round.answer(wrong[0]) == .incorrect(correctName: question.answer.name))
75 #expect(round.answer(wrong[1]) == .ignored)
76 #expect(round.answer(question.answer) == .ignored)
77 round.resume()
78 #expect(round.answer(question.answer) == .correct)
79 }
80
81 @Test func finalAnswerCelebratesBeforeCompleting() {
82 var round = Round(source: picker, rng: SeededGenerator(seed: 9))
83 for _ in 0..<(Round.length - 1) {
84 _ = round.answer(round.current.answer)
85 round.advance()
86 }
87 _ = round.answer(round.current.answer)
88 #expect(round.isFinished)
89 #expect(!round.isComplete)
90 round.advance()
91 #expect(round.isComplete)
92 }
93
94 @Test func choicesGrowWithinRound() {
95 var round = Round(source: picker, rng: SeededGenerator(seed: 10))
96 var counts: [Int] = []
97 for _ in 0..<Round.length {
98 counts.append(round.current.choices.count)
99 _ = round.answer(round.current.answer)
100 round.advance()
101 }
102 #expect(counts == [3, 3, 4, 4, 4])
103 }
104
105 @Test func distractorsComeFromTheirPool() {
106 var rng = SeededGenerator(seed: 11)
107 let source = QuestionPicker(answers: Array(days.prefix(3)), distractors: days) { $0.name }
108 var distractorsSeen = Set<QuizItem>()
109 for _ in 0..<300 {
110 let q = source.next(correctCount: 4, excluding: nil, using: &rng)
111 #expect(days.prefix(3).contains(q.answer))
112 #expect(q.choices.allSatisfy(days.contains))
113 #expect(Set(q.choices).count == 4)
114 distractorsSeen.formUnion(q.choices.filter { $0 != q.answer })
115 }
116 #expect(distractorsSeen.contains { !days.prefix(3).contains($0) })
117 }
118
119 @Test func smallAnswerPoolStillVaries() {
120 var rng = SeededGenerator(seed: 12)
121 let source = QuestionPicker(answers: Array(days.prefix(2)), distractors: days) { $0.name }
122 for _ in 0..<100 {
123 #expect(source.next(correctCount: 0, excluding: days[0], using: &rng).answer == days[1])
124 }
125 }
126
127 @Test func soundComesFromClosure() {
128 var rng = SeededGenerator(seed: 13)
129 let source = QuestionPicker(answers: days, prompt: { $0.name }, sound: { _ in .moo })
130 #expect(source.next(correctCount: 0, excluding: nil, using: &rng).sound == .moo)
131 #expect(picker.next(correctCount: 0, excluding: nil, using: &rng).sound == nil)
132 }
133
134 let food = Category.food
135 var fruits: [QuizItem] { LearnFood.allCases.filter(\.isFruit).map(food.item) }
136 var vegetables: [QuizItem] { LearnFood.allCases.filter { !$0.isFruit }.map(food.item) }
137 var fruitOrVegetable: OddOneOut {
138 OddOneOut(.init(members: fruits, question: "Which one is a fruit?") { "\($0.name) is a fruit." },
139 .init(members: vegetables, question: "Which one is a vegetable?") { "\($0.name) is a vegetable." })
140 }
141
142 @Test func correctionUsesFactWhenPresent() {
143 let apple = food.item(LearnFood.apple), carrot = food.item(LearnFood.carrot)
144 let q = Question(prompt: "", choices: [apple, carrot], answer: carrot, facts: [apple: "Apple is a fruit."])
145 #expect(q.correction(for: apple) == "Apple is a fruit. Try again!")
146 let plain = Question(prompt: "", choices: [apple, carrot], answer: carrot)
147 #expect(plain.correction(for: apple) == "That's Carrot! Try again.")
148 }
149
150 @Test func oddOneOutShape() {
151 var rng = SeededGenerator(seed: 14)
152 for correctCount in [0, 4] {
153 for _ in 0..<300 {
154 let q = fruitOrVegetable.next(correctCount: correctCount, excluding: nil, using: &rng)
155 let oddGroup = fruits.contains(q.answer) ? fruits : vegetables
156 #expect(q.choices.filter(oddGroup.contains) == [q.answer])
157 #expect(q.choices.count == Round.choiceCount(correctCount: correctCount))
158 #expect(Set(q.choices).count == q.choices.count)
159 #expect(q.facts.count == q.choices.count)
160 #expect(q.prompt == (oddGroup == fruits ? "Which one is a fruit?" : "Which one is a vegetable?"))
161 }
162 }
163 }
164
165 @Test func oddOneOutNeverRepeatsPrevious() {
166 var rng = SeededGenerator(seed: 15)
167 let previous = vegetables[0]
168 for _ in 0..<200 {
169 #expect(fruitOrVegetable.next(correctCount: 0, excluding: previous, using: &rng).answer != previous)
170 }
171 }
172
173 @Test func oddOneOutFallsBackToThree() {
174 var rng = SeededGenerator(seed: 16)
175 let source = OddOneOut(.init(members: Array(fruits.prefix(2)), question: "") { $0.name },
176 .init(members: Array(vegetables.prefix(2)), question: "") { $0.name })
177 #expect(source.next(correctCount: 4, excluding: nil, using: &rng).choices.count == 3)
178 }
179
180 @Test func choiceCountGrows() {
181 #expect((0...4).map(Round.choiceCount) == [3, 3, 4, 4, 4])
182 }
183
184 @Test func newRoundAvoidsPreviousAnswer() {
185 for seed in 0..<200 {
186 let round = Round(source: picker, after: days[3], rng: SeededGenerator(seed: UInt64(seed)))
187 #expect(round.current.answer != days[3])
188 }
189 }
190
191 @Test func weightedPickFavorsLearning() {
192 var weighted = picker
193 weighted.weights = [days[3]: 3]
194 var rng = SeededGenerator(seed: 70)
195 let hits = (0..<2000).filter { _ in weighted.next(correctCount: 0, excluding: nil, using: &rng).answer == days[3] }.count
196 // 3 of 9 shares, against 1 of 7 unweighted.
197 #expect((500...900).contains(hits))
198 }
199
200 @Test func allLearningStillVaries() {
201 var weighted = picker
202 weighted.weights = Dictionary(uniqueKeysWithValues: days.map { ($0, 3.0) })
203 var rng = SeededGenerator(seed: 71)
204 let answers = Set((0..<200).map { _ in weighted.next(correctCount: 0, excluding: nil, using: &rng).answer })
205 #expect(answers.count == days.count)
206 }
207
208 @Test func weightsKeepPreviousRule() {
209 var weighted = picker
210 weighted.weights = [days[0]: 3]
211 var rng = SeededGenerator(seed: 72)
212 for _ in 0..<200 {
213 #expect(weighted.next(correctCount: 0, excluding: days[0], using: &rng).answer != days[0])
214 }
215 var single = QuestionPicker(answers: [days[0]], distractors: days) { $0.name }
216 single.weights = [days[0]: 3]
217 #expect(single.next(correctCount: 0, excluding: days[0], using: &rng).answer == days[0])
218 }
219
220 @Test func emptyWeightsMatchOldPicks() {
221 var a = SeededGenerator(seed: 73), b = SeededGenerator(seed: 73)
222 var unweighted = picker
223 unweighted.weights = [:]
224 for _ in 0..<50 {
225 #expect(picker.next(correctCount: 2, excluding: nil, using: &a) == unweighted.next(correctCount: 2, excluding: nil, using: &b))
226 }
227 }
228
229 @Test func oddOneOutUsesWeights() {
230 guard var source = Category.animals.questionSource(for: .oddOneOut) as? OddOneOut else {
231 Issue.record("not odd one out"); return
232 }
233 let bird = Category.animals.item(LearnAnimal.bird)
234 source.weights = [bird: 50]
235 var rng = SeededGenerator(seed: 74)
236 let hits = (0..<400).filter { _ in source.next(correctCount: 0, excluding: nil, using: &rng).answer == bird }.count
237 #expect(hits > 120)
238 }
239}