-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathfloat_test.go
133 lines (117 loc) · 3.03 KB
/
float_test.go
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
package enigma
import (
"encoding/json"
"math"
"testing"
"github.com/stretchr/testify/assert"
)
func TestFloatMarshalJSON(t *testing.T) {
cases := []struct {
float float64
exp string
}{
{math.NaN(), `"NaN"`},
{math.Inf(1), `"Infinity"`},
{math.Inf(-1), `"-Infinity"`},
{1.0, "1"},
}
for _, cas := range cases {
float := Float64(cas.float)
ptr := &float
var floatBytes, ptrBytes []byte
var err error
if floatBytes, err = json.Marshal(float); err != nil {
t.Error(err)
continue
}
if ptrBytes, err = json.Marshal(ptr); err != nil {
t.Error(err)
continue
}
if string(floatBytes) != string(ptrBytes) {
t.Errorf("JSON should be equal for pointer and value")
t.Log(" Float64:", string(floatBytes))
t.Log("*Float64:", string(ptrBytes))
}
if string(floatBytes) != cas.exp {
t.Errorf("Expected %q got %q", cas.exp, string(floatBytes))
}
}
}
func TestFloatUnmarshalJSON(t *testing.T) {
cases := []struct {
b string
exp Float64
}{
{`"NaN"`, Float64(math.NaN())},
{`"Infinity"`, Float64(math.Inf(1))},
{`"-Infinity"`, Float64(math.Inf(-1))},
{"1", 1},
}
for _, cas := range cases {
var f Float64
err := json.Unmarshal([]byte(cas.b), &f)
if err != nil {
t.Error(err)
continue
}
if f != cas.exp {
if !(math.IsNaN(float64(f)) && math.IsNaN(float64(cas.exp))) {
t.Errorf("Expected '%T: %v' got '%T: %v'",
cas.exp, cas.exp, f, f)
}
}
}
}
func TestFloatNonPointerUnmarshalJSON(t *testing.T) {
var f Float64
err := json.Unmarshal([]byte("1"), f)
if err == nil {
t.Error("Should not be able to unmarshal non-pointer")
}
}
func TestFloatCompoundUnmarshalJSON(t *testing.T) {
var cell NxCell
_ = json.Unmarshal([]byte(`{"qNum":"Infinity"}`), &cell)
assert.True(t, math.IsInf(float64(cell.Num), 1))
_ = json.Unmarshal([]byte(`{"qNum":"+Infinity"}`), &cell)
assert.True(t, math.IsInf(float64(cell.Num), 1))
_ = json.Unmarshal([]byte(`{"qNum":"-Infinity"}`), &cell)
assert.True(t, math.IsInf(float64(cell.Num), -1))
_ = json.Unmarshal([]byte(`{"qNum":"NaN"}`), &cell)
assert.True(t, math.IsNaN(float64(cell.Num)))
_ = json.Unmarshal([]byte(`{"qNum":-125}`), &cell)
assert.Equal(t, float64(-125), float64(cell.Num))
}
func TestFloatCompoundMarshalJSON(t *testing.T) {
var cell NxCell
var result []byte
cell.Num = -125
result, _ = json.Marshal(&cell)
assert.Equal(t, `{"qNum":-125}`, string(result))
cell.Num = Float64(math.Inf(+1))
result, _ = json.Marshal(&cell)
assert.Equal(t, `{"qNum":"Infinity"}`, string(result))
cell.Num = Float64(math.Inf(-1))
result, _ = json.Marshal(&cell)
assert.Equal(t, `{"qNum":"-Infinity"}`, string(result))
cell.Num = Float64(math.NaN())
result, _ = json.Marshal(&cell)
assert.Equal(t, `{"qNum":"NaN"}`, string(result))
}
func BenchmarkFloatUnmarshal(b *testing.B) {
data := [][]byte{
[]byte(`"NaN"`),
[]byte(`"Infinity"`),
[]byte(`"-Infinity"`),
[]byte("1"),
[]byte("3.14"),
}
for i := 0; i < b.N; i++ {
var f Float64
err := json.Unmarshal(data[i%len(data)], &f)
if err != nil {
b.Error(err)
}
}
}