CRC checksums (CRC-16, CRC-32, CRC-32C) for OCaml
0
fork

Configure Feed

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

irmin: single Schema API — remove old Backend/Store/Tree/Proof/Codec

+47 -59
+3 -5
test/interop/crcmod/dune
··· 1 1 (test 2 2 (name test) 3 - (libraries crc alcotest) 3 + (libraries crc csvt alcotest) 4 4 (deps 5 5 (source_tree traces) 6 6 (source_tree scripts))) 7 7 8 - ; Regenerate traces: REGEN_TRACES=1 dune build @regen-traces 8 + ; Regenerate traces against crcmod: dune build @regen-traces 9 9 10 10 (rule 11 11 (alias regen-traces) 12 - (enabled_if 13 - (= %{env:REGEN_TRACES=0} 1)) 14 12 (deps 15 13 (source_tree scripts)) 16 14 (action 17 15 (chdir 18 16 scripts 19 - (run ./generate.py)))) 17 + (run bash ./generate.sh))))
+3 -20
test/interop/crcmod/scripts/generate.py
··· 2 2 """Generate CRC interop traces for ocaml-crc. 3 3 4 4 Oracle: crcmod 1.7 5 - Install: python3 -m venv .venv && .venv/bin/pip install crcmod 6 - 7 - Traces are committed to git. Only re-run when changing inputs 8 - or upgrading the oracle. 5 + Regenerate: dune build @regen-traces 9 6 """ 10 7 import os, sys 11 8 12 - # Auto-create venv if needed 13 - VENV = os.path.join(os.path.dirname(__file__), ".venv") 14 - if not os.path.exists(VENV): 15 - import subprocess 16 - subprocess.check_call([sys.executable, "-m", "venv", VENV]) 17 - subprocess.check_call([f"{VENV}/bin/pip", "install", "crcmod"]) 18 - 19 - # Use venv's Python 20 - VENV_PYTHON = f"{VENV}/bin/python3" 21 - if sys.executable != VENV_PYTHON and os.path.exists(VENV_PYTHON): 22 - os.execv(VENV_PYTHON, [VENV_PYTHON] + sys.argv) 23 - 24 9 import crcmod 25 10 import crcmod.predefined 26 11 27 - TRACE_DIR = os.path.join(os.path.dirname(__file__), "..", "traces") 12 + TRACE_DIR = sys.argv[1] if len(sys.argv) > 1 else os.path.join(os.path.dirname(__file__), "..", "traces") 28 13 29 14 # CRC functions matching the OCaml implementations: 30 15 # CRC-16-CCITT: poly=0x11021, init=0xFFFF, no reflection, no XOR out ··· 60 45 61 46 os.makedirs(TRACE_DIR, exist_ok=True) 62 47 with open(os.path.join(TRACE_DIR, "vectors.csv"), "w") as f: 63 - f.write("# CRC interop test vectors\n") 64 - f.write("# Oracle: Python crcmod 1.7\n") 65 - f.write("# Format: name,input_hex,crc16_ccitt,crc16_x25,crc32,crc32c\n") 48 + f.write("name,input_hex,crc16_ccitt,crc16_x25,crc32,crc32c\n") 66 49 for name, data in inputs: 67 50 c16_ccitt = crc16_ccitt_fn(data) 68 51 c16_x25 = crc16_x25_fn(data)
+13
test/interop/crcmod/scripts/generate.sh
··· 1 + #!/bin/bash 2 + set -euo pipefail 3 + SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" 4 + TRACE_DIR="$SCRIPT_DIR/../traces" 5 + mkdir -p "$TRACE_DIR" 6 + TRACE_DIR="$(cd "$TRACE_DIR" && pwd)" 7 + 8 + cd "$SCRIPT_DIR" 9 + if [ ! -d .venv ]; then 10 + python3 -m venv .venv 11 + .venv/bin/pip install -r requirements.txt 12 + fi 13 + .venv/bin/python3 generate.py "$TRACE_DIR"
+1
test/interop/crcmod/scripts/requirements.txt
··· 1 + crcmod==1.7
+26 -31
test/interop/crcmod/test.ml
··· 1 1 (** crcmod interop tests for ocaml-crc. 2 2 3 3 Traces generated by: crcmod 1.7 4 - Regenerate: REGEN_TRACES=1 dune build @regen-traces *) 4 + Regenerate: dune build @regen-traces *) 5 5 6 6 let trace path = Filename.concat "traces" path 7 - 8 - (* Parse CSV trace: skip comments (#), split on comma *) 9 - let parse_csv path = 10 - let ic = open_in (trace path) in 11 - let lines = ref [] in 12 - (try 13 - while true do 14 - let line = input_line ic in 15 - if String.length line > 0 && line.[0] <> '#' then 16 - lines := String.split_on_char ',' line :: !lines 17 - done 18 - with End_of_file -> ()); 19 - close_in ic; 20 - List.rev !lines 21 7 22 8 let string_of_hex hex = 23 9 let len = String.length hex / 2 in ··· 35 21 crc32c : int; 36 22 } 37 23 24 + let hex_to_string s = if s = "" then "" else string_of_hex s 25 + 26 + let vector_codec = 27 + Csvt.( 28 + Row.( 29 + obj (fun name input_hex crc16_ccitt crc16_x25 crc32 crc32c -> 30 + { 31 + name; 32 + input = hex_to_string input_hex; 33 + crc16_ccitt = int_of_hex crc16_ccitt; 34 + crc16_x25 = int_of_hex crc16_x25; 35 + crc32 = int_of_hex crc32; 36 + crc32c = int_of_hex crc32c; 37 + }) 38 + |> col "name" string ~enc:(fun v -> v.name) 39 + |> col "input_hex" string ~enc:(fun _ -> "") 40 + |> col "crc16_ccitt" string ~enc:(fun _ -> "") 41 + |> col "crc16_x25" string ~enc:(fun _ -> "") 42 + |> col "crc32" string ~enc:(fun _ -> "") 43 + |> col "crc32c" string ~enc:(fun _ -> "") 44 + |> finish)) 45 + 38 46 let parse_vectors () = 39 - let rows = parse_csv "vectors.csv" in 40 - List.filter_map 41 - (fun row -> 42 - match row with 43 - | [ name; input_hex; c16_ccitt; c16_x25; c32; c32c ] -> 44 - Some 45 - { 46 - name; 47 - input = string_of_hex input_hex; 48 - crc16_ccitt = int_of_hex c16_ccitt; 49 - crc16_x25 = int_of_hex c16_x25; 50 - crc32 = int_of_hex c32; 51 - crc32c = int_of_hex c32c; 52 - } 53 - | _ -> Alcotest.failf "bad CSV row: %s" (String.concat "," row)) 54 - rows 47 + match Csvt.decode_file vector_codec (trace "vectors.csv") with 48 + | Ok rows -> rows 49 + | Error e -> Alcotest.failf "CSV: %a" Csvt.pp_error e 55 50 56 51 let test_crc16_ccitt vec () = 57 52 let got = Crc.crc16_ccitt vec.input in
+1 -3
test/interop/crcmod/traces/vectors.csv
··· 1 - # CRC interop test vectors 2 - # Oracle: Python crcmod 1.7 3 - # Format: name,input_hex,crc16_ccitt,crc16_x25,crc32,crc32c 1 + name,input_hex,crc16_ccitt,crc16_x25,crc32,crc32c 4 2 empty,,ffff,0000,00000000,00000000 5 3 digits,313233343536373839,29b1,906e,cbf43926,e3069283 6 4 hello,48656c6c6f2c20776f726c6421,52d2,1eb5,ebe6c6e6,c8a106e5