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_param_entry_roundtrip () = let p = Param_entry.v ~param_id:42 ~generation:7 "hello" in let encoded = encode Param_entry.codec p in Alcotest.(check int) "param size" 252 (String.length encoded); let decoded = decode Param_entry.codec encoded in Alcotest.(check bool) "roundtrip" true (Param_entry.equal p decoded) let test_param_entry_crc () = let p = Param_entry.v ~param_id:42 ~generation:1 "value" in Alcotest.(check bool) "crc ok" true (Param_entry.check_crc p); let bad = { p with Param_entry.param_id = 999 } in Alcotest.(check bool) "crc bad" false (Param_entry.check_crc bad) let test_param_entry_layout () = let p = Param_entry.v ~param_id:0x01020304 ~generation:3 "AB" in let encoded = encode Param_entry.codec p in (* param_id = 0x01020304 big-endian at offset 0-3 *) Alcotest.(check int) "pid[0]" 0x01 (Char.code encoded.[0]); Alcotest.(check int) "pid[1]" 0x02 (Char.code encoded.[1]); Alcotest.(check int) "pid[2]" 0x03 (Char.code encoded.[2]); Alcotest.(check int) "pid[3]" 0x04 (Char.code encoded.[3]); (* len=2 at offset 4-5 *) Alcotest.(check int) "len high" 0x00 (Char.code encoded.[4]); Alcotest.(check int) "len low" 0x02 (Char.code encoded.[5]); (* generation=3 at offset 6-7 *) Alcotest.(check int) "gen high" 0x00 (Char.code encoded.[6]); Alcotest.(check int) "gen low" 0x03 (Char.code encoded.[7]); (* value starts at offset 8 *) Alcotest.(check char) "value[0]" 'A' encoded.[8]; Alcotest.(check char) "value[1]" 'B' encoded.[9] let suite = ( "param_entry", [ Alcotest.test_case "roundtrip" `Quick test_param_entry_roundtrip; Alcotest.test_case "crc" `Quick test_param_entry_crc; Alcotest.test_case "layout" `Quick test_param_entry_layout; ] )