My own corner of monopam
2
fork

Configure Feed

Select the types of activity you want to include in your feed.

fix(ocaml-tm): fix naming, Fmt.invalid_arg, and add docs

- Rename get_u8→u8, get_u16_be→u16_be, get_u32_be→u32_be (private helpers)
- Rename get_clcw→find_clcw (returns option type)
- Rename make_header→header, make_clcw→clcw, make→v
- Replace invalid_arg (Fmt.str ...) with Fmt.invalid_arg
- Add docs to tm.mli: pp_clcw, pp, struct_, module_, wire decode/encode fns
- Add module/suite docs to fuzz_tm.mli and test_tm.mli

+58 -34
+4 -6
ocaml-tm/fuzz/fuzz_tm.ml
··· 41 41 let fhp = fhp mod 2048 in 42 42 match (Tm.scid scid_val, Tm.vcid vcid_val) with 43 43 | Some scid, Some vcid -> ( 44 - let hdr = Tm.make_header ~scid ~vcid ~mcfc ~vcfc ~first_hdr_ptr:fhp () in 44 + let hdr = Tm.header ~scid ~vcid ~mcfc ~vcfc ~first_hdr_ptr:fhp () in 45 45 let encoded = Tm.encode_header hdr in 46 46 match Tm.decode_header encoded with 47 47 | Ok decoded -> ··· 67 67 let farm_b = farm_b mod 4 in 68 68 match Tm.vcid vcid_val with 69 69 | Some vcid -> ( 70 - let clcw = 71 - Tm.make_clcw ~vcid ~report_value:report ~farm_b_counter:farm_b () 72 - in 70 + let clcw = Tm.clcw ~vcid ~report_value:report ~farm_b_counter:farm_b () in 73 71 let encoded = Tm.encode_clcw clcw in 74 72 match Tm.decode_clcw encoded with 75 73 | Ok decoded -> ··· 85 83 else 86 84 match (Tm.scid 100, Tm.vcid 2) with 87 85 | Some scid, Some vcid -> ( 88 - let frame = Tm.make ~scid ~vcid ~mcfc:0 ~vcfc:0 ~ocf_flag:false data in 86 + let frame = Tm.v ~scid ~vcid ~mcfc:0 ~vcfc:0 ~ocf_flag:false data in 89 87 let encoded = Tm.encode frame in 90 88 match 91 89 Tm.decode ~frame_len:(String.length encoded) ~expect_ocf:false encoded ··· 102 100 else 103 101 match (Tm.scid 0, Tm.vcid 0) with 104 102 | Some scid, Some vcid -> ( 105 - let frame = Tm.make ~scid ~vcid ~mcfc:0 ~vcfc:0 data in 103 + let frame = Tm.v ~scid ~vcid ~mcfc:0 ~vcfc:0 data in 106 104 let encoded = Tm.encode frame in 107 105 let len = String.length encoded in 108 106 if len < 3 then ()
+3
ocaml-tm/fuzz/fuzz_tm.mli
··· 1 + (** Fuzz tests for TM frame encoding/decoding. *) 2 + 1 3 val suite : string * Crowbar.test_case list 4 + (** Crowbar fuzz test suite. *)
+9 -10
ocaml-tm/lib/tm.ml
··· 13 13 14 14 let scid_exn n = 15 15 if n >= 0 && n <= 1023 then n 16 - else invalid_arg (Fmt.str "scid: %d out of range 0-1023" n) 16 + else Fmt.invalid_arg "scid: %d out of range 0-1023" n 17 17 18 18 let scid_to_int s = s 19 19 ··· 23 23 let vcid n = if n >= 0 && n <= 7 then Some n else None 24 24 25 25 let vcid_exn n = 26 - if n >= 0 && n <= 7 then n 27 - else invalid_arg (Fmt.str "vcid: %d out of range 0-7" n) 26 + if n >= 0 && n <= 7 then n else Fmt.invalid_arg "vcid: %d out of range 0-7" n 28 27 29 28 let vcid_to_int v = v 30 29 ··· 243 242 let decode_clcw word = Clcw.decode word 244 243 let encode_clcw clcw = Clcw.encode clcw 245 244 246 - let get_clcw frame = 245 + let find_clcw frame = 247 246 match frame.ocf with None -> None | Some word -> Some (Clcw.decode word) 248 247 249 248 (* Constructors *) 250 - let make_header ?(version = 0) ?(ocf_flag = true) ?(sec_hdr = false) 249 + let header ?(version = 0) ?(ocf_flag = true) ?(sec_hdr = false) 251 250 ?(sync_flag = false) ?(pkt_order = false) ?(seg_len_id = 3) 252 251 ?(first_hdr_ptr = 0) ~scid ~vcid ~mcfc ~vcfc () = 253 252 { ··· 264 263 first_hdr_ptr; 265 264 } 266 265 267 - let make ?(version = 0) ?(ocf_flag = true) ?(sec_hdr = false) 268 - ?(sync_flag = false) ?(pkt_order = false) ?(seg_len_id = 3) 269 - ?(first_hdr_ptr = 0) ?sec_hdr_data ?ocf ?fecf ~scid ~vcid ~mcfc ~vcfc data = 266 + let v ?(version = 0) ?(ocf_flag = true) ?(sec_hdr = false) ?(sync_flag = false) 267 + ?(pkt_order = false) ?(seg_len_id = 3) ?(first_hdr_ptr = 0) ?sec_hdr_data 268 + ?ocf ?fecf ~scid ~vcid ~mcfc ~vcfc data = 270 269 let header = 271 - make_header ~version ~ocf_flag ~sec_hdr ~sync_flag ~pkt_order ~seg_len_id 270 + header ~version ~ocf_flag ~sec_hdr ~sync_flag ~pkt_order ~seg_len_id 272 271 ~first_hdr_ptr ~scid ~vcid ~mcfc ~vcfc () 273 272 in 274 273 { header; sec_hdr_data; data; ocf; fecf } 275 274 276 - let make_clcw ?(control_word_type = 0) ?(version = 0) ?(status = Ready) 275 + let clcw ?(control_word_type = 0) ?(version = 0) ?(status = Ready) 277 276 ?(cop_in_effect = 1) ?(no_rf_available = false) ?(no_bit_lock = false) 278 277 ?(lockout = false) ?(wait = false) ?(retransmit = false) 279 278 ?(farm_b_counter = 0) ~vcid ~report_value () =
+30 -5
ocaml-tm/lib/tm.mli
··· 182 182 val encode_clcw : Clcw.t -> int 183 183 (** [encode_clcw clcw] encodes a CLCW to a 32-bit word. *) 184 184 185 - val get_clcw : t -> (Clcw.t, Clcw.error) result option 186 - (** [get_clcw frame] extracts and decodes the CLCW from the frame's OCF if 185 + val find_clcw : t -> (Clcw.t, Clcw.error) result option 186 + (** [find_clcw frame] extracts and decodes the CLCW from the frame's OCF if 187 187 present. Returns [None] if no OCF. *) 188 188 189 189 (** {1 Constructors} *) 190 190 191 - val make_header : 191 + val header : 192 192 ?version:int -> 193 193 ?ocf_flag:bool -> 194 194 ?sec_hdr:bool -> ··· 204 204 header 205 205 (** Create a TM frame header with the given parameters. *) 206 206 207 - val make : 207 + val v : 208 208 ?version:int -> 209 209 ?ocf_flag:bool -> 210 210 ?sec_hdr:bool -> ··· 223 223 t 224 224 (** [make ~scid ~vcid ~mcfc ~vcfc data] creates a TM frame. *) 225 225 226 - val make_clcw : 226 + val clcw : 227 227 ?control_word_type:int -> 228 228 ?version:int -> 229 229 ?status:clcw_status -> ··· 243 243 (** {1 Pretty-printing} *) 244 244 245 245 val pp_header : Format.formatter -> header -> unit 246 + 246 247 val pp_clcw : Format.formatter -> clcw -> unit 248 + (** Pretty-print a CLCW value. *) 249 + 247 250 val pp : Format.formatter -> t -> unit 251 + (** Pretty-print a TM frame. *) 248 252 249 253 (** {1 Packed Header Wire Representation} *) 250 254 ··· 276 280 (** {1 Wire Codec} *) 277 281 278 282 val codec : packed_header Wire.Codec.t 283 + (** Wire codec for [packed_header]. *) 284 + 279 285 val struct_ : Wire.struct_ 286 + (** Wire struct descriptor. *) 287 + 280 288 val module_ : Wire.module_ 289 + (** Wire module descriptor. *) 281 290 282 291 (** {1 Wire Parse/Encode} *) 283 292 284 293 val wire_size : int 294 + (** Wire size of a packed header in bytes. *) 295 + 285 296 val decode_packed_header : bytes -> int -> packed_header 297 + (** [decode_packed_header buf off] decodes a packed header from [buf] at offset 298 + [off]. *) 299 + 286 300 val encode_packed_header : packed_header -> bytes -> int -> unit 301 + (** [encode_packed_header h buf off] encodes [h] into [buf] at offset [off]. *) 302 + 287 303 val decode_bytes : bytes -> (packed_header, Wire.parse_error) result 304 + (** [decode_bytes buf] decodes a packed header from [buf]. *) 305 + 288 306 val decode_string : string -> (packed_header, Wire.parse_error) result 307 + (** [decode_string s] decodes a packed header from string [s]. *) 308 + 289 309 val encode_string : packed_header -> string 310 + (** [encode_string h] encodes [h] as a string. *) 311 + 290 312 val encode_bytes : packed_header -> bytes 313 + (** [encode_bytes h] encodes [h] as bytes. *) 291 314 292 315 (** {1 FFI Code Generation} *) 293 316 294 317 val c_stubs : unit -> string 318 + 295 319 val ml_stubs : unit -> string 320 + (** Generate OCaml FFI stub code. *)
+9 -13
ocaml-tm/test/test_tm.ml
··· 36 36 let test_header_roundtrip () = 37 37 let scid = Tm.scid_exn 123 in 38 38 let vcid = Tm.vcid_exn 5 in 39 - let hdr = 40 - Tm.make_header ~scid ~vcid ~mcfc:42 ~vcfc:99 ~first_hdr_ptr:0x100 () 41 - in 39 + let hdr = Tm.header ~scid ~vcid ~mcfc:42 ~vcfc:99 ~first_hdr_ptr:0x100 () in 42 40 let encoded = Tm.encode_header hdr in 43 41 Alcotest.(check int) "header is 6 bytes" 6 (String.length encoded); 44 42 match Tm.decode_header encoded with ··· 49 47 let test_clcw_roundtrip () = 50 48 let vcid = Tm.vcid_exn 3 in 51 49 let clcw = 52 - Tm.make_clcw ~vcid ~report_value:127 ~lockout:true ~retransmit:true () 50 + Tm.clcw ~vcid ~report_value:127 ~lockout:true ~retransmit:true () 53 51 in 54 52 let encoded = Tm.encode_clcw clcw in 55 53 match Tm.decode_clcw encoded with ··· 62 60 let vcid = Tm.vcid_exn 2 in 63 61 let data = String.make 1103 '\x55' in 64 62 (* 1115 - 6 header - 4 OCF - 2 FECF = 1103 *) 65 - let frame = Tm.make ~scid ~vcid ~mcfc:1 ~vcfc:2 ~ocf:0x12345678 data in 63 + let frame = Tm.v ~scid ~vcid ~mcfc:1 ~vcfc:2 ~ocf:0x12345678 data in 66 64 let encoded = Tm.encode frame in 67 65 Alcotest.(check int) "encoded length" 1115 (String.length encoded); 68 66 match Tm.decode encoded with ··· 112 110 let scid = Tm.scid_exn 0 in 113 111 let vcid = Tm.vcid_exn 0 in 114 112 let data = String.make 1103 '\x00' in 115 - let frame = Tm.make ~scid ~vcid ~mcfc:0 ~vcfc:0 ~ocf:0 data in 113 + let frame = Tm.v ~scid ~vcid ~mcfc:0 ~vcfc:0 ~ocf:0 data in 116 114 let encoded = Tm.encode frame in 117 115 let frame_len = String.length encoded in 118 116 (* Corrupt the FECF (last 2 bytes) *) ··· 168 166 let test_wire_vs_manual () = 169 167 let scid = Tm.scid_exn 123 in 170 168 let vcid = Tm.vcid_exn 5 in 171 - let hdr = 172 - Tm.make_header ~scid ~vcid ~mcfc:42 ~vcfc:99 ~first_hdr_ptr:0x100 () 173 - in 169 + let hdr = Tm.header ~scid ~vcid ~mcfc:42 ~vcfc:99 ~first_hdr_ptr:0x100 () in 174 170 let manual = Tm.encode_header hdr in 175 171 let packed = Tm.to_packed_header hdr in 176 172 let wire = Tm.encode_string packed in ··· 182 178 let vcid = Tm.vcid_exn 0 in 183 179 let data = String.make 1103 '\x00' in 184 180 (* mcfc = 255 (max 8-bit value) *) 185 - let frame_max = Tm.make ~scid ~vcid ~mcfc:255 ~vcfc:0 ~ocf:0 data in 181 + let frame_max = Tm.v ~scid ~vcid ~mcfc:255 ~vcfc:0 ~ocf:0 data in 186 182 let encoded_max = Tm.encode frame_max in 187 183 (match Tm.decode ~frame_len:(String.length encoded_max) encoded_max with 188 184 | Error e -> Alcotest.failf "decode mcfc=255 failed: %a" Tm.pp_error e 189 185 | Ok decoded -> Alcotest.(check int) "mcfc=255" 255 decoded.header.mcfc); 190 186 (* mcfc = 0 (after wraparound) *) 191 - let frame_zero = Tm.make ~scid ~vcid ~mcfc:0 ~vcfc:0 ~ocf:0 data in 187 + let frame_zero = Tm.v ~scid ~vcid ~mcfc:0 ~vcfc:0 ~ocf:0 data in 192 188 let encoded_zero = Tm.encode frame_zero in 193 189 match Tm.decode ~frame_len:(String.length encoded_zero) encoded_zero with 194 190 | Error e -> Alcotest.failf "decode mcfc=0 failed: %a" Tm.pp_error e ··· 200 196 let vcid = Tm.vcid_exn 0 in 201 197 let data = String.make 1103 '\x00' in 202 198 (* vcfc = 255 (max 8-bit value) *) 203 - let frame_max = Tm.make ~scid ~vcid ~mcfc:0 ~vcfc:255 ~ocf:0 data in 199 + let frame_max = Tm.v ~scid ~vcid ~mcfc:0 ~vcfc:255 ~ocf:0 data in 204 200 let encoded_max = Tm.encode frame_max in 205 201 (match Tm.decode ~frame_len:(String.length encoded_max) encoded_max with 206 202 | Error e -> Alcotest.failf "decode vcfc=255 failed: %a" Tm.pp_error e 207 203 | Ok decoded -> Alcotest.(check int) "vcfc=255" 255 decoded.header.vcfc); 208 204 (* vcfc = 0 (after wraparound) *) 209 - let frame_zero = Tm.make ~scid ~vcid ~mcfc:0 ~vcfc:0 ~ocf:0 data in 205 + let frame_zero = Tm.v ~scid ~vcid ~mcfc:0 ~vcfc:0 ~ocf:0 data in 210 206 let encoded_zero = Tm.encode frame_zero in 211 207 match Tm.decode ~frame_len:(String.length encoded_zero) encoded_zero with 212 208 | Error e -> Alcotest.failf "decode vcfc=0 failed: %a" Tm.pp_error e
+3
ocaml-tm/test/test_tm.mli
··· 1 + (** Test suite for TM transfer frames. *) 2 + 1 3 val suite : string * unit Alcotest.test_case list 4 + (** Alcotest test suite. *)