Skip to content

Commit

Permalink
Rewrite remaining DoubleInt tests (#110).
Browse files Browse the repository at this point in the history
  • Loading branch information
oscbyspro committed Nov 4, 2024
1 parent 3469599 commit e8151d8
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 212 deletions.
102 changes: 20 additions & 82 deletions Tests/DoubleIntKitTests/DoubleInt+Addition.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<B>(_ type: B.Type) where B: SystemsInteger & SignedInteger {
typealias T = DoubleInt<B>
typealias F = Fallible<T>
//=----------------------------------=
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<B>(_ type: B.Type) where B: SystemsInteger & UnsignedInteger {
typealias T = DoubleInt<B>
typealias F = Fallible<T>

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<B>(_ type: B.Type) where B: SystemsInteger & SignedInteger {
typealias T = DoubleInt<B>
typealias F = Fallible<DoubleInt<B>>
//=----------------------------------=
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<B>(_ type: B.Type) where B: SystemsInteger & UnsignedInteger {
try whereIs(base)
func whereIs<B>(_ base: B.Type) throws where B: SystemsInteger {
typealias T = DoubleInt<B>
typealias F = Fallible<DoubleInt<B>>

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))
}
}
}
}
33 changes: 18 additions & 15 deletions Tests/DoubleIntKitTests/DoubleInt+Multiplication.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,36 +13,39 @@ 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
//=------------------------------------------------------------------------=
// 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<B>(_ base: B.Type) where B: SystemsInteger {
try whereIs(base)
func whereIs<B>(_ base: B.Type) throws where B: SystemsInteger {
typealias T = DoubleInt<B>

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))
}
}
}
Expand Down
Loading

0 comments on commit e8151d8

Please sign in to comment.