2022-09-30 07:38:44 +00:00
|
|
|
package bytesutil
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
2024-02-24 00:07:51 +00:00
|
|
|
"sync/atomic"
|
2022-09-30 07:38:44 +00:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestFastStringMatcher(t *testing.T) {
|
|
|
|
fsm := NewFastStringMatcher(func(s string) bool {
|
|
|
|
return strings.HasPrefix(s, "foo")
|
|
|
|
})
|
|
|
|
f := func(s string, resultExpected bool) {
|
|
|
|
t.Helper()
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
result := fsm.Match(s)
|
|
|
|
if result != resultExpected {
|
|
|
|
t.Fatalf("unexpected result for Match(%q) at iteration %d; got %v; want %v", s, i, result, resultExpected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
f("", false)
|
|
|
|
f("foo", true)
|
|
|
|
f("a_b-C", false)
|
|
|
|
f("foobar", true)
|
|
|
|
}
|
2022-12-21 20:57:28 +00:00
|
|
|
|
|
|
|
func TestNeedCleanup(t *testing.T) {
|
|
|
|
f := func(lastCleanupTime, currentTime uint64, resultExpected bool) {
|
|
|
|
t.Helper()
|
2024-02-24 00:07:51 +00:00
|
|
|
var lct atomic.Uint64
|
|
|
|
lct.Store(lastCleanupTime)
|
2022-12-21 20:57:28 +00:00
|
|
|
result := needCleanup(&lct, currentTime)
|
|
|
|
if result != resultExpected {
|
|
|
|
t.Fatalf("unexpected result for needCleanup(%d, %d); got %v; want %v", lastCleanupTime, currentTime, result, resultExpected)
|
|
|
|
}
|
|
|
|
if result {
|
2024-02-24 00:07:51 +00:00
|
|
|
if n := lct.Load(); n != currentTime {
|
|
|
|
t.Fatalf("unexpected value for lct; got %d; want currentTime=%d", n, currentTime)
|
2022-12-21 20:57:28 +00:00
|
|
|
}
|
|
|
|
} else {
|
2024-02-24 00:07:51 +00:00
|
|
|
if n := lct.Load(); n != lastCleanupTime {
|
|
|
|
t.Fatalf("unexpected value for lct; got %d; want lastCleanupTime=%d", n, lastCleanupTime)
|
2022-12-21 20:57:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
f(0, 0, false)
|
|
|
|
f(0, 61, false)
|
|
|
|
f(0, 62, true)
|
|
|
|
f(10, 100, true)
|
|
|
|
}
|