diff options
Diffstat (limited to 'src/pkg/index/suffixarray/suffixarray_test.go')
-rw-r--r-- | src/pkg/index/suffixarray/suffixarray_test.go | 50 |
1 files changed, 48 insertions, 2 deletions
diff --git a/src/pkg/index/suffixarray/suffixarray_test.go b/src/pkg/index/suffixarray/suffixarray_test.go index 023748500..9b4d89f42 100644 --- a/src/pkg/index/suffixarray/suffixarray_test.go +++ b/src/pkg/index/suffixarray/suffixarray_test.go @@ -6,7 +6,8 @@ package suffixarray import ( "bytes" - "regexp" + "exp/regexp" + "rand" "sort" "strings" "testing" @@ -213,7 +214,33 @@ func (a *index) at(i int) []byte { return a.data[a.sa[i]:] } func testConstruction(t *testing.T, tc *testCase, x *Index) { if !sort.IsSorted((*index)(x)) { - t.Errorf("testConstruction failed %s", tc.name) + t.Errorf("failed testConstruction %s", tc.name) + } +} + +func equal(x, y *Index) bool { + if !bytes.Equal(x.data, y.data) { + return false + } + for i, j := range x.sa { + if j != y.sa[i] { + return false + } + } + return true +} + +func testSaveRestore(t *testing.T, tc *testCase, x *Index) { + var buf bytes.Buffer + if err := x.Write(&buf); err != nil { + t.Errorf("failed writing index %s (%s)", tc.name, err) + } + var y Index + if err := y.Read(&buf); err != nil { + t.Errorf("failed reading index %s (%s)", tc.name, err) + } + if !equal(x, &y) { + t.Errorf("restored index doesn't match saved index %s", tc.name) } } @@ -221,6 +248,7 @@ func TestIndex(t *testing.T) { for _, tc := range testCases { x := New([]byte(tc.source)) testConstruction(t, &tc, x) + testSaveRestore(t, &tc, x) testLookups(t, &tc, x, 0) testLookups(t, &tc, x, 1) testLookups(t, &tc, x, 10) @@ -228,3 +256,21 @@ func TestIndex(t *testing.T) { testLookups(t, &tc, x, -1) } } + +func BenchmarkSaveRestore(b *testing.B) { + b.StopTimer() + r := rand.New(rand.NewSource(0x5a77a1)) // guarantee always same sequence + data := make([]byte, 10<<20) // 10MB index data + for i := range data { + data[i] = byte(r.Intn(256)) + } + x := New(data) + testSaveRestore(nil, nil, x) // verify correctness + buf := bytes.NewBuffer(make([]byte, len(data))) // avoid frequent growing + b.StartTimer() + for i := 0; i < b.N; i++ { + x.Write(buf) + var y Index + y.Read(buf) + } +} |