github.com/prysmaticlabs/prysm@v1.4.4/shared/trieutil/helpers_test.go (about) 1 package trieutil_test 2 3 import ( 4 "math/rand" 5 "testing" 6 7 "github.com/prysmaticlabs/prysm/shared/trieutil" 8 ) 9 10 func TestNextPowerOf2(t *testing.T) { 11 tests := []struct { 12 input int 13 result int 14 }{ 15 {input: 0, result: 1}, 16 {input: 1, result: 1}, 17 {input: 2, result: 2}, 18 {input: 3, result: 4}, 19 {input: 5, result: 8}, 20 {input: 9, result: 16}, 21 {input: 20, result: 32}, 22 } 23 for _, tt := range tests { 24 if got := trieutil.NextPowerOf2(tt.input); got != tt.result { 25 t.Errorf("NextPowerOf2() = %d, result %d", got, tt.result) 26 } 27 } 28 } 29 30 func TestPrevPowerOf2(t *testing.T) { 31 tests := []struct { 32 input int 33 result int 34 }{ 35 {input: 0, result: 1}, 36 {input: 1, result: 1}, 37 {input: 2, result: 2}, 38 {input: 3, result: 2}, 39 {input: 5, result: 4}, 40 {input: 9, result: 8}, 41 {input: 20, result: 16}, 42 } 43 for _, tt := range tests { 44 if got := trieutil.PrevPowerOf2(tt.input); got != tt.result { 45 t.Errorf("PrevPowerOf2() = %d, result %d", got, tt.result) 46 } 47 } 48 } 49 50 func TestMerkleTreeLength(t *testing.T) { 51 tests := []struct { 52 leaves [][]byte 53 length int 54 }{ 55 {[][]byte{{'A'}, {'B'}, {'C'}}, 8}, 56 {[][]byte{{'A'}, {'B'}, {'C'}, {'D'}}, 8}, 57 {[][]byte{{'A'}, {'B'}, {'C'}, {'D'}, {'E'}}, 16}, 58 } 59 for _, tt := range tests { 60 if got := trieutil.MerkleTree(tt.leaves); len(got) != tt.length { 61 t.Errorf("len(MerkleTree()) = %d, result %d", got, tt.length) 62 } 63 } 64 } 65 66 func TestConcatGeneralizedIndices(t *testing.T) { 67 tests := []struct { 68 indices []int 69 result int 70 }{ 71 {[]int{1, 2}, 2}, 72 {[]int{1, 3, 6}, 14}, 73 {[]int{1, 2, 4, 8}, 64}, 74 {[]int{1, 2, 5, 10}, 74}, 75 } 76 for _, tt := range tests { 77 if got := trieutil.ConcatGeneralizedIndices(tt.indices); got != tt.result { 78 t.Errorf("ConcatGeneralizedIndices() = %d, result %d", got, tt.result) 79 } 80 } 81 } 82 83 func TestGeneralizedIndexLength(t *testing.T) { 84 tests := []struct { 85 index int 86 result int 87 }{ 88 {index: 20, result: 4}, 89 {index: 200, result: 7}, 90 {index: 1987, result: 10}, 91 {index: 34989843, result: 25}, 92 {index: 97282, result: 16}, 93 } 94 for _, tt := range tests { 95 result := trieutil.GeneralizedIndexLength(tt.index) 96 if tt.result != result { 97 t.Errorf("GeneralizedIndexLength() = %d, result %d", tt.result, result) 98 } 99 } 100 } 101 102 func TestGeneralizedIndexBit(t *testing.T) { 103 tests := []struct { 104 index uint64 105 pos uint64 106 result bool 107 }{ 108 {index: 7, pos: 2, result: true}, 109 {index: 7, pos: 3, result: false}, 110 {index: 10, pos: 2, result: false}, 111 {index: 10, pos: 3, result: true}, 112 } 113 for _, tt := range tests { 114 result := trieutil.GeneralizedIndexBit(tt.index, tt.pos) 115 if result != tt.result { 116 t.Errorf("GeneralizedIndexBit() = %v, result %v", tt.result, result) 117 } 118 } 119 } 120 121 func TestGeneralizedIndexChild(t *testing.T) { 122 tests := []struct { 123 index int 124 right bool 125 result int 126 }{ 127 {index: 5, right: true, result: 11}, 128 {index: 10, right: false, result: 20}, 129 {index: 1000, right: true, result: 2001}, 130 {index: 9999, right: false, result: 19998}, 131 } 132 for _, tt := range tests { 133 result := trieutil.GeneralizedIndexChild(tt.index, tt.right) 134 if result != tt.result { 135 t.Errorf("GeneralizedIndexChild() = %v, result %v", tt.result, result) 136 } 137 } 138 } 139 140 func TestGeneralizedIndexSibling(t *testing.T) { 141 tests := []struct { 142 index int 143 result int 144 }{ 145 {index: 5, result: 4}, 146 {index: 10, result: 11}, 147 {index: 1000, result: 1001}, 148 {index: 9999, result: 9998}, 149 } 150 for _, tt := range tests { 151 result := trieutil.GeneralizedIndexSibling(tt.index) 152 if result != tt.result { 153 t.Errorf("GeneralizedIndexSibling() = %v, result %v", tt.result, result) 154 } 155 } 156 } 157 158 func TestGeneralizedIndexParent(t *testing.T) { 159 tests := []struct { 160 index int 161 result int 162 }{ 163 {index: 5, result: 2}, 164 {index: 10, result: 5}, 165 {index: 1000, result: 500}, 166 {index: 9999, result: 4999}, 167 } 168 for _, tt := range tests { 169 result := trieutil.GeneralizedIndexParent(tt.index) 170 if result != tt.result { 171 t.Errorf("GeneralizedIndexParent() = %v, result %v", tt.result, result) 172 } 173 } 174 } 175 176 func BenchmarkMerkleTree_Generate(b *testing.B) { 177 leaves := make([][]byte, 1<<20) 178 for i := 0; i < len(leaves); i++ { 179 b := make([]byte, 32) 180 rand.Read(b) 181 leaves[i] = b 182 } 183 184 b.ResetTimer() 185 for i := 0; i < b.N; i++ { 186 trieutil.MerkleTree(leaves) 187 } 188 }