Skip to content

Commit

Permalink
Lower Expect+Division.swift utilities (#110).
Browse files Browse the repository at this point in the history
  • Loading branch information
oscbyspro committed Nov 8, 2024
1 parent a303489 commit 63ee104
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 84 deletions.
69 changes: 0 additions & 69 deletions Sources/TestKit/Expect+Division.swift
Original file line number Diff line number Diff line change
Expand Up @@ -138,72 +138,3 @@ import CoreKit
#expect(lhs.value.high == rhs.value.high, "dividend - remainder == divisor * quotient [high]", sourceLocation: location)
}
}

//*============================================================================*
// MARK: * Expect x Division x Data Integer
//*============================================================================*

@inlinable public func Ɣexpect<Element>(
_ dividend: [Element],
division divisor: Nonzero<Element>,
is quotient: [Element],
and remainder: Element,
at location: SourceLocation = #_sourceLocation
) throws where Element: SystemsIntegerAsUnsigned, Element.Element == Element {
//=--------------------------------------=
try #require(dividend.count >= quotient.count, sourceLocation: location)
//=--------------------------------------=
let divider21 = Divider21(divisor)
//=--------------------------------------=
// division: 1-by-1
//=--------------------------------------=
if dividend.count == 1 {
let expectation: Division = dividend.first!.division(divisor)
#expect([expectation.quotient ] == quotient, "BinaryInteger/division(_:)", sourceLocation: location)
#expect((expectation.remainder) == remainder, "BinaryInteger/division(_:)", sourceLocation: location)
}
//=--------------------------------------=
// division: remainder
//=--------------------------------------=
remainder: do {
var i = dividend
let o = i.withUnsafeMutableBinaryIntegerBody {
$0.remainder(divisor)
}

#expect(i == dividend, "DataInt/remainder(_:)", sourceLocation: location)
#expect(o == remainder, "DataInt/remainder(_:)", sourceLocation: location)
}

remainder: do {
var i = dividend
let o = i.withUnsafeMutableBinaryIntegerBody {
$0.remainder(divider21)
}

#expect(i == dividend, "DataInt/remainder(_:) - Divider21", sourceLocation: location)
#expect(o == remainder, "DataInt/remainder(_:) - Divider21", sourceLocation: location)
}
//=--------------------------------------=
// division: quotient and remainder
//=--------------------------------------=
division: do {
var i = dividend
let o = i.withUnsafeMutableBinaryIntegerBody {
$0.divisionSetQuotientGetRemainder(divisor)
}

#expect(i == quotient, "DataInt/divisionSetQuotientGetRemainder(_:)", sourceLocation: location)
#expect(o == remainder, "DataInt/divisionSetQuotientGetRemainder(_:)", sourceLocation: location)
}

division: do {
var i = dividend
let o = i.withUnsafeMutableBinaryIntegerBody {
$0.divisionSetQuotientGetRemainder(divider21)
}

#expect(i == quotient, "DataInt/divisionSetQuotientGetRemainder(_:) - Divider21", sourceLocation: location)
#expect(o == remainder, "DataInt/divisionSetQuotientGetRemainder(_:) - Divider21", sourceLocation: location)
}
}
100 changes: 87 additions & 13 deletions Tests/CoreKitTests/DataInteger+Division.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,35 @@ import TestKit
@Suite struct DataIntegerTestsOnDivision {

//=------------------------------------------------------------------------=
// MARK: Tests x Many ÷ Some
// MARK: Tests
//=------------------------------------------------------------------------=

@Test("DataInt/division(_:) - 0-by-1 [uniform]", arguments: typesAsCoreIntegerAsUnsigned, fuzzers)
func division01(type: any CoreIntegerAsUnsigned.Type, randomness: consuming FuzzerInt) throws {
try whereIs(type)
@Test(
"DataInt/division: none ÷ some",
Tag.List.tags(.generic, .random),
arguments: typesAsCoreIntegerAsUnsigned, fuzzers
) func noneBySome(
type: any CoreIntegerAsUnsigned.Type, randomness: consuming FuzzerInt
) throws {

try whereIs(type)
func whereIs<T>(_ type: T.Type) throws where T: CoreIntegerAsUnsigned {
for _ in 0 ..< conditional(debug: 64, release: 1024) {
let dividend = [] as [T]
let divisor = Nonzero(T.random(in: T.positives, using: &randomness))
try Ɣexpect(dividend, division: divisor, is: [], and: T())
try Ɣrequire(dividend, division: divisor, is: [], and: T())
}
}
}

@Test("DataInt/division(_:) - 1-by-1 [uniform]", arguments: typesAsCoreIntegerAsUnsigned, fuzzers)
func division11(type: any CoreIntegerAsUnsigned.Type, randomness: consuming FuzzerInt) throws {
try whereIs(type)
@Test(
"DataInt/division: some ÷ some",
arguments: typesAsCoreIntegerAsUnsigned, fuzzers
) func someBySome(
type: any CoreIntegerAsUnsigned.Type, randomness: consuming FuzzerInt
) throws {

try whereIs(type)
func whereIs<T>(_ type: T.Type) throws where T: CoreIntegerAsUnsigned {
for _ in 0 ..< conditional(debug: 64, release: 1024) {
let divisor = Nonzero(T.random(in: T.positives, using: &randomness))
Expand All @@ -49,15 +58,19 @@ import TestKit
try #require(product.multiply(by: divisor.value, add: remainder).isZero)
}

try Ɣexpect(dividend, division: divisor, is: quotient, and: remainder)
try Ɣrequire(dividend, division: divisor, is: quotient, and: remainder)
}
}
}

@Test("DataInt/division(_:) - X-by-1 [uniform]", arguments: typesAsCoreIntegerAsUnsigned, fuzzers)
func divisionX1(type: any CoreIntegerAsUnsigned.Type, randomness: consuming FuzzerInt) throws {
@Test(
"DataInt/division: many ÷ some",
arguments: typesAsCoreIntegerAsUnsigned, fuzzers
) func manyBySome(
type: any CoreIntegerAsUnsigned.Type, randomness: consuming FuzzerInt
) throws {

try whereIs(type)

func whereIs<T>(_ type: T.Type) throws where T: CoreIntegerAsUnsigned {
for _ in 0 ..< conditional(debug: 64, release: 1024) {
let divisor = Nonzero(T.random(in: T.positives, using: &randomness))
Expand All @@ -69,8 +82,69 @@ import TestKit
try #require(product.multiply(by: divisor.value, add: remainder).isZero)
}

try Ɣexpect(dividend, division: divisor, is: quotient, and: remainder)
try Ɣrequire(dividend, division: divisor, is: quotient, and: remainder)
}
}
}

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

private func Ɣrequire<Element>(
_ dividend: [Element],
division divisor: Nonzero<Element>,
is quotient: [Element],
and remainder: Element,
at location: SourceLocation = #_sourceLocation
) throws where Element: SystemsIntegerAsUnsigned, Element.Element == Element {
//=--------------------------------------=
let divider21 = Divider21(divisor)
//=--------------------------------------=
if dividend.count == 1 {
let expectation: Division = try #require(dividend.first).division(divisor)
try #require([expectation.quotient ] == quotient, "division(_:)", sourceLocation: location)
try #require((expectation.remainder) == remainder, "division(_:)", sourceLocation: location)
}

remainder: do {
var i = dividend
let o = i.withUnsafeMutableBinaryIntegerBody {
$0.remainder(divisor)
}

try #require(i == dividend, "remainder(_:)", sourceLocation: location)
try #require(o == remainder, "remainder(_:)", sourceLocation: location)
}

remainder: do {
var i = dividend
let o = i.withUnsafeMutableBinaryIntegerBody {
$0.remainder(divider21)
}

try #require(i == dividend, "remainder(_:) - Divider21", sourceLocation: location)
try #require(o == remainder, "remainder(_:) - Divider21", sourceLocation: location)
}

division: do {
var i = dividend
let o = i.withUnsafeMutableBinaryIntegerBody {
$0.divisionSetQuotientGetRemainder(divisor)
}

try #require(i == quotient, "divisionSetQuotientGetRemainder(_:)", sourceLocation: location)
try #require(o == remainder, "divisionSetQuotientGetRemainder(_:)", sourceLocation: location)
}

division: do {
var i = dividend
let o = i.withUnsafeMutableBinaryIntegerBody {
$0.divisionSetQuotientGetRemainder(divider21)
}

try #require(i == quotient, "divisionSetQuotientGetRemainder(_:) - Divider21", sourceLocation: location)
try #require(o == remainder, "divisionSetQuotientGetRemainder(_:) - Divider21", sourceLocation: location)
}
}
}
4 changes: 2 additions & 2 deletions Tests/CoreKitTests/DataInteger+Multiplication.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ import TestKit
let rhs = (T).random(using: &randomness)
let add = (T).random(using: &randomness)
let res = lhs.multiplication(rhs, plus: add)
try Ɣrequire([lhs], times: [rhs], plus: add, is: [res.low, res.high])
try Ɣrequire([rhs], times: [lhs], plus: add, is: [res.low, res.high])
try Ɣrequire([lhs], times: [rhs], plus: add, is: [res.low, res.high])
try Ɣrequire([rhs], times: [lhs], plus: add, is: [res.low, res.high])
}

for _ in 0 ..< conditional(debug: 64, release: 1024) {
Expand Down

0 comments on commit 63ee104

Please sign in to comment.