示例工程 04-collections-cow

04-collections-cow/Tests/Ch04KitTests/CollectionTests.swift

swift · 1.5 KB

import Testing
 
@testable import Ch04Kit
 
@Suite("集合与排序")
struct CollectionTests {
    @Test func setDeduplicatesAndSortsForOutput() {
        #expect(CollectionLab.sortedSet([3, 1, 2, 3]) == [1, 2, 3])
    }
 
    @Test func dictionaryKeysAndEntriesAreSorted() {
        let pairs = [("b", 2), ("a", 1), ("c", 3)]
        #expect(CollectionLab.sortedKeys(pairs) == ["a", "b", "c"])
        #expect(CollectionLab.sortedEntries(pairs) == ["a=1", "b=2", "c=3"])
    }
 
    @Test func tiesAreBrokenByName() {
        let scores = [
            Score(name: "bob", points: 90),
            Score(name: "ann", points: 90),
            Score(name: "cy", points: 80),
        ]
        #expect(SortingLab.describe(scores) == ["ann:90", "bob:90", "cy:80"])
    }
 
    @Test func rankingIsIndependentOfInputOrder() {
        let scores = [
            Score(name: "cy", points: 80),
            Score(name: "bob", points: 90),
            Score(name: "ann", points: 90),
        ]
        #expect(SortingLab.describe(scores) == ["ann:90", "bob:90", "cy:80"])
    }
 
    @Test func reserveCapacityGrowsBuffer() {
        let result = CollectionLab.reserveCapacityProbe(10)
        #expect(result.enough)
        #expect(result.reserved >= 10)
    }
 
    @Test func collectionShellsAreSmallStructs() {
        #expect(MemoryLayout<Array<Int>>.size == 8)
        #expect(MemoryLayout<ArraySlice<Int>>.size == 32)
        #expect(MemoryLayout<Set<Int>>.size == 8)
        #expect(MemoryLayout<Dictionary<String, Int>>.size == 8)
    }
}