From d324af89c91bba624337f2600f16285beb439e7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oscar=20Bystr=C3=B6m=20Ericsson?= Date: Fri, 8 Nov 2024 06:29:16 +0100 Subject: [PATCH] Lower Expect+Bitwise.swift utilities (#110). --- Sources/TestKit/Expect+Bitwise.swift | 54 ------------------ Sources/TestKit/Utilities+Reduce.swift | 19 +++++++ Tests/CoreKitTests/Bit+Bitwise.swift | 77 ++++++++++++++++---------- Tests/CoreKitTests/Sign+Bitwise.swift | 75 ++++++++++++++++--------- 4 files changed, 116 insertions(+), 109 deletions(-) delete mode 100644 Sources/TestKit/Expect+Bitwise.swift diff --git a/Sources/TestKit/Expect+Bitwise.swift b/Sources/TestKit/Expect+Bitwise.swift deleted file mode 100644 index 9497f173..00000000 --- a/Sources/TestKit/Expect+Bitwise.swift +++ /dev/null @@ -1,54 +0,0 @@ -//=----------------------------------------------------------------------------= -// 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 - -//*============================================================================* -// MARK: * Expect x Bitwise -//*============================================================================* - -@inlinable public func Ɣexpect( - not instance: T, - is expectation: T, - at location: SourceLocation = #_sourceLocation -) where T: BitOperable & Equatable { - #expect(~instance == expectation, "BitOperable.~(_:)", sourceLocation: location) - #expect( instance.toggled() == expectation, "BitOperable/toggled()", sourceLocation: location) - #expect({ var x = instance; x.toggle(); return x }() == expectation, "BitOperable/toggle()", sourceLocation: location) -} - -@inlinable public func Ɣexpect( - _ lhs: T, - and rhs: T, - is expectation: T, - at location: SourceLocation = #_sourceLocation -) where T: BitOperable & Equatable { - #expect(lhs & rhs == expectation, "BitOperable.&(_:_:)", sourceLocation: location) - #expect({ var x = lhs; x &= rhs; return x }() == expectation, "BitOperable.&=(_:_:)", sourceLocation: location) -} - -@inlinable public func Ɣexpect( - _ lhs: T, - or rhs: T, - is expectation: T, - at location: SourceLocation = #_sourceLocation -) where T: BitOperable & Equatable { - #expect(lhs | rhs == expectation, "BitOperable.|(_:_:)", sourceLocation: location) - #expect({ var x = lhs; x |= rhs; return x }() == expectation, "BitOperable.|=(_:_:)", sourceLocation: location) -} - -@inlinable public func Ɣexpect( - _ lhs: T, - xor rhs: T, - is expectation: T, - at location: SourceLocation = #_sourceLocation -) where T: BitOperable & Equatable { - #expect(lhs ^ rhs == expectation, "BitOperable.^(_:_:)", sourceLocation: location) - #expect({ var x = lhs; x ^= rhs; return x }() == expectation, "BitOperable.^=(_:_:)", sourceLocation: location) -} diff --git a/Sources/TestKit/Utilities+Reduce.swift b/Sources/TestKit/Utilities+Reduce.swift index fe7102db..9903e3b6 100644 --- a/Sources/TestKit/Utilities+Reduce.swift +++ b/Sources/TestKit/Utilities+Reduce.swift @@ -18,6 +18,13 @@ try transform(rhs) } +@inlinable public func reduce( + _ lhs: A, + _ transform: (consuming A) throws -> B +) rethrows -> B { + try transform(lhs) +} + @inlinable public func reduce( _ lhs: consuming A, _ transform: (consuming A, borrowing B) throws -> C, @@ -26,6 +33,18 @@ try transform(lhs, rhs) } +//=----------------------------------------------------------------------------= +// MARK: + Inout +//=----------------------------------------------------------------------------= + +@inlinable public func reduce( + _ lhs: consuming A, + _ transform: (inout A) throws -> Void +) rethrows -> A { + try transform(&lhs) + return lhs +} + @inlinable public func reduce( _ lhs: consuming A, _ transform: (inout A, borrowing B) throws -> Void, diff --git a/Tests/CoreKitTests/Bit+Bitwise.swift b/Tests/CoreKitTests/Bit+Bitwise.swift index 40e9f2d9..d7cb1cdd 100644 --- a/Tests/CoreKitTests/Bit+Bitwise.swift +++ b/Tests/CoreKitTests/Bit+Bitwise.swift @@ -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) { - Ɣ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) { - Ɣ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) { - Ɣ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) { - Ɣ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)) } } diff --git a/Tests/CoreKitTests/Sign+Bitwise.swift b/Tests/CoreKitTests/Sign+Bitwise.swift index 916ddd43..9f3a728c 100644 --- a/Tests/CoreKitTests/Sign+Bitwise.swift +++ b/Tests/CoreKitTests/Sign+Bitwise.swift @@ -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) { - Ɣ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) { - Ɣ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) { - Ɣ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) { - Ɣ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)) } }