|
| 1 | +package index_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/dk-open/go-mmr/merkle/index" |
| 5 | + "github.com/stretchr/testify/assert" |
| 6 | + "testing" |
| 7 | +) |
| 8 | + |
| 9 | +func getNodeIndex(value uint32) index.Index[uint32] { |
| 10 | + return index.NodeIndex[uint32](value) |
| 11 | +} |
| 12 | + |
| 13 | +func TestNodeIndex_Creation(t *testing.T) { |
| 14 | + // Test creating a node index |
| 15 | + value := uint32(4) |
| 16 | + node := getNodeIndex(value) |
| 17 | + assert.Equal(t, value, node.Index(), "Node index should be set correctly") |
| 18 | + assert.Equal(t, 2, node.GetHeight(), "Height should be calculated correctly") |
| 19 | +} |
| 20 | + |
| 21 | +func TestNodeIndex_LeftBranch(t *testing.T) { |
| 22 | + // Test left branch calculation |
| 23 | + node := getNodeIndex(uint32(6)) |
| 24 | + leftBranch := node.LeftBranch() |
| 25 | + assert.NotNil(t, leftBranch, "Left branch should exist") |
| 26 | + assert.Equal(t, uint32(2), leftBranch.Index(), "Left branch should be calculated correctly") |
| 27 | +} |
| 28 | + |
| 29 | +func TestNodeIndex_GetSibling(t *testing.T) { |
| 30 | + // Test sibling calculation for both left and right nodes |
| 31 | + node := getNodeIndex(uint32(3)) // Right sibling |
| 32 | + sibling := node.GetSibling() |
| 33 | + assert.Equal(t, uint32(1), sibling.Index(), "Right sibling should be calculated correctly") |
| 34 | + |
| 35 | + node = getNodeIndex(uint32(2)) // Left sibling |
| 36 | + sibling = node.GetSibling() |
| 37 | + assert.Equal(t, uint32(6), sibling.Index(), "Left sibling should be calculated correctly") |
| 38 | +} |
| 39 | + |
| 40 | +func TestNodeIndex_RightUp(t *testing.T) { |
| 41 | + // Test moving to the parent node from a right child |
| 42 | + node := getNodeIndex(uint32(6)) // Right child of parent |
| 43 | + parent := node.RightUp() |
| 44 | + assert.NotNil(t, parent, "Parent should exist") |
| 45 | + assert.Equal(t, uint32(4), parent.Index(), "Parent node should be calculated correctly") |
| 46 | + |
| 47 | + // Test when already at the top |
| 48 | + node = getNodeIndex(uint32(4)) // Top node |
| 49 | + parent = node.RightUp() |
| 50 | + assert.Nil(t, parent, "There should be no parent when already at the top") |
| 51 | +} |
| 52 | + |
| 53 | +func TestNodeIndex_Up(t *testing.T) { |
| 54 | + // Test moving up in the hierarchy |
| 55 | + node := getNodeIndex(uint32(3)) // Right child |
| 56 | + parent := node.Up() |
| 57 | + assert.Equal(t, uint32(2), parent.Index(), "Parent node should be calculated correctly") |
| 58 | + |
| 59 | + node = getNodeIndex(uint32(2)) // Left child |
| 60 | + parent = node.Up() |
| 61 | + assert.Equal(t, uint32(4), parent.Index(), "Parent node should be calculated correctly") |
| 62 | +} |
| 63 | + |
| 64 | +func TestNodeIndex_IsRight(t *testing.T) { |
| 65 | + // Test checking if a node is a right child |
| 66 | + node := getNodeIndex(uint32(3)) // Right child |
| 67 | + assert.True(t, node.IsRight(), "Node should be identified as a right child") |
| 68 | + |
| 69 | + node = getNodeIndex(uint32(2)) // Left child |
| 70 | + assert.False(t, node.IsRight(), "Node should be identified as a left child") |
| 71 | +} |
| 72 | + |
| 73 | +func TestNodeIndex_Top(t *testing.T) { |
| 74 | + // Test finding the top ancestor |
| 75 | + node := getNodeIndex(uint32(6)) // Start at node 6 |
| 76 | + top := node.Top() |
| 77 | + assert.Equal(t, uint32(4), top.Index(), "Top ancestor should be calculated correctly") |
| 78 | +} |
| 79 | + |
| 80 | +func TestNodeIndex_Children(t *testing.T) { |
| 81 | + // Test retrieving the children of a node |
| 82 | + node := getNodeIndex(uint32(6)) // Node with height > 0 |
| 83 | + children := node.Children() |
| 84 | + assert.Len(t, children, 2, "Node should have two children") |
| 85 | + assert.Equal(t, uint32(5), children[0].Index(), "Left child should be calculated correctly") |
| 86 | + assert.Equal(t, uint32(7), children[1].Index(), "Right child should be calculated correctly") |
| 87 | + |
| 88 | + node = getNodeIndex(uint32(1)) // Node with height 0 |
| 89 | + children = node.Children() |
| 90 | + assert.Len(t, children, 2, "Leaf node should have two children") |
| 91 | + assert.Equal(t, uint32(0), children[0].Index(), "Left child should be calculated correctly for leaf") |
| 92 | + assert.Equal(t, uint32(1), children[1].Index(), "Right child should be calculated correctly for leaf") |
| 93 | +} |
| 94 | + |
| 95 | +func TestNodeIndex_IsLeaf(t *testing.T) { |
| 96 | + // Test checking if a node is a leaf |
| 97 | + node := getNodeIndex(uint32(1)) // Node at height 0 |
| 98 | + assert.False(t, node.IsLeaf(), "Node should not be identified as a leaf") |
| 99 | +} |
| 100 | + |
| 101 | +func TestNodeIndex_Key(t *testing.T) { |
| 102 | + // Test generating the key for the node |
| 103 | + node := getNodeIndex(uint32(5)) |
| 104 | + key := node.Key() |
| 105 | + assert.Equal(t, "node_5", key, "Node key should be generated correctly") |
| 106 | +} |
0 commit comments