This repository has been archived by the owner on Jan 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
merged_test.go
145 lines (121 loc) · 2.41 KB
/
merged_test.go
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
Copyright 2020 Google LLC
Use of this source code is governed by a BSD-style
license that can be found in the LICENSE file or at
https://developers.google.com/open-source/licenses/bsd
*/
package reftable
import (
"fmt"
"math/rand"
"reflect"
"testing"
)
func TestPQ(t *testing.T) {
pq := mergedIterPQueue{}
rec := func(k string) pqEntry {
return pqEntry{
rec: &RefRecord{
RefName: k,
},
index: 0,
}
}
var names []string
for i := 0; i < 30; i++ {
names = append(names, fmt.Sprintf("%02d", i))
}
for _, j := range rand.Perm(len(names)) {
pq.add(rec(names[j]))
pq.check()
}
var res []string
for !pq.isEmpty() {
r := pq.remove()
pq.check()
res = append(res, r.rec.key())
}
if !reflect.DeepEqual(names, res) {
t.Errorf("got \n%v want \n%v", res, names)
}
}
func constructMergedRefTestTable(t *testing.T, recs ...[]RefRecord) *Merged {
var tabs []Table
for _, rec := range recs {
opts := Config{}
_, reader := constructTestTable(t, rec, nil, opts)
tabs = append(tabs, reader)
}
m, err := NewMerged(tabs, SHA1ID)
if err != nil {
t.Fatalf("NewMerged: %v", err)
}
return m
}
func TestMerged(t *testing.T) {
r1 := []RefRecord{{
RefName: "a",
UpdateIndex: 1,
Value: testHash(1),
}, {
RefName: "b",
UpdateIndex: 1,
Value: testHash(1),
}, {
RefName: "c",
UpdateIndex: 1,
Value: testHash(1),
}}
r2 := []RefRecord{{
RefName: "a",
UpdateIndex: 2,
}}
r3 := []RefRecord{{
RefName: "c",
UpdateIndex: 3,
Value: testHash(2),
}, {
RefName: "d",
UpdateIndex: 3,
Value: testHash(1),
}}
merged := constructMergedRefTestTable(t, r1, r2, r3)
{
want := []record{
// the deletion is also produced in the merged
// iteration, for compaction.
&r2[0],
&r1[1],
&r3[0],
&r3[1],
}
iter, err := merged.SeekRef("a")
if err != nil {
t.Fatalf("Seek: %v", err)
}
got, err := readIter(blockTypeRef, iter.impl)
if err != nil {
t.Fatalf("readIter: %v", err)
}
if !reflect.DeepEqual(got, want) {
t.Fatalf("got %#v, want %#v", got, want)
}
}
{
iter, err := merged.RefsFor(testHash(1))
if err != nil {
t.Fatalf("Seek: %v", err)
}
got, err := readIter(blockTypeRef, iter.impl)
if err != nil {
t.Fatalf("readIter: %v", err)
}
want := []record{
&r1[1],
&r3[1],
}
if !reflect.DeepEqual(got, want) {
t.Fatalf("got %v, want %#v", got, want)
}
}
}