-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
98 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
//=----------------------------------------------------------------------------= | ||
// 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. | ||
//=----------------------------------------------------------------------------= | ||
|
||
//*============================================================================* | ||
// MARK: * Division x Validation | ||
//*============================================================================* | ||
|
||
extension Division { | ||
|
||
//=------------------------------------------------------------------------= | ||
// MARK: Transformations | ||
//=------------------------------------------------------------------------= | ||
|
||
/// Returns the `quotient` and an `error` indicator. | ||
/// | ||
/// The `error` is set when the `remainder` is nonzero. | ||
/// | ||
@inlinable public consuming func exactly() -> Fallible<Quotient> { | ||
self.quotient.veto(!self.remainder.isZero) | ||
} | ||
} | ||
|
||
//=----------------------------------------------------------------------------= | ||
// MARK: + Recoverable | ||
//=----------------------------------------------------------------------------= | ||
|
||
extension Fallible { | ||
|
||
//=------------------------------------------------------------------------= | ||
// MARK: Transformations | ||
//=------------------------------------------------------------------------= | ||
|
||
/// Returns the `quotient` and an `error` indicator. | ||
/// | ||
/// The `error` is set when the `remainder` is nonzero. | ||
/// | ||
@inlinable public consuming func exactly<Quotient, Remainder>() -> Fallible<Quotient> where Value == Division<Quotient, Remainder> { | ||
self.value.exactly().veto(self.error) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
//=----------------------------------------------------------------------------= | ||
// 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 | ||
import TestKit | ||
|
||
//*============================================================================* | ||
// MARK: * Division x Validation | ||
//*============================================================================* | ||
|
||
extension DivisionTests { | ||
|
||
//=------------------------------------------------------------------------= | ||
// MARK: Tests | ||
//=------------------------------------------------------------------------= | ||
|
||
func testExactly() { | ||
func whereIs<Q, R>(_ quotient: Q.Type, _ remainder: R.Type) where Q: SystemsInteger, R: SystemsInteger { | ||
let patterns = I8(-2)...I8(2) | ||
for quotient in patterns.lazy.map(Q.init(load:)) { | ||
for remainder in patterns.lazy.map(R.init(load:)) { | ||
let error = !remainder.isZero | ||
let division = Division(quotient: quotient, remainder: remainder) | ||
Test().same(division .exactly(), Fallible(quotient, error: error)) | ||
Test().same(division.veto(false).exactly(), Fallible(quotient, error: error)) | ||
Test().same(division.veto(true ).exactly(), Fallible(quotient, error: true )) | ||
} | ||
} | ||
} | ||
|
||
for quotient in coreSystemsIntegers { | ||
for remainder in coreSystemsIntegers { | ||
whereIs(quotient, remainder) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters