A linear time byte pattern finder, useful to discover that two bytes seqences are almost identical, probably they are but during construction of one of them the developer has accidently either reversed the sequence, or they originate from integers, which might accidently use the wrong endianess, or a combination of both.
All examples below assume that the bytes sequences LHS and RHS
have the same length.
identical
Two byte sequences are identical, i.e. equal to each other.
Two byte sequences would be identical if one of them where initialized from a hex string that was revered before passed to byte sequence init.
Or in otherwords, if every byte in one of the sequences were rotated (ciruclar shifted) by 4 bits. This operator is not part of the standard library but is implemented by: (input >> 4) | (input << 4).
Two byte sequences would be identical if one of them were “chunked” into segments of length 2/4/8, i.e. as if loaded UInt16/UInt32/UInt64 at start of each segment, and then this list of integers were reversed, for reverseOrderOfUInt16sFromBytes/reverseOrderOfUInt32sFromBytes/reverseOrderOfUInt64sFromBytes.
Two byte sequences would be identical if one of them were “chunked” into segments of length 2/4/8, i.e. as if loaded UInt16/UInt32/UInt64 and we were to swap endianness of each integer in this list, for swapEndianessOfUInt16sFromBytes/swapEndianessOfUInt32sFromBytes/swapEndianessOfUInt64sFromBytes.
A small test util package which enables you to conveniently compare byte sequences using the BytePatternFinder. It contains some XCTAssert like methods, but specially tailored for byte sequence comparision.
XCTAssertBytesEqual
// This test will fail.
// Assertion failure message is:
// "Expected bytes in LHS to equal RHS, but they are not, however, they resemble each other with according to byte pattern: sameIf([swapEndianessOfUInt16sFromBytes])."
func test_data_failing() throws {
try XCTAssertBytesEqual(
Data(hex: "ab12 cd34"),
Data(hex: "12ab 34cd"),
"An optional message goes here."
)
}
You can change the behaviour of the test to pass for non-identical patterns found - but will still fail for no pattern found of course, by passing passOnPatternNonIdentical: true.
You can also globally change default value of passOnPatternNonIdentical and haltOnPatternNonIdentical by setting these properties on global type DefaultXCTAssertBytesEqualParameters. A good place to do this is in the setUp() method of your test class.
The examples above can be simplified by used of XCTAssertBytesFromHexEqual, which will fail with error if you’re passing in invalid hexadecimal strings.
BytePattern
A linear time byte pattern finder, useful to discover that two bytes seqences are almost identical, probably they are but during construction of one of them the developer has accidently either reversed the sequence, or they originate from integers, which might accidently use the wrong endianess, or a combination of both.
All examples below assume that the bytes sequences
LHS
andRHS
have the same length.identical
Two byte sequences are identical, i.e. equal to each other.
reversed
Two byte sequences would be identical if one of them where reversed in its entirety.
reversedHex
Two byte sequences would be identical if one of them where initialized from a hex string that was revered before passed to byte sequence init.
Or in otherwords, if every byte in one of the sequences were rotated (ciruclar shifted) by 4 bits. This operator is not part of the standard library but is implemented by:
(input >> 4) | (input << 4)
.reverseOrderOfUInt16/32/64sFromBytes
Two byte sequences would be identical if one of them were “chunked” into segments of length 2/4/8, i.e. as if loaded
UInt16
/UInt32
/UInt64
at start of each segment, and then this list of integers were reversed, forreverseOrderOfUInt16sFromBytes
/reverseOrderOfUInt32sFromBytes
/reverseOrderOfUInt64sFromBytes
.swapEndianessOfUInt16/32/64sFromBytes
Two byte sequences would be identical if one of them were “chunked” into segments of length 2/4/8, i.e. as if loaded
UInt16
/UInt32
/UInt64
and we were to swap endianness of each integer in this list, forswapEndianessOfUInt16sFromBytes
/swapEndianessOfUInt32sFromBytes
/swapEndianessOfUInt64sFromBytes
.Combined
XCTAssertBytesEqual
A small test util package which enables you to conveniently compare byte sequences using the
BytePatternFinder
. It contains someXCTAssert
like methods, but specially tailored for byte sequence comparision.XCTAssertBytesEqual
You can change the behaviour of the test to pass for non-
identical
patterns found - but will still fail for no pattern found of course, by passingpassOnPatternNonIdentical: true
.You can also opt in to interrupt on non-identical patterns found by passing
haltOnPatternNonIdentical: true
:You can also globally change default value of
passOnPatternNonIdentical
andhaltOnPatternNonIdentical
by setting these properties on global typeDefaultXCTAssertBytesEqualParameters
. A good place to do this is in thesetUp()
method of your test class.XCTAssertBytesFromHexEqual
The examples above can be simplified by used of
XCTAssertBytesFromHexEqual
, which will fail with error if you’re passing in invalid hexadecimal strings.BytesMutation
This small package allows you to perform mutation on any byte sequence conforming to
ContiguousBytes
and which names mirror those ofBytePattern
.