-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from mvirgo/bracket-file-tests
test: Basic tests for brackets, probabilities and teams
- Loading branch information
Showing
5 changed files
with
354 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// | ||
// foam_madness_tests.swift | ||
// foam-madness-tests | ||
// | ||
// Created by Michael Virgo on 3/11/23. | ||
// Copyright © 2023 mvirgo. All rights reserved. | ||
// | ||
|
||
import XCTest | ||
|
||
final class foam_madness_bracket_file_tests: XCTestCase { | ||
let bracketIndexFile = "bracketIndex" | ||
|
||
var brackets: [Dictionary<String, String>] = [] | ||
var currentBracketName: String = "" | ||
var hasFirstFour = false | ||
var regions = [String: [String: Int16]]() | ||
|
||
override func setUpWithError() throws { | ||
loadBrackets(); | ||
} | ||
|
||
override func tearDownWithError() throws {} | ||
|
||
func loadBrackets() { | ||
// Load the bracket index | ||
let path = Bundle.main.path(forResource: bracketIndexFile, ofType: "plist")! | ||
brackets = NSArray(contentsOfFile: path) as! [Dictionary<String, String>] | ||
} | ||
|
||
func loadSingleBracket(bracket: Dictionary<String, String>) { | ||
currentBracketName = bracket.first!.value; | ||
let bracketLocation = bracket.first!.key; | ||
// Load the given bracket | ||
let bracketPath = Bundle.main.path(forResource: bracketLocation, ofType: "plist")! | ||
let bracketDict = NSDictionary(contentsOfFile: bracketPath)! | ||
let isWomens = bracketDict.value(forKey: "IsWomens") as! Bool | ||
let year = bracketDict.value(forKey: "Year") as! Int | ||
hasFirstFour = !isWomens || year >= 2022 | ||
regions = bracketDict.value(forKey: "Regions") as! Dictionary<String, [String: Int16]> | ||
} | ||
|
||
func hasValidTeamCount(count: Int) -> Bool { | ||
return (hasFirstFour && count == 68) || (!hasFirstFour && count == 64) | ||
} | ||
|
||
func testFourRegions() throws { | ||
brackets.forEach { | ||
loadSingleBracket(bracket: $0) | ||
if regions.count != 4 { | ||
XCTFail("Bracket \(currentBracketName) does not have four regions.") | ||
} | ||
} | ||
} | ||
|
||
func testNumberOfTeams() throws { | ||
brackets.forEach { | ||
var count = 0 | ||
loadSingleBracket(bracket: $0) | ||
regions.forEach { | ||
count += $0.value.count | ||
} | ||
if (!hasValidTeamCount(count: count)) { | ||
XCTFail("Bracket \(currentBracketName) does not have the correct number of teams.") | ||
} | ||
} | ||
} | ||
|
||
func testNoDuplicateTeams() throws { | ||
brackets.forEach { | ||
var teamIdsSet = Set<Int16>() | ||
loadSingleBracket(bracket: $0) | ||
regions.forEach { | ||
$0.value.forEach { | ||
teamIdsSet.insert($0.value) | ||
} | ||
} | ||
if (!hasValidTeamCount(count: teamIdsSet.count)) { | ||
XCTFail("Bracket \(currentBracketName) has a duplicate team.") | ||
} | ||
} | ||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
foam-madness-tests/foam_madness_probability_file_test.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// | ||
// foam_madness_probability_file_test.swift | ||
// foam-madness-tests | ||
// | ||
// Created by Michael Virgo on 3/11/23. | ||
// Copyright © 2023 mvirgo. All rights reserved. | ||
// | ||
|
||
import XCTest | ||
|
||
final class foam_madness_probability_file_test: XCTestCase { | ||
let mensProbabilitiesFile = "historicalProbabilities" | ||
let womensProbabilitiesFile = "historicalProbabilitiesWomen" | ||
|
||
var mensProbabilitiesDict = NSDictionary() | ||
var womensProbabilitiesDict = NSDictionary() | ||
|
||
override func setUpWithError() throws { | ||
loadProbabilities() | ||
} | ||
|
||
override func tearDownWithError() throws {} | ||
|
||
func loadProbabilities() { | ||
let mensPath = Bundle.main.path(forResource: mensProbabilitiesFile, ofType: "plist")! | ||
let womensPath = Bundle.main.path(forResource: womensProbabilitiesFile, ofType: "plist")! | ||
mensProbabilitiesDict = NSDictionary(contentsOfFile: mensPath)! | ||
womensProbabilitiesDict = NSDictionary(contentsOfFile: womensPath)! | ||
} | ||
|
||
func logFailureMessage(gender: String, message: String) { | ||
XCTFail(gender + message) | ||
} | ||
|
||
func haveProbabilitiesForEachMatchup(dict: NSDictionary) -> Bool { | ||
for i in 1...16 { | ||
let seedProbabilities = dict.object(forKey: "\(i)") as! Dictionary<String, NSNumber> | ||
// Set up is such that #1 seed has all 16 probabilties, #2 has all but against #1, etc (since duplicative) | ||
if (seedProbabilities.count != 16 - i + 1) { | ||
return false | ||
} | ||
} | ||
return true | ||
} | ||
|
||
func allProbabilitiesValid(dict: NSDictionary) -> Bool { | ||
for i in 1...16 { | ||
let seedProbabilities = dict.object(forKey: "\(i)") as! Dictionary<String, NSNumber> | ||
for prob in seedProbabilities { | ||
if prob.value.floatValue < 0.0 || prob.value.floatValue > 1.0 { | ||
return false | ||
} | ||
} | ||
} | ||
return true | ||
} | ||
|
||
func testHaveProbabilitiesForEachMatchup() throws { | ||
let failMessage = " file had missing probabilities" | ||
if (!haveProbabilitiesForEachMatchup(dict: mensProbabilitiesDict)) { | ||
logFailureMessage(gender: "Men's", message: failMessage) | ||
} | ||
if (!haveProbabilitiesForEachMatchup(dict: womensProbabilitiesDict)) { | ||
logFailureMessage(gender: "Women's", message: failMessage) | ||
} | ||
} | ||
|
||
func testAllProbabilitiesValid() throws { | ||
let failMessage = " file had invalid probabilities" | ||
if (!allProbabilitiesValid(dict: mensProbabilitiesDict)) { | ||
logFailureMessage(gender: "Men's", message: failMessage) | ||
} | ||
if (!allProbabilitiesValid(dict: womensProbabilitiesDict)) { | ||
logFailureMessage(gender: "Women's", message: failMessage) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// | ||
// foam_madness_team_file_test.swift | ||
// foam-madness-tests | ||
// | ||
// Created by Michael Virgo on 3/11/23. | ||
// Copyright © 2023 mvirgo. All rights reserved. | ||
// | ||
|
||
import XCTest | ||
|
||
final class foam_madness_teams_file_test: XCTestCase { | ||
var teamsDict = NSDictionary() | ||
|
||
override func setUpWithError() throws { | ||
loadTeams() | ||
} | ||
|
||
override func tearDownWithError() throws {} | ||
|
||
func loadTeams() { | ||
let path = Bundle.main.path(forResource: "teams", ofType: "plist")! | ||
teamsDict = NSDictionary(contentsOfFile: path)! | ||
} | ||
|
||
func testEachTeamHasAbbreviationAndName() throws { | ||
for team in teamsDict.allKeys { | ||
let teamDict = teamsDict.value(forKey: team as! String) as! NSDictionary | ||
let abbreviation = teamDict.value(forKey: "abbreviation") as? String | ||
let name = teamDict.value(forKey: "name") as? String | ||
|
||
if ((abbreviation ?? "").isEmpty) { | ||
XCTFail("A team was missing an abbreviation") | ||
} | ||
if ((name ?? "").isEmpty) { | ||
XCTFail("A team was missing a name") | ||
} | ||
} | ||
} | ||
|
||
func testAllTeamsHaveDistinctNames() throws { | ||
// Abbreviations can overlap, but team names should not | ||
var teamNamesSet = Set<String>() | ||
for team in teamsDict.allKeys { | ||
let teamDict = teamsDict.value(forKey: team as! String) as! NSDictionary | ||
teamNamesSet.insert(teamDict.value(forKey: "name") as! String) | ||
} | ||
if (teamNamesSet.count != teamsDict.count) { | ||
XCTFail("Not all team names are unique") | ||
} | ||
} | ||
} |
Oops, something went wrong.