erigon-pulse/compress/decompress_test.go
primal_concrete_sledge e69a5da702
Issue 248 refinements for decompressor api (#271)
* issue/ISSUE-248-refinements_for_decompressor_api

* Fix match tedst expectations

* Remove unneeded comment
2022-01-24 09:18:08 +00:00

140 lines
3.1 KiB
Go

package compress
import (
"context"
"fmt"
"path"
"strings"
"testing"
)
func prepareLoremDict(t *testing.T) *Decompressor {
tmpDir := t.TempDir()
file := path.Join(tmpDir, "compressed")
t.Name()
c, err := NewCompressor(context.Background(), t.Name(), file, tmpDir, 1, 2)
if err != nil {
t.Fatal(err)
}
defer c.Close()
for k, w := range loremStrings {
if err = c.AddWord([]byte(fmt.Sprintf("%s %d", w, k))); err != nil {
t.Fatal(err)
}
}
if err = c.Compress(); err != nil {
t.Fatal(err)
}
var d *Decompressor
if d, err = NewDecompressor(file); err != nil {
t.Fatal(err)
}
return d
}
func TestDecompressSkip(t *testing.T) {
d := prepareLoremDict(t)
defer d.Close()
g := d.MakeGetter()
i := 0
for g.HasNext() {
w := loremStrings[i]
if i%2 == 0 {
g.Skip()
} else {
word, _ := g.Next(nil)
expected := fmt.Sprintf("%s %d", w, i)
if string(word) != expected {
t.Errorf("expected %s, got (hex) %s", expected, word)
}
}
i++
}
}
func TestDecompressMatchOK(t *testing.T) {
d := prepareLoremDict(t)
defer d.Close()
g := d.MakeGetter()
i := 0
for g.HasNext() {
w := loremStrings[i]
if i%2 != 0 {
expected := fmt.Sprintf("%s %d", w, i)
ok, _ := g.Match([]byte(expected))
if !ok {
t.Errorf("expexted match with %s", expected)
}
} else {
word, _ := g.Next(nil)
expected := fmt.Sprintf("%s %d", w, i)
if string(word) != expected {
t.Errorf("expected %s, got (hex) %s", expected, word)
}
}
i++
}
}
func TestDecompressMatchNotOK(t *testing.T) {
d := prepareLoremDict(t)
defer d.Close()
g := d.MakeGetter()
i := 0
skipCount := 0
for g.HasNext() {
w := loremStrings[i]
expected := fmt.Sprintf("%s %d", w, i+1)
ok, _ := g.Match([]byte(expected))
if ok {
t.Errorf("not expexted match with %s", expected)
} else {
g.Skip()
skipCount++
}
i++
}
if skipCount != i {
t.Errorf("something wrong with match logic")
}
}
func TestDecompressMatchPrefix(t *testing.T) {
d := prepareDict(t)
defer d.Close()
g := d.MakeGetter()
i := 0
skipCount := 0
for g.HasNext() {
expected := fmt.Sprintf("longlongwords %d", i)
l := len(expected)
if i < l {
l = i
}
ok := g.MatchPrefix([]byte(expected)[:l])
expectedLen := 13
switch {
case ok && l < expectedLen: // good case
case !ok && l >= expectedLen: // good case
case ok && l >= expectedLen:
t.Errorf("not expexted match prefix with %s", expected)
case !ok && l < expectedLen:
t.Errorf("not expexted not matched prefix with %s", expected)
}
g.Skip()
skipCount++
i++
}
if skipCount != i {
t.Errorf("something wrong with match prefix logic")
}
}
const lorem = `Lorem ipsum dolor sit amet consectetur adipiscing elit sed do eiusmod tempor incididunt ut labore et
dolore magna aliqua Ut enim ad minim veniam quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur
Excepteur sint occaecat cupidatat non proident sunt in culpa qui officia deserunt mollit anim id est laborum`
var loremStrings = strings.Split(lorem, " ")