Skip to content

Commit

Permalink
Some loading tests (#110).
Browse files Browse the repository at this point in the history
  • Loading branch information
oscbyspro committed Nov 4, 2024
1 parent e51d7da commit 266d178
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Sources/CoreKit/BinaryInteger+Loading.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ extension BinaryInteger {
/// Loads the bit pattern of `body` and `appendix` that fits.
@inlinable public init<Body: Contiguous>(
load body: borrowing Body,
repeating appendix: Bit = .zero
repeating appendix: Bit = Bit.zero
) where Body.Element: SystemsInteger & UnsignedInteger {

self = body.withUnsafeBufferPointer {
Expand Down
50 changes: 37 additions & 13 deletions Tests/UltimathnumTests/BinaryInteger+Loading.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -117,17 +120,21 @@ import TestKit2
try Ɣrequire(other, matches: body[..<capacity], repeating: appendix)
}
}

func Ɣrequire(_ instance: T, matches body: ArraySlice<E>, 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, Body>(
_ 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])
}
}
}
Expand All @@ -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
//*============================================================================*
Expand Down

0 comments on commit 266d178

Please sign in to comment.