implement server-rendered (non-js) per-package results view
[debiancodesearch.git] / index / concat_test.go
blob8658935bfd88641b5c93327d61bdc532433d0efa
1 // Copyright 2011 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 package index
7 import (
8 "io/ioutil"
9 "os"
10 "testing"
13 func TestConcat(t *testing.T) {
14 var mergePaths1 = []string{
15 "/a",
16 "/b",
17 "/c",
20 var mergePaths2 = []string{
21 "/b",
22 "/cc",
25 var mergeFiles1 = map[string]string{
26 "/a/x": "hello world",
27 "/a/y": "goodbye world",
28 "/b/xx": "now is the time",
29 "/b/xy": "for all good men",
30 "/c/ab": "give me all the potatoes",
31 "/c/de": "or give me death now",
34 var mergeFiles2 = map[string]string{
35 "/cc": "come to the aid of his potatoes",
36 "/d/www": "world wide indeed",
37 "/d/xz": "no, not now",
38 "/d/yy": "first potatoes, now liberty?",
41 f1, _ := ioutil.TempFile("", "index-test")
42 f2, _ := ioutil.TempFile("", "index-test")
43 f3, _ := ioutil.TempFile("", "index-test")
44 defer os.Remove(f1.Name())
45 defer os.Remove(f2.Name())
46 defer os.Remove(f3.Name())
48 out1 := f1.Name()
49 out2 := f2.Name()
50 out3 := f3.Name()
52 buildIndex(out1, mergePaths1, mergeFiles1)
53 buildIndex(out2, mergePaths2, mergeFiles2)
55 Concat(out3, out1, out2)
57 ix1 := Open(out1)
58 ix2 := Open(out2)
59 ix3 := Open(out3)
61 nameof := func(ix *Index) string {
62 switch {
63 case ix == ix1:
64 return "ix1"
65 case ix == ix2:
66 return "ix2"
67 case ix == ix3:
68 return "ix3"
70 return "???"
73 checkFiles := func(ix *Index, l ...string) {
74 for i, s := range l {
75 if n := ix.Name(uint32(i)); n != s {
76 t.Errorf("%s: Name(%d) = %s, want %s", nameof(ix), i, n, s)
81 checkFiles(ix1, "/a/x", "/a/y", "/b/xx", "/b/xy", "/c/ab", "/c/de")
82 checkFiles(ix2, "/cc", "/d/www", "/d/xz", "/d/yy")
84 checkFiles(ix3, "/a/x", "/a/y", "/b/xx", "/b/xy", "/c/ab", "/c/de", "/cc", "/d/www", "/d/xz", "/d/yy")
86 check := func(ix *Index, trig string, l ...uint32) {
87 l1 := ix.PostingList(tri(trig[0], trig[1], trig[2]))
88 if !equalList(l1, l) {
89 t.Errorf("PostingList(%s, %s) = %v, want %v", nameof(ix), trig, l1, l)
93 check(ix1, "wor", 0, 1)
94 check(ix1, "now", 2, 5)
95 check(ix1, "all", 3, 4)
97 check(ix2, "now", 2, 3)
99 check(ix3, "all", 3, 4)
100 check(ix3, "wor", 0, 1, 7)
101 check(ix3, "now", 2, 5, 8, 9)
102 check(ix3, "pot", 4, 6, 9)