Skip to content

Commit

Permalink
Cleanup of Divider<T> tests (#110).
Browse files Browse the repository at this point in the history
  • Loading branch information
oscbyspro committed Nov 7, 2024
1 parent 36d7216 commit 71b36b1
Show file tree
Hide file tree
Showing 4 changed files with 293 additions and 137 deletions.
28 changes: 0 additions & 28 deletions Sources/TestKit/Expect+Division.swift
Original file line number Diff line number Diff line change
Expand Up @@ -139,34 +139,6 @@ import CoreKit
}
}

//*============================================================================*
// MARK: * Expect x Division x Divider
//*============================================================================*

@inlinable public func Ɣexpect<T>(
_ dividend: T,
division divider: Divider<T>,
is expectation: Division<T, T>,
at location: SourceLocation = #_sourceLocation
) where T: SystemsIntegerAsUnsigned {

let quotient: T = expectation.quotient
#expect(dividend.division(divider) == expectation, "T/division(_:)", sourceLocation: location)
#expect(dividend.quotient(divider) == quotient, "T/quotient(_:)", sourceLocation: location)
}

@inlinable public func Ɣexpect<T>(
_ dividend: Doublet<T>,
division divider: Divider21<T>,
is expectation: Fallible<Division<T, T>>,
at location: SourceLocation = #_sourceLocation
) where T: SystemsIntegerAsUnsigned {

let quotient: Fallible<T> = expectation.map({ $0.quotient })
#expect(divider.division(dividing: dividend) == expectation, "Divider21/division(dividing:)", sourceLocation: location)
#expect(divider.quotient(dividing: dividend) == quotient, "Divider21/quotient(dividing:)", sourceLocation: location)
}

//*============================================================================*
// MARK: * Expect x Division x Data Integer
//*============================================================================*
Expand Down
156 changes: 106 additions & 50 deletions Tests/UltimathnumTests/Divider+Division.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,69 +21,95 @@ import TestKit
// MARK: Tests
//=------------------------------------------------------------------------=

@Test("Divider/division - each (U8, U8)")
func divisionForEachBytePair() {
var success: IX = 0

for dividend in U8.all {
for divisor in U8.all {
if let divider = Divider(exactly: divisor) {
let expectation: Division = dividend.division(Nonzero(divider.div))
success &+= IX(Bit(divider.division(dividing: dividend) == expectation))
success &+= IX(Bit(divider.quotient(dividing: dividend) == expectation.quotient))
@Test(
"Divider/division: each (U8, U8) vs BinaryInteger",
Tag.List.tags(.exhaustive)
) func eachBytePairVersusBinaryInteger() {
withOnlyOneCallToExpect { expect in
for dividend in U8.all {
for divisor in U8.all {
if let divider = Divider(exactly: divisor) {
let expectation: Division = dividend.division(Nonzero(divider.div))
expect(divider.division(dividing: dividend) == expectation)
expect(divider.quotient(dividing: dividend) == expectation.quotient)
}
}
}
}

#expect(success == 2 &* 256 &* 255)
}

//=------------------------------------------------------------------------=
// MARK: Tests
//=------------------------------------------------------------------------=

@Test("Divider/division(_:) - [uniform]", arguments: typesAsSystemsIntegerAsUnsigned, fuzzers)
func divisionByFuzzingValues(_ type: any SystemsIntegerAsUnsigned.Type, randomness: consuming FuzzerInt) {
whereIs(type)
@Test(
"Divider/division: values",
Tag.List.tags(.generic, .random),
arguments: typesAsSystemsIntegerAsUnsigned, fuzzers
) func values(
_ type: any SystemsIntegerAsUnsigned.Type, randomness: consuming FuzzerInt
) throws {

func whereIs<T>(_ type: T.Type) where T: SystemsIntegerAsUnsigned {
try whereIs(type)
func whereIs<T>(_ type: T.Type) throws where T: SystemsIntegerAsUnsigned {
for _ in 0 ..< conditional(debug: 1024, release: 8192) {
let divider = Divider(T.random(in: T.positives, using: &randomness))
let dividend = T.random(using: &randomness)
let expectation: Division = dividend.division(Nonzero(divider.div))
Ɣexpect(dividend, division: divider, is: expectation)
let expectation: Division = dividend.division(Nonzero(divider.div))
try Ɣrequire(dividend, division: divider, is: expectation)
}
}
}

@Test("Divider/division(_:) - [entropic]", arguments: typesAsSystemsIntegerAsUnsigned, fuzzers)
func divisionByFuzzingEntropies(_ type: any SystemsIntegerAsUnsigned.Type, randomness: consuming FuzzerInt) {
whereIs(type)
@Test(
"Divider/division: entropies",
Tag.List.tags(.generic, .random),
arguments: typesAsSystemsIntegerAsUnsigned, fuzzers
) func entropies(
_ type: any SystemsIntegerAsUnsigned.Type, randomness: consuming FuzzerInt
) throws {

func whereIs<T>(_ type: T.Type) where T: SystemsIntegerAsUnsigned {
try whereIs(type)
func whereIs<T>(_ type: T.Type) throws where T: SystemsIntegerAsUnsigned {
for _ in 0 ..< conditional(debug: 1024, release: 8192) {
if let divider = Divider(exactly: T.entropic(using: &randomness)) {
let dividend = T.entropic(using: &randomness)
let expectation: Division = dividend.division(Nonzero(divider.div))
Ɣexpect(dividend, division: divider, is: expectation)
let expectation: Division = dividend.division(Nonzero(divider.div))
try Ɣrequire(dividend, division: divider, is: expectation)
}
}
}
}

@Test("Divider/division(_:) - [power-of-2]", arguments: typesAsSystemsIntegerAsUnsigned, fuzzers)
func divisionByFuzzingPowerOf2s(_ type: any SystemsIntegerAsUnsigned.Type, randomness: consuming FuzzerInt) {
whereIs(type)
@Test(
"Divider/division: where divisor is power of 2",
Tag.List.tags(.generic, .important, .random),
arguments: typesAsSystemsIntegerAsUnsigned, fuzzers
) func whereDivisorIsPowersOf2(
_ type: any SystemsIntegerAsUnsigned.Type, randomness: consuming FuzzerInt
) throws {

func whereIs<T>(_ type: T.Type) where T: SystemsIntegerAsUnsigned {
try whereIs(type)
func whereIs<T>(_ type: T.Type) throws where T: SystemsIntegerAsUnsigned {
for _ in 0 ..< conditional(debug: 1024, release: 8192) {
let divider = Divider(T.lsb.up(Shift.random(using: &randomness)))
let dividend: T = T.entropic(using: &randomness)
let expectation: Division = dividend.division(Nonzero(divider.div))
Ɣexpect(dividend, division: divider, is: expectation)
let expectation: Division = dividend.division(Nonzero(divider.div))
try Ɣrequire(dividend, division: divider, is: expectation)
}
}
}

//=------------------------------------------------------------------------=
// MARK: Utilities
//=------------------------------------------------------------------------=

private func Ɣrequire<T>(
_ dividend: T,
division divider: Divider<T>,
is expectation: Division<T, T>,
at location: SourceLocation = #_sourceLocation
) throws {
let quotient = expectation.quotient
try #require(dividend.division(divider) == expectation, sourceLocation: location)
try #require(dividend.quotient(divider) == quotient, sourceLocation: location)
}
}

//*============================================================================*
Expand All @@ -95,53 +121,83 @@ import TestKit
//=------------------------------------------------------------------------=
// MARK: Tests
//=------------------------------------------------------------------------=

@Test("Divider21/division(_:) - [uniform]", arguments: typesAsSystemsIntegerAsUnsigned, fuzzers)
func divisionByFuzzingValues(_ type: any SystemsIntegerAsUnsigned.Type, randomness: consuming FuzzerInt) {
whereIs(type)

@Test(
"Divider21/division: values",
Tag.List.tags(.generic, .random),
arguments: typesAsSystemsIntegerAsUnsigned, fuzzers
) func values(
_ type: any SystemsIntegerAsUnsigned.Type, randomness: consuming FuzzerInt
) throws {

func whereIs<T>(_ type: T.Type) where T: SystemsIntegerAsUnsigned {
try whereIs(type)
func whereIs<T>(_ type: T.Type) throws where T: SystemsIntegerAsUnsigned {
for _ in 0 ..< conditional(debug: 1024, release: 8192) {
let divider = Divider21(T.random(in: T.positives, using: &randomness))
let low = T.random(using: &randomness)
let high = T.random(using: &randomness)
let dividend = Doublet(low: low, high: high)
let expectation = T.division(dividend, by: Nonzero(divider.div))
Ɣexpect(dividend, division: divider, is: expectation)
try Ɣrequire(dividend, division: divider, is: expectation)
}
}
}

@Test("Divider21/division(_:) - [entropic]", arguments: typesAsSystemsIntegerAsUnsigned, fuzzers)
func divisionByFuzzingEntropies(_ type: any SystemsIntegerAsUnsigned.Type, randomness: consuming FuzzerInt) {
whereIs(type)
@Test(
"Divider21/division: entropies",
Tag.List.tags(.generic, .random),
arguments: typesAsSystemsIntegerAsUnsigned, fuzzers
) func entropies(
_ type: any SystemsIntegerAsUnsigned.Type, randomness: consuming FuzzerInt
) throws {

func whereIs<T>(_ type: T.Type) where T: SystemsIntegerAsUnsigned {
try whereIs(type)
func whereIs<T>(_ type: T.Type) throws where T: SystemsIntegerAsUnsigned {
for _ in 0 ..< conditional(debug: 1024, release: 8192) {
if let divider = Divider21(exactly: T.entropic(using: &randomness)) {
let low = T.entropic(using: &randomness)
let high = T.entropic(using: &randomness)
let dividend = Doublet(low: low, high: high)
let expectation = T.division(dividend, by: Nonzero(divider.div))
Ɣexpect(dividend, division: divider, is: expectation)
try Ɣrequire(dividend, division: divider, is: expectation)
}
}
}
}

@Test("Divider21/division(_:) - [power-of-2]", arguments: typesAsSystemsIntegerAsUnsigned, fuzzers)
func divisionByFuzzingPowerOf2s(_ type: any SystemsIntegerAsUnsigned.Type, randomness: consuming FuzzerInt) {
whereIs(type)
@Test(
"Divider21/division: where divisor is power of 2",
Tag.List.tags(.generic, .important, .random),
arguments: typesAsSystemsIntegerAsUnsigned, fuzzers
) func whereDivisorIsPowersOf2(
_ type: any SystemsIntegerAsUnsigned.Type, randomness: consuming FuzzerInt
) throws {

func whereIs<T>(_ type: T.Type) where T: SystemsIntegerAsUnsigned {
try whereIs(type)
func whereIs<T>(_ type: T.Type) throws where T: SystemsIntegerAsUnsigned {
for _ in 0 ..< conditional(debug: 1024, release: 8192) {
let divider = Divider21(T.lsb.up(Shift.random(using: &randomness)))
let low: T = T.entropic(using: &randomness)
let high: T = T.entropic(using: &randomness)
let dividend = Doublet(low: low, high: high)
let expectation = T.division(dividend, by: Nonzero(divider.div))
Ɣexpect(dividend, division: divider, is: expectation)
try Ɣrequire(dividend, division: divider, is: expectation)
}
}
}

//=------------------------------------------------------------------------=
// MARK: Utilities
//=------------------------------------------------------------------------=

private func Ɣrequire<T>(
_ dividend: Doublet<T>,
division divider: Divider21<T>,
is expectation: Fallible<Division<T, T>>,
at location: SourceLocation = #_sourceLocation
) throws {
let quotient = expectation.map({ $0.quotient })
try #require(divider.division(dividing: dividend) == expectation, sourceLocation: location)
try #require(divider.quotient(dividing: dividend) == quotient, sourceLocation: location)
}
}
42 changes: 28 additions & 14 deletions Tests/UltimathnumTests/Divider+Validation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,24 @@ import TestKit
// MARK: Tests
//=------------------------------------------------------------------------=

@Test("Divider.init - [entropic]", arguments: typesAsSystemsIntegerAsUnsigned, fuzzers)
func initByFuzzingEntropies(_ type: any SystemsIntegerAsUnsigned.Type, randomness: consuming FuzzerInt) {
whereIs(type)

func whereIs<T>(_ type: T.Type) where T: SystemsIntegerAsUnsigned {
@Test(
"Divider/validation: initialization",
Tag.List.tags(.generic, .random),
arguments: typesAsSystemsIntegerAsUnsigned, fuzzers
) func initialization(
_ type: any SystemsIntegerAsUnsigned.Type, randomness: consuming FuzzerInt
) throws {

try whereIs(type)
func whereIs<T>(_ type: T.Type) throws where T: SystemsIntegerAsUnsigned {
for _ in 0 ..< 32 {
let random = T.entropic(using: &randomness)
Ɣexpect(random, as: Divider.self, if: !random.isZero)

if let result = Divider(exactly: random) {
#expect(result.div == random)
try #require(result.div == random)
}

Ɣexpect(random, as: Divider.self, if: !random.isZero)
}
}
}
Expand All @@ -47,17 +54,24 @@ import TestKit
// MARK: Tests
//=------------------------------------------------------------------------=

@Test("Divider21.init - [entropic]", arguments: typesAsSystemsIntegerAsUnsigned, fuzzers)
func initByFuzzingEntropies(_ type: any SystemsIntegerAsUnsigned.Type, randomness: consuming FuzzerInt) {
whereIs(type)

func whereIs<T>(_ type: T.Type) where T: SystemsIntegerAsUnsigned {
@Test(
"Divider21/validation: initialization",
Tag.List.tags(.generic, .random),
arguments: typesAsSystemsIntegerAsUnsigned, fuzzers
) func initialization(
_ type: any SystemsIntegerAsUnsigned.Type, randomness: consuming FuzzerInt
) throws {

try whereIs(type)
func whereIs<T>(_ type: T.Type) throws where T: SystemsIntegerAsUnsigned {
for _ in 0 ..< 32 {
let random = T.entropic(using: &randomness)
Ɣexpect(random, as: Divider21.self, if: !random.isZero)

if let result = Divider21(exactly: random) {
#expect(result.div == random)
try #require(result.div == random)
}

Ɣexpect(random, as: Divider21.self, if: !random.isZero)
}
}
}
Expand Down
Loading

0 comments on commit 71b36b1

Please sign in to comment.