-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconvert_test.go
More file actions
141 lines (131 loc) · 3.48 KB
/
convert_test.go
File metadata and controls
141 lines (131 loc) · 3.48 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
package float16
import (
"math"
"testing"
)
func TestFromFloat64WithMode_Extra(t *testing.T) {
tests := []struct {
name string
input float64
convMode ConversionMode
roundMode RoundingMode
expected Float16
hasError bool
errCode ErrorCode
}{
{
name: "Strict mode overflow",
input: 70000.0,
convMode: ModeStrict,
roundMode: RoundNearestEven,
hasError: true,
errCode: ErrOverflow,
},
{
name: "Strict mode underflow",
input: 1e-6,
convMode: ModeStrict,
roundMode: RoundNearestEven,
hasError: true,
errCode: ErrUnderflow,
},
{
name: "Strict mode NaN",
input: math.NaN(),
convMode: ModeStrict,
roundMode: RoundNearestEven,
hasError: true,
errCode: ErrNaN,
},
{
name: "Strict mode Inf",
input: math.Inf(1),
convMode: ModeStrict,
roundMode: RoundNearestEven,
hasError: true,
errCode: ErrInfinity,
},
{
name: "IEEE mode overflow",
input: 70000.0,
convMode: ModeIEEE,
roundMode: RoundNearestEven,
expected: PositiveInfinity,
},
{
name: "IEEE mode underflow",
input: 1e-6,
convMode: ModeIEEE,
roundMode: RoundNearestEven,
expected: 0x0011,
},
{
name: "IEEE mode out of range",
input: 80000.0,
convMode: ModeIEEE,
roundMode: RoundNearestEven,
expected: PositiveInfinity,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result, err := FromFloat64WithMode(tt.input, tt.convMode, tt.roundMode)
if tt.hasError {
if err == nil {
t.Fatalf("Expected error, got nil")
}
err16, ok := err.(*Float16Error)
if !ok {
t.Fatalf("Expected Float16Error, got %T", err)
}
if err16.Code != tt.errCode {
t.Errorf("Expected error code %v, got %v", tt.errCode, err16.Code)
}
return
}
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if result != tt.expected {
t.Errorf("FromFloat64WithMode() = %v, want %v", result, tt.expected)
}
})
}
}
func TestShouldRound(t *testing.T) {
tests := []struct {
name string
mantissa uint32
shift int
mode RoundingMode
sign uint16
shouldRound bool
}{
{"NearestEven, no round", 0b1010_0000, 4, RoundNearestEven, 0, false},
{"NearestEven, round up", 0b1011_1000, 4, RoundNearestEven, 0, true},
{"NearestEven, halfway, even", 0b1010_1000, 4, RoundNearestEven, 0, false},
{"NearestEven, halfway, odd", 0b1011_1000, 4, RoundNearestEven, 0, true},
{"NearestAway, round up", 0b1010_1000, 4, RoundNearestAway, 0, true},
{"TowardZero, no round", 0b1010_1111, 4, RoundTowardZero, 0, false},
{"TowardPositive, round up", 0b1010_0001, 4, RoundTowardPositive, 0, true},
{"TowardNegative, no round", 0b1010_0001, 4, RoundTowardNegative, 0, false},
{"TowardPositive, no round (negative)", 0b1010_0001, 4, RoundTowardPositive, 0x8000, false},
{"TowardNegative, round up (negative)", 0b1010_0001, 4, RoundTowardNegative, 0x8000, true},
// Default case
{"Invalid rounding mode", 0b1010_0001, 4, 99, 0, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := shouldRoundWithMode(tt.mantissa, tt.shift, tt.sign, tt.mode)
if got != tt.shouldRound {
t.Errorf("shouldRoundWithMode(%d, %d, %d, %d) = %v, want %v", tt.mantissa, tt.shift, tt.sign, tt.mode, got, tt.shouldRound)
}
})
}
}
func TestParse(t *testing.T) {
_, err := Parse("1.0")
if err == nil {
t.Error("Expected error, got nil")
}
}