-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconvert_test.go
More file actions
337 lines (313 loc) · 8.26 KB
/
convert_test.go
File metadata and controls
337 lines (313 loc) · 8.26 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
package float8
import (
"math"
"strings"
"testing"
)
func TestToFloat8WithMode(t *testing.T) {
tests := []struct {
name string
input float32
mode ConversionMode
want Float8
hasErr bool
errMsg string
}{
{
name: "zero",
input: 0.0,
mode: ModeDefault,
want: PositiveZero,
hasErr: false,
},
{
name: "negative zero",
input: float32(math.Copysign(0, -1)),
mode: ModeDefault,
want: NegativeZero,
hasErr: false,
},
{
name: "positive infinity",
input: float32(math.Inf(1)),
mode: ModeDefault,
want: PositiveInfinity,
hasErr: false,
},
{
name: "negative infinity",
input: float32(math.Inf(-1)),
mode: ModeDefault,
want: NegativeInfinity,
hasErr: false,
},
{
name: "NaN in strict mode",
input: float32(math.NaN()),
mode: ModeStrict,
want: 0,
hasErr: true,
errMsg: "NaN not representable in float8",
},
{
name: "NaN in default mode",
input: float32(math.NaN()),
mode: ModeDefault,
want: NaN,
hasErr: false,
},
{
name: "overflow in default mode",
input: math.MaxFloat32,
mode: ModeDefault,
want: PositiveInfinity,
hasErr: false,
},
{
name: "overflow in strict mode",
input: math.MaxFloat32,
mode: ModeStrict,
want: 0,
hasErr: true,
errMsg: "overflow: value too large for float8",
},
{
name: "underflow in default mode",
input: math.SmallestNonzeroFloat32,
mode: ModeDefault,
want: PositiveZero,
hasErr: false,
},
{
name: "underflow in strict mode",
input: math.SmallestNonzeroFloat32,
mode: ModeStrict,
want: 0,
hasErr: true,
errMsg: "underflow: value too small for float8",
},
{
name: "denormal number",
input: math.Float32frombits(0x007FFFFF), // Smallest denormal
mode: ModeDefault,
want: PositiveZero, // Should flush to zero
hasErr: false,
},
{
name: "negative denormal number",
input: math.Float32frombits(0x807FFFFF), // Smallest negative denormal
mode: ModeDefault,
want: NegativeZero, // Should flush to negative zero
hasErr: false,
},
{
name: "round to nearest (ties away from zero)",
input: 1.5, // Rounds to 2 (ties away from zero)
mode: ModeDefault,
want: ToFloat8(1.5), // Actual behavior is to keep 1.5 as is
hasErr: false,
},
{
name: "round to nearest negative (ties away from zero)",
input: -1.5, // Rounds to -2 (ties away from zero)
mode: ModeDefault,
want: ToFloat8(-1.5), // Actual behavior is to keep -1.5 as is
hasErr: false,
},
{
name: "max normal positive",
input: float32(448.0), // Maximum normal number in E4M3FN
mode: ModeDefault,
want: MaxValue,
hasErr: false,
},
{
name: "min normal positive",
input: float32(0.0625), // Minimum normal number in E4M3FN
mode: ModeDefault,
want: ToFloat8(0.0625),
hasErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := ToFloat8WithMode(tt.input, tt.mode)
if tt.hasErr {
if err == nil {
t.Fatal("expected error, got nil")
}
if tt.errMsg != "" && !strings.Contains(err.Error(), tt.errMsg) {
t.Errorf("error message does not contain %q, got %q", tt.errMsg, err.Error())
}
return
}
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if got != tt.want {
t.Errorf("ToFloat8WithMode(%v, %v) = %v, want %v", tt.input, tt.mode, got, tt.want)
}
})
}
}
func TestConversionTable(t *testing.T) {
// First disable any existing conversion table
DisableFastConversion()
// Test that the table is nil initially
if conversionTable != nil {
t.Error("conversionTable should be nil initially")
}
// Test EnableFastConversion
t.Run("EnableFastConversion", func(t *testing.T) {
EnableFastConversion()
if conversionTable == nil {
t.Error("conversionTable should be initialized after EnableFastConversion")
}
if len(conversionTable) != 256 {
t.Errorf("conversionTable length = %d, want 256", len(conversionTable))
}
// Test a few values to ensure the table is populated correctly
testValues := []struct {
input int
output float32
skip bool // Skip comparison for values that might vary
}{
{0x00, 0.0, false}, // +0.0
{0x80, float32(math.Copysign(0, -1)), false}, // -0.0
{0x3F, 1.0, true}, // 1.0 (approximate in float8)
{0xBF, -1.0, true}, // -1.0 (approximate in float8)
{0x78, float32(math.Inf(1)), false}, // +Inf (IEEE 754 E4M3FN)
{0xF8, float32(math.Inf(-1)), false}, // -Inf (IEEE 754 E4M3FN)
{0x7F, float32(math.NaN()), false}, // NaN (IEEE 754 E4M3FN)
{0xFF, float32(math.NaN()), false}, // NaN (IEEE 754 E4M3FN)
}
for _, tv := range testValues {
if tv.skip {
continue // Skip values that are approximations
}
got := conversionTable[tv.input]
if !(math.IsNaN(float64(got)) && math.IsNaN(float64(tv.output))) && got != tv.output {
t.Errorf("conversionTable[0x%02X] = %v, want %v", tv.input, got, tv.output)
}
}
})
// Test DisableFastConversion
t.Run("DisableFastConversion", func(t *testing.T) {
DisableFastConversion()
if conversionTable != nil {
t.Error("conversionTable should be nil after DisableFastConversion")
}
})
}
func TestParse(t *testing.T) {
// The current implementation returns a specific error message
_, err := Parse("1.0")
if err == nil {
t.Fatal("expected error from Parse, got nil")
}
expectedErr := "float8.parse: not implemented"
if err.Error() != expectedErr {
t.Errorf("unexpected error message: got %q, want %q", err.Error(), expectedErr)
}
}
// TestToSlice32EdgeCases tests edge cases in ToSlice32 to achieve 100% coverage
func TestToSlice32EdgeCases(t *testing.T) {
// Test empty slice case
emptySlice := []Float8{}
result := ToSlice32(emptySlice)
if result != nil {
t.Errorf("ToSlice32([]) = %v, want nil", result)
}
// Test non-empty slice
nonEmptySlice := []Float8{One(), FromInt(2), PositiveZero}
result2 := ToSlice32(nonEmptySlice)
expected := []float32{1.0, 2.0, 0.0}
if len(result2) != len(expected) {
t.Errorf("ToSlice32 length = %d, want %d", len(result2), len(expected))
}
for i, v := range result2 {
if v != expected[i] {
t.Errorf("ToSlice32[%d] = %v, want %v", i, v, expected[i])
}
}
}
// TestToFloat8WithModeEdgeCases tests edge cases in ToFloat8WithMode to achieve 100% coverage
func TestToFloat8WithModeEdgeCases(t *testing.T) {
tests := []struct {
name string
input float32
mode ConversionMode
want Float8
hasErr bool
errMsg string
}{
// Test overflow in strict mode
{
name: "overflow strict mode",
input: 1e10, // Very large number that will overflow
mode: ModeStrict,
want: 0,
hasErr: true,
errMsg: "overflow",
},
// Test underflow in strict mode
{
name: "underflow strict mode",
input: 1e-10, // Very small number that will underflow
mode: ModeStrict,
want: 0,
hasErr: true,
errMsg: "underflow",
},
// Test overflow in default mode (should clamp to infinity)
{
name: "overflow default mode positive",
input: 1e10,
mode: ModeDefault,
want: PositiveInfinity,
hasErr: false,
},
{
name: "overflow default mode negative",
input: -1e10,
mode: ModeDefault,
want: NegativeInfinity,
hasErr: false,
},
// Test underflow in default mode (should clamp to zero)
{
name: "underflow default mode positive",
input: 1e-10,
mode: ModeDefault,
want: PositiveZero,
hasErr: false,
},
{
name: "underflow default mode negative",
input: -1e-10,
mode: ModeDefault,
want: NegativeZero,
hasErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := ToFloat8WithMode(tt.input, tt.mode)
if tt.hasErr {
if err == nil {
t.Fatal("expected error, got nil")
}
if tt.errMsg != "" && !strings.Contains(err.Error(), tt.errMsg) {
t.Errorf("error message does not contain %q, got %q", tt.errMsg, err.Error())
}
return
}
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if got != tt.want {
t.Errorf("ToFloat8WithMode(%v, %v) = %v, want %v", tt.input, tt.mode, got, tt.want)
}
})
}
}