mirror of
https://github.com/redis/go-redis.git
synced 2025-06-02 08:21:36 +03:00
* utils: expose ParseFloat via new public utils package * add tests for special float values in vector search
41 lines
780 B
Go
41 lines
780 B
Go
package util
|
|
|
|
import (
|
|
"math"
|
|
"testing"
|
|
)
|
|
|
|
func TestParseStringToFloat(t *testing.T) {
|
|
tests := []struct {
|
|
in string
|
|
want float64
|
|
ok bool
|
|
}{
|
|
{"1.23", 1.23, true},
|
|
{"inf", math.Inf(1), true},
|
|
{"-inf", math.Inf(-1), true},
|
|
{"nan", math.NaN(), true},
|
|
{"oops", 0, false},
|
|
}
|
|
|
|
for _, tc := range tests {
|
|
got, err := ParseStringToFloat(tc.in)
|
|
if tc.ok {
|
|
if err != nil {
|
|
t.Fatalf("ParseFloat(%q) error: %v", tc.in, err)
|
|
}
|
|
if math.IsNaN(tc.want) {
|
|
if !math.IsNaN(got) {
|
|
t.Errorf("ParseFloat(%q) = %v; want NaN", tc.in, got)
|
|
}
|
|
} else if got != tc.want {
|
|
t.Errorf("ParseFloat(%q) = %v; want %v", tc.in, got, tc.want)
|
|
}
|
|
} else {
|
|
if err == nil {
|
|
t.Errorf("ParseFloat(%q) expected error, got nil", tc.in)
|
|
}
|
|
}
|
|
}
|
|
}
|