open Space_wire let encode codec v = let ws = Wire.Codec.wire_size codec in let buf = Bytes.create ws in Wire.Codec.encode codec v buf 0; Bytes.unsafe_to_string buf let decode codec s = Wire.Codec.decode codec (Bytes.of_string s) 0 let test_error_roundtrip () = let err = Error_payload.v Unknown_type ~offending_type:0xFF ~offending_apid:0x42 ~offending_pay_len:248 in let encoded = encode Error_payload.codec err in Alcotest.(check int) "error size" 8 (String.length encoded); let decoded = decode Error_payload.codec encoded in Alcotest.(check bool) "roundtrip" true (Error_payload.equal err decoded) let test_error_layout () = let err = Error_payload.v Host_busy ~offending_type:0x07 ~offending_apid:0x100 ~offending_pay_len:0x1234 in let encoded = encode Error_payload.codec err in Alcotest.(check int) "error_code" 0x05 (Char.code encoded.[0]); Alcotest.(check int) "offending_type" 0x07 (Char.code encoded.[1]); (* apid 0x0100 big-endian *) Alcotest.(check int) "apid high" 0x01 (Char.code encoded.[2]); Alcotest.(check int) "apid low" 0x00 (Char.code encoded.[3]); (* pay_len 0x1234 big-endian at offset 4-5 *) Alcotest.(check int) "pay_len high" 0x12 (Char.code encoded.[4]); Alcotest.(check int) "pay_len low" 0x34 (Char.code encoded.[5]); (* reserved = 0 at offset 6-7 *) Alcotest.(check int) "reserved high" 0x00 (Char.code encoded.[6]); Alcotest.(check int) "reserved low" 0x00 (Char.code encoded.[7]) let suite = ( "error_payload", [ Alcotest.test_case "roundtrip" `Quick test_error_roundtrip; Alcotest.test_case "layout" `Quick test_error_layout; ] )