Skip to content

Commit

Permalink
Rework BinaryInteger+Multiplication.swift tests (#108) (#110).
Browse files Browse the repository at this point in the history
  • Loading branch information
oscbyspro committed Oct 15, 2024
1 parent 5d7e16d commit 81d493c
Show file tree
Hide file tree
Showing 19 changed files with 512 additions and 1,105 deletions.
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ let package = Package(
),
.testTarget(
name: "DoubleIntKitTests",
dependencies: ["DoubleIntKit", "TestKit"]
dependencies: ["DoubleIntKit", "TestKit", "TestKit2"]
),
.target(
name: "FibonacciKit",
Expand Down
124 changes: 124 additions & 0 deletions Sources/TestKit2/Expect+Multiplication.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,130 @@

import CoreKit

//*============================================================================*
// MARK: * Expect x Multiplication x Binary Integer
//*============================================================================*

@inlinable public func Ɣexpect<T>(
_ lhs: T,
times rhs: T,
is low: Fallible<T>,
and high: T? = nil,
derivatives: Bool = true,
division: Bool = true,
at location: SourceLocation = #_sourceLocation
) where T: BinaryInteger {
//=------------------------------------------=
let inputsAreEqual = lhs == rhs
let high = high ?? T(repeating: low.value.appendix)
let wide = Doublet(low: T.Magnitude(raw: low.value), high: high)
//=------------------------------------------=
if derivatives {
derivativesOneWayOnly(lhs, rhs)
}

if derivatives, !inputsAreEqual {
derivativesOneWayOnly(rhs, lhs)
}

always: do {
#expect(lhs.times(rhs) == low, "BinaryInteger/times(_:)", sourceLocation: location)
}

if !inputsAreEqual {
#expect(rhs.times(lhs) == low, "BinaryInteger/times(_:)", sourceLocation: location)
}

if inputsAreEqual {
#expect(lhs.squared() == low, "BinaryInteger/squared()", sourceLocation: location)
}

always: do {
#expect(lhs.multiplication(rhs) == wide, "BinaryInteger/multiplication(_:)", sourceLocation: location)
}

if !inputsAreEqual {
#expect(rhs.multiplication(lhs) == wide, "BinaryInteger/multiplication(_:)", sourceLocation: location)
}

if division, !low.error {
divisionOneWayOnly(lhs, rhs)
}

if division, !low.error, !inputsAreEqual {
divisionOneWayOnly(rhs, lhs)
}

complements: do {
let lhsComplement = lhs.complement()
let rhsComplement = rhs.complement()
let lowComplement = low.value.complement()

always: do {
#expect(lhs.times(rhsComplement).value == (((lowComplement))), "BinaryInteger/times(_:) - complement [0]", sourceLocation: location)
#expect(lhsComplement.times(rhs).value == (((lowComplement))), "BinaryInteger/times(_:) - complement [1]", sourceLocation: location)
#expect(lhsComplement.times(rhsComplement).value == low.value, "BinaryInteger/times(_:) - complement [2]", sourceLocation: location)
}

if !inputsAreEqual {
#expect(rhs.times(lhsComplement).value == (((lowComplement))), "BinaryInteger/times(_:) - complement [3]", sourceLocation: location)
#expect(rhsComplement.times(lhs).value == (((lowComplement))), "BinaryInteger/times(_:) - complement [4]", sourceLocation: location)
}

if inputsAreEqual {
#expect(lhsComplement.squared( ).value == (((((low.value))))), "BinaryInteger/squared() - complement [5]", sourceLocation: location)
}
}

drain: do {
guard let a = lhs.ascending(Bit.zero).natural().optional() else { break drain }
guard let b = rhs.ascending(Bit.zero).natural().optional() else { break drain }
guard let c = a.plus(b).optional() else { break drain }

let lhsDown = (lhs >> a)
let rhsDown = (rhs >> b)

always: do {
#expect(((lhs)).times(rhsDown).value << b == low.value, "BinaryInteger/times(_:) - drain [0]", sourceLocation: location)
#expect(lhsDown.times(rhsDown).value << c == low.value, "BinaryInteger/times(_:) - drain [1]", sourceLocation: location)
}

if !inputsAreEqual {
#expect(lhsDown.times(((rhs))).value << a == low.value, "BinaryInteger/times(_:) - drain [2]", sourceLocation: location)
}

if inputsAreEqual {
#expect(lhsDown.squared( ).value << c == low.value, "BinaryInteger/times(_:) - drain [3]", sourceLocation: location)
}
}
//=------------------------------------------=
func divisionOneWayOnly(_ lhs: T, _ rhs: T) {
if let divisor = Nonzero(exactly: rhs) {
let division = low.value.division(divisor)
#expect(division.value.quotient == lhs, "invariant: (a * b) / b == a", sourceLocation: location)
#expect(division.value.remainder.isZero, "invariant: (a * b) % b == 0", sourceLocation: location)
}
}

func derivativesOneWayOnly(_ lhs: T, _ rhs: T) {
scope: do {
#expect(lhs &* rhs == low.value, "BinaryInteger.&*(_:_:)", sourceLocation: location)
}

scope: if !low.error {
#expect(lhs * rhs == low.value, "BinaryInteger.*(_:_:)", sourceLocation: location)
}

scope: do {
#expect({ var x = lhs; x &*= rhs; return x }() == low.value, "BinaryInteger.&*=(_:_:)", sourceLocation: location)
}

scope: if !low.error {
#expect({ var x = lhs; x *= rhs; return x }() == low.value, "BinaryInteger.*=(_:_:)", sourceLocation: location)
}
}
}

//*============================================================================*
// MARK: * Expect x Multiplication x Data Integer
//*============================================================================*
Expand Down
22 changes: 22 additions & 0 deletions Sources/TestKit2/Global+Info.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//=----------------------------------------------------------------------------=
// This source file is part of the Ultimathnum open source project.
//
// Copyright (c) 2023 Oscar Byström Ericsson
// Licensed under Apache License, Version 2.0
//
// See http://www.apache.org/licenses/LICENSE-2.0 for license information.
//=----------------------------------------------------------------------------=

import CoreKit

//*============================================================================*
// MARK: * Global x Info
//*============================================================================*

#if DEBUG
public let isDebug: Bool = true
public let isRelease: Bool = false
#else
public let isDebug: Bool = false
public let isRelease: Bool = true
#endif
2 changes: 1 addition & 1 deletion Sources/TestKit2/Global+Types.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public let i8u8: [any CoreInteger.Type] = [
U8.self,
]

public let coreIntegers: [any CoreInteger.Type] = {
public let typesAsCoreInteger: [any CoreInteger.Type] = {
typesAsCoreIntegersAsSigned +
typesAsCoreIntegersAsUnsigned
}()
Expand Down
6 changes: 6 additions & 0 deletions Sources/TestKit2/Models/Tag.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,15 @@ extension Tag {
/// Sending to another destination.
@Tag public static var forwarding: Self

/// Of great significance or value.
@Tag public static var important: Self

/// Something that may be regained.
@Tag public static var recoverable: Self

/// Happening, done, or chosen by chance.
@Tag public static var random: Self

/// Not officially authorized or confirmed.
@Tag public static var unofficial: Self

Expand Down
102 changes: 0 additions & 102 deletions Tests/CoreKitTests/CoreInt+Multiplication.swift

This file was deleted.

4 changes: 2 additions & 2 deletions Tests/CoreKitTests/Division+Validation.swift.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ import TestKit2
@Test("Division/exactly() - T.init(load:)", .serialized, arguments: I8(-2)...I8(2), I8(-2)...I8(2))
func exactly(quotient: I8, remainder: I8) {
let error = !remainder.isZero
for quotient in coreIntegers {
for remainder in coreIntegers {
for quotient in typesAsCoreInteger {
for remainder in typesAsCoreInteger {
whereIs(quotient, remainder)
}
}
Expand Down
8 changes: 4 additions & 4 deletions Tests/CoreKitTests/Division.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ import TestKit2

@Test("Division/init(raw:) - T.init(load:)", .serialized, arguments: I8(-2)...I8(2), I8(-2)...I8(2))
func pattern(quotient: I8, remainder: I8) {
for quotient in coreIntegers {
for remainder in coreIntegers {
for quotient in typesAsCoreInteger {
for remainder in typesAsCoreInteger {
whereIs(quotient, remainder)
}
}
Expand All @@ -40,8 +40,8 @@ import TestKit2

@Test("Division/components() - T.init(load:)", .serialized, arguments: I8(-2)...I8(2), I8(-2)...I8(2))
func components(quotient: I8, remainder: I8) {
for quotient in coreIntegers {
for remainder in coreIntegers {
for quotient in typesAsCoreInteger {
for remainder in typesAsCoreInteger {
whereIs(quotient, remainder)
}
}
Expand Down
2 changes: 1 addition & 1 deletion Tests/CoreKitTests/Fallible.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import TestKit2
whereIs(Bool.self)
whereIs(Void.self)

for type in coreIntegers {
for type in typesAsCoreInteger {
whereIsBinaryInteger(type)
}

Expand Down
2 changes: 1 addition & 1 deletion Tests/CoreKitTests/Stdlib/Swift+BinaryInteger.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import TestKit2
// MARK: Tests
//=------------------------------------------------------------------------=

@Test("T.init(raw:)", arguments: coreIntegers, fuzzers)
@Test("T.init(raw:)", arguments: typesAsCoreInteger, fuzzers)
func bitcasting(type: any CoreInteger.Type, randomness: consuming FuzzerInt) {
whereIs(type)

Expand Down
4 changes: 2 additions & 2 deletions Tests/CoreKitTests/Stdlib/Swift+Optional.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import TestKit2
// MARK: Tests
//=------------------------------------------------------------------------=

@Test("T.init(raw:)", arguments: coreIntegers, fuzzers)
@Test("T.init(raw:)", arguments: typesAsCoreInteger, fuzzers)
func bitcasting(type: any SystemsInteger.Type, randomness: consuming FuzzerInt) {
whereIs(type)

Expand All @@ -39,7 +39,7 @@ import TestKit2
}
}

@Test("Optional/unwrap()", arguments: coreIntegers, fuzzers)
@Test("Optional/unwrap()", arguments: typesAsCoreInteger, fuzzers)
func unwrapping(type: any SystemsInteger.Type, randomness: consuming FuzzerInt) {
whereIs(type)

Expand Down
Loading

0 comments on commit 81d493c

Please sign in to comment.