tailscale/util/jsonutil/unmarshal_test.go
Will Norris 71029cea2d all: update copyright and license headers
This updates all source files to use a new standard header for copyright
and license declaration.  Notably, copyright no longer includes a date,
and we now use the standard SPDX-License-Identifier header.

This commit was done almost entirely mechanically with perl, and then
some minimal manual fixes.

Updates #6865

Signed-off-by: Will Norris <will@tailscale.com>
2023-01-27 15:36:29 -08:00

65 lines
1.3 KiB
Go

// Copyright (c) Tailscale Inc & AUTHORS
// SPDX-License-Identifier: BSD-3-Clause
package jsonutil
import (
"encoding/json"
"reflect"
"testing"
)
func TestCompareToStd(t *testing.T) {
tests := []string{
`{}`,
`{"a": 1}`,
`{]`,
`"abc"`,
`5`,
`{"a": 1} `,
`{"a": 1} {}`,
`{} bad data`,
`{"a": 1} "hello"`,
`[]`,
` {"x": {"t": [3,4,5]}}`,
}
for _, test := range tests {
b := []byte(test)
var ourV, stdV any
ourErr := Unmarshal(b, &ourV)
stdErr := json.Unmarshal(b, &stdV)
if (ourErr == nil) != (stdErr == nil) {
t.Errorf("Unmarshal(%q): our err = %#[2]v (%[2]T), std err = %#[3]v (%[3]T)", test, ourErr, stdErr)
}
// if !reflect.DeepEqual(ourErr, stdErr) {
// t.Logf("Unmarshal(%q): our err = %#[2]v (%[2]T), std err = %#[3]v (%[3]T)", test, ourErr, stdErr)
// }
if ourErr != nil {
// TODO: if we zero ourV on error, remove this continue.
continue
}
if !reflect.DeepEqual(ourV, stdV) {
t.Errorf("Unmarshal(%q): our val = %v, std val = %v", test, ourV, stdV)
}
}
}
func BenchmarkUnmarshal(b *testing.B) {
var m any
j := []byte("5")
b.ReportAllocs()
for i := 0; i < b.N; i++ {
Unmarshal(j, &m)
}
}
func BenchmarkStdUnmarshal(b *testing.B) {
var m any
j := []byte("5")
b.ReportAllocs()
for i := 0; i < b.N; i++ {
json.Unmarshal(j, &m)
}
}