From e8151d8a6416fc6a9fb7de350f55cbbde95eff2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oscar=20Bystr=C3=B6m=20Ericsson?= Date: Mon, 4 Nov 2024 08:24:43 +0100 Subject: [PATCH] Rewrite remaining DoubleInt tests (#110). --- .../DoubleInt+Addition.swift | 102 ++-------- .../DoubleInt+Multiplication.swift | 33 ++-- Tests/DoubleIntKitTests/DoubleInt.swift | 176 ++++++++---------- .../TripleInt+Addition.swift | 8 +- .../TripleInt+Division.swift | 2 +- .../TripleInt+Subtraction.swift | 8 +- Tests/DoubleIntKitTests/TripleInt.swift | 8 +- 7 files changed, 125 insertions(+), 212 deletions(-) diff --git a/Tests/DoubleIntKitTests/DoubleInt+Addition.swift b/Tests/DoubleIntKitTests/DoubleInt+Addition.swift index 262f7430..13629bcd 100644 --- a/Tests/DoubleIntKitTests/DoubleInt+Addition.swift +++ b/Tests/DoubleIntKitTests/DoubleInt+Addition.swift @@ -9,102 +9,40 @@ import CoreKit import DoubleIntKit -import TestKit +import RandomIntKit +import TestKit2 //*============================================================================* -// MARK: * Double Int x Addition +// MARK: * Double Int x Addition x Not In Protocol //*============================================================================* -extension DoubleIntTests { +@Suite struct DoubleIntTestsOnAdditionNotInProtocol { //=------------------------------------------------------------------------= - // MARK: Tests x 2 by 1 + // MARK: Tests x 2 by 1 as 2 //=------------------------------------------------------------------------= // TODO: HalveableInteger/plus (_:) would let us hoist these tests // TODO: HalveableInteger/minus(_:) would let us hoist these tests //=------------------------------------------------------------------------= - func testAddition21() { - func whereTheBaseIsSigned(_ type: B.Type) where B: SystemsInteger & SignedInteger { - typealias T = DoubleInt - typealias F = Fallible - //=----------------------------------= - let x = B.msb - //=----------------------------------= - Test().same(T(low: 0, high: 0).plus(B.Magnitude(~3)), F(T(low: ~3, high: 0))) - Test().same(T(low: 1, high: 2).plus(B.Magnitude(~3)), F(T(low: ~2, high: 2))) - Test().same(T(low: ~1, high: ~2).plus(B.Magnitude( 3)), F(T(low: 1, high: ~1))) - Test().same(T(low: ~0, high: ~0).plus(B.Magnitude( 3)), F(T(low: 2, high: 0))) - - Test().same(T(low: 3, high: x).plus(B.Magnitude(~3)), F(T(low: ~0, high: x))) - Test().same(T(low: 4, high: x).plus(B.Magnitude(~3)), F(T(low: 0, high: x ^ 1))) - Test().same(T(low: ~3, high: ~x).plus(B.Magnitude( 3)), F(T(low: ~0, high: ~x))) - Test().same(T(low: ~2, high: ~x).plus(B.Magnitude( 3)), F(T(low: 0, high: x), error: true)) - } - - func whereTheBaseIsUnsigned(_ type: B.Type) where B: SystemsInteger & UnsignedInteger { - typealias T = DoubleInt - typealias F = Fallible - - Test().same(T(low: 0, high: 0).plus(B.Magnitude(~3)), F(T(low: ~3, high: 0))) - Test().same(T(low: 1, high: 2).plus(B.Magnitude(~3)), F(T(low: ~2, high: 2))) - Test().same(T(low: ~1, high: ~2).plus(B.Magnitude( 3)), F(T(low: 1, high: ~1))) - Test().same(T(low: ~0, high: ~0).plus(B.Magnitude( 3)), F(T(low: 2, high: 0), error: true)) - - Test().same(T(low: 3, high: ~0).plus(B.Magnitude(~3)), F(T(low: ~0, high: ~0))) - Test().same(T(low: 4, high: ~0).plus(B.Magnitude(~3)), F(T(low: 0, high: 0), error: true)) - Test().same(T(low: ~3, high: ~0).plus(B.Magnitude( 3)), F(T(low: ~0, high: ~0))) - Test().same(T(low: ~2, high: ~0).plus(B.Magnitude( 3)), F(T(low: 0, high: 0), error: true)) - } - - for base in Self.basesWhereIsSigned { - whereTheBaseIsSigned(base) - } - - for base in Self.basesWhereIsUnsigned { - whereTheBaseIsUnsigned(base) - } - } - - func testSubtraction21() { - func whereTheBaseIsSigned(_ type: B.Type) where B: SystemsInteger & SignedInteger { - typealias T = DoubleInt - typealias F = Fallible> - //=----------------------------------= - let x = B.msb - //=----------------------------------= - Test().same(T(low: 0, high: 0).minus(B.Magnitude(~3)), F(T(low: 4, high: ~0))) - Test().same(T(low: 1, high: 2).minus(B.Magnitude(~3)), F(T(low: 5, high: 1))) - Test().same(T(low: ~1, high: ~2).minus(B.Magnitude( 3)), F(T(low: ~4, high: ~2))) - Test().same(T(low: ~0, high: ~0).minus(B.Magnitude( 3)), F(T(low: ~3, high: ~0))) - - Test().same(T(low: ~3, high: ~x).minus(B.Magnitude(~3)), F(T(low: 0, high: ~x))) - Test().same(T(low: ~4, high: ~x).minus(B.Magnitude(~3)), F(T(low: ~0, high: ~x ^ 1))) - Test().same(T(low: 3, high: x).minus(B.Magnitude( 3)), F(T(low: 0, high: x))) - Test().same(T(low: 2, high: x).minus(B.Magnitude( 3)), F(T(low: ~0, high: ~x), error: true)) - } + @Test( + "DoubleInt/addition/not-in-protocol: random 212 vs 222", + Tag.List.tags(.generic, .random), + arguments: DoubleIntTests.bases, fuzzers + ) func random212vs222(base: any SystemsInteger.Type, randomness: consuming FuzzerInt) throws { - func whereTheBaseIsUnsigned(_ type: B.Type) where B: SystemsInteger & UnsignedInteger { + try whereIs(base) + func whereIs(_ base: B.Type) throws where B: SystemsInteger { typealias T = DoubleInt - typealias F = Fallible> - Test().same(T(low: 0, high: 0).minus(B.Magnitude(~3)), F(T(low: 4, high: ~0), error: true)) - Test().same(T(low: 1, high: 2).minus(B.Magnitude(~3)), F(T(low: 5, high: 1))) - Test().same(T(low: ~1, high: ~2).minus(B.Magnitude( 3)), F(T(low: ~4, high: ~2))) - Test().same(T(low: ~0, high: ~0).minus(B.Magnitude( 3)), F(T(low: ~3, high: ~0))) - - Test().same(T(low: ~3, high: 0).minus(B.Magnitude(~3)), F(T(low: 0, high: 0))) - Test().same(T(low: ~4, high: 0).minus(B.Magnitude(~3)), F(T(low: ~0, high: ~0), error: true)) - Test().same(T(low: 3, high: 0).minus(B.Magnitude( 3)), F(T(low: 0, high: 0))) - Test().same(T(low: 2, high: 0).minus(B.Magnitude( 3)), F(T(low: ~0, high: ~0), error: true)) - } - - for base in Self.basesWhereIsSigned { - whereTheBaseIsSigned(base) - } - - for base in Self.basesWhereIsUnsigned { - whereTheBaseIsUnsigned(base) + for _ in 0 ..< 1024 { + let lhs2 = T.entropic(using: &randomness) + let rhs1 = B.Magnitude.entropic(using: &randomness) + let rhs2 = T(load: rhs1) + + try #require(lhs2.plus (rhs1) == lhs2.plus (rhs2)) + try #require(lhs2.minus(rhs1) == lhs2.minus(rhs2)) + } } } } diff --git a/Tests/DoubleIntKitTests/DoubleInt+Multiplication.swift b/Tests/DoubleIntKitTests/DoubleInt+Multiplication.swift index a51be867..a694ce64 100644 --- a/Tests/DoubleIntKitTests/DoubleInt+Multiplication.swift +++ b/Tests/DoubleIntKitTests/DoubleInt+Multiplication.swift @@ -13,11 +13,10 @@ import RandomIntKit import TestKit2 //*============================================================================* -// MARK: * Double Int x Multiplication +// MARK: * Double Int x Multiplication x Not In Protocol //*============================================================================* -@Suite("DoubleInt/multiplication - not in protocol") -struct DoubleIntTestsOnMultiplicationNotInProtocol { +@Suite struct DoubleIntTestsOnMultiplicationNotInProtocol { //=------------------------------------------------------------------------= // MARK: Tests x 2 by 1 as 3 @@ -25,24 +24,28 @@ struct DoubleIntTestsOnMultiplicationNotInProtocol { // TODO: HalveableInteger/multiplication(_:) would let us hoist these tests //=------------------------------------------------------------------------= - @Test("DoubleInt/multiplication - 213 vs 224 [entropic]", arguments: typesAsCoreInteger, fuzzers) - func multiplication213vs224(base: any CoreInteger.Type, randomness: consuming FuzzerInt) { - whereIs(base) + @Test( + "DoubleInt/multiplication/not-in-protocol: random 213 vs 224", + Tag.List.tags(.generic, .random), + arguments: DoubleIntTests.bases, fuzzers + ) func random213vs224(base: any SystemsInteger.Type, randomness: consuming FuzzerInt) throws { - func whereIs(_ base: B.Type) where B: SystemsInteger { + try whereIs(base) + func whereIs(_ base: B.Type) throws where B: SystemsInteger { typealias T = DoubleInt for _ in 0 ..< 1024 { - let lhs = T.entropic(using: &randomness) - let rhs = B.entropic(using: &randomness) + let lhs2 = T.entropic(using: &randomness) + let rhs1 = B.entropic(using: &randomness) + let rhs2 = T(load: rhs1) - let result3 = lhs.multiplication(rhs) - let result4 = lhs.multiplication(T(load: rhs)) + let result3 = lhs2.multiplication(rhs1) + let result4 = lhs2.multiplication(rhs2) - #expect(result4.low.low == result3.low) - #expect(result4.low.high == result3.mid) - #expect(result4.high.low == B.Magnitude.init(raw: result3.high)) - #expect(result4.high.high == B(repeating: result3.high.appendix)) + try #require(result4.low.low == result3.low) + try #require(result4.low.high == result3.mid) + try #require(result4.high.low == B.Magnitude.init(raw: result3.high)) + try #require(result4.high.high == B(repeating: result3.high.appendix)) } } } diff --git a/Tests/DoubleIntKitTests/DoubleInt.swift b/Tests/DoubleIntKitTests/DoubleInt.swift index 5eb6a884..5ad3f183 100644 --- a/Tests/DoubleIntKitTests/DoubleInt.swift +++ b/Tests/DoubleIntKitTests/DoubleInt.swift @@ -9,15 +9,14 @@ import CoreKit import DoubleIntKit -import TestKit +import RandomIntKit +import TestKit2 //*============================================================================* // MARK: * Double Int //*============================================================================* -final class DoubleIntTests: XCTestCase { - - typealias X2 = DoubleInt where Base: SystemsInteger +@Suite struct DoubleIntTests { typealias I8x2 = DoubleInt typealias U8x2 = DoubleInt @@ -36,18 +35,18 @@ final class DoubleIntTests: XCTestCase { //=------------------------------------------------------------------------= static let types: [any SystemsInteger.Type] = { - typesWhereIsSigned + - typesWhereIsUnsigned + typesAsSigned + + typesAsUnsigned }() - static let typesWhereIsSigned: [any (SystemsInteger & SignedInteger).Type] = [ + static let typesAsSigned: [any SystemsIntegerAsSigned.Type] = [ I8x2.self, I8x4.self, IXx2.self, IXx4.self, ] - static let typesWhereIsUnsigned: [any (SystemsInteger & UnsignedInteger).Type] = [ + static let typesAsUnsigned: [any SystemsIntegerAsUnsigned.Type] = [ U8x2.self, U8x4.self, UXx2.self, @@ -55,18 +54,18 @@ final class DoubleIntTests: XCTestCase { ] static let bases: [any SystemsInteger.Type] = { - basesWhereIsSigned + - basesWhereIsUnsigned + basesAsSigned + + basesAsUnsigned }() - static let basesWhereIsSigned: [any (SystemsInteger & SignedInteger).Type] = [ + static let basesAsSigned: [any SystemsIntegerAsSigned.Type] = [ I8x2.High.self, I8x4.High.self, IXx2.High.self, IXx4.High.self, ] - static let basesWhereIsUnsigned: [any (SystemsInteger & UnsignedInteger).Type] = [ + static let basesAsUnsigned: [any SystemsIntegerAsUnsigned.Type] = [ U8x2.High.self, U8x4.High.self, UXx2.High.self, @@ -77,108 +76,81 @@ final class DoubleIntTests: XCTestCase { // MARK: Tests //=------------------------------------------------------------------------= - func testComponents() { - func whereTheBaseIs(_ type: B.Type) where B: SystemsInteger { - typealias T = DoubleInt - - Test().same(T( ).low, 0 as B.Magnitude) - Test().same(T( ).high, 0 as B) - Test().same(T(low: 0 ).low, 0 as B.Magnitude) - Test().same(T(low: 0 ).high, 0 as B) - Test().same(T(low: 1 ).low, 1 as B.Magnitude) - Test().same(T(low: 1 ).high, 0 as B) - Test().same(T(low: 0, high: 0).low, 0 as B.Magnitude) - Test().same(T(low: 0, high: 0).high, 0 as B) - Test().same(T(low: 1, high: 2).low, 1 as B.Magnitude) - Test().same(T(low: 1, high: 2).high, 2 as B) - - setters: do { - let rhs = T(low: 1, high: 2) - var lhs = T(low: 0, high: 0) - - lhs.low = rhs.low - lhs.high = rhs.high - - Test().same(lhs, rhs) - } - - components: do { - var low: B.Magnitude - var high: B - - (low, high) = T(low: 0, high: 0).components() - Test().same(low, 0 as B.Magnitude) - Test().same(high, 0 as B) - - (low, high) = T(low: 1, high: 2).components() - Test().same(low, 1 as B.Magnitude) - Test().same(high, 2 as B) - } - } + @Test("DoubleInt: layout", .tags(.generic), arguments: Self.bases) + func layout(base: any SystemsInteger.Type) throws { - for base in Self.bases { - whereTheBaseIs(base) - } - } - - func testBitCast() { - func whereTheBaseIs(_ type: B.Type) where B: SystemsInteger { + try whereIs(base) + func whereIs(_ base: B.Type) throws where B: SystemsInteger { typealias T = DoubleInt - typealias S = T.Signitude - typealias M = T.Magnitude + typealias U = (B, B) - Test().same(M(raw: T(low: 1, high: 2)), M(low: 1, high: 2)) - Test().same(S(raw: T(low: 1, high: 2)), S(low: 1, high: 2)) - Test().same(M(raw: T(low: ~1, high: ~2)), M(low: ~1, high: ~2)) - Test().same(S(raw: T(low: ~1, high: ~2)), S(low: ~1, high: ~2)) - } - - for base in Self.bases { - whereTheBaseIs(base) + #expect(MemoryLayout.size == 2 * MemoryLayout.size) + #expect(MemoryLayout.stride == 2 * MemoryLayout.stride) + #expect(MemoryLayout.alignment == 1 * MemoryLayout.alignment) + Ɣexpect(MemoryLayout.self, equals: MemoryLayout.self) } } - func testMemoryLayout() { - func whereTheBaseIs(_ type: B.Type) where B: SystemsInteger { + + @Test("DoubleInt: bitcasting", .tags(.generic, .random), arguments: Self.bases, fuzzers) + func bitcasting(base: any SystemsInteger.Type, randomness: consuming FuzzerInt) throws { + + try whereIs(base) + func whereIs(_ base: B.Type) throws where B: SystemsInteger { typealias T = DoubleInt - Test().same(MemoryLayout.self, MemoryLayout<(B, B)>.self) - Test().same(MemoryLayout.size, 2 * MemoryLayout.size) - Test().same(MemoryLayout.stride, 2 * MemoryLayout.stride) - Test().same(MemoryLayout.alignment, 1 * MemoryLayout.alignment) - } - - for base in Self.bases { - whereTheBaseIs(base) + for _ in 0 ..< 32 { + let low = T.Low .random(using: &randomness) + let high = T.High.random(using: &randomness) + let full = T(low: low, high: high) + + try #require(full == T(raw: T.Magnitude(raw: full))) + try #require(full == T(raw: T.Signitude(raw: full))) + + try #require(full == T(raw: T.Magnitude(low: low, high: B.Magnitude(raw: high)))) + try #require(full == T(raw: T.Signitude(low: low, high: B.Signitude(raw: high)))) + } } } - - //*========================================================================* - // MARK: * Case - //*========================================================================* - struct Case { - - typealias Item = DoubleInt - - //=--------------------------------------------------------------------= - // MARK: State - //=--------------------------------------------------------------------= + @Test("DoubleInt: components", .tags(.generic, .random), arguments: Self.bases, fuzzers) + func components(base: any SystemsInteger.Type, randomness: consuming FuzzerInt) throws { - var test: Test - var item: Item - - //=--------------------------------------------------------------------= - // MARK: Initializers - //=--------------------------------------------------------------------= - - init(_ item: Item, test: Test) { - self.test = test - self.item = item - } - - init(_ item: Item, file: StaticString = #file, line: UInt = #line) { - self.init(item, test: Test(file: file, line: line)) + try whereIs(base) + func whereIs(_ base: B.Type) throws where B: SystemsInteger { + typealias T = DoubleInt + + for _ in 0 ..< 32 { + let low = T.Low .random(using: &randomness) + let high = T.High.random(using: &randomness) + + always: do { + try #require(T().low .isZero) + try #require(T().high.isZero) + } + + always: do { + try #require(T(low: low).low == low) + try #require(T(low: low).high.isZero) + } + + always: do { + try #require(T(low: low, high: high).low == low ) + try #require(T(low: low, high: high).high == high) + } + + getter: do { + try #require(T(low: low, high: high).components() == (low, high)) + } + + setter: do { + var full = T() + full.low = low + full.high = high + try #require(full.low == low ) + try #require(full.high == high) + } + } } } } diff --git a/Tests/DoubleIntKitTests/TripleInt+Addition.swift b/Tests/DoubleIntKitTests/TripleInt+Addition.swift index c3ab2ea9..9f8ca35c 100644 --- a/Tests/DoubleIntKitTests/TripleInt+Addition.swift +++ b/Tests/DoubleIntKitTests/TripleInt+Addition.swift @@ -56,11 +56,11 @@ extension TripleIntTests { Test().same(Y(low: ~3, mid: ~5, high: ~0).plus(X(low: 4, high: 5)), F(Y(low: 0, mid: 0, high: 0), error: true)) } - for base in Self.basesWhereIsSigned { + for base in Self.basesAsSigned { whereTheBaseIsSigned(base) } - for base in Self.basesWhereIsUnsigned { + for base in Self.basesAsUnsigned { whereTheBaseIsUnsigned(base) } } @@ -100,11 +100,11 @@ extension TripleIntTests { Test().same(Y(low: ~3, mid: ~5, high: ~6).plus(Y(low: 4, mid: 5, high: 6)), F(Y(low: 0, mid: 0, high: 0), error: true)) } - for base in Self.basesWhereIsSigned { + for base in Self.basesAsSigned { whereTheBaseIsSigned(base) } - for base in Self.basesWhereIsUnsigned { + for base in Self.basesAsUnsigned { whereTheBaseIsUnsigned(base) } } diff --git a/Tests/DoubleIntKitTests/TripleInt+Division.swift b/Tests/DoubleIntKitTests/TripleInt+Division.swift index 8ccb9a8c..1d3b484c 100644 --- a/Tests/DoubleIntKitTests/TripleInt+Division.swift +++ b/Tests/DoubleIntKitTests/TripleInt+Division.swift @@ -38,7 +38,7 @@ extension TripleIntTests { Test().division3212MSB(Y(low: ~0, mid: ~0, high: B.msb - 1), X(low: ~0, high: B.msb), D(quotient: ~1 as B, remainder: X(low: ~2, high: 2))) // 1 } - for base in Self.basesWhereIsUnsigned { + for base in Self.basesAsUnsigned { whereTheBaseIs(base) } } diff --git a/Tests/DoubleIntKitTests/TripleInt+Subtraction.swift b/Tests/DoubleIntKitTests/TripleInt+Subtraction.swift index 7e1ccd5a..e9884e5c 100644 --- a/Tests/DoubleIntKitTests/TripleInt+Subtraction.swift +++ b/Tests/DoubleIntKitTests/TripleInt+Subtraction.swift @@ -56,11 +56,11 @@ extension TripleIntTests { Test().same(Y(low: 3, mid: 5, high: 0).minus(X(low: 4, high: 5)), F(Y(low: ~0, mid: ~0, high: ~0), error: true)) } - for base in Self.basesWhereIsSigned { + for base in Self.basesAsSigned { whereTheBaseIsSigned(base) } - for base in Self.basesWhereIsUnsigned { + for base in Self.basesAsUnsigned { whereTheBaseIsUnsigned(base) } } @@ -100,11 +100,11 @@ extension TripleIntTests { Test().same(Y(low: 3, mid: 5, high: 6).minus(Y(low: 4, mid: 5, high: 6)), F(Y(low: ~0, mid: ~0, high: ~0), error: true)) } - for base in Self.basesWhereIsSigned { + for base in Self.basesAsSigned { whereTheBaseIsSigned(base) } - for base in Self.basesWhereIsUnsigned { + for base in Self.basesAsUnsigned { whereTheBaseIsUnsigned(base) } } diff --git a/Tests/DoubleIntKitTests/TripleInt.swift b/Tests/DoubleIntKitTests/TripleInt.swift index 5f12531a..f41b6fa9 100644 --- a/Tests/DoubleIntKitTests/TripleInt.swift +++ b/Tests/DoubleIntKitTests/TripleInt.swift @@ -30,15 +30,15 @@ final class TripleIntTests: XCTestCase { //=------------------------------------------------------------------------= static let bases: [any SystemsInteger.Type] = { - basesWhereIsSigned + - basesWhereIsUnsigned + basesAsSigned + + basesAsUnsigned }() - static let basesWhereIsSigned: [any (SystemsInteger & SignedInteger).Type] = { + static let basesAsSigned: [any (SystemsInteger & SignedInteger).Type] = { typesAsCoreIntegerAsSigned }() - static let basesWhereIsUnsigned: [any (SystemsInteger & UnsignedInteger).Type] = { + static let basesAsUnsigned: [any (SystemsInteger & UnsignedInteger).Type] = { typesAsCoreIntegerAsUnsigned }()