github.com/ledgerwatch/erigon-lib@v1.0.0/bptree/bulk_test.go (about) 1 /* 2 Copyright 2022 Erigon contributors 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package bptree 18 19 import ( 20 "testing" 21 22 "github.com/stretchr/testify/assert" 23 ) 24 25 func assertNodeEqual(t *testing.T, expected, actual *Node23) { 26 t.Helper() 27 assert.Equal(t, expected.keysInLevelOrder(), actual.keysInLevelOrder(), "different keys by level") 28 } 29 30 type MergeTest struct { 31 left *Node23 32 right *Node23 33 final *Node23 34 } 35 36 func KV(keys []Felt, values []Felt) KeyValues { 37 keyPointers := make([]*Felt, len(keys)) 38 valuePointers := make([]*Felt, len(values)) 39 for i := 0; i < len(keyPointers); i++ { 40 keyPointers[i] = &keys[i] 41 valuePointers[i] = &values[i] 42 } 43 return KeyValues{keyPointers, valuePointers} 44 } 45 46 func K2K(keys []Felt) []*Felt { 47 kv := KV(keys, keys) 48 return kv.keys 49 } 50 51 func K2KV(keys []Felt) ([]*Felt, []*Felt) { 52 values := make([]Felt, len(keys)) 53 copy(values, keys) 54 kv := KV(keys, values) 55 return kv.keys, kv.values 56 } 57 58 func newInternalNode(children []*Node23, keys []*Felt) *Node23 { 59 return makeInternalNode(children, keys, &Stats{}) 60 } 61 62 func newLeafNode(keys, values []*Felt) *Node23 { 63 return makeLeafNode(keys, values, &Stats{}) 64 } 65 66 var mergeLeft2RightTestTable = []MergeTest{ 67 { 68 newInternalNode([]*Node23{ 69 newLeafNode(K2KV([]Felt{12, 127})), 70 }, K2K([]Felt{127})), 71 newInternalNode([]*Node23{ 72 newLeafNode(K2KV([]Felt{127, 128})), 73 newLeafNode(K2KV([]Felt{128, 135, 173})), 74 }, K2K([]Felt{128})), 75 newInternalNode([]*Node23{ 76 newLeafNode(K2KV([]Felt{12, 127})), 77 newLeafNode(K2KV([]Felt{127, 128})), 78 newLeafNode(K2KV([]Felt{128, 135, 173})), 79 }, K2K([]Felt{127, 128})), 80 }, 81 { 82 newInternalNode([]*Node23{ 83 newInternalNode([]*Node23{ 84 newLeafNode(K2KV([]Felt{12, 127})), 85 }, K2K([]Felt{127})), 86 }, K2K([]Felt{44})), 87 newInternalNode([]*Node23{ 88 newInternalNode([]*Node23{ 89 newLeafNode(K2KV([]Felt{127, 128})), 90 newLeafNode(K2KV([]Felt{128, 135, 173})), 91 }, K2K([]Felt{128})), 92 newInternalNode([]*Node23{ 93 newLeafNode(K2KV([]Felt{173, 237})), 94 newLeafNode(K2KV([]Felt{237, 1000})), 95 }, K2K([]Felt{237})), 96 }, K2K([]Felt{173})), 97 newInternalNode([]*Node23{ 98 newInternalNode([]*Node23{ 99 newLeafNode(K2KV([]Felt{12, 127})), 100 newLeafNode(K2KV([]Felt{127, 128})), 101 newLeafNode(K2KV([]Felt{128, 135, 173})), 102 }, K2K([]Felt{127, 128})), 103 newInternalNode([]*Node23{ 104 newLeafNode(K2KV([]Felt{173, 237})), 105 newLeafNode(K2KV([]Felt{237, 1000})), 106 }, K2K([]Felt{237})), 107 }, K2K([]Felt{173})), 108 }, 109 } 110 111 var mergeRight2LeftTestTable = []MergeTest{ 112 { 113 newInternalNode([]*Node23{ 114 newLeafNode(K2KV([]Felt{127, 128})), 115 newLeafNode(K2KV([]Felt{128, 135, 173})), 116 }, K2K([]Felt{128})), 117 newInternalNode([]*Node23{ 118 newLeafNode(K2KV([]Felt{173, 190})), 119 }, K2K([]Felt{190})), 120 newInternalNode([]*Node23{ 121 newLeafNode(K2KV([]Felt{127, 128})), 122 newLeafNode(K2KV([]Felt{128, 135, 173})), 123 newLeafNode(K2KV([]Felt{173, 190})), 124 }, K2K([]Felt{128, 173})), 125 }, 126 { 127 newInternalNode([]*Node23{ 128 newInternalNode([]*Node23{ 129 newLeafNode(K2KV([]Felt{127, 128})), 130 newLeafNode(K2KV([]Felt{128, 135, 173})), 131 }, K2K([]Felt{128})), 132 newInternalNode([]*Node23{ 133 newLeafNode(K2KV([]Felt{173, 237})), 134 newLeafNode(K2KV([]Felt{237, 1000})), 135 }, K2K([]Felt{237})), 136 }, K2K([]Felt{173})), 137 newInternalNode([]*Node23{ 138 newInternalNode([]*Node23{ 139 newLeafNode(K2KV([]Felt{1000, 1002})), 140 }, K2K([]Felt{1002})), 141 }, K2K([]Felt{1100})), 142 newInternalNode([]*Node23{ 143 newInternalNode([]*Node23{ 144 newLeafNode(K2KV([]Felt{127, 128})), 145 newLeafNode(K2KV([]Felt{128, 135, 173})), 146 }, K2K([]Felt{128})), 147 newInternalNode([]*Node23{ 148 newLeafNode(K2KV([]Felt{173, 237})), 149 newLeafNode(K2KV([]Felt{237, 1000})), 150 newLeafNode(K2KV([]Felt{1000, 1002})), 151 }, K2K([]Felt{237, 1000})), 152 }, K2K([]Felt{173})), 153 }, 154 } 155 156 func TestMergeLeft2Right(t *testing.T) { 157 for _, data := range mergeLeft2RightTestTable { 158 _, merged := mergeLeft2Right(data.left, data.right, &Stats{}) 159 assertNodeEqual(t, data.final, merged) 160 } 161 } 162 163 func TestMergeRight2Left(t *testing.T) { 164 for _, data := range mergeRight2LeftTestTable { 165 merged, _ := mergeRight2Left(data.left, data.right, &Stats{}) 166 assertNodeEqual(t, data.final, merged) 167 } 168 }