mirror of
https://gitlab.com/pulsechaincom/erigon-pulse.git
synced 2025-01-01 00:31:21 +00:00
156 lines
3.7 KiB
Go
156 lines
3.7 KiB
Go
/*
|
|
Copyright 2021 Erigon contributors
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
*/
|
|
|
|
package compress
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func prepareLoremDict(t *testing.T) *Decompressor {
|
|
tmpDir := t.TempDir()
|
|
file := filepath.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, " ")
|