forked from lichess-org/lila
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPairingTest.scala
162 lines (154 loc) · 4.78 KB
/
PairingTest.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package lila
package tournament
import tournament._
import ornicar.scalalib.Random.nextString
class PairingTest extends LilaSpec {
import Pairing._
val u1 = "u1"
val u2 = "u2"
val u3 = "u3"
val u4 = "u4"
val u5 = "u5"
val u6 = "u6"
"All pair combinations" should {
//"2 elems" in {
//val elems = List(u1, u2)
//allPairCombinations(elems) must contain(List(u1 -> u2)).only
//}
"4 elems" in {
val elems = List(u1, u2, u3, u4)
allPairCombinations(elems) must contain(
List(u1 -> u2, u3 -> u4),
List(u1 -> u3, u2 -> u4),
List(u1 -> u4, u2 -> u3)).only
}
"6 elems" in {
val elems = List(u1, u2, u3, u4, u5, u6)
"size" in {
allPairCombinations(elems).size must_== 15
}
"pairings" in {
allPairCombinations(elems) must contain(
List(u1 -> u2, u3 -> u4, u5 -> u6),
List(u1 -> u2, u3 -> u5, u4 -> u6),
List(u1 -> u2, u3 -> u6, u4 -> u5),
List(u1 -> u3, u2 -> u4, u5 -> u6),
List(u1 -> u3, u2 -> u5, u4 -> u6),
List(u1 -> u3, u2 -> u6, u4 -> u5),
List(u1 -> u4, u2 -> u3, u5 -> u6),
List(u1 -> u4, u2 -> u5, u3 -> u6),
List(u1 -> u4, u2 -> u6, u3 -> u5),
List(u1 -> u4, u2 -> u3, u5 -> u6),
List(u1 -> u5, u2 -> u3, u4 -> u6),
List(u1 -> u6, u2 -> u3, u4 -> u5))
}
}
"14 elems" in {
val elems = List.fill(14)(nextString(8)).pp
"size" in {
allPairCombinations(elems).size must_== 3 * 5 * 7 * 9 * 11 * 13
}
}
}
"Create new pairings" should {
"initial pairings" in {
"even players" in {
val users = List.fill(100)(nextString(8))
pairs(users, Nil) must have size 50
}
"odd players" in {
val users = List.fill(99)(nextString(8))
pairs(users, Nil) must have size 49
}
}
"2 players" in {
val users = List(u1, u2)
"first pairing" in {
val pairings = List[Pairing]()
pairs(users, pairings) must_== List(u1 -> u2)
}
"finished pairing" in {
val pairings = List(mate(u1, u2))
pairs(users, pairings) must_== List(u1 -> u2)
}
"started pairing" in {
val pairings = List(started(u1, u2))
pairs(users, pairings) must_== Nil
}
"finished and started pairing" in {
val pairings = List(mate(u1, u2), started(u1, u2))
pairs(users, pairings) must_== Nil
}
}
}
"3 players" should {
val users = List(u1, u2, u3)
"first pairing" in {
val pairings = List[Pairing]()
pairs(users, pairings) must beOneOf(List(u1 -> u2), List(u1 -> u3), List(u2 -> u3))
}
"finished pairing" in {
val pairings = List(mate(u1, u2))
pairs(users, pairings) must beOneOf(List(u1 -> u3), List(u2 -> u3))
}
"started pairing" in {
val pairings = List(started(u1, u2))
pairs(users, pairings) must_== Nil
}
"finished and started pairing" in {
val pairings = List(mate(u1, u2), started(u1, u3))
pairs(users, pairings) must_== Nil
}
"many finished pairings" in {
"without ambiguity" in {
val pairings = List(mate(u1, u3), mate(u1, u2))
pairs(users, pairings) must beOneOf(List(u2 -> u3), List(u1 -> u2))
}
"favor longer idle" in {
val pairings = List(mate(u2, u3), mate(u1, u3), mate(u1, u2))
pairs(users, pairings) must beOneOf(List(u1 -> u2), List(u1 -> u3))
}
}
}
"4 players" should {
val users = List(u1, u2, u3, u4)
"finished pairing" in {
"one finished" in {
val pairings = List(mate(u1, u2))
"size" in {
pairs(users, pairings) must have size 2
}
"no rematch" in {
pairs(users, pairings) must not contain(u1 -> u2)
}
}
"two finished" in {
val pairings = List(mate(u1, u2), mate(u3, u4))
"size" in {
pairs(users, pairings) must have size 2
}
"no rematch" in {
pairs(users, pairings) must beOneOf(
List(u1 -> u3, u2 -> u4),
List(u1 -> u4, u2 -> u3))
}
}
}
"started pairing" in {
val pairings = List(started(u1, u2))
pairs(users, pairings) must_== List(u3 -> u4)
}
"finished and started pairing" in {
val pairings = List(started(u3, u4), mate(u1, u2))
pairs(users, pairings) must_== Nil
}
}
private def pairs(users: List[String], pairings: Pairings): List[(String, String)] =
createNewPairings(users, pairings, users.size) map (_.usersPair) map {
case (a, b) ⇒ (a < b).fold(a -> b, b -> a)
}
private def started(a: String, b: String) =
Pairing(a, b) withStatus chess.Status.Started
private def mate(a: String, b: String) =
Pairing(a, b) withStatus chess.Status.Mate
}