ControlByte payloadSize determined for values smaller than 29. On to the next challenge.
Signed-off-by: Adam Rocska <adam.rocska@adams.solutions>
This commit is contained in:
parent
7bc612efee
commit
934dadc269
|
@ -2,7 +2,8 @@ import Foundation
|
||||||
|
|
||||||
struct ControlByte {
|
struct ControlByte {
|
||||||
|
|
||||||
let type: DataType
|
let type: DataType
|
||||||
|
let payloadSize: UInt32
|
||||||
|
|
||||||
init?(bytes: Data) {
|
init?(bytes: Data) {
|
||||||
if bytes.count == 0 || bytes.count > 5 { return nil }
|
if bytes.count == 0 || bytes.count > 5 { return nil }
|
||||||
|
@ -16,6 +17,15 @@ struct ControlByte {
|
||||||
else {
|
else {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let payloadSizeDefinition = firstByte & 0b0001_1111
|
||||||
|
switch payloadSizeDefinition {
|
||||||
|
case _ where payloadSizeDefinition < 29:
|
||||||
|
payloadSize = UInt32(payloadSizeDefinition)
|
||||||
|
default:
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
self.type = type
|
self.type = type
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -51,4 +51,25 @@ class ControlByteTest: XCTestCase {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func testInit_nilIfDataTypeNotRecognized() {
|
||||||
|
XCTAssertNil(ControlByte(bytes: Data([0b0000_1111, 0b0000_1111, 0b0000_0000])))
|
||||||
|
}
|
||||||
|
|
||||||
|
func testInit_payloadSizeDefinition_lessThan29() {
|
||||||
|
let testInput: [(expectedPayloadSize: UInt32, byte: UInt8)] = ControlByteTest
|
||||||
|
.nonExtendedRawValues
|
||||||
|
.reduce([]) { byteSequence, typeDefinition in
|
||||||
|
byteSequence + (0..<29).map({
|
||||||
|
(expectedPayloadSize: UInt32($0), byte: $0 | (typeDefinition << 5))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
for (expectedPayloadSize, byte) in testInput {
|
||||||
|
XCTAssertEqual(
|
||||||
|
expectedPayloadSize,
|
||||||
|
ControlByte(bytes: Data([byte]))?.payloadSize
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue