2024-09-18 22:28:53 +00:00
|
|
|
// SPDX-License-Identifier: Unlicense
|
|
|
|
|
2024-09-10 22:50:14 +00:00
|
|
|
package bytes_test
|
|
|
|
|
2024-09-18 02:48:38 +00:00
|
|
|
import (
|
2024-09-24 17:53:32 +00:00
|
|
|
"bytes"
|
2024-10-16 19:32:25 +00:00
|
|
|
"errors"
|
2024-09-25 22:56:48 +00:00
|
|
|
"strings"
|
2024-09-18 02:48:38 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"git.codemonkeysoftware.net/b/gigaparsec"
|
2024-09-24 17:53:32 +00:00
|
|
|
pbytes "git.codemonkeysoftware.net/b/gigaparsec/bytes"
|
|
|
|
ptest "git.codemonkeysoftware.net/b/gigaparsec/test"
|
|
|
|
pgen "git.codemonkeysoftware.net/b/gigaparsec/test/generator"
|
2024-09-18 02:48:38 +00:00
|
|
|
"github.com/shoenig/test"
|
|
|
|
"github.com/shoenig/test/must"
|
|
|
|
"pgregory.net/rapid"
|
|
|
|
)
|
2024-09-10 22:50:14 +00:00
|
|
|
|
|
|
|
func Todo(t *testing.T) {
|
|
|
|
t.Fatalf("TODO")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRegexp(t *testing.T) {
|
2024-10-16 19:32:25 +00:00
|
|
|
alpha := rapid.SliceOfBytesMatching(`[A-Za-z]{1,100}`)
|
|
|
|
t.Run("position and value are correct after match", rapid.MakeCheck(func(t *rapid.T) {
|
|
|
|
needle := alpha.Draw(t, "needle")
|
|
|
|
input := rapid.Map(alpha, func(suffix []byte) []byte { return append(needle, suffix...) }).
|
|
|
|
Draw(t, "input")
|
2024-09-18 02:48:38 +00:00
|
|
|
|
2024-10-16 19:32:25 +00:00
|
|
|
p := pbytes.Regexp(string(needle))
|
|
|
|
result, err := p(gigaparsec.MakeState(bytes.NewReader(input)))
|
|
|
|
succeeded, val, next := result.Status()
|
|
|
|
|
|
|
|
must.NoError(t, err)
|
|
|
|
test.True(t, succeeded)
|
|
|
|
test.EqOp(t, string(needle), val)
|
|
|
|
ptest.StateIsAt(t, next, uint64(len(needle)))
|
2024-09-18 02:48:38 +00:00
|
|
|
}))
|
2024-10-16 19:32:25 +00:00
|
|
|
t.Run("only searches the beginning of input", rapid.MakeCheck(func(t *rapid.T) {
|
|
|
|
needle := alpha.Draw(t, "needle")
|
|
|
|
input := rapid.Map(alpha, func(prefix []byte) []byte { return append(prefix, needle...) }).
|
|
|
|
Filter(func(b []byte) bool { return !bytes.HasPrefix(b, needle) }).
|
|
|
|
Draw(t, "input")
|
|
|
|
|
|
|
|
p := pbytes.Regexp(string(needle))
|
|
|
|
result, err := p(gigaparsec.MakeState(bytes.NewReader(input)))
|
|
|
|
succeeded, _, _ := result.Status()
|
|
|
|
|
2024-09-18 02:48:38 +00:00
|
|
|
must.NoError(t, err)
|
2024-10-16 19:32:25 +00:00
|
|
|
test.False(t, succeeded)
|
|
|
|
}))
|
|
|
|
t.Run("fails on unexpected error", func(t *testing.T) {
|
|
|
|
expectedErr := errors.New("it broke")
|
|
|
|
p := pbytes.Regexp("nope")
|
|
|
|
result, err := p(gigaparsec.MakeState(ptest.ErrReaderAt(expectedErr)))
|
|
|
|
succeeded, _, _ := result.Status()
|
|
|
|
test.ErrorIs(t, err, expectedErr)
|
|
|
|
test.False(t, succeeded)
|
2024-09-18 02:48:38 +00:00
|
|
|
})
|
2024-10-16 19:32:25 +00:00
|
|
|
t.Run("returns a useful Got value", Todo)
|
2024-09-10 22:50:14 +00:00
|
|
|
}
|
2024-09-10 22:52:08 +00:00
|
|
|
|
|
|
|
func TestRuneReader(t *testing.T) {
|
2024-09-25 22:56:48 +00:00
|
|
|
var s = "abcdefghijklmnopqrstuvwxyz"
|
2024-09-27 15:29:27 +00:00
|
|
|
rr := pbytes.NewRuneReader(gigaparsec.MakeState(strings.NewReader(s)))
|
2024-09-18 02:48:38 +00:00
|
|
|
for i, b := range s {
|
|
|
|
r, n, err := rr.ReadRune()
|
|
|
|
test.NoError(t, err)
|
|
|
|
test.EqOp(t, 1, n)
|
|
|
|
test.EqOp(t, r, rune(b))
|
|
|
|
if t.Failed() {
|
|
|
|
t.Fatalf("failed at %d", i)
|
|
|
|
}
|
|
|
|
}
|
2024-09-10 22:52:08 +00:00
|
|
|
}
|
2024-09-24 17:53:32 +00:00
|
|
|
|
|
|
|
func TestMatchString(t *testing.T) {
|
|
|
|
t.Run("fails on unexpected error", rapid.MakeCheck(func(t *rapid.T) {
|
|
|
|
s := rapid.StringN(-1, -1, 100).Draw(t, "s")
|
|
|
|
readErr := pgen.Error().Draw(t, "readErr")
|
2024-09-27 15:29:27 +00:00
|
|
|
result, err := pbytes.MatchString(s)(gigaparsec.MakeState(ptest.ErrReaderAt(readErr)))
|
2024-09-24 17:53:32 +00:00
|
|
|
test.ErrorIs(t, err, readErr)
|
2024-09-24 19:16:30 +00:00
|
|
|
success, _, _ := result.Status()
|
|
|
|
test.False(t, success)
|
2024-09-24 18:56:10 +00:00
|
|
|
test.False(t, result.Consumed())
|
2024-09-24 17:53:32 +00:00
|
|
|
}))
|
|
|
|
t.Run("does not succeed or consume on mismatch", rapid.MakeCheck(func(t *rapid.T) {
|
|
|
|
bgen := rapid.SliceOfN(rapid.Byte(), 1, 100)
|
|
|
|
input := bgen.Draw(t, "input")
|
|
|
|
notPrefix := func(b []byte) bool { return !bytes.HasPrefix(input, b) }
|
|
|
|
s := string(bgen.Filter(notPrefix).Draw(t, "s"))
|
|
|
|
|
2024-09-27 15:29:27 +00:00
|
|
|
result, err := pbytes.MatchString(s)(gigaparsec.MakeState(bytes.NewReader(input)))
|
2024-09-24 19:16:30 +00:00
|
|
|
test.NoError(t, err)
|
|
|
|
success, _, _ := result.Status()
|
|
|
|
test.False(t, success)
|
2024-09-24 18:56:10 +00:00
|
|
|
test.False(t, result.Consumed())
|
2024-09-24 17:53:32 +00:00
|
|
|
}))
|
|
|
|
t.Run("succeeds with correct value, consumption, and position", rapid.MakeCheck(func(t *rapid.T) {
|
|
|
|
input := rapid.SliceOfN(rapid.Byte(), 1, 100).Draw(t, "input")
|
|
|
|
slen := rapid.IntRange(0, len(input)).Draw(t, "slen")
|
|
|
|
s := string(input[:slen])
|
2024-09-27 15:29:27 +00:00
|
|
|
result, err := pbytes.MatchString(s)(gigaparsec.MakeState(bytes.NewReader(input)))
|
2024-09-24 17:53:32 +00:00
|
|
|
must.NoError(t, err)
|
2024-09-24 19:16:30 +00:00
|
|
|
success, value, next := result.Status()
|
|
|
|
must.True(t, success)
|
2024-09-24 18:56:10 +00:00
|
|
|
test.True(t, result.Consumed())
|
2024-09-24 17:53:32 +00:00
|
|
|
test.EqOp(t, s, value)
|
|
|
|
ptest.StateIsAt(t, next, uint64(slen))
|
|
|
|
}))
|
|
|
|
}
|