Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
AsterDY committed Aug 25, 2024
1 parent 9643f4a commit 5069157
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 11 deletions.
1 change: 1 addition & 0 deletions decoder/decoder_native.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ const (
OptionDisableUnknown Options = api.OptionDisableUnknown
OptionCopyString Options = api.OptionCopyString
OptionValidateString Options = api.OptionValidateString
OptionNoValidateJSON Options = api.OptionNoValidateJSON
)

// StreamDecoder is the decoder context object for streaming input.
Expand Down
68 changes: 58 additions & 10 deletions decoder/decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,17 @@
package decoder

import (
`encoding/json`
`runtime`
`runtime/debug`
`strings`
`sync`
`testing`
`time`

`github.com/stretchr/testify/assert`
`github.com/stretchr/testify/require`
"encoding/json"
"fmt"
"runtime"
"runtime/debug"
"strings"
"sync"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestMain(m *testing.M) {
Expand Down Expand Up @@ -85,6 +86,53 @@ func init() {
_ = json.Unmarshal([]byte(TwitterJson), &_BindingValue)
}

func TestFastSkip(t *testing.T) {
type skiptype struct {
A int `json:"a"` // mismatched
B string `json:"-"` // ommited
C [1]float64 `json:"c"` // fast int
D struct {} `json:"d"` // empty struct
// Unknonwn
}
type C struct {
name string
json string
expTime float64
}
var compt = `[+`+TwitterJson+`,`+TwitterJson+`,`+TwitterJson+`,`+TwitterJson+`,`+TwitterJson+`,`+TwitterJson+`,`+TwitterJson+`,`+TwitterJson+`+]`
var cases = []C{
{"mismatched", `{"a":`+compt+`}`, 2},
{"ommited", `{"b":`+compt+`}`, 2},
// {"fast int", `{"c":[`+strings.Repeat("-1.23456e-19,", 2000)+`1]}`, 1.2},
{"unknown", `{"unknown":`+compt+`}`, 2},
{"empty", `{"d":`+compt+`}`, 2},
}
_ = NewDecoder(`{}`).Decode(&skiptype{})
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
var obj1, obj2 = &skiptype{}, &skiptype{}
// validate skip
d := NewDecoder(c.json)
t1 := time.Now()
err1 := d.Decode(obj1)
d1 := time.Since(t1)
// fask skip
d = NewDecoder(c.json)
d.SetOptions(OptionNoValidateJSON)
t2 := time.Now()
err2 := d.Decode(obj2)
d2 := time.Since(t2)

require.Equal(t, err1 == nil, err2 == nil)
require.Equal(t, obj1, obj2)
// fast skip must be 5x faster
println(d1, d2)
require.True(t, float64(d1)/float64(d2) > c.expTime, fmt.Sprintf("%v/%v=%v", d1, d2, float64(d1)/float64(d2)))
})
}
// var data = `{"a":`+TwitterJson+`,"b":`+TwitterJson+`,"c":[`+strings.Repeat("1,", 1024)+`1], "d":`+TwitterJson+`, "UNKNOWN":`+TwitterJson+`}`
}


func TestSkipMismatchTypeError(t *testing.T) {
t.Run("struct", func(t *testing.T) {
Expand Down
1 change: 1 addition & 0 deletions internal/decoder/api/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ const (
OptionDisableUnknown = consts.OptionDisableUnknown
OptionCopyString = consts.OptionCopyString
OptionValidateString = consts.OptionValidateString
OptionNoValidateJSON = consts.OptionNoValidateJSON
)

type (
Expand Down
2 changes: 1 addition & 1 deletion internal/decoder/consts/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const (
OptionDisableUnknown Options = 1 << F_disable_unknown
OptionCopyString Options = 1 << F_copy_string
OptionValidateString Options = 1 << F_validate_string
OptionNoValidateRawJSON Options = 1 << F_no_validate_json
OptionNoValidateJSON Options = 1 << F_no_validate_json
)

const (
Expand Down

0 comments on commit 5069157

Please sign in to comment.