diff options
author | Luke Shumaker <lukeshu@datawire.io> | 2022-08-14 20:52:33 -0600 |
---|---|---|
committer | Luke Shumaker <lukeshu@datawire.io> | 2022-08-17 02:02:42 -0600 |
commit | 35997d235f3bac7c3f9bcd4b8d2b26b0d88dc387 (patch) | |
tree | 47386ceb19133207508c32e3669a483b4d43e7f7 /compat | |
parent | 3ae8e37b8ca2b9a7769d659e134ee2711dc94b89 (diff) |
Get the new borrowed tests running right [ci-skip]
Diffstat (limited to 'compat')
-rw-r--r-- | compat/json/borrowed_bench_test.go | 27 | ||||
-rw-r--r-- | compat/json/borrowed_stream_test.go | 2 | ||||
-rw-r--r-- | compat/json/compat.go | 37 | ||||
-rw-r--r-- | compat/json/compat_test.go | 13 |
4 files changed, 64 insertions, 15 deletions
diff --git a/compat/json/borrowed_bench_test.go b/compat/json/borrowed_bench_test.go index 9560914..f5595ff 100644 --- a/compat/json/borrowed_bench_test.go +++ b/compat/json/borrowed_bench_test.go @@ -13,14 +13,9 @@ package json import ( "bytes" "compress/gzip" - "fmt" - "internal/testenv" "io" "os" - "reflect" - "runtime" "strings" - "sync" "testing" ) @@ -42,7 +37,7 @@ type codeNode struct { var codeJSON []byte var codeStruct codeResponse -func codeInit() { +func codeInit(b *testing.B) { // MODIFIED: use the test logger f, err := os.Open("testdata/code.json.gz") if err != nil { panic(err) @@ -68,12 +63,12 @@ func codeInit() { } if !bytes.Equal(data, codeJSON) { - println("different lengths", len(data), len(codeJSON)) + b.Log("different lengths", len(data), len(codeJSON)) // MODIFIED: use the test logger for i := 0; i < len(data) && i < len(codeJSON); i++ { if data[i] != codeJSON[i] { - println("re-marshal: changed at byte", i) - println("orig: ", string(codeJSON[i-10:i+10])) - println("new: ", string(data[i-10:i+10])) + b.Log("re-marshal: changed at byte", i) // MODIFIED: use the test logger + b.Log("orig: ", string(codeJSON[i-10:i+10])) // MODIFIED: use the test logger + b.Log("new: ", string(data[i-10:i+10])) // MODIFIED: use the test logger break } } @@ -85,7 +80,7 @@ func BenchmarkCodeEncoder(b *testing.B) { b.ReportAllocs() if codeJSON == nil { b.StopTimer() - codeInit() + codeInit(b) // MODIFIED: use the test logger b.StartTimer() } b.RunParallel(func(pb *testing.PB) { @@ -103,7 +98,7 @@ func BenchmarkCodeMarshal(b *testing.B) { b.ReportAllocs() if codeJSON == nil { b.StopTimer() - codeInit() + codeInit(b) // MODIFIED: use the test logger b.StartTimer() } b.RunParallel(func(pb *testing.PB) { @@ -149,7 +144,7 @@ func BenchmarkCodeDecoder(b *testing.B) { b.ReportAllocs() if codeJSON == nil { b.StopTimer() - codeInit() + codeInit(b) // MODIFIED: use the test logger b.StartTimer() } b.RunParallel(func(pb *testing.PB) { @@ -213,7 +208,7 @@ func BenchmarkCodeUnmarshal(b *testing.B) { b.ReportAllocs() if codeJSON == nil { b.StopTimer() - codeInit() + codeInit(b) // MODIFIED: use the test logger b.StartTimer() } b.RunParallel(func(pb *testing.PB) { @@ -231,7 +226,7 @@ func BenchmarkCodeUnmarshalReuse(b *testing.B) { b.ReportAllocs() if codeJSON == nil { b.StopTimer() - codeInit() + codeInit(b) // MODIFIED: use the test logger b.StartTimer() } b.RunParallel(func(pb *testing.PB) { @@ -326,6 +321,7 @@ func BenchmarkUnmapped(b *testing.B) { }) } +/* // MODIFIED: we don't have a cache func BenchmarkTypeFieldsCache(b *testing.B) { b.ReportAllocs() var maxTypes int = 1e6 @@ -389,6 +385,7 @@ func BenchmarkTypeFieldsCache(b *testing.B) { }) } } +*/ // MODIFIED: we don't have a cache func BenchmarkEncodeMarshaler(b *testing.B) { b.ReportAllocs() diff --git a/compat/json/borrowed_stream_test.go b/compat/json/borrowed_stream_test.go index 0e156d9..658e347 100644 --- a/compat/json/borrowed_stream_test.go +++ b/compat/json/borrowed_stream_test.go @@ -333,6 +333,7 @@ type decodeThis struct { v any } +/* // MODIFIED: we don't have tokens var tokenStreamCases = []tokenStreamCase{ // streaming token cases {json: `10`, expTokens: []any{float64(10)}}, @@ -438,6 +439,7 @@ func TestDecodeInStream(t *testing.T) { } } } +*/ // MODIFIED: we don't have tokens // Test from golang.org/issue/11893 func TestHTTPDecoding(t *testing.T) { diff --git a/compat/json/compat.go b/compat/json/compat.go index 59eff6c..37a0d5b 100644 --- a/compat/json/compat.go +++ b/compat/json/compat.go @@ -70,6 +70,43 @@ func Marshal(v any) ([]byte, error) { }) } +type Encoder struct { + inner lowmemjson.ReEncoder +} + +func NewEncoder(w io.Writer) *Encoder { + return &Encoder{ + inner: lowmemjson.ReEncoder{ + Out: w, + Compact: true, + }, + } +} + +func (enc *Encoder) Encode(v any) error { + if err := convertEncodeError(lowmemjson.Encode(&enc.inner, v)); err != nil { + return err + } + if err := convertEncodeError(enc.inner.Close()); err != nil { + return err + } + return nil +} + +func (enc *Encoder) SetEscapeHTML(on bool) { + if on { + enc.inner.BackslashEscape = nil + } else { + enc.inner.BackslashEscape = lowmemjson.EscapeJSSafe + } +} + +func (enc *Encoder) SetIndent(prefix, indent string) { + enc.inner.Compact = prefix == "" && indent == "" + enc.inner.Prefix = prefix + enc.inner.Indent = indent +} + // ReEncode wrappers ///////////////////////////////////////////////// func convertReEncodeError(err error) error { diff --git a/compat/json/compat_test.go b/compat/json/compat_test.go index 2cb1e87..997d07e 100644 --- a/compat/json/compat_test.go +++ b/compat/json/compat_test.go @@ -21,6 +21,19 @@ func checkValid(in []byte, scan *lowmemjson.ReEncoder) error { return reencode(in, scan) } +func isValidNumber(s string) bool { + var parser lowmemjson.Parser + for _, r := range s { + if t, _ := parser.HandleRune(r); !t.IsNumber() { + return false + } + } + if t, _ := parser.HandleEOF(); t == lowmemjson.RuneTypeError { + return false + } + return true +} + const ( startDetectingCyclesAfter = 1000 ) |