-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfloat16_extra_test.go
More file actions
283 lines (251 loc) · 7.02 KB
/
float16_extra_test.go
File metadata and controls
283 lines (251 loc) · 7.02 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
package float16
import (
"testing"
)
func TestGetVersion(t *testing.T) {
version := GetVersion()
if version == "" {
t.Error("Expected version string, got empty string")
}
}
func TestZero(t *testing.T) {
if Zero() != PositiveZero {
t.Error("Zero() should return PositiveZero")
}
}
func TestOne(t *testing.T) {
if One() != 0x3C00 {
t.Error("One() should return 0x3C00")
}
}
func TestInf(t *testing.T) {
if Inf(1) != PositiveInfinity {
t.Error("Inf(1) should return PositiveInfinity")
}
if Inf(-1) != NegativeInfinity {
t.Error("Inf(-1) should return NegativeInfinity")
}
}
func TestIsInf(t *testing.T) {
if !PositiveInfinity.IsInf(1) {
t.Error("PositiveInfinity.IsInf(1) should be true")
}
if !NegativeInfinity.IsInf(-1) {
t.Error("NegativeInfinity.IsInf(-1) should be true")
}
if PositiveInfinity.IsInf(-1) {
t.Error("PositiveInfinity.IsInf(-1) should be false")
}
if !PositiveInfinity.IsInf(0) {
t.Error("PositiveInfinity.IsInf(0) should be true")
}
// Test IsInf from float16.go
if !IsInf(PositiveInfinity, 1) {
t.Error("IsInf(PositiveInfinity, 1) should be true")
}
if !IsInf(NegativeInfinity, -1) {
t.Error("IsInf(NegativeInfinity, -1) should be true")
}
if IsInf(PositiveInfinity, -1) {
t.Error("IsInf(PositiveInfinity, -1) should be false")
}
if !IsInf(PositiveInfinity, 0) {
t.Error("IsInf(PositiveInfinity, 0) should be true")
}
}
func TestIsNaN(t *testing.T) {
if !QuietNaN.IsNaN() {
t.Error("QuietNaN.IsNaN() should be true")
}
if !SignalingNaN.IsNaN() {
t.Error("SignalingNaN.IsNaN() should be true")
}
if PositiveZero.IsNaN() {
t.Error("PositiveZero.IsNaN() should be false")
}
// Test IsNaN from float16.go
if !IsNaN(QuietNaN) {
t.Error("IsNaN(QuietNaN) should be true")
}
if IsNaN(PositiveZero) {
t.Error("IsNaN(PositiveZero) should be false")
}
}
func TestSignbit(t *testing.T) {
if PositiveZero.Signbit() {
t.Error("PositiveZero.Signbit() should be false")
}
if NegativeZero.Signbit() == false {
t.Error("NegativeZero.Signbit() should be true")
}
// Test Signbit from float16.go
if Signbit(PositiveZero) {
t.Error("Signbit(PositiveZero) should be false")
}
if !Signbit(NegativeZero) {
t.Error("Signbit(NegativeZero) should be true")
}
}
func TestIsFinite(t *testing.T) {
if !PositiveZero.IsFinite() {
t.Error("PositiveZero.IsFinite() should be true")
}
if PositiveInfinity.IsFinite() {
t.Error("PositiveInfinity.IsFinite() should be false")
}
if QuietNaN.IsFinite() {
t.Error("QuietNaN.IsFinite() should be false")
}
// Test IsFinite from float16.go
if !IsFinite(PositiveZero) {
t.Error("IsFinite(PositiveZero) should be true")
}
if IsFinite(PositiveInfinity) {
t.Error("IsFinite(PositiveInfinity) should be false")
}
if IsFinite(QuietNaN) {
t.Error("IsFinite(QuietNaN) should be false")
}
}
func TestIsNormal(t *testing.T) {
if !One().IsNormal() {
t.Error("One().IsNormal() should be true")
}
if PositiveZero.IsNormal() {
t.Error("PositiveZero.IsNormal() should be false")
}
if SmallestSubnormal.IsNormal() {
t.Error("SmallestSubnormal.IsNormal() should be false")
}
// Test IsNormal from float16.go
if !IsNormal(One()) {
t.Error("IsNormal(One()) should be true")
}
if IsNormal(PositiveZero) {
t.Error("IsNormal(PositiveZero) should be false")
}
if IsNormal(SmallestSubnormal) {
t.Error("IsNormal(SmallestSubnormal) should be false")
}
}
func TestIsSubnormal(t *testing.T) {
if !SmallestSubnormal.IsSubnormal() {
t.Error("SmallestSubnormal.IsSubnormal() should be true")
}
if One().IsSubnormal() {
t.Error("One().IsSubnormal() should be false")
}
// Test IsSubnormal from float16.go
if !IsSubnormal(SmallestSubnormal) {
t.Error("IsSubnormal(SmallestSubnormal) should be true")
}
if IsSubnormal(One()) {
t.Error("IsSubnormal(One()) should be false")
}
}
func TestFpClassify(t *testing.T) {
if FpClassify(One()) != ClassPositiveNormal {
t.Error("FpClassify(One()) should be ClassPositiveNormal")
}
if FpClassify(PositiveZero) != ClassPositiveZero {
t.Error("FpClassify(PositiveZero) should be ClassPositiveZero")
}
if FpClassify(SmallestSubnormal) != ClassPositiveSubnormal {
t.Error("FpClassify(SmallestSubnormal) should be ClassPositiveSubnormal")
}
if FpClassify(PositiveInfinity) != ClassPositiveInfinity {
t.Error("FpClassify(PositiveInfinity) should be ClassPositiveInfinity")
}
if FpClassify(QuietNaN) != ClassQuietNaN {
t.Error("FpClassify(QuietNaN) should be ClassQuietNaN")
}
}
func TestGetBenchmarkOperations(t *testing.T) {
ops := GetBenchmarkOperations()
if len(ops) == 0 {
t.Error("Expected benchmark operations, got empty slice")
}
}
func TestValidateSliceLength(t *testing.T) {
tests := []struct {
name string
a, b []Float16
expectError bool
}{
{"equal lengths", []Float16{1}, []Float16{2}, false},
{"unequal lengths", []Float16{1}, []Float16{2, 3}, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := ValidateSliceLength(tt.a, tt.b)
if (err != nil) != tt.expectError {
t.Errorf("ValidateSliceLength() error = %v, expectError %v", err, tt.expectError)
}
})
}
}
func TestFastAdd(t *testing.T) {
a := One()
b := One()
result := FastAdd(a, b)
if result != 0x4000 {
t.Errorf("FastAdd(1, 1) = %v, want 2", result)
}
}
func TestFastMul(t *testing.T) {
a := FromInt(2)
b := FromInt(3)
result := FastMul(a, b)
if result != 0x4600 {
t.Errorf("FastMul(2, 3) = %v, want 6", result)
}
}
func TestVectorAdd(t *testing.T) {
a := []Float16{One(), One()}
b := []Float16{One(), One()}
result := VectorAdd(a, b)
if result[0] != 0x4000 || result[1] != 0x4000 {
t.Errorf("VectorAdd([1, 1], [1, 1]) = %v, want [2, 2]", result)
}
}
func TestVectorMul(t *testing.T) {
a := []Float16{FromInt(2), FromInt(3)}
b := []Float16{FromInt(3), FromInt(4)}
result := VectorMul(a, b)
if !Equal(result[0], 0x4600) || !Equal(result[1], 0x4A00) {
t.Errorf("VectorMul([2, 3], [3, 4]) = [%v (0x%04X), %v (0x%04X)], want [6 (0x4600), 12 (0x4A00)]", result[0], uint16(result[0]), result[1], uint16(result[1]))
}
}
func TestToSlice16(t *testing.T) {
input := []float32{0.0, 1.0, 2.0, -1.0}
expected := []Float16{PositiveZero, ToFloat16(1.0), ToFloat16(2.0), ToFloat16(-1.0)}
result := ToSlice16(input)
if len(result) != len(expected) {
t.Fatalf("Length mismatch: got %d, expected %d", len(result), len(expected))
}
for i := range result {
if result[i] != expected[i] {
t.Errorf("ToSlice16[%d] = 0x%04x, expected 0x%04x", i, result[i], expected[i])
}
}
}
func TestToSlice32(t *testing.T) {
input := []Float16{PositiveZero, ToFloat16(1.0), ToFloat16(2.0), ToFloat16(-1.0)}
expected := []float32{0.0, 1.0, 2.0, -1.0}
result := ToSlice32(input)
if len(result) != len(expected) {
t.Fatalf("Length mismatch: got %d, expected %d", len(result), len(expected))
}
for i := range result {
if result[i] != expected[i] {
t.Errorf("ToSlice32[%d] = %g, expected %g", i, result[i], expected[i])
}
}
}
func TestToSlice32Empty(t *testing.T) {
input := []Float16{}
result := ToSlice32(input)
if len(result) != 0 {
t.Errorf("Expected empty slice, got %v", result)
}
}