Skip to content

Commit

Permalink
🧪 Improved Tests
Browse files Browse the repository at this point in the history
jobearrr committed Jul 22, 2024
1 parent 9f3268d commit df656d1
Showing 7 changed files with 51 additions and 14 deletions.
14 changes: 1 addition & 13 deletions Sources/TestSupport/TestCases/Problem001TestCaseProvider.swift
Original file line number Diff line number Diff line change
@@ -8,20 +8,8 @@
protocol Problem001TestCaseProvider: TestCaseProviding { }

extension Problem001TestCaseProvider {

func validateInput(_ input: (nums: [Int], target: Int)) -> Bool {
// TODO: Complete validation
input.nums.count >= 2 &&
input.nums.count <= 10_000 &&
input.target >= -1_000_000_000 &&
input.target <= 1_000_000_000
}


var data: [TestData<(nums: [Int], target: Int), [Int]>] { [
TestData(
input: ([], 0),
expectedOutput: []
),
TestData(
input: ([2, 7, 11, 15], 9),
expectedOutput: [0, 1]
1 change: 0 additions & 1 deletion Sources/TestSupport/Utils/TestCaseProviding.swift
Original file line number Diff line number Diff line change
@@ -9,5 +9,4 @@ protocol TestCaseProviding {
associatedtype Input
associatedtype Output
var data: [TestData<Input, Output>] { get }
func validateInput(_ input: Input) -> Bool
}
Original file line number Diff line number Diff line change
@@ -9,6 +9,7 @@ import class XCTest.XCTestCase
import func XCTest.XCTAssertEqual
@testable import protocol Solutions.Problem001Definition
@testable import class Solutions.Problem001BruteForceSolution
@testable import struct TestSupport.TestData
@testable import protocol TestSupport.Problem001TestCaseProvider

final class Problem001BruteForceSolutionTests: XCTestCase {
@@ -24,6 +25,17 @@ final class Problem001BruteForceSolutionTests: XCTestCase {
XCTAssertEqual(output, testData.expectedOutput)
}
}

func testNoSolution() {
let invalidTestData = TestData<(nums: [Int], target: Int), [Int]>(
input: ([], 0),
expectedOutput: []
)

let output = solution.twoSum(invalidTestData.input.nums, invalidTestData.input.target)

XCTAssertEqual(output, invalidTestData.expectedOutput)
}
}

extension Problem001BruteForceSolutionTests: Problem001TestCaseProvider { }
Original file line number Diff line number Diff line change
@@ -9,6 +9,7 @@ import class XCTest.XCTestCase
import func XCTest.XCTAssertEqual
@testable import protocol Solutions.Problem001Definition
@testable import class Solutions.Problem001HashTableSolution
@testable import struct TestSupport.TestData
@testable import protocol TestSupport.Problem001TestCaseProvider

final class Problem001HashTableSolutionTests: XCTestCase {
@@ -24,6 +25,17 @@ final class Problem001HashTableSolutionTests: XCTestCase {
XCTAssertEqual(output, testData.expectedOutput)
}
}

func testNoSolution() {
let invalidTestData = TestData<(nums: [Int], target: Int), [Int]>(
input: ([], 0),
expectedOutput: []
)

let output = solution.twoSum(invalidTestData.input.nums, invalidTestData.input.target)

XCTAssertEqual(output, invalidTestData.expectedOutput)
}
}

extension Problem001HashTableSolutionTests: Problem001TestCaseProvider { }
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//
// Problem001TestCaseProviderTests.swift
// LeetSwift
//
// Created by Jobert Sá on 22/07/2024.
//

import class XCTest.XCTestCase
import func XCTest.XCTAssertGreaterThanOrEqual
import func XCTest.XCTAssertLessThanOrEqual
@testable import protocol TestSupport.Problem001TestCaseProvider

final class Problem001TestCaseProviderTests: XCTestCase {

func testInputIsValid() {
for testData in data {
let input = testData.input
XCTAssertGreaterThanOrEqual(input.nums.count, 2)
XCTAssertLessThanOrEqual(input.nums.count, 10_000)
XCTAssertGreaterThanOrEqual(input.target, -1_000_000_000)
XCTAssertLessThanOrEqual(input.target, 1_000_000_000)
}
}
}

extension Problem001TestCaseProviderTests: Problem001TestCaseProvider { }

0 comments on commit df656d1

Please sign in to comment.