aboutsummaryrefslogtreecommitdiffstats
path: root/test/unit/kernel
diff options
context:
space:
mode:
authorEstevan Castilho <tevo@riajuu.net>2026-08-01 01:44:43 +0000
committerGitHub <noreply@github.com>2026-08-01 01:44:43 +0000
commit117fe888ef9325dbc6965e36c38e40c92d7bc1ee (patch)
tree366688654cf0a38b302c22b5151a11f6734dc6d8 /test/unit/kernel
parentd57288a6143146a99fd1792c543dbade2758ad01 (diff)
parentb7a09b8c8913b26eb22b98aa77a324a1c23736e2 (diff)
downloadRNT-117fe888ef9325dbc6965e36c38e40c92d7bc1ee.tar.gz
RNT-117fe888ef9325dbc6965e36c38e40c92d7bc1ee.zip
Merge pull request #48 from dont-rely-on-nulls/merkle-tree-tests
Basic tests for the B-tree, and a slightly friendlier storage interface
Diffstat (limited to 'test/unit/kernel')
-rw-r--r--test/unit/kernel/Merkle.ml87
1 files changed, 87 insertions, 0 deletions
diff --git a/test/unit/kernel/Merkle.ml b/test/unit/kernel/Merkle.ml
new file mode 100644
index 0000000..f12182a
--- /dev/null
+++ b/test/unit/kernel/Merkle.ml
@@ -0,0 +1,87 @@
+open Rnt
+
+module StringKey : Rnt.Kernel.Merkle.KEY with type t = string = struct
+ type t = string
+
+ let encode s = String.to_bytes s |> Concepts.Representation.blob_of_bytes
+ let compare s1 s2 = String.compare s1 s2 |> Concepts.Ordering.of_int
+ let decode b = Concepts.Representation.bytes_of_blob b |> String.of_bytes |> Result.ok
+end
+
+module Make (S : Abstract.Storage.STORAGE) (C : Helpers.Storage.CONFIGURATOR) = struct
+ open Alcotest
+
+ module H = Helpers.Storage.Make (S) (C)
+ module T = Rnt.Kernel.Merkle.Interface (S) (StringKey) (StringKey)
+
+ (* TODO: be a bit more comprehensive (ideally, we want to test splits as well) *)
+ let insert_and_lookup conn =
+ let v1, v2, v3, v3', bogus =
+ begin
+ let open Utilities.Result in
+ let* tx = S.start conn in
+ let* node = T.empty
+ |> T.insert tx "k1" "v1"
+ |> fmap (T.insert tx "k2" "v2")
+ |> fmap (T.insert tx "k3" "v3")
+ in
+ let* node' = T.insert tx "k3" "toodles" node in
+ let* v1 = T.lookup tx "k1" node in
+ let* v2 = T.lookup tx "k2" node in
+ let* v3 = T.lookup tx "k3" node in
+ let* v3' = T.lookup tx "k3" node' in
+ let* bogus = T.lookup tx "fnord" node in
+ Ok (v1, v2, v3, v3', bogus)
+ end
+ |> Helpers.condition_as_failure
+ in
+ check (option string) "" (Some "v1") v1;
+ check (option string) "" (Some "v2") v2;
+ check (option string) "" (Some "v3") v3;
+ check (option string) "" (Some "toodles") v3';
+ check (option string) "" None bogus
+
+ let persistence conn =
+ let addr =
+ begin
+ let open Utilities.Result in
+ let* tx = S.start conn in
+ let* node = T.empty
+ |> T.insert tx "k1" "v1"
+ |> fmap (T.insert tx "k2" "v2")
+ |> fmap (T.insert tx "k3" "v3")
+ in
+ let* () = S.commit tx in
+ Ok (T.hash_of node)
+ end
+ |> Helpers.condition_as_failure
+ in
+ let v1, v2, v3 =
+ begin
+ let open Utilities.Result in
+ let* tx = S.start conn in
+ let* node = T.find tx addr |> Result.map Option.get in
+ let* v1 = T.lookup tx "k1" node in
+ let* v2 = T.lookup tx "k2" node in
+ let* v3 = T.lookup tx "k3" node in
+ Ok (v1, v2, v3)
+ end
+ |> Helpers.condition_as_failure
+ in
+ check (option string) "" (Some "v1") v1;
+ check (option string) "" (Some "v2") v2;
+ check (option string) "" (Some "v3") v3
+
+ (* TODO: this will need `remove` *)
+ (* let determinism _conn = *)
+ (* () *)
+
+ let suite prefix =
+ ( "merkle/" ^ prefix,
+ [test_case "insert-and-lookup" `Quick (H.with_connection insert_and_lookup "merkle-test");
+ test_case "persistence" `Quick (H.with_connection persistence "merkle-test")])
+end
+
+module LMDB = Make (Rnt.Backend.Storage.LMDB) (Helpers.Storage.LMDB_Configurator)
+
+let suites () = [LMDB.suite "lmdb"]