github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/libraries/doltcore/remotestorage/utils_test.go (about) 1 // Copyright 2019 Dolthub, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package remotestorage 16 17 import ( 18 "math/rand" 19 "reflect" 20 "testing" 21 "time" 22 23 "github.com/dolthub/dolt/go/store/hash" 24 ) 25 26 func TestHashesToSlices(t *testing.T) { 27 const numHashes = 32 28 29 rng := rand.New(rand.NewSource(time.Now().UnixNano())) 30 31 var randomHashes []hash.Hash 32 var randomHashBytes [][]byte 33 for i := 0; i < numHashes; i++ { 34 var h hash.Hash 35 36 for j := 0; j < len(h); j++ { 37 h[j] = byte(rng.Intn(255)) 38 } 39 40 randomHashes = append(randomHashes, h) 41 randomHashBytes = append(randomHashBytes, h[:]) 42 } 43 44 var zeroHash hash.Hash 45 tests := []struct { 46 name string 47 in []hash.Hash 48 expected [][]byte 49 }{ 50 { 51 "test nil", 52 nil, 53 [][]byte{}, 54 }, 55 { 56 "test empty", 57 []hash.Hash{}, 58 [][]byte{}, 59 }, 60 { 61 "test one hash", 62 []hash.Hash{zeroHash}, 63 [][]byte{zeroHash[:]}, 64 }, 65 { 66 "test many random hashes", 67 randomHashes, 68 randomHashBytes, 69 }, 70 } 71 72 for _, test := range tests { 73 t.Run(test.name, func(t *testing.T) { 74 actual := HashesToSlices(test.in) 75 76 if !reflect.DeepEqual(test.expected, actual) { 77 t.Error("unexpected result") 78 } 79 }) 80 } 81 } 82 83 func TestHashSetToSlices(t *testing.T) { 84 const numHashes = 32 85 86 rng := rand.New(rand.NewSource(time.Now().UnixNano())) 87 88 randomHashSet := make(hash.HashSet) 89 90 var randomHashes []hash.Hash 91 var randomHashBytes [][]byte 92 for i := 0; i < numHashes; i++ { 93 var h hash.Hash 94 95 for j := 0; j < len(h); j++ { 96 h[j] = byte(rng.Intn(255)) 97 } 98 99 randomHashSet.Insert(h) 100 randomHashes = append(randomHashes, h) 101 randomHashBytes = append(randomHashBytes, h[:]) 102 } 103 104 var zeroHash hash.Hash 105 tests := []struct { 106 name string 107 hashes hash.HashSet 108 expectedHashes []hash.Hash 109 expectedBytes [][]byte 110 }{ 111 { 112 "test nil", 113 nil, 114 []hash.Hash{}, 115 [][]byte{}, 116 }, 117 { 118 "test empty", 119 hash.HashSet{}, 120 []hash.Hash{}, 121 [][]byte{}, 122 }, 123 { 124 "test one hash", 125 hash.HashSet{zeroHash: struct{}{}}, 126 []hash.Hash{zeroHash}, 127 [][]byte{zeroHash[:]}, 128 }, 129 { 130 "test many random hashes", 131 randomHashSet, 132 randomHashes, 133 randomHashBytes, 134 }, 135 } 136 137 for _, test := range tests { 138 t.Run(test.name, func(t *testing.T) { 139 hashes, bytes := HashSetToSlices(test.hashes) 140 141 if len(hashes) != len(test.hashes) || len(bytes) != len(test.hashes) { 142 t.Error("unexpected size") 143 } 144 145 for i := 0; i < len(test.hashes); i++ { 146 h, hBytes := hashes[i], bytes[i] 147 148 if !test.hashes.Has(h) { 149 t.Error("missing hash") 150 } 151 152 if !reflect.DeepEqual(h[:], hBytes) { 153 t.Error("unexpected bytes") 154 } 155 } 156 }) 157 } 158 } 159 160 func TestParseByteSlices(t *testing.T) { 161 const numHashes = 32 162 163 rng := rand.New(rand.NewSource(time.Now().UnixNano())) 164 165 var randomHashBytes [][]byte 166 for i := 0; i < numHashes; i++ { 167 var h hash.Hash 168 169 for j := 0; j < len(h); j++ { 170 h[j] = byte(rng.Intn(255)) 171 } 172 173 randomHashBytes = append(randomHashBytes, h[:]) 174 } 175 176 var zeroHash hash.Hash 177 tests := []struct { 178 name string 179 bytes [][]byte 180 }{ 181 { 182 "test nil", 183 [][]byte{}, 184 }, 185 { 186 "test empty", 187 [][]byte{}, 188 }, 189 { 190 "test one hash", 191 [][]byte{zeroHash[:]}, 192 }, 193 { 194 "test many random hashes", 195 randomHashBytes, 196 }, 197 } 198 199 for _, test := range tests { 200 t.Run(test.name, func(t *testing.T) { 201 hashes, hashToIndex := ParseByteSlices(test.bytes) 202 203 if len(hashes) != len(test.bytes) { 204 t.Error("unexpected size") 205 } 206 207 for h := range hashes { 208 idx := hashToIndex[h] 209 210 if !reflect.DeepEqual(test.bytes[idx], h[:]) { 211 t.Error("unexpected value") 212 } 213 } 214 }) 215 } 216 217 }