1
exercism fetch swift poker

Tests/LinuxMain.swift

1
2
3
4
5
6
import XCTest
@testable import PokerTests

XCTMain([
    testCase(PokerTests.allTests),
    ])

Tests/PokerTests/PokerTests.swift

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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
import XCTest
@testable import Poker

class PokerTests: XCTestCase {
    var validTestCases:[(name: String, hands: [String], best: String)] = []
    var invalidTestCases:[(name: String, hand: String)] = []

    func testInvalidCases() {
        for each in invalidTestCases {
            XCTAssertNil(PokerHand(each.hand), "\(each.name)")
        }
    }

    func testAllValid() {
        for each in validTestCases {
            XCTAssertEqual(Poker.bestHand(each.hands), each.best, "\(each.name)")
        }
    }

    override func setUp() {
        super.setUp()

        validTestCases = [
            (
                name:  "single hand is always best",
                hands: ["3♡ 10♢ 7♧ 8♤ A♢"],
                best:  "3♡ 10♢ 7♧ 8♤ A♢"
            ),
            (
                name:  "highest card",
                hands: ["3♢ 2♢ 5♤ 6♤ 9♡", "3♡ 2♡ 5♧ 6♢ 10♡"],
                best:  "3♡ 2♡ 5♧ 6♢ 10♡"
            ),
            (
                name:  "One pair",
                hands: ["3♢ 2♢ 5♤ 6♤ 9♡", "3♡ 3♤ 5♧ 6♢ 9♢"],
                best:  "3♡ 3♤ 5♧ 6♢ 9♢"
            ),
            (
                name:  "pair beats lower",
                hands: ["4♢ 3♤ 4♤ J♤ K♤", "A♡ K♡ J♢ 10♧ 9♡"],
                best:  "4♢ 3♤ 4♤ J♤ K♤"
            ),
            (
                name:  "best pair",
                hands: ["4♡ 2♡ 5♧ 4♢ 10♡", "3♢ 3♡ 5♤ 6♤ 9♡"],
                best:  "4♡ 2♡ 5♧ 4♢ 10♡"
            ),
            (
                name:  "best pair with same pair and highest cards",
                hands: ["4♡ 2♡ 5♧ 4♢ 10♡", "4♤ 4♧ 5♡ 10♢ 3♡"],
                best:  "4♤ 4♧ 5♡ 10♢ 3♡"
            ),
            (
                name: "two pair beats lower",
                hands: [
                    "4♢ 3♤ 4♤ J♤ K♤",
                    "A♡ K♡ J♢ 10♧ 9♡",
                    "2♢ 8♡ 5♢ 2♡ 8♧"
                ],
                best:  "2♢ 8♡ 5♢ 2♡ 8♧"
            ),
            (
                name: "best two pair",
                hands: [
                    "4♢ J♧ 4♤ J♤ K♤",
                    "A♡ K♡ J♢ 10♧ 9♡",
                    "2♢ 8♡ 5♢ 2♡ 8♧"
                ],
                best:  "4♢ J♧ 4♤ J♤ K♤"
            ),
            (
                name: "best two pair with equal highest pair",
                hands: [
                    "4♢ J♧ 4♤ J♤ K♤",
                    "A♡ K♡ J♢ 10♧ 9♡",
                    "3♢ J♡ 5♢ 3♡ J♢"
                ],
                best:  "4♢ J♧ 4♤ J♤ K♤"
            ),
            (
                name: "best two pair with equal pairs",
                hands: [
                    "4♢ J♧ 4♤ J♤ 2♤",
                    "A♡ K♡ J♢ 10♧ 9♡",
                    "4♧ J♡ 5♢ 4♡ J♢"
                ],
                best:  "4♧ J♡ 5♢ 4♡ J♢"
            ),
            (
                name: "full house",
                hands: [
                    "4♢ 3♤ 4♤ J♤ K♤",
                    "A♡ K♡ J♢ 10♧ 9♡",
                    "3♡ 8♡ 3♢ 3♧ 8♧",
                    "2♢ 8♡ 5♢ 2♡ 8♧"
                ],
                best:  "3♡ 8♡ 3♢ 3♧ 8♧"
            ),
            (
                name: "best three of a kind",
                hands: [
                    "4♢ 3♤ 4♤ J♤ 4♡",
                    "A♡ K♡ J♢ 10♧ 9♡",
                    "3♢ 8♡ 3♡ 3♧ 9♧",
                    "2♢ 8♡ 5♢ 2♡ 8♧"
                ],
                best:  "4♢ 3♤ 4♤ J♤ 4♡"
            ),
            (
                name: "straight beats lower",
                hands: [
                    "4♢ 3♤ 4♤ J♤ K♤",
                    "Q♡ K♡ J♢ 10♧ 9♡",
                    "3♡ 8♡ 3♢ 3♧ 9♧",
                    "2♢ 8♡ 5♢ 2♡ 8♧"
                ],
                best:  "Q♡ K♡ J♢ 10♧ 9♡"
            ),
            (
                name: "straight includes ace as one",
                hands: [
                    "4♢ 3♤ 4♤ J♤ K♤",
                    "2♤ 3♡ A♤ 5♤ 4♤",
                    "3♢ 8♡ 3♡ 3♧ 9♧",
                    "2♢ 8♡ 5♢ 2♡ 8♧"
                ],
                best:  "2♤ 3♡ A♤ 5♤ 4♤"
            ),
            (
                name: "best straight",
                hands: [
                    "4♢ 3♤ 4♤ J♤ K♤",
                    "Q♡ K♡ J♢ 10♧ 9♡",
                    "A♢ K♧ 10♢ J♢ Q♢",
                    "2♢ 8♡ 5♢ 2♡ 8♧"
                ],
                best:  "A♢ K♧ 10♢ J♢ Q♢"
            ),
            (
                name: "flush beats lower",
                hands: [
                    "4♤ 3♤ 8♤ J♤ K♤",
                    "Q♡ K♡ J♢ 10♧ 9♡",
                    "3♢ 8♡ 3♢ 3♧ 9♧",
                    "2♢ 8♡ 5♢ 2♡ 8♧"
                ],
                best:  "4♤ 3♤ 8♤ J♤ K♤"
            ),
            (
                name: "best flush",
                hands: [
                    "4♤ 3♤ 8♤ J♤ K♤",
                    "Q♡ K♡ J♢ 10♧ 9♡",
                    "3♢ 8♢ A♢ 2♢ 7♢",
                    "2♢ 8♡ 5♢ 2♡ 8♧"
                ],
                best:  "3♢ 8♢ A♢ 2♢ 7♢"
            ),
            (
                name: "full house beats lower",
                hands: [
                    "4♤ 3♤ 8♤ J♤ K♤",
                    "2♢ 8♡ 8♢ 2♡ 8♧",
                    "Q♡ K♡ J♢ 10♧ 9♡",
                    "3♡ A♡ 3♢ 3♧ A♧"
                ],
                best:  "2♢ 8♡ 8♢ 2♡ 8♧"
            ),
            (
                name: "best full house",
                hands: [
                    "4♤ 3♤ 8♤ J♤ K♤",
                    "2♢ 8♡ 8♢ 2♡ 8♧",
                    "5♡ 5♢ A♤ 5♧ A♢",
                    "3♡ A♡ 3♢ 3♧ A♧"
                ],
                best:  "2♢ 8♡ 8♢ 2♡ 8♧"
            ),
            (
                name: "four of a kind beats lower",
                hands: [
                    "4♤ 5♤ 8♤ J♤ K♤",
                    "2♢ 8♡ 8♢ 2♡ 8♧",
                    "Q♡ K♡ J♢ 10♧ 9♡",
                    "3♢ 3♡ 3♤ 3♧ A♧"
                ],
                best:  "3♢ 3♡ 3♤ 3♧ A♧"
            ),
            (
                name: "best four of a kind",
                hands: [
                    "4♤ 5♤ 8♤ J♤ K♤",
                    "2♢ 2♧ 8♢ 2♡ 2♤",
                    "Q♡ K♡ J♢ 10♧ 9♡",
                    "3♢ 3♡ 3♤ 3♧ A♧"
                ],
                best:  "3♢ 3♡ 3♤ 3♧ A♧"
            ),
            (
                name: "straight flush beats lower",
                hands: [
                    "4♤ 4♢ 4♡ 4♧ K♤",
                    "2♢ 8♡ 8♢ 2♡ 8♧",
                    "Q♡ K♡ 8♡ 10♡ 9♡",
                    "2♤ 3♤ A♤ 5♤ 4♤"
                ],
                best:  "2♤ 3♤ A♤ 5♤ 4♤"
            ),
            (
                name: "best straight flush is royal flush",
                hands: [
                    "4♤ 5♤ 8♤ J♤ K♤",
                    "2♢ 8♡ 8♢ 2♡ 8♧",
                    "Q♡ K♡ J♡ 10♡ 9♡",
                    "Q♢ K♢ J♢ 10♢ A♢"
                ],
                best:  "Q♢ K♢ J♢ 10♢ A♢"
            ),
            (
                name:  "tie for best pair: brake tide by suit",
                hands: ["4♡ 2♡ 5♧ 4♢ 10♡", "4♧ 10♢ 5♤ 2♤ 4♤"],
                best:  "4♡ 2♡ 5♧ 4♢ 10♡"
            ),
            (
                name: "tie of three: brake tide by suit",
                hands: [
                    "A♡ 2♡ 3♡ 4♡ 5♡",
                    "A♤ 2♤ 3♤ 4♤ 5♤",
                    "5♧ 4♧ 3♧ 2♧ A♧",
                    "A♢ 2♢ 6♢ 4♢ 5♢"
                ],
                best:  "A♤ 2♤ 3♤ 4♤ 5♤"
            )
        ]

        invalidTestCases  =
            [
                (
                    name: "1 is an invalid card rank",
                    hand: "1♢ 2♡ 3♡ 4♡ 5♡"
                ),
                (
                    name: "15 is an invalid card rank",
                    hand: "15♢ 2♡ 3♡ 4♡ 5♡"
                ),
                (
                    name: "too few cards",
                    hand: "2♡ 3♡ 4♡ 5♡"
                ),
                (
                    name: "too many cards",
                    hand: "2♡ 3♡ 4♡ 5♡ 6♡ 7♡"
                ),
                (
                    name: "lack of rank",
                    hand: "11♢ 2♡ ♡ 4♡ 5♡"
                ),
                (
                    name: "lack of suit",
                    hand: "2♡ 3♡ 4 5♡ 7♡"
                ),
                (
                    name: "H is an invalid suit",
                    hand: "2♡ 3♡ 4H 5♡ 7♡"
                ),
                (
                    name: "♥ is an invalid suit",
                    hand: "2♡ 3♡ 4♥ 5♡ 7♡"
                ),
                (
                    name: "lack of spacing",
                    hand: "2♡ 3♡ 5♡7♡ 8♡"
                ),
                (
                    name: "double suits after rank",
                    hand: "2♡ 3♡ 5♡♡ 8♡ 9♡"
                )
        ]

    }

    static var allTests: [(String, (PokerTests) -> () throws -> Void)] {
        return [
            ("testInvalidCases", testInvalidCases),
            ("testAllValid", testAllValid),
        ]
    }
}