From 266d1784f61c86443f213ee4c515237e94397741 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oscar=20Bystr=C3=B6m=20Ericsson?= Date: Mon, 4 Nov 2024 08:24:43 +0100 Subject: [PATCH] Some loading tests (#110). --- Sources/CoreKit/BinaryInteger+Loading.swift | 2 +- .../BinaryInteger+Loading.swift | 50 ++++++++++++++----- 2 files changed, 38 insertions(+), 14 deletions(-) diff --git a/Sources/CoreKit/BinaryInteger+Loading.swift b/Sources/CoreKit/BinaryInteger+Loading.swift index ad3fc046..95b96e25 100644 --- a/Sources/CoreKit/BinaryInteger+Loading.swift +++ b/Sources/CoreKit/BinaryInteger+Loading.swift @@ -20,7 +20,7 @@ extension BinaryInteger { /// Loads the bit pattern of `body` and `appendix` that fits. @inlinable public init( load body: borrowing Body, - repeating appendix: Bit = .zero + repeating appendix: Bit = Bit.zero ) where Body.Element: SystemsInteger & UnsignedInteger { self = body.withUnsafeBufferPointer { diff --git a/Tests/UltimathnumTests/BinaryInteger+Loading.swift b/Tests/UltimathnumTests/BinaryInteger+Loading.swift index d109951f..cea6a573 100644 --- a/Tests/UltimathnumTests/BinaryInteger+Loading.swift +++ b/Tests/UltimathnumTests/BinaryInteger+Loading.swift @@ -32,10 +32,13 @@ import TestKit2 /// - Note: The `junk` part starts at the end of the type's body. /// @Test( - "BinaryInteger/loading: random", + "BinaryInteger/loading: random payload-extension-junk", Tag.List.tags(.forwarding, .generic, .random), arguments: typesAsBinaryInteger, fuzzers - ) func random(type: any BinaryInteger.Type, randomness: consuming FuzzerInt) throws { + ) func randomPayloadExtensionJunk( + type: any BinaryInteger.Type, + randomness: consuming FuzzerInt + ) throws { for element in typesAsSystemsIntegerAsUnsigned { try whereIs(type, element: element) @@ -117,17 +120,21 @@ import TestKit2 try Ɣrequire(other, matches: body[.., repeating appendix: Bit) throws { - try withOnlyOneCallToRequire((instance, body)) { require in - instance.withUnsafeBinaryIntegerElements(as: U8.self) { instance in - require(instance.appendix == appendix) - body.withUnsafeBytes { body in - for index: Swift.Int in body.indices { - let lhs = instance[UX(IX(index))] - let rhs = body.load(fromByteOffset: index, as: U8.self) - require(lhs == rhs) - } + } + } + + func Ɣrequire( + _ integer: Integer, + matches body: Body, + repeating appendix: Bit + ) throws where Integer: BinaryInteger, Body: Contiguous, Body.Element: SystemsIntegerAsUnsigned { + try withOnlyOneCallToRequire((integer, body)) { require in + integer.withUnsafeBinaryIntegerElements(as: U8.self) { integer in + require(integer.appendix == appendix) + body.withUnsafeBufferPointer { body in + body.withMemoryRebound(to: U8.self) { body in + for index in body.indices { + require(integer[UX(IX(index))] == body[index]) } } } @@ -136,6 +143,23 @@ import TestKit2 } } +//*============================================================================* +// MARK: * Binary Integer x Loading x Conveniences +//*============================================================================* + +@Suite struct BinaryIntegerTestsOnLoadingConveniences { + + //=------------------------------------------------------------------------= + // MARK: Tests + //=------------------------------------------------------------------------= + + @Test("BinaryInteger/loading/conveniences: default appendix is zero") + func defaultAppendixIsZero() { + #expect(IX(load: [] as [U8]) == IX.zero) + #expect(UX(load: [] as [U8]) == UX.zero) + } +} + //*============================================================================* // MARK: * Binary Integer x Loading x Disambiguation //*============================================================================*