fork of https://github.com/sourcegraph/zoekt
0

Configure Feed

Select the types of activity you want to include in your feed.

testing/quick tests for functionality in bits

Change-Id: Ie1250f12eb55df32ad4116e1f3255ffa29c6919b

+123
+120
bits_test.go
··· 15 15 package zoekt 16 16 17 17 import ( 18 + "encoding/binary" 18 19 "log" 20 + "math/rand" 19 21 "reflect" 22 + "sort" 20 23 "testing" 24 + "testing/quick" 25 + 26 + "github.com/google/go-cmp/cmp" 21 27 ) 22 28 23 29 var _ = log.Println ··· 28 34 if n.String() != "abc" { 29 35 t.Errorf("got %q, want %q", n, "abc") 30 36 } 37 + 38 + f := func(b ngramRunes) bool { 39 + n := runesToNGram(b) 40 + got := ngramRunes(ngramToRunes(n)) 41 + if !reflect.DeepEqual(b, got) { 42 + t.Log(cmp.Diff(b, got)) 43 + return false 44 + } 45 + return true 46 + } 47 + if err := quick.Check(f, nil); err != nil { 48 + t.Error(err) 49 + } 50 + } 51 + 52 + type ngramRunes [ngramSize]rune 53 + 54 + func (ngramRunes) Generate(rand *rand.Rand, size int) reflect.Value { 55 + // Same implementation used by testing/quick to generate strings. But we 56 + // force it to ngramSize runes. 57 + var b ngramRunes 58 + for i := range b { 59 + b[i] = rune(rand.Intn(0x10ffff)) 60 + } 61 + return reflect.ValueOf(b) 31 62 } 32 63 33 64 func TestDocSection(t *testing.T) { ··· 76 107 } 77 108 } 78 109 } 110 + 111 + func TestSizedDeltas(t *testing.T) { 112 + encode := func(nums []uint32) []byte { 113 + return toSizedDeltas(nums) 114 + } 115 + decode := func(data []byte) []uint32 { 116 + if len(data) == 0 { 117 + return nil 118 + } 119 + return fromSizedDeltas(data, nil) 120 + } 121 + testIncreasingIntCoder(t, encode, decode) 122 + } 123 + 124 + func TestFromDeltas(t *testing.T) { 125 + decode := func(data []byte) []uint32 { 126 + if len(data) == 0 { 127 + return nil 128 + } 129 + return fromDeltas(data, nil) 130 + } 131 + testIncreasingIntCoder(t, toDeltas, decode) 132 + } 133 + 134 + func TestCompressedPostingIterator(t *testing.T) { 135 + decode := func(data []byte) []uint32 { 136 + if len(data) == 0 { 137 + return nil 138 + } 139 + 140 + var nums []uint32 141 + i := newCompressedPostingIterator(data, stringToNGram("abc")) 142 + for i.first() != maxUInt32 { 143 + nums = append(nums, i.first()) 144 + i.next(i.first()) 145 + } 146 + return nums 147 + } 148 + testIncreasingIntCoder(t, toDeltas, decode) 149 + } 150 + 151 + func toDeltas(offsets []uint32) []byte { 152 + var enc [8]byte 153 + 154 + deltas := make([]byte, 0, len(offsets)*2) 155 + 156 + var last uint32 157 + for _, p := range offsets { 158 + delta := p - last 159 + last = p 160 + 161 + m := binary.PutUvarint(enc[:], uint64(delta)) 162 + deltas = append(deltas, enc[:m]...) 163 + } 164 + return deltas 165 + } 166 + 167 + func testIncreasingIntCoder(t *testing.T, encode func([]uint32) []byte, decode func([]byte) []uint32) { 168 + f := func(nums []uint32) bool { 169 + nums = sortedUnique(nums) 170 + b := encode(nums) 171 + got := decode(b) 172 + if len(nums) == len(got) && len(nums) == 0 { 173 + return true 174 + } 175 + if !reflect.DeepEqual(got, nums) { 176 + t.Log(cmp.Diff(nums, got)) 177 + return false 178 + } 179 + return true 180 + } 181 + if err := quick.Check(f, nil); err != nil { 182 + t.Error(err) 183 + } 184 + } 185 + 186 + func sortedUnique(nums []uint32) []uint32 { 187 + if len(nums) == 0 { 188 + return nums 189 + } 190 + sort.Slice(nums, func(i, j int) bool { return nums[i] < nums[j] }) 191 + filtered := nums[:1] 192 + for _, n := range nums[1:] { 193 + if filtered[len(filtered)-1] != n { 194 + filtered = append(filtered, n) 195 + } 196 + } 197 + return filtered 198 + }
+1
go.mod
··· 4 4 github.com/andygrunwald/go-gerrit v0.0.0-20181026193842-43cfd7a94eb4 5 5 github.com/fsnotify/fsnotify v1.4.7 6 6 github.com/gfleury/go-bitbucket-v1 v0.0.0-20181102191809-4910839b609e 7 + github.com/google/go-cmp v0.3.0 7 8 github.com/google/go-github v17.0.0+incompatible 8 9 github.com/google/slothfs v0.0.0-20170112234537-ecdd255f653d 9 10 github.com/kylelemons/godebug v0.0.0-20170820004349-d65d576e9348
+2
go.sum
··· 20 20 github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= 21 21 github.com/google/go-cmp v0.2.0 h1:+dTQ8DZQJz0Mb/HjFlkptS1FeQ4cWSnN941F8aEG4SQ= 22 22 github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= 23 + github.com/google/go-cmp v0.3.0 h1:crn/baboCvb5fXaQ0IJ1SGTsTVrWpDsCWC8EGETZijY= 24 + github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= 23 25 github.com/google/go-github v17.0.0+incompatible h1:N0LgJ1j65A7kfXrZnUDaYCs/Sf4rEjNlfyDHW9dolSY= 24 26 github.com/google/go-github v17.0.0+incompatible/go.mod h1:zLgOLi98H3fifZn+44m+umXrS52loVEgC2AApnigrVQ= 25 27 github.com/google/go-querystring v1.0.0 h1:Xkwi/a1rcvNg1PPYe5vI8GbeBY/jrVuDX5ASuANWTrk=