-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroundtrip_test.go
More file actions
139 lines (122 loc) · 2.31 KB
/
roundtrip_test.go
File metadata and controls
139 lines (122 loc) · 2.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package zon
import (
"bytes"
"fmt"
"reflect"
"testing"
)
func TestEncoderDecoderRoundTrip(t *testing.T) {
v := map[string]any{
"name": "Bob",
"age": 42,
"active": true,
}
var buf bytes.Buffer
if err := NewEncoder(&buf).Encode(v); err != nil {
t.Fatalf("Encoder.Encode failed: %v", err)
}
var v2 map[string]any
if err := NewDecoder(&buf).Decode(&v2); err != nil {
t.Fatalf("Decoder.Decode failed: %v", err)
}
if !mapsDeepEqual(v, v2) {
t.Errorf("Encoder/Decoder round-trip mismatch\nexpected: %#v\nactual: %#v", v, v2)
}
}
func mapsDeepEqual(a, b map[string]any) bool {
if len(a) != len(b) {
return false
}
toInt64 := func(v any) int64 {
switch x := v.(type) {
case int:
return int64(x)
case int8:
return int64(x)
case int16:
return int64(x)
case int32:
return int64(x)
case int64:
return x
case float32:
return int64(x)
case float64:
return int64(x)
}
panic(fmt.Sprintf("toInt64: unsupported type %T", v))
}
toUint64 := func(v any) uint64 {
switch x := v.(type) {
case uint:
return uint64(x)
case uint8:
return uint64(x)
case uint16:
return uint64(x)
case uint32:
return uint64(x)
case uint64:
return x
case float32:
return uint64(x)
case float64:
return uint64(x)
}
panic(fmt.Sprintf("toUint64: unsupported type %T", v))
}
toFloat64 := func(v any) float64 {
switch x := v.(type) {
case float32:
return float64(x)
case float64:
return x
case int:
return float64(x)
case int8:
return float64(x)
case int16:
return float64(x)
case int32:
return float64(x)
case int64:
return float64(x)
case uint:
return float64(x)
case uint8:
return float64(x)
case uint16:
return float64(x)
case uint32:
return float64(x)
case uint64:
return float64(x)
}
panic(fmt.Sprintf("toFloat64: unsupported type %T", v))
}
for k, v := range a {
w, ok := b[k]
if !ok {
return false
}
switch x := v.(type) {
case int, int8, int16, int32, int64:
if toInt64(x) != toInt64(w) {
return false
}
case uint, uint8, uint16, uint32, uint64:
if toUint64(x) != toUint64(w) {
return false
}
case float32, float64:
if toFloat64(x) != toFloat64(w) {
return false
}
default:
if !reflect.DeepEqual(v, w) {
return false
}
}
}
return true
}