Skip to content

Commit

Permalink
Merge pull request #3 from mvirgo/bracket-file-tests
Browse files Browse the repository at this point in the history
test: Basic tests for brackets, probabilities and teams
  • Loading branch information
mvirgo authored Mar 12, 2023
2 parents 45617ad + 215be45 commit c3154ae
Show file tree
Hide file tree
Showing 5 changed files with 354 additions and 4 deletions.
83 changes: 83 additions & 0 deletions foam-madness-tests/foam_madness_bracket_file_tests.swift
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 foam-madness-tests/foam_madness_probability_file_test.swift
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)
}
}
}
51 changes: 51 additions & 0 deletions foam-madness-tests/foam_madness_teams_file_test.swift
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")
}
}
}
Loading

0 comments on commit c3154ae

Please sign in to comment.