MCPcopy
hub / github.com/rs/zerolog / TestDecodeFloat

Function TestDecodeFloat

internal/cbor/decoder_test.go:113–159  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

111}
112
113func TestDecodeFloat(t *testing.T) {
114 for _, tc := range internal.Float32TestCases {
115 got, _ := decodeFloat(getReader(tc.Binary))
116 if got != float64(tc.Val) && math.IsNaN(got) != math.IsNaN(float64(tc.Val)) {
117 t.Errorf("decodeFloat(0x%s)=%f, want:%f", hex.EncodeToString([]byte(tc.Binary)), got, tc.Val)
118 }
119 }
120 for _, tc := range internal.Float64TestCases {
121 got, _ := decodeFloat(getReader(tc.Binary))
122 if got != tc.Val && math.IsNaN(got) != math.IsNaN(tc.Val) {
123 t.Errorf("decodeFloat(0x%s)=%f, want:%f", hex.EncodeToString([]byte(tc.Binary)), got, tc.Val)
124 }
125 }
126
127 // Test float64 special values with correct CBOR encoding
128 float64Tests := []struct {
129 name string
130 input string
131 want float64
132 }{
133 {"float64 NaN", "\xfb\x7f\xf8\x00\x00\x00\x00\x00\x00", math.NaN()},
134 {"float64 +Inf", "\xfb\x7f\xf0\x00\x00\x00\x00\x00\x00", math.Inf(0)},
135 {"float64 -Inf", "\xfb\xff\xf0\x00\x00\x00\x00\x00\x00", math.Inf(-1)},
136 {"float64 1.0", "\xfb\x3f\xf0\x00\x00\x00\x00\x00\x00", 1.0},
137 }
138
139 for _, tt := range float64Tests {
140 t.Run(tt.name, func(t *testing.T) {
141 got, _ := decodeFloat(getReader(tt.input))
142 if math.IsNaN(tt.want) {
143 if !math.IsNaN(got) {
144 t.Errorf("decodeFloat(%q) = %f, want NaN", tt.input, got)
145 }
146 } else if math.IsInf(tt.want, 0) {
147 if !math.IsInf(got, 0) {
148 t.Errorf("decodeFloat(%q) = %f, want +Inf", tt.input, got)
149 }
150 } else if math.IsInf(tt.want, -1) {
151 if !math.IsInf(got, -1) {
152 t.Errorf("decodeFloat(%q) = %f, want -Inf", tt.input, got)
153 }
154 } else if got != tt.want {
155 t.Errorf("decodeFloat(%q) = %f, want %f", tt.input, got, tt.want)
156 }
157 })
158 }
159}
160
161func TestDecodeTimestamp(t *testing.T) {
162 decodeTimeZone, _ = time.LoadLocation("UTC")

Callers

nothing calls this directly

Calls 3

decodeFloatFunction · 0.85
getReaderFunction · 0.85
RunMethod · 0.65

Tested by

no test coverage detected