Skip to content

Commit dc04a6d

Browse files
author
Jarrod Parkes
committed
add domino set and tests
1 parent dd21c6e commit dc04a6d

File tree

4 files changed

+134
-1
lines changed

4 files changed

+134
-1
lines changed

Sources/DominoSet.swift

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
//
2+
// DominoSet.swift
3+
// DominoKit
4+
//
5+
// Created by Jarrod Parkes on 1/31/17.
6+
// Copyright © 2017 Jarrod Parkes. All rights reserved.
7+
//
8+
9+
import FisherYates
10+
11+
// MARK: - DominoSet
12+
13+
public struct DominoSet {
14+
15+
// MARK: Properties
16+
17+
fileprivate var dominoes: [Domino]
18+
19+
// MARK: Computed Properties
20+
21+
public var count: Int {
22+
return dominoes.count
23+
}
24+
25+
// MARK: Initializers
26+
27+
public init(_ dominoes: [Domino]) {
28+
self.dominoes = dominoes
29+
}
30+
31+
public static func standardSet(_ highestSuit: Suit) -> DominoSet {
32+
33+
var dominoes: [Domino] = []
34+
for suitValueOne in 0...highestSuit.rawValue {
35+
for suitValueTwo in 0...suitValueOne {
36+
dominoes.append(Domino(suitOne: Suit(rawValue: suitValueOne)!,
37+
suitTwo: Suit(rawValue: suitValueTwo)!))
38+
}
39+
}
40+
41+
return DominoSet(dominoes)
42+
}
43+
44+
// MARK: Mutators
45+
46+
public mutating func shuffle() {
47+
dominoes.shuffle()
48+
}
49+
50+
public mutating func deal() -> Domino? {
51+
guard !dominoes.isEmpty else { return nil }
52+
return dominoes.removeLast()
53+
}
54+
}
55+
56+
// MARK: - DominoSet: ExpressibleByArrayLiteral
57+
58+
extension DominoSet: ExpressibleByArrayLiteral {
59+
public init(arrayLiteral elements: Domino...) {
60+
self.init(elements)
61+
}
62+
}
63+
64+
// MARK: - DominoSet: Equatable
65+
66+
extension DominoSet: Equatable {}
67+
68+
public func ==(lhs: DominoSet, rhs: DominoSet) -> Bool {
69+
return lhs.dominoes == rhs.dominoes
70+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
@testable import DominoKit
2+
3+
import XCTest
4+
5+
// MARK: - DominoSetTests: XCTestCase
6+
7+
class DominoSetTests: XCTestCase {
8+
func testDominoSetInit() {
9+
let highestSuit = Suit.six
10+
var ranks = [Int: Bool]()
11+
var doubleSixSet = DominoSet.standardSet(highestSuit)
12+
13+
XCTAssertEqual(doubleSixSet.count,
14+
(highestSuit.rawValue + 1) * (highestSuit.rawValue + 2) / 2)
15+
16+
for suitValueOne in 0...highestSuit.rawValue {
17+
for suitTwoValue in 0...suitValueOne {
18+
ranks[suitValueOne * 10 + suitTwoValue * 100] = false
19+
}
20+
}
21+
while let domino = doubleSixSet.deal() {
22+
ranks[domino.suitOne.rawValue * 10 + domino.suitTwo.rawValue * 100] = true
23+
}
24+
25+
for (_, rankFound) in ranks {
26+
XCTAssert(rankFound)
27+
}
28+
}
29+
30+
func testDominoSetCount() {
31+
var dominoSet = DominoSet([Domino(doubleSuit: .zero), Domino(doubleSuit: .six)])
32+
33+
XCTAssertEqual(dominoSet.count, 2)
34+
let _ = dominoSet.deal()
35+
XCTAssertEqual(dominoSet.count, 1)
36+
let _ = dominoSet.deal()
37+
XCTAssertEqual(dominoSet.count, 0)
38+
}
39+
40+
func testDominoSetDeal() {
41+
let doubleSix = Domino(doubleSuit: .six)
42+
var dominoSet = DominoSet([doubleSix])
43+
44+
XCTAssertEqual(dominoSet.deal(), doubleSix)
45+
XCTAssertNil(dominoSet.deal())
46+
}
47+
}

Tests/DominoKitTests/XCTestManifests.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// MARK: - SuitTests (All Tests)
2+
13
extension SuitTests {
24

35
static var allTests = [
@@ -7,6 +9,8 @@ extension SuitTests {
79
]
810
}
911

12+
// MARK: - DominoTests (All Tests)
13+
1014
extension DominoTests {
1115

1216
static var allTests = [
@@ -20,3 +24,14 @@ extension DominoTests {
2024
("testDominoEquatable", testDominoEquatable),
2125
]
2226
}
27+
28+
// MARK: - DominoSetTests (All Tests)
29+
30+
extension DominoSetTests {
31+
32+
static var allTests = [
33+
("testDominoSetInit", testDominoSetInit),
34+
("testDominoSetCount", testDominoSetCount),
35+
("testDominoSetDeal", testDominoSetDeal)
36+
]
37+
}

Tests/LinuxMain.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ import XCTest
44

55
XCTMain([
66
testCase(SuitTests.allTests),
7-
testCase(DominoTests.allTests),
7+
testCase(DominoTests.allTests),
8+
testCase(DominoSetTests.allTests)
89
])

0 commit comments

Comments
 (0)