this repo has no description

tessera-npy: add fixture-based tests with real numpy dtypes

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

+87
+5
tessera-npy/test/dune
··· 1 1 (test 2 2 (name test_npy) 3 3 (libraries tessera-npy alcotest)) 4 + 5 + (test 6 + (name test_real_tiles) 7 + (libraries tessera-npy alcotest) 8 + (deps (glob_files fixtures/*.npy)))
tessera-npy/test/fixtures/float64_1d.npy

This is a binary file and will not be displayed.

tessera-npy/test/fixtures/small_embedding.npy

This is a binary file and will not be displayed.

tessera-npy/test/fixtures/small_scales.npy

This is a binary file and will not be displayed.

tessera-npy/test/fixtures/uint8_2d.npy

This is a binary file and will not be displayed.

+82
tessera-npy/test/test_real_tiles.ml
··· 1 + let read_file path = 2 + let ic = open_in_bin path in 3 + let len = in_channel_length ic in 4 + let buf = Bytes.create len in 5 + really_input ic buf 0 len; 6 + close_in ic; 7 + Bytes.to_string buf 8 + 9 + let fixture name = 10 + let dir = Sys.getenv_opt "NPY_TEST_FIXTURES" 11 + |> Option.value ~default:"fixtures" 12 + in 13 + Filename.concat dir name 14 + 15 + let test_small_embedding () = 16 + let data = read_file (fixture "small_embedding.npy") in 17 + match Npy.of_string data with 18 + | Error e -> Alcotest.fail e 19 + | Ok t -> 20 + Alcotest.(check (array int)) "shape" [|10; 8; 128|] (Npy.shape t); 21 + Alcotest.(check bool) "fortran_order" false (Npy.fortran_order t); 22 + (match Npy.data_int8 t with 23 + | None -> Alcotest.fail "expected int8 data" 24 + | Some ba -> 25 + Alcotest.(check int) "length" (10 * 8 * 128) (Bigarray.Array1.dim ba)) 26 + 27 + let test_small_scales () = 28 + let data = read_file (fixture "small_scales.npy") in 29 + match Npy.of_string data with 30 + | Error e -> Alcotest.fail e 31 + | Ok t -> 32 + Alcotest.(check (array int)) "shape" [|10; 8|] (Npy.shape t); 33 + (match Npy.data_float32 t with 34 + | None -> Alcotest.fail "expected float32 data" 35 + | Some ba -> 36 + Alcotest.(check int) "length" (10 * 8) (Bigarray.Array1.dim ba); 37 + for i = 0 to Bigarray.Array1.dim ba - 1 do 38 + let v = Bigarray.Array1.get ba i in 39 + if v < 0.0 || v > 0.11 then 40 + Alcotest.failf "scale value out of range: %f" v 41 + done) 42 + 43 + let test_float64_1d () = 44 + let data = read_file (fixture "float64_1d.npy") in 45 + match Npy.of_string data with 46 + | Error e -> Alcotest.fail e 47 + | Ok t -> 48 + Alcotest.(check (array int)) "shape" [|5|] (Npy.shape t); 49 + (match Npy.data_float64 t with 50 + | None -> Alcotest.fail "expected float64 data" 51 + | Some ba -> 52 + for i = 0 to 4 do 53 + let expected = Float.of_int (i + 1) in 54 + let actual = Bigarray.Array1.get ba i in 55 + if Float.abs (actual -. expected) > 1e-10 then 56 + Alcotest.failf "elem %d: expected %f, got %f" i expected actual 57 + done) 58 + 59 + let test_uint8_2d () = 60 + let data = read_file (fixture "uint8_2d.npy") in 61 + match Npy.of_string data with 62 + | Error e -> Alcotest.fail e 63 + | Ok t -> 64 + Alcotest.(check (array int)) "shape" [|2; 3|] (Npy.shape t); 65 + (match Npy.data_uint8 t with 66 + | None -> Alcotest.fail "expected uint8 data" 67 + | Some ba -> 68 + Alcotest.(check int) "elem 0" 0 (Bigarray.Array1.get ba 0); 69 + Alcotest.(check int) "elem 1" 128 (Bigarray.Array1.get ba 1); 70 + Alcotest.(check int) "elem 2" 255 (Bigarray.Array1.get ba 2)) 71 + 72 + let () = 73 + Alcotest.run "tessera-npy-fixtures" 74 + [ 75 + ( "fixtures", 76 + [ 77 + Alcotest.test_case "small embedding" `Quick test_small_embedding; 78 + Alcotest.test_case "small scales" `Quick test_small_scales; 79 + Alcotest.test_case "float64 1d" `Quick test_float64_1d; 80 + Alcotest.test_case "uint8 2d" `Quick test_uint8_2d; 81 + ] ); 82 + ]