1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
|
// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package index
import (
"io/ioutil"
"os"
"testing"
)
var postFiles = map[string]string{
"file0": "",
"file1": "Google Code Search",
"file2": "Google Code Project Hosting",
"file3": "Google Web Search",
}
func tri(x, y, z byte) uint32 {
return uint32(x)<<16 | uint32(y)<<8 | uint32(z)
}
func TestTrivialPosting(t *testing.T) {
f, _ := ioutil.TempFile("", "index-test")
defer os.Remove(f.Name())
out := f.Name()
buildIndex(out, nil, postFiles)
ix := Open(out)
if l := ix.PostingList(tri('S', 'e', 'a')); !equalList(l, []uint32{1, 3}) {
t.Errorf("PostingList(Sea) = %v, want [1 3]", l)
}
if l := ix.PostingList(tri('G', 'o', 'o')); !equalList(l, []uint32{1, 2, 3}) {
t.Errorf("PostingList(Goo) = %v, want [1 2 3]", l)
}
if l := ix.PostingAnd(ix.PostingList(tri('S', 'e', 'a')), tri('G', 'o', 'o')); !equalList(l, []uint32{1, 3}) {
t.Errorf("PostingList(Sea&Goo) = %v, want [1 3]", l)
}
if l := ix.PostingAnd(ix.PostingList(tri('G', 'o', 'o')), tri('S', 'e', 'a')); !equalList(l, []uint32{1, 3}) {
t.Errorf("PostingList(Goo&Sea) = %v, want [1 3]", l)
}
if l := ix.PostingOr(ix.PostingList(tri('S', 'e', 'a')), tri('G', 'o', 'o')); !equalList(l, []uint32{1, 2, 3}) {
t.Errorf("PostingList(Sea|Goo) = %v, want [1 2 3]", l)
}
if l := ix.PostingOr(ix.PostingList(tri('G', 'o', 'o')), tri('S', 'e', 'a')); !equalList(l, []uint32{1, 2, 3}) {
t.Errorf("PostingList(Goo|Sea) = %v, want [1 2 3]", l)
}
}
func equalList(x, y []uint32) bool {
if len(x) != len(y) {
return false
}
for i, xi := range x {
if xi != y[i] {
return false
}
}
return true
}
|