...
1 package util
2
3 import (
4 "math"
5 "testing"
6 )
7
8 func TestParseStringToFloat(t *testing.T) {
9 tests := []struct {
10 in string
11 want float64
12 ok bool
13 }{
14 {"1.23", 1.23, true},
15 {"inf", math.Inf(1), true},
16 {"-inf", math.Inf(-1), true},
17 {"nan", math.NaN(), true},
18 {"oops", 0, false},
19 }
20
21 for _, tc := range tests {
22 got, err := ParseStringToFloat(tc.in)
23 if tc.ok {
24 if err != nil {
25 t.Fatalf("ParseFloat(%q) error: %v", tc.in, err)
26 }
27 if math.IsNaN(tc.want) {
28 if !math.IsNaN(got) {
29 t.Errorf("ParseFloat(%q) = %v; want NaN", tc.in, got)
30 }
31 } else if got != tc.want {
32 t.Errorf("ParseFloat(%q) = %v; want %v", tc.in, got, tc.want)
33 }
34 } else {
35 if err == nil {
36 t.Errorf("ParseFloat(%q) expected error, got nil", tc.in)
37 }
38 }
39 }
40 }
41
View as plain text