Skip to content

Commit

Permalink
Lower Expect+Bitwise.swift utilities (#110).
Browse files Browse the repository at this point in the history
  • Loading branch information
oscbyspro committed Nov 8, 2024
1 parent 2473d2f commit d324af8
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 109 deletions.
54 changes: 0 additions & 54 deletions Sources/TestKit/Expect+Bitwise.swift

This file was deleted.

19 changes: 19 additions & 0 deletions Sources/TestKit/Utilities+Reduce.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@
try transform(rhs)
}

@inlinable public func reduce<A, B>(
_ lhs: A,
_ transform: (consuming A) throws -> B
) rethrows -> B {
try transform(lhs)
}

@inlinable public func reduce<A, B, C>(
_ lhs: consuming A,
_ transform: (consuming A, borrowing B) throws -> C,
Expand All @@ -26,6 +33,18 @@
try transform(lhs, rhs)
}

//=----------------------------------------------------------------------------=
// MARK: + Inout
//=----------------------------------------------------------------------------=

@inlinable public func reduce<A>(
_ lhs: consuming A,
_ transform: (inout A) throws -> Void
) rethrows -> A {
try transform(&lhs)
return lhs
}

@inlinable public func reduce<A, B>(
_ lhs: consuming A,
_ transform: (inout A, borrowing B) throws -> Void,
Expand Down
77 changes: 49 additions & 28 deletions Tests/CoreKitTests/Bit+Bitwise.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,51 +14,72 @@ import TestKit
// MARK: * Bit x Bitwise
//*============================================================================*

@Suite struct BitTestsOnBitwise {
@Suite(.serialized) struct BitTestsOnBitwise {

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

@Test("Bit.~(_:)", .serialized, arguments: [
@Test(
"Bit/bitwise: ~(_:)",
Tag.List.tags(.documentation, .exhaustive),
ParallelizationTrait.serialized,
arguments: Array<(Bit, Bit)>([
Some(Bit.zero, yields: Bit.one ),
Some(Bit.one, yields: Bit.zero),
(Bit.zero, Bit.one ),
(Bit.one, Bit.zero),
]) func not(_ argument: Some<Bit, Bit>) {
Ɣexpect(not: argument.input, is: argument.output)
])) func not(instance: Bit, expectation: Bit) {
#expect(expectation == reduce(instance) { ~$0 })
#expect(expectation == reduce(instance) { $0.toggle () })
#expect(expectation == reduce(instance) { $0.toggled() })
}

@Test("Bit.&(_:_:)", .serialized, arguments: [
@Test(
"Bit/bitwise: &(_:_:)",
Tag.List.tags(.documentation, .exhaustive),
ParallelizationTrait.serialized,
arguments: Array<(Bit, Bit, Bit)>([
Some(Bit.zero, Bit.zero, yields: Bit.zero),
Some(Bit.zero, Bit.one, yields: Bit.zero),
Some(Bit.one, Bit.zero, yields: Bit.zero),
Some(Bit.one, Bit.one, yields: Bit.one ),
]) func and(_ argument: Some<Bit, Bit, Bit>) {
Ɣexpect(argument.0, and: argument.1, is: argument.output)
(Bit.zero, Bit.zero, Bit.zero),
(Bit.zero, Bit.one, Bit.zero),
(Bit.one, Bit.zero, Bit.zero),
(Bit.one, Bit.one, Bit.one ),
])) func and(lhs: Bit, rhs: Bit, expectation: Bit) {
#expect(expectation == reduce(lhs, &, rhs))
#expect(expectation == reduce(lhs, &=, rhs))
}

@Test("Bit.|(_:_:)", .serialized, arguments: [
@Test(
"Bit/bitwise: |(_:_:)",
Tag.List.tags(.documentation, .exhaustive),
ParallelizationTrait.serialized,
arguments: Array<(Bit, Bit, Bit)>([
Some(Bit.zero, Bit.zero, yields: Bit.zero),
Some(Bit.zero, Bit.one, yields: Bit.one ),
Some(Bit.one, Bit.zero, yields: Bit.one ),
Some(Bit.one, Bit.one, yields: Bit.one ),
(Bit.zero, Bit.zero, Bit.zero),
(Bit.zero, Bit.one, Bit.one ),
(Bit.one, Bit.zero, Bit.one ),
(Bit.one, Bit.one, Bit.one ),
]) func or(_ argument: Some<Bit, Bit, Bit>) {
Ɣexpect(argument.0, or: argument.1, is: argument.output)
])) func or(lhs: Bit, rhs: Bit, expectation: Bit) {
#expect(expectation == reduce(lhs, |, rhs))
#expect(expectation == reduce(lhs, |=, rhs))
}

@Test("Bit.^(_:_:)", .serialized, arguments: [
@Test(
"Bit/bitwise: ^(_:_:)",
Tag.List.tags(.documentation, .exhaustive),
ParallelizationTrait.serialized,
arguments: Array<(Bit, Bit, Bit)>([
Some(Bit.zero, Bit.zero, yields: Bit.zero),
Some(Bit.zero, Bit.one, yields: Bit.one ),
Some(Bit.one, Bit.zero, yields: Bit.one ),
Some(Bit.one, Bit.one, yields: Bit.zero),
(Bit.zero, Bit.zero, Bit.zero),
(Bit.zero, Bit.one, Bit.one ),
(Bit.one, Bit.zero, Bit.one ),
(Bit.one, Bit.one, Bit.zero),
]) func xor(_ argument: Some<Bit, Bit, Bit>) {
Ɣexpect(argument.0, xor: argument.1, is: argument.output)
])) func xor(lhs: Bit, rhs: Bit, expectation: Bit) {
#expect(expectation == reduce(lhs, ^, rhs))
#expect(expectation == reduce(lhs, ^=, rhs))
}
}
75 changes: 48 additions & 27 deletions Tests/CoreKitTests/Sign+Bitwise.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,51 +14,72 @@ import TestKit
// MARK: * Sign x Bitwise
//*============================================================================*

@Suite struct SignTestsOnBitwise {
@Suite(.serialized) struct SignTestsOnBitwise {

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

@Test("Sign.~(_:)", .serialized, arguments: [
@Test(
"Sign/bitwise: ~(_:)",
Tag.List.tags(.documentation, .exhaustive),
ParallelizationTrait.serialized,
arguments: Array<(Sign, Sign)>([
Some(Sign.plus, yields: Sign.minus),
Some(Sign.minus, yields: Sign.plus ),
(Sign.plus, Sign.minus),
(Sign.minus, Sign.plus ),
]) func not(_ argument: Some<Sign, Sign>) {
Ɣexpect(not: argument.input, is: argument.output)
])) func not(instance: Sign, expectation: Sign) {
#expect(expectation == reduce(instance) { ~$0 })
#expect(expectation == reduce(instance) { $0.toggle () })
#expect(expectation == reduce(instance) { $0.toggled() })
}

@Test("Sign.&(_:_:)", .serialized, arguments: [
@Test(
"Sign/bitwise: &(_:_:)",
Tag.List.tags(.documentation, .exhaustive),
ParallelizationTrait.serialized,
arguments: Array<(Sign, Sign, Sign)>([
Some(Sign.plus, Sign.plus, yields: Sign.plus ),
Some(Sign.plus, Sign.minus, yields: Sign.plus ),
Some(Sign.minus, Sign.plus, yields: Sign.plus ),
Some(Sign.minus, Sign.minus, yields: Sign.minus),
(Sign.plus, Sign.plus, Sign.plus ),
(Sign.plus, Sign.minus, Sign.plus ),
(Sign.minus, Sign.plus, Sign.plus ),
(Sign.minus, Sign.minus, Sign.minus),
]) func and(_ argument: Some<Sign, Sign, Sign>) {
Ɣexpect(argument.0, and: argument.1, is: argument.output)
])) func and(lhs: Sign, rhs: Sign, expectation: Sign) {
#expect(expectation == reduce(lhs, &, rhs))
#expect(expectation == reduce(lhs, &=, rhs))
}

@Test("Sign.|(_:_:)", .serialized, arguments: [
@Test(
"Sign/bitwise: |(_:_:)",
Tag.List.tags(.documentation, .exhaustive),
ParallelizationTrait.serialized,
arguments: Array<(Sign, Sign, Sign)>([
Some(Sign.plus, Sign.plus, yields: Sign.plus ),
Some(Sign.plus, Sign.minus, yields: Sign.minus),
Some(Sign.minus, Sign.plus, yields: Sign.minus),
Some(Sign.minus, Sign.minus, yields: Sign.minus),
(Sign.plus, Sign.plus, Sign.plus ),
(Sign.plus, Sign.minus, Sign.minus),
(Sign.minus, Sign.plus, Sign.minus),
(Sign.minus, Sign.minus, Sign.minus),
]) func or(_ argument: Some<Sign, Sign, Sign>) {
Ɣexpect(argument.0, or: argument.1, is: argument.output)
])) func or(lhs: Sign, rhs: Sign, expectation: Sign) {
#expect(expectation == reduce(lhs, |, rhs))
#expect(expectation == reduce(lhs, |=, rhs))
}

@Test("Sign.^(_:_:)", .serialized, arguments: [
@Test(
"Sign/bitwise: ^(_:_:)",
Tag.List.tags(.documentation, .exhaustive),
ParallelizationTrait.serialized,
arguments: Array<(Sign, Sign, Sign)>([
Some(Sign.plus, Sign.plus, yields: Sign.plus ),
Some(Sign.plus, Sign.minus, yields: Sign.minus),
Some(Sign.minus, Sign.plus, yields: Sign.minus),
Some(Sign.minus, Sign.minus, yields: Sign.plus ),
(Sign.plus, Sign.plus, Sign.plus ),
(Sign.plus, Sign.minus, Sign.minus),
(Sign.minus, Sign.plus, Sign.minus),
(Sign.minus, Sign.minus, Sign.plus ),
]) func xor(_ argument: Some<Sign, Sign, Sign>) {
Ɣexpect(argument.0, xor: argument.1, is: argument.output)
])) func xor(lhs: Sign, rhs: Sign, expectation: Sign) {
#expect(expectation == reduce(lhs, ^, rhs))
#expect(expectation == reduce(lhs, ^=, rhs))
}
}

0 comments on commit d324af8

Please sign in to comment.