Skip to content

Commit fa68d69

Browse files
committed
Fix staticcheck issues
- Simplify variable declaration in SumSlice function (QF1011) - Apply De Morgan's law in TestAddSlice (QF1001) - Fix negative zero literal usage in convert_test.go (SA4026) - Add nil check in TestDefaultConfig to prevent nil pointer dereference (SA5011) - Remove unnecessary uint8 < 0 comparisons in test files (SA4003)
1 parent 246f9ba commit fa68d69

4 files changed

Lines changed: 67 additions & 64 deletions

File tree

arithmetic.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ func Less(a, b Float8) bool {
373373
// Handle infinities
374374
aInf := a.IsInf()
375375
bInf := b.IsInf()
376-
376+
377377
if aInf && bInf {
378378
// Both are infinities, compare signs
379379
// -Inf < +Inf is true, +Inf < -Inf is false, same infinities are equal
@@ -565,7 +565,7 @@ func ScaleSlice(s []Float8, scalar Float8) []Float8 {
565565
// s := []Float8{1.0, 2.0, 3.0, 4.0}
566566
// sum := SumSlice(s) // Returns 10.0
567567
func SumSlice(s []Float8) Float8 {
568-
var sum Float8 = PositiveZero
568+
sum := PositiveZero
569569
for _, v := range s {
570570
sum = Add(sum, v)
571571
}

arithmetic_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ func TestLess(t *testing.T) {
110110
// If a < b is true, then b < a should be false and vice versa
111111
result2 := Less(tt.b, tt.a)
112112
if result && result2 {
113-
t.Errorf("Both Less(%v, %v) and Less(%v, %v) returned true",
113+
t.Errorf("Both Less(%v, %v) and Less(%v, %v) returned true",
114114
tt.a, tt.b, tt.b, tt.a)
115115
}
116116
}
@@ -174,7 +174,7 @@ func TestAddSlice(t *testing.T) {
174174
}
175175

176176
for i := range result {
177-
if !(result[i] == tt.expected[i] || (result[i].IsNaN() && tt.expected[i].IsNaN())) {
177+
if result[i] != tt.expected[i] && !(result[i].IsNaN() && tt.expected[i].IsNaN()) {
178178
t.Errorf("At index %d: expected %v, got %v", i, tt.expected[i], result[i])
179179
}
180180
}
@@ -234,17 +234,17 @@ func TestDivisionEdgeCases(t *testing.T) {
234234
{"negative inf / negative inf", NegativeInfinity, NegativeInfinity, NaN},
235235
{"positive inf / negative inf", PositiveInfinity, NegativeInfinity, NaN},
236236
{"negative inf / positive inf", NegativeInfinity, PositiveInfinity, NaN},
237-
237+
238238
// Test finite / infinity = 0 with proper sign
239239
{"positive / positive inf", FromInt(5), PositiveInfinity, PositiveZero},
240240
{"negative / positive inf", FromInt(-5), PositiveInfinity, NegativeZero},
241241
{"positive / negative inf", FromInt(5), NegativeInfinity, NegativeZero},
242242
{"negative / negative inf", FromInt(-5), NegativeInfinity, PositiveZero},
243-
243+
244244
// Test overflow cases that result in infinity (using max values)
245245
{"max positive / small positive", ToFloat8(448.0), ToFloat8(0.0625), PositiveInfinity},
246246
{"max negative / small positive", ToFloat8(-448.0), ToFloat8(0.0625), NegativeInfinity},
247-
247+
248248
// Test cases that trigger overflow detection in float32 division
249249
{"large positive / tiny positive", ToFloat8(240.0), ToFloat8(0.0078125), PositiveInfinity},
250250
{"large negative / tiny positive", ToFloat8(-240.0), ToFloat8(0.0078125), NegativeInfinity},

convert_test.go

Lines changed: 39 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ import (
88

99
func TestToFloat8WithMode(t *testing.T) {
1010
tests := []struct {
11-
name string
12-
input float32
13-
mode ConversionMode
14-
want Float8
15-
hasErr bool
16-
errMsg string
11+
name string
12+
input float32
13+
mode ConversionMode
14+
want Float8
15+
hasErr bool
16+
errMsg string
1717
}{
1818
{
1919
name: "zero",
@@ -44,12 +44,12 @@ func TestToFloat8WithMode(t *testing.T) {
4444
hasErr: false,
4545
},
4646
{
47-
name: "NaN in strict mode",
48-
input: float32(math.NaN()),
49-
mode: ModeStrict,
50-
want: 0,
51-
hasErr: true,
52-
errMsg: "NaN not representable in float8",
47+
name: "NaN in strict mode",
48+
input: float32(math.NaN()),
49+
mode: ModeStrict,
50+
want: 0,
51+
hasErr: true,
52+
errMsg: "NaN not representable in float8",
5353
},
5454
{
5555
name: "NaN in default mode",
@@ -66,12 +66,12 @@ func TestToFloat8WithMode(t *testing.T) {
6666
hasErr: false,
6767
},
6868
{
69-
name: "overflow in strict mode",
69+
name: "overflow in strict mode",
7070
input: math.MaxFloat32,
7171
mode: ModeStrict,
72-
want: 0,
73-
hasErr: true,
74-
errMsg: "overflow: value too large for float8",
72+
want: 0,
73+
hasErr: true,
74+
errMsg: "overflow: value too large for float8",
7575
},
7676
{
7777
name: "underflow in default mode",
@@ -81,12 +81,12 @@ func TestToFloat8WithMode(t *testing.T) {
8181
hasErr: false,
8282
},
8383
{
84-
name: "underflow in strict mode",
84+
name: "underflow in strict mode",
8585
input: math.SmallestNonzeroFloat32,
8686
mode: ModeStrict,
87-
want: 0,
88-
hasErr: true,
89-
errMsg: "underflow: value too small for float8",
87+
want: 0,
88+
hasErr: true,
89+
errMsg: "underflow: value too small for float8",
9090
},
9191
{
9292
name: "denormal number",
@@ -135,7 +135,7 @@ func TestToFloat8WithMode(t *testing.T) {
135135
for _, tt := range tests {
136136
t.Run(tt.name, func(t *testing.T) {
137137
got, err := ToFloat8WithMode(tt.input, tt.mode)
138-
138+
139139
if tt.hasErr {
140140
if err == nil {
141141
t.Fatal("expected error, got nil")
@@ -160,7 +160,7 @@ func TestToFloat8WithMode(t *testing.T) {
160160
func TestConversionTable(t *testing.T) {
161161
// First disable any existing conversion table
162162
DisableFastConversion()
163-
163+
164164
// Test that the table is nil initially
165165
if conversionTable != nil {
166166
t.Error("conversionTable should be nil initially")
@@ -169,31 +169,31 @@ func TestConversionTable(t *testing.T) {
169169
// Test EnableFastConversion
170170
t.Run("EnableFastConversion", func(t *testing.T) {
171171
EnableFastConversion()
172-
172+
173173
if conversionTable == nil {
174174
t.Error("conversionTable should be initialized after EnableFastConversion")
175175
}
176-
176+
177177
if len(conversionTable) != 256 {
178178
t.Errorf("conversionTable length = %d, want 256", len(conversionTable))
179179
}
180-
180+
181181
// Test a few values to ensure the table is populated correctly
182182
testValues := []struct {
183183
input int
184184
output float32
185185
skip bool // Skip comparison for values that might vary
186186
}{
187-
{0x00, 0.0, false}, // +0.0
188-
{0x80, -0.0, false}, // -0.0
189-
{0x3F, 1.0, true}, // 1.0 (approximate in float8)
190-
{0xBF, -1.0, true}, // -1.0 (approximate in float8)
187+
{0x00, 0.0, false}, // +0.0
188+
{0x80, float32(math.Copysign(0, -1)), false}, // -0.0
189+
{0x3F, 1.0, true}, // 1.0 (approximate in float8)
190+
{0xBF, -1.0, true}, // -1.0 (approximate in float8)
191191
{0x78, float32(math.Inf(1)), false}, // +Inf (IEEE 754 E4M3FN)
192192
{0xF8, float32(math.Inf(-1)), false}, // -Inf (IEEE 754 E4M3FN)
193193
{0x7F, float32(math.NaN()), false}, // NaN (IEEE 754 E4M3FN)
194194
{0xFF, float32(math.NaN()), false}, // NaN (IEEE 754 E4M3FN)
195195
}
196-
196+
197197
for _, tv := range testValues {
198198
if tv.skip {
199199
continue // Skip values that are approximations
@@ -208,7 +208,7 @@ func TestConversionTable(t *testing.T) {
208208
// Test DisableFastConversion
209209
t.Run("DisableFastConversion", func(t *testing.T) {
210210
DisableFastConversion()
211-
211+
212212
if conversionTable != nil {
213213
t.Error("conversionTable should be nil after DisableFastConversion")
214214
}
@@ -221,7 +221,7 @@ func TestParse(t *testing.T) {
221221
if err == nil {
222222
t.Fatal("expected error from Parse, got nil")
223223
}
224-
224+
225225
expectedErr := "float8.parse: not implemented"
226226
if err.Error() != expectedErr {
227227
t.Errorf("unexpected error message: got %q, want %q", err.Error(), expectedErr)
@@ -236,7 +236,7 @@ func TestToSlice32EdgeCases(t *testing.T) {
236236
if result != nil {
237237
t.Errorf("ToSlice32([]) = %v, want nil", result)
238238
}
239-
239+
240240
// Test non-empty slice
241241
nonEmptySlice := []Float8{One(), FromInt(2), PositiveZero}
242242
result2 := ToSlice32(nonEmptySlice)
@@ -254,12 +254,12 @@ func TestToSlice32EdgeCases(t *testing.T) {
254254
// TestToFloat8WithModeEdgeCases tests edge cases in ToFloat8WithMode to achieve 100% coverage
255255
func TestToFloat8WithModeEdgeCases(t *testing.T) {
256256
tests := []struct {
257-
name string
258-
input float32
259-
mode ConversionMode
260-
want Float8
261-
hasErr bool
262-
errMsg string
257+
name string
258+
input float32
259+
mode ConversionMode
260+
want Float8
261+
hasErr bool
262+
errMsg string
263263
}{
264264
// Test overflow in strict mode
265265
{
@@ -309,7 +309,6 @@ func TestToFloat8WithModeEdgeCases(t *testing.T) {
309309
want: NegativeZero,
310310
hasErr: false,
311311
},
312-
313312
}
314313

315314
for _, tt := range tests {

float8_core_test.go

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,27 +24,31 @@ func TestDefaultConfig(t *testing.T) {
2424
}
2525

2626
// Verify default values
27-
if config.EnableFastArithmetic {
28-
t.Error("Expected EnableFastArithmetic to be false by default")
29-
}
30-
if config.EnableFastConversion {
31-
t.Error("Expected EnableFastConversion to be false by default")
32-
}
33-
if config.DefaultMode != ModeDefault {
34-
t.Errorf("Expected DefaultMode to be ModeDefault, got %v", config.DefaultMode)
35-
}
36-
if config.ArithmeticMode != ArithmeticAuto {
37-
t.Errorf("Expected ArithmeticMode to be ArithmeticAuto, got %v", config.ArithmeticMode)
27+
if config != nil {
28+
if config.EnableFastArithmetic {
29+
t.Error("Expected EnableFastArithmetic to be false by default")
30+
}
31+
if config.EnableFastConversion {
32+
t.Error("Expected EnableFastConversion to be false by default")
33+
}
34+
if config.DefaultMode != ModeDefault {
35+
t.Errorf("Expected DefaultMode to be ModeDefault, got %v", config.DefaultMode)
36+
}
37+
if config.ArithmeticMode != ArithmeticAuto {
38+
t.Errorf("Expected ArithmeticMode to be ArithmeticAuto, got %v", config.ArithmeticMode)
39+
}
40+
} else {
41+
t.Error("DefaultConfig() returned nil")
3842
}
3943
}
4044

4145
func TestGetMemoryUsage(t *testing.T) {
4246
// Save the current configuration to restore it later
4347
origConfig := DefaultConfig()
44-
48+
4549
// Restore the original configuration after the test
4650
defer Configure(origConfig)
47-
51+
4852
tests := []struct {
4953
name string
5054
config *Config
@@ -88,10 +92,10 @@ func TestGetMemoryUsage(t *testing.T) {
8892
t.Run(tt.name, func(t *testing.T) {
8993
// Apply the test configuration
9094
Configure(tt.config)
91-
95+
9296
// Get the memory usage
9397
memUsage := GetMemoryUsage()
94-
98+
9599
// Verify the memory usage matches expectations
96100
if memUsage != tt.expectedMemory {
97101
t.Errorf("GetMemoryUsage() = %d, want %d", memUsage, tt.expectedMemory)
@@ -105,7 +109,7 @@ func TestGetVersion(t *testing.T) {
105109
if version == "" {
106110
t.Error("Empty version string")
107111
}
108-
112+
109113
// Version should be in format X.Y.Z
110114
if version != "2.0.0" {
111115
t.Errorf("Unexpected version: %s", version)
@@ -243,7 +247,7 @@ func TestDebugInfo(t *testing.T) {
243247
if info == nil {
244248
t.Error("DebugInfo() returned nil")
245249
}
246-
250+
247251
// Check for version key which should always be present
248252
if _, exists := info["version"]; !exists {
249253
t.Error("DebugInfo() missing version key")

0 commit comments

Comments
 (0)