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.
16 var trivialFiles
= map[string]string{
25 var trivialIndex
= join(
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(),
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),
88 func join(s
...string) string {
89 return strings
.Join(s
, "")
92 func u32(x
uint32) string {
94 buf
[0] = byte(x
>> 24)
95 buf
[1] = byte(x
>> 16)
101 func fileList(list
...uint32) string {
105 for _
, x
:= range list
{
108 buf
= append(buf
, byte(delta
)|
0x80)
111 buf
= append(buf
, byte(delta
))
118 func buildFlushIndex(out
string, paths
[]string, doFlush
bool, fileData
map[string]string) {
122 for name
:= range fileData
{
123 files
= append(files
, name
)
126 for _
, name
:= range files
{
127 ix
.Add(name
, strings
.NewReader(fileData
[name
]))
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())
143 buildFlushIndex(out
, nil, doFlush
, trivialFiles
)
145 data
, err
:= ioutil
.ReadFile(out
)
147 t
.Fatalf("reading _test/index.triv: %v", err
)
149 want
:= []byte(trivialIndex
)
150 if !bytes
.Equal(data
, want
) {
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)