this repo has no description

tessera-npy: add data access tests

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

+51
+51
tessera-npy/test/test_npy.ml
··· 54 54 | Ok _ -> Alcotest.fail "should have failed" 55 55 | Error _ -> () 56 56 57 + let test_int8_data () = 58 + let data = String.init 6 (fun i -> 59 + Char.chr ([| 1; 255; 127; 128; 0; 42 |].(i)) 60 + ) in 61 + let npy = make_npy_v1 ~descr:"|i1" ~fortran_order:false ~shape:[2; 3] data in 62 + match Npy.of_string npy with 63 + | Error e -> Alcotest.fail e 64 + | Ok t -> 65 + (match Npy.data_int8 t with 66 + | None -> Alcotest.fail "expected Some for data_int8" 67 + | Some ba -> 68 + Alcotest.(check int) "elem 0" 1 (Bigarray.Array1.get ba 0); 69 + Alcotest.(check int) "elem 1" (-1) (Bigarray.Array1.get ba 1); 70 + Alcotest.(check int) "elem 2" 127 (Bigarray.Array1.get ba 2); 71 + Alcotest.(check int) "elem 3" (-128) (Bigarray.Array1.get ba 3); 72 + Alcotest.(check int) "elem 4" 0 (Bigarray.Array1.get ba 4); 73 + Alcotest.(check int) "elem 5" 42 (Bigarray.Array1.get ba 5)) 74 + 75 + let test_float32_data () = 76 + let bits = Int32.bits_of_float 3.14 in 77 + let data = String.init 4 (fun i -> 78 + Char.chr (Int32.to_int (Int32.logand (Int32.shift_right_logical bits (i * 8)) 0xffl)) 79 + ) in 80 + let npy = make_npy_v1 ~descr:"<f4" ~fortran_order:false ~shape:[1] data in 81 + match Npy.of_string npy with 82 + | Error e -> Alcotest.fail e 83 + | Ok t -> 84 + (match Npy.data_float32 t with 85 + | None -> Alcotest.fail "expected Some for data_float32" 86 + | Some ba -> 87 + let v = Bigarray.Array1.get ba 0 in 88 + if Float.abs (v -. 3.14) > 0.001 then 89 + Alcotest.failf "expected ~3.14, got %f" v) 90 + 91 + let test_wrong_dtype_returns_none () = 92 + let data = String.init 6 (fun i -> 93 + Char.chr ([| 1; 255; 127; 128; 0; 42 |].(i)) 94 + ) in 95 + let npy = make_npy_v1 ~descr:"|i1" ~fortran_order:false ~shape:[2; 3] data in 96 + match Npy.of_string npy with 97 + | Error e -> Alcotest.fail e 98 + | Ok t -> 99 + Alcotest.(check bool) "data_float32 is None" true (Option.is_none (Npy.data_float32 t)); 100 + Alcotest.(check bool) "data_int8 is Some" true (Option.is_some (Npy.data_int8 t)) 101 + 57 102 let () = 58 103 Alcotest.run "tessera-npy" 59 104 [ ··· 63 108 Alcotest.test_case "float32 header" `Quick test_parse_float32_header; 64 109 Alcotest.test_case "3d shape" `Quick test_parse_3d_shape; 65 110 Alcotest.test_case "bad magic" `Quick test_bad_magic; 111 + ] ); 112 + ( "data", 113 + [ 114 + Alcotest.test_case "int8 data" `Quick test_int8_data; 115 + Alcotest.test_case "float32 data" `Quick test_float32_data; 116 + Alcotest.test_case "wrong dtype returns none" `Quick test_wrong_dtype_returns_none; 66 117 ] ); 67 118 ]