示例工程 02-values-variables-types

02-values-variables-types/Tests/Ch02KitTests/TupleTests.swift

swift · 0.9 KB

import Testing
 
@testable import Ch02Kit
 
@Suite("元组与别名")
struct TupleTests {
    @Test func labelsDoNotChangeTheType() {
        let labeled: (x: Int, y: Int) = TupleLab.labeledPoint
        let plain: (Int, Int) = labeled
        #expect(plain == (3, 4))
        #expect(TupleLab.distanceSquared(labeled) == 25)
    }
 
    @Test func swapKeepsBothElements() {
        #expect(TupleLab.swapped((1, 2)) == (2, 1))
        #expect(TupleLab.swapped(("a", "b")) == ("b", "a"))
    }
 
    @Test func decomposeKeepsOrder() {
        #expect(TupleLab.decompose((7, 9)) == "x=7, y=9")
    }
 
    @Test func minMaxHandlesEmptyAndNonEmpty() {
        #expect(TupleLab.minMax([]) == nil)
        let result = TupleLab.minMax([4, 1, 9, 2])
        #expect(result?.min == 1)
        #expect(result?.max == 9)
    }
 
    @Test func typealiasIsNotANewType() {
        let id: UserID = 42
        let raw: Int = id
        #expect(raw == 42)
    }
}