Skip to content

Commit

Permalink
Improve test coverage of the ArrayMetadata module.
Browse files Browse the repository at this point in the history
  • Loading branch information
zoj613 committed Jul 6, 2024
1 parent 02ec67c commit 4cb6563
Show file tree
Hide file tree
Showing 5 changed files with 362 additions and 52 deletions.
20 changes: 10 additions & 10 deletions lib/extensions.ml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ module RegularGrid = struct
|> Util.Indexing.cartesian_prod
|> List.map Array.of_list

let equal : t -> t -> bool = fun x y -> x = y
let equal x y = x = y

let to_yojson t =
let chunk_shape =
Expand All @@ -43,17 +43,17 @@ module RegularGrid = struct
Util.get_name x,
Yojson.Safe.Util.(member "configuration" x |> to_assoc)
with
| "regular", [("chunk_shape", `List l)] ->
| "regular", [("chunk_shape", `List xs)] ->
List.fold_right
(fun a acc ->
acc >>= fun k ->
match a with
| `Int i when i > 0 -> Ok (i :: k)
| _ ->
let msg =
"Regular grid chunk_shape must only
contain positive integers." in
Error msg) l (Ok [])
acc >>= fun k ->
match a with
| `Int i when i > 0 -> Ok (i :: k)
| _ ->
let msg =
"Regular grid chunk_shape must only contain positive integers."
in
Error msg) xs (Ok [])
>>| fun l' -> Array.of_list l'
| _ -> Error "Invalid Chunk grid name or configuration."
end
Expand Down
19 changes: 6 additions & 13 deletions lib/metadata.ml
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,10 @@ module FillValue = struct
match x with
| `Bool b -> Ok (Bool b)
| `Int i -> Result.ok @@ Int (Int64.of_int i)
| `String "Infinity" -> Ok (Float Float.infinity)
| `String "-Infinity" -> Ok (Float Float.neg_infinity)
| `String "NaN" -> Ok (Float Float.nan)
| `Float f -> Ok (Float f)
| `String "Infinity" ->
Ok (Float Float.infinity)
| `String "-Infinity" ->
Ok (Float Float.neg_infinity)
| `String "NaN" ->
Ok (Float Float.nan)
| `String s when String.length s = 1 ->
Ok (Char (String.get s 0))
| `String s when String.starts_with ~prefix:"0x" s ->
Expand All @@ -70,20 +67,20 @@ module FillValue = struct
Ok (BFComplex Complex.{re; im})
| FloatBits re, FloatBits im ->
Ok (BBComplex Complex.{re; im})
| _ -> Error "Unsupported fill value")
| _ -> Error "Unsupported fill value.")
| _ -> Error "Unsupported fill value."

let rec to_yojson = function
| Bool b -> `Bool b
| Int i -> `Int (Int64.to_int i)
| Char c ->
`String (String.of_seq @@ List.to_seq [c])
| Float f when Float.is_nan f ->
`String "NaN"
| Float f when f = Float.infinity ->
`String "Infinity"
| Float f when f = Float.neg_infinity ->
`String "-Infinity"
| Float f when f = Float.nan ->
`String "NaN"
| Float f -> `Float f
| FloatBits f ->
`String (Stdint.Int64.to_string_hex @@ Int64.bits_of_float f)
Expand Down Expand Up @@ -142,10 +139,6 @@ module ArrayMetadata = struct

let codecs t = t.codecs

let data_type t =
Yojson.Safe.to_string @@
Extensions.Datatype.to_yojson t.data_type

let ndim t = Array.length @@ shape t

let dimension_names t = t.dimension_names
Expand Down
3 changes: 0 additions & 3 deletions lib/metadata.mli
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,6 @@ module ArrayMetadata : sig
val chunk_shape : t -> int array
(** [chunk_shape t] returns the shape a chunk in this zarr array. *)

val data_type : t -> string
(** [data_type t] returns the data type as specified in the array metadata.*)

val is_valid_kind : t -> ('a, 'b) Bigarray.kind -> bool
(** [is_valid_kind t kind] checks if [kind] is a valid Bigarray kind that
matches the data type of the zarr array represented by this metadata type. *)
Expand Down
Loading

0 comments on commit 4cb6563

Please sign in to comment.