(*--------------------------------------------------------------------------- Copyright (c) 2025 Thomas Gazagnaire. All rights reserved. SPDX-License-Identifier: MIT ---------------------------------------------------------------------------*) open Crowbar (* TLV roundtrip: encode(decode(x)) should produce valid TLV *) let test_tlv_decode_encode input = let decoded = Hap.Tlv.decode input in let _ = Hap.Tlv.encode decoded in check true (* TLV encode/decode roundtrip for valid TLV *) let test_tlv_roundtrip typ value = let tlv = Hap.Tlv.(add typ value empty) in let encoded = Hap.Tlv.encode tlv in let decoded = Hap.Tlv.decode encoded in let retrieved = Hap.Tlv.get typ decoded in check_eq ~pp:Format.pp_print_bool (retrieved = Some value) true (* Multiple TLV entries roundtrip - deduplicate types since later adds overwrite *) let test_tlv_multi_roundtrip entries = (* Keep only the last value for each type, matching add semantics *) let deduped = List.fold_left (fun acc (typ, value) -> List.filter (fun (t, _) -> t <> typ) acc @ [ (typ, value) ]) [] entries in let tlv = List.fold_left (fun acc (typ, value) -> Hap.Tlv.add typ value acc) Hap.Tlv.empty deduped in let encoded = Hap.Tlv.encode tlv in let decoded = Hap.Tlv.decode encoded in List.iter (fun (typ, value) -> let retrieved = Hap.Tlv.get typ decoded in check_eq ~pp:Format.pp_print_bool (retrieved = Some value) true) deduped (* Category name should never crash *) let test_category_name code = let _ = Hap.category_name code in check true let suite = ( "hap", [ test_case "TLV decode/encode no crash" [ bytes ] test_tlv_decode_encode; test_case "TLV roundtrip" [ range 256; bytes ] test_tlv_roundtrip; test_case "TLV multi roundtrip" [ list (pair (range 256) bytes) ] test_tlv_multi_roundtrip; test_case "category_name no crash" [ int ] test_category_name; ] )