aboutsummaryrefslogblamecommitdiff
path: root/test/mst_test.exs
blob: 6615b6e974806f7dc7f1a1e103c036b36e2ebb11 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13












                                                           







                                   
















                                                                




                               




                                              
defmodule ShardTest.MST do
  use ExUnit.Case
  alias SData.MerkleSearchTree, as: MST
  doctest Shard.Application

  test "merkle search tree 1" do
    y = Enum.reduce(0..1000, MST.new(),
                    fn i, acc -> MST.insert(acc, i, i) end)


    z = Enum.reduce(Enum.shuffle(0..1000), MST.new(),
                    fn i, acc -> MST.insert(acc, i, i) end)

    for i <- 0..1000 do
      assert MST.get(y, i) == i
      assert MST.get(z, i) == i
    end
    assert MST.get(y, 9999) == nil
    assert MST.get(z, -1001) == nil
    assert MST.get(z, 1.01) == nil

    IO.puts "y.root: #{y.root|>Base.encode16}"
    IO.puts "z.root: #{z.root|>Base.encode16}"
    assert y.root == z.root
  end

  test "merkle search tree 2" do
    items = for i <- 0..1000 do
              h = SData.term_hash i
              {h, SData.term_hash h}
            end

    y = Enum.reduce(items, MST.new(),
                    fn {k, v}, acc -> MST.insert(acc, k, v) end)

    z = Enum.reduce(Enum.shuffle(items), MST.new(),
                    fn {k, v}, acc -> MST.insert(acc, k, v) end)

    for {k, v} <- items do
      assert MST.get(y, k) == v
      assert MST.get(z, k) == v
    end

    IO.puts "y.root: #{y.root|>Base.encode16}"
    IO.puts "z.root: #{z.root|>Base.encode16}"
    assert y.root == z.root
  end
end