fix server-rendered results by generating result pages into a buffer and reading...
[debiancodesearch.git] / index / write_test.go
blobd2653aec612406d3ce703f1a025e846a31255818
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 "bytes"
9 "io/ioutil"
10 "os"
11 "sort"
12 "strings"
13 "testing"
16 var trivialFiles = map[string]string{
17 "f0": "\n\n",
18 "file1": "\na\n",
19 "thefile2": "\nab\n",
20 "file3": "\nabc\n",
21 "afile4": "\ndabc\n",
22 "file5": "\nxyzw\n",
25 var trivialIndex = join(
26 // header
27 "csearch index 1\n",
29 // list of paths
30 "\x00",
32 // list of names
33 "afile4\x00",
34 "f0\x00",
35 "file1\x00",
36 "file3\x00",
37 "file5\x00",
38 "thefile2\x00",
39 "\x00",
41 // list of posting lists
42 "\na\n", fileList(2), // file1
43 "\nab", fileList(3, 5), // file3, thefile2
44 "\nda", fileList(0), // afile4
45 "\nxy", fileList(4), // file5
46 "ab\n", fileList(5), // thefile2
47 "abc", fileList(0, 3), // afile4, file3
48 "bc\n", fileList(0, 3), // afile4, file3
49 "dab", fileList(0), // afile4
50 "xyz", fileList(4), // file5
51 "yzw", fileList(4), // file5
52 "zw\n", fileList(4), // file5
53 "\xff\xff\xff", fileList(),
55 // name index
56 u32(0),
57 u32(6+1),
58 u32(6+1+2+1),
59 u32(6+1+2+1+5+1),
60 u32(6+1+2+1+5+1+5+1),
61 u32(6+1+2+1+5+1+5+1+5+1),
62 u32(6+1+2+1+5+1+5+1+5+1+8+1),
64 // posting list index,
65 "\na\n", u32(1), u32(0),
66 "\nab", u32(2), u32(5),
67 "\nda", u32(1), u32(5+6),
68 "\nxy", u32(1), u32(5+6+5),
69 "ab\n", u32(1), u32(5+6+5+5),
70 "abc", u32(2), u32(5+6+5+5+5),
71 "bc\n", u32(2), u32(5+6+5+5+5+6),
72 "dab", u32(1), u32(5+6+5+5+5+6+6),
73 "xyz", u32(1), u32(5+6+5+5+5+6+6+5),
74 "yzw", u32(1), u32(5+6+5+5+5+6+6+5+5),
75 "zw\n", u32(1), u32(5+6+5+5+5+6+6+5+5+5),
76 "\xff\xff\xff", u32(0), u32(5+6+5+5+5+6+6+5+5+5+5),
78 // trailer
79 u32(16),
80 u32(16+1),
81 u32(16+1+38),
82 u32(16+1+38+62),
83 u32(16+1+38+62+28),
85 "\ncsearch trailr\n",
88 func join(s ...string) string {
89 return strings.Join(s, "")
92 func u32(x uint32) string {
93 var buf [4]byte
94 buf[0] = byte(x >> 24)
95 buf[1] = byte(x >> 16)
96 buf[2] = byte(x >> 8)
97 buf[3] = byte(x)
98 return string(buf[:])
101 func fileList(list ...uint32) string {
102 var buf []byte
104 last := ^uint32(0)
105 for _, x := range list {
106 delta := x - last
107 for delta >= 0x80 {
108 buf = append(buf, byte(delta)|0x80)
109 delta >>= 7
111 buf = append(buf, byte(delta))
112 last = x
114 buf = append(buf, 0)
115 return string(buf)
118 func buildFlushIndex(out string, paths []string, doFlush bool, fileData map[string]string) {
119 ix := Create(out)
120 ix.AddPaths(paths)
121 var files []string
122 for name := range fileData {
123 files = append(files, name)
125 sort.Strings(files)
126 for _, name := range files {
127 ix.Add(name, strings.NewReader(fileData[name]))
129 if doFlush {
130 ix.flushPost()
132 ix.Flush()
135 func buildIndex(name string, paths []string, fileData map[string]string) {
136 buildFlushIndex(name, paths, false, fileData)
139 func testTrivialWrite(t *testing.T, doFlush bool) {
140 f, _ := ioutil.TempFile("", "index-test")
141 defer os.Remove(f.Name())
142 out := f.Name()
143 buildFlushIndex(out, nil, doFlush, trivialFiles)
145 data, err := ioutil.ReadFile(out)
146 if err != nil {
147 t.Fatalf("reading _test/index.triv: %v", err)
149 want := []byte(trivialIndex)
150 if !bytes.Equal(data, want) {
151 i := 0
152 for i < len(data) && i < len(want) && data[i] == want[i] {
155 t.Fatalf("wrong index:\nhave: %q %q\nwant: %q %q", data[:i], data[i:], want[:i], want[i:])
159 func TestTrivialWrite(t *testing.T) {
160 testTrivialWrite(t, false)
163 func TestTrivialWriteDisk(t *testing.T) {
164 testTrivialWrite(t, true)