示例工程 02-values-variables-types

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

swift · 1.1 KB

import Testing
 
@testable import Ch02Kit
 
@Suite("整数溢出")
struct IntegerOverflowTests {
    @Test func wrappingAddWrapsToIntMin() {
        #expect(IntegerOverflow.wrappingAdd(Int.max, 1) == Int.min)
    }
 
    @Test func checkedAddReportsOverflowInsteadOfTrapping() {
        #expect(IntegerOverflow.checkedAdd(Int.max, 1) == nil)
        #expect(IntegerOverflow.checkedAdd(2, 3) == 5)
    }
 
    @Test func narrowingReturnsNilWhenValueDoesNotFit() {
        #expect(IntegerOverflow.narrowToInt32(3_000_000_000) == nil)
        #expect(IntegerOverflow.narrowToInt32(42) == 42)
    }
 
    @Test func intIsEightBytesOnThisTarget() {
        #expect(IntegerWidth.intBytes == 8)
        #expect(IntegerWidth.int32Bytes == 4)
        #expect(IntegerWidth.doubleBytes == 8)
        #expect(IntegerWidth.decimalBytes > 0)
    }
 
    @Test func remainderSignFollowsDividend() {
        #expect(-7 % 3 == -1)
        #expect(7 % -3 == 1)
    }
 
    @Test func mixedLayoutPadsBoolUpToIntAlignment() {
        #expect(LayoutSamples.mixedSize == 16)
        #expect(LayoutSamples.mixedStride == 16)
        #expect(LayoutSamples.mixedAlignment == 8)
    }
}