Refactored detection logic to include ports and a confidence score
This commit is contained in:
215
protocol/detest_test.go
Normal file
215
protocol/detest_test.go
Normal file
@@ -0,0 +1,215 @@
|
||||
package protocol
|
||||
|
||||
import (
|
||||
"math"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestCompareFloats(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
a, b float64
|
||||
expected int
|
||||
}{
|
||||
// Basic comparisons
|
||||
{
|
||||
name: "a less than b",
|
||||
a: 1.0,
|
||||
b: 2.0,
|
||||
expected: -1,
|
||||
},
|
||||
{
|
||||
name: "a greater than b",
|
||||
a: 2.0,
|
||||
b: 1.0,
|
||||
expected: 1,
|
||||
},
|
||||
{
|
||||
name: "a equals b exact",
|
||||
a: 1.0,
|
||||
b: 1.0,
|
||||
expected: 0,
|
||||
},
|
||||
|
||||
// Floating-point precision cases
|
||||
{
|
||||
name: "famous 0.1 + 0.2 equals 0.3 within tolerance",
|
||||
a: 0.1 + 0.2,
|
||||
b: 0.3,
|
||||
expected: 0,
|
||||
},
|
||||
{
|
||||
name: "very close numbers within tolerance",
|
||||
a: 1.0000000001,
|
||||
b: 1.0000000002,
|
||||
expected: 0,
|
||||
},
|
||||
{
|
||||
name: "numbers outside tolerance a < b",
|
||||
a: 1.0,
|
||||
b: 1.0001,
|
||||
expected: -1,
|
||||
},
|
||||
{
|
||||
name: "numbers outside tolerance a > b",
|
||||
a: 1.0001,
|
||||
b: 1.0,
|
||||
expected: 1,
|
||||
},
|
||||
|
||||
// Edge cases with very small numbers
|
||||
{
|
||||
name: "very small numbers equal",
|
||||
a: 1e-20,
|
||||
b: 1e-20,
|
||||
expected: 0,
|
||||
},
|
||||
{
|
||||
name: "very small numbers a < b",
|
||||
a: 1e-15,
|
||||
b: 2e-15,
|
||||
expected: -1,
|
||||
},
|
||||
|
||||
// Zero and negative zero
|
||||
{
|
||||
name: "zero equals zero",
|
||||
a: 0.0,
|
||||
b: 0.0,
|
||||
expected: 0,
|
||||
},
|
||||
{
|
||||
name: "zero equals negative zero",
|
||||
a: 0.0,
|
||||
b: -0.0,
|
||||
expected: 0,
|
||||
},
|
||||
{
|
||||
name: "zero less than small positive",
|
||||
a: 0.0,
|
||||
b: 1e-20,
|
||||
expected: -1,
|
||||
},
|
||||
{
|
||||
name: "zero greater than small negative",
|
||||
a: 0.0,
|
||||
b: -1e-20,
|
||||
expected: 1,
|
||||
},
|
||||
|
||||
// Negative numbers
|
||||
{
|
||||
name: "negative numbers a > b",
|
||||
a: -1.0,
|
||||
b: -2.0,
|
||||
expected: 1,
|
||||
},
|
||||
{
|
||||
name: "negative numbers a < b",
|
||||
a: -2.0,
|
||||
b: -1.0,
|
||||
expected: -1,
|
||||
},
|
||||
{
|
||||
name: "negative numbers equal",
|
||||
a: -1.0,
|
||||
b: -1.0,
|
||||
expected: 0,
|
||||
},
|
||||
|
||||
// Mixed signs
|
||||
{
|
||||
name: "negative less than positive",
|
||||
a: -1.0,
|
||||
b: 1.0,
|
||||
expected: -1,
|
||||
},
|
||||
{
|
||||
name: "positive greater than negative",
|
||||
a: 1.0,
|
||||
b: -1.0,
|
||||
expected: 1,
|
||||
},
|
||||
|
||||
// Special values: NaN
|
||||
{
|
||||
name: "NaN equals NaN",
|
||||
a: math.NaN(),
|
||||
b: math.NaN(),
|
||||
expected: 0,
|
||||
},
|
||||
{
|
||||
name: "NaN less than number",
|
||||
a: math.NaN(),
|
||||
b: 1.0,
|
||||
expected: -1,
|
||||
},
|
||||
{
|
||||
name: "number greater than NaN",
|
||||
a: 1.0,
|
||||
b: math.NaN(),
|
||||
expected: 1,
|
||||
},
|
||||
|
||||
// Special values: Infinity
|
||||
{
|
||||
name: "positive infinity equals positive infinity",
|
||||
a: math.Inf(1),
|
||||
b: math.Inf(1),
|
||||
expected: 0,
|
||||
},
|
||||
{
|
||||
name: "negative infinity equals negative infinity",
|
||||
a: math.Inf(-1),
|
||||
b: math.Inf(-1),
|
||||
expected: 0,
|
||||
},
|
||||
{
|
||||
name: "positive infinity greater than negative infinity",
|
||||
a: math.Inf(1),
|
||||
b: math.Inf(-1),
|
||||
expected: 1,
|
||||
},
|
||||
{
|
||||
name: "negative infinity less than positive infinity",
|
||||
a: math.Inf(-1),
|
||||
b: math.Inf(1),
|
||||
expected: -1,
|
||||
},
|
||||
{
|
||||
name: "positive infinity greater than large number",
|
||||
a: math.Inf(1),
|
||||
b: 1e308,
|
||||
expected: 1,
|
||||
},
|
||||
{
|
||||
name: "negative infinity less than small number",
|
||||
a: math.Inf(-1),
|
||||
b: -1e308,
|
||||
expected: -1,
|
||||
},
|
||||
|
||||
// Large numbers
|
||||
{
|
||||
name: "large numbers equal",
|
||||
a: 1e15,
|
||||
b: 1e15,
|
||||
expected: 0,
|
||||
},
|
||||
{
|
||||
name: "large numbers a < b",
|
||||
a: 1e15,
|
||||
b: 2e15,
|
||||
expected: -1,
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
result := compareFloats(test.a, test.b)
|
||||
if result != test.expected {
|
||||
t.Errorf("compareFloats(%g, %g) = %d, want %d", test.a, test.b, result, test.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user