Skip to content

Commit

Permalink
test: reorganize test, add more cases to improve coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
tdakkota committed Jan 16, 2022
1 parent fb6652f commit 5af0eb4
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 63 deletions.
89 changes: 45 additions & 44 deletions dec_skip_cases_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,50 +13,51 @@ import (
)

var testStrings = []string{
`""`, // valid
`"hello"`, // valid
`"`, // invalid
`"foo`, // invalid
`"\`, // invalid
`"\"`, // invalid
`"\u`, // invalid
`"\u1`, // invalid
`"\u12`, // invalid
`"\u123`, // invalid
`"\u\n"`, // invalid
`"\u1\n"`, // invalid
`"\u12\n"`, // invalid
`"\u12\n"`, // invalid
`"\u123\n"`, // invalid
`"\u1d`, // invalid
`"\u$`, // invalid
`"\21412`, // invalid
`"\uD834\1`, // invalid
`"\uD834\u3`, // invalid
`"\uD834\`, // invalid
`"\uD834`, // invalid
`"\u07F9`, // invalid
`"\u1234\n"`, // valid
`"\x00"`, // invalid
"\"\x00\"", // invalid
"\"\t\"", // invalid
"\"\\b\x06\"", // invalid
`"\t"`, // valid
`"\n"`, // valid
`"\r"`, // valid
`"\b"`, // valid
`"\f"`, // valid
`"\/"`, // valid
`"\\"`, // valid
"\"\\u000X\"", // invalid
"\"\\uxx0X\"", // invalid
"\"\\uxxxx\"", // invalid
"\"\\u000.\"", // invalid
"\"\\u0000\"", // valid
"\"\\ua123\"", // valid
"\"\\uffff\"", // valid
"\"\\ueeee\"", // valid
"\"\\uFFFF\"", // valid
`""`, // valid
`"hello"`, // valid
`"`, // invalid
`"foo`, // invalid
`"\`, // invalid
`"\"`, // invalid
`"\u`, // invalid
`"\u1`, // invalid
`"\u12`, // invalid
`"\u123`, // invalid
`"\u\n"`, // invalid
`"\u1\n"`, // invalid
`"\u12\n"`, // invalid
`"\u12\n"`, // invalid
`"\u123\n"`, // invalid
`"\u1d`, // invalid
`"\u$`, // invalid
`"\21412`, // invalid
`"\uD834\1`, // invalid
`"\uD834\u3`, // invalid
`"\uD834\`, // invalid
`"\uD834`, // invalid
`"\u07F9`, // invalid
`"\u1234\n"`, // valid
`"\x00"`, // invalid
"\"\x00\"", // invalid
"\"\t\"", // invalid
"\"\\b\x06\"", // invalid
`"\t"`, // valid
`"\n"`, // valid
`"\r"`, // valid
`"\b"`, // valid
`"\f"`, // valid
`"\/"`, // valid
`"\\"`, // valid
"\"\\u000X\"", // invalid
"\"\\uxx0X\"", // invalid
"\"\\uxxxx\"", // invalid
"\"\\u000.\"", // invalid
"\"\\u0000\"", // valid
"\"\\ua123\"", // valid
"\"\\uffff\"", // valid
"\"\\ueeee\"", // valid
"\"\\uFFFF\"", // valid
`"ab\n` + "\x00" + `"`, // invalid
}

var testObjs = []string{
Expand Down
50 changes: 31 additions & 19 deletions dec_str_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strings"
"testing"
"testing/iotest"
"unicode/utf8"

"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -35,8 +36,14 @@ func TestUnexpectedTokenErr_Error(t *testing.T) {
}

func TestDecoder_Str(t *testing.T) {
testStr := func(d *Decoder, valid bool) func(t *testing.T) {
testStr := func(d *Decoder, input string, valid bool) func(t *testing.T) {
return func(t *testing.T) {
t.Cleanup(func() {
if t.Failed() {
t.Logf("Input: %q", input)
}
})

_, err := d.Str()
if valid {
require.NoError(t, err)
Expand All @@ -45,19 +52,21 @@ func TestDecoder_Str(t *testing.T) {
}
}
}
for _, input := range testStrings {
for i, input := range testStrings {
valid := json.Valid([]byte(input))

t.Run("Buffer", testStr(DecodeStr(input), valid))
t.Run(fmt.Sprintf("Test%d", i), func(t *testing.T) {
t.Run("Buffer", testStr(DecodeStr(input), input, valid))

r := strings.NewReader(input)
d := Decode(r, 512)
t.Run("Reader", testStr(d, valid))
r := strings.NewReader(input)
d := Decode(r, 512)
t.Run("Reader", testStr(d, input, valid))

r.Reset(input)
obr := iotest.OneByteReader(r)
d.Reset(obr)
t.Run("OneByteReader", testStr(d, valid))
r.Reset(input)
obr := iotest.OneByteReader(r)
d.Reset(obr)
t.Run("OneByteReader", testStr(d, input, valid))
})
}
}

Expand Down Expand Up @@ -92,14 +101,17 @@ func benchmarkDecoderStrBytes(str string) func(b *testing.B) {
}

func BenchmarkDecoder_StrBytes(b *testing.B) {
for _, size := range []int{
2, 8, 16, 64, 128, 1024,
} {
b.Run("Escaped", func(b *testing.B) {
b.Run(fmt.Sprintf("%db", size), benchmarkDecoderStrBytes(strings.Repeat("ф", size/'ф')))
})
b.Run("Plain", func(b *testing.B) {
b.Run(fmt.Sprintf("%db", size), benchmarkDecoderStrBytes(strings.Repeat("a", size)))
})
runBench := func(char string) func(b *testing.B) {
return func(b *testing.B) {
for _, size := range []int{
2, 8, 16, 64, 128, 1024,
} {
count := utf8.RuneCountInString(char)
b.Run(fmt.Sprintf("%db", size), benchmarkDecoderStrBytes(strings.Repeat(char, size/count)))
}
}
}

b.Run("Plain", runBench("a"))
b.Run("Escaped", runBench("ф"))
}

0 comments on commit 5af0eb4

Please sign in to comment.