示例工程 06-functions-closures

06-functions-closures/Tests/Ch06KitTests/FunctionTypeTests.swift

swift · 0.9 KB

import Testing
 
@testable import Ch06Kit
 
@Suite("函数值")
struct FunctionTypeTests {
    @Test func applyMapsWithGivenFunction() {
        #expect(FunctionTypeLab.apply({ $0 * 2 }, to: [1, 2, 3]) == [2, 4, 6])
        #expect(FunctionTypeLab.apply({ _ in 0 }, to: [1, 2, 3]) == [0, 0, 0])
    }
 
    @Test func adderCapturesBase() {
        let addTen = FunctionTypeLab.makeAdder(10)
        #expect(addTen(1) == 11)
        #expect(addTen(-10) == 0)
    }
 
    @Test func sortingUsesLengthThenLexicographic() {
        #expect(
            FunctionTypeLab.sortedNames(["bob", "al", "cy", "anna"]) == ["al", "cy", "bob", "anna"])
    }
 
    @Test func sortingIsIndependentOfInputOrder() {
        let first = FunctionTypeLab.sortedNames(["anna", "bob", "al", "cy"])
        let second = FunctionTypeLab.sortedNames(["cy", "al", "bob", "anna"])
        #expect(first == second)
    }
}