-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfloat_test.go
59 lines (52 loc) · 1.16 KB
/
float_test.go
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
package g
import (
"testing"
)
func TestStringToFloat(t *testing.T) {
tests := []struct {
input string
expected float64
def float64
err bool
}{
{"3.14", 3.14, 0, false},
{"abc", 1.23, 1.23, true},
{"", 1.23, 1.23, true},
{"", 0, 0, true},
}
for _, test := range tests {
result, err := StringToFloat(test.input, test.def)
if (err != nil) != test.err {
t.Errorf("StringToFloat(%q) error = %v, wantErr %v", test.input, err, test.err)
continue
}
if result != test.expected {
t.Errorf("StringToFloat(%q) = %f, want %f", test.input, result, test.expected)
}
}
}
func TestFloatToString(t *testing.T) {
tests := []struct {
input any // Using 'any' to handle multiple types
expected string
}{
{3.14, "3.14"},
{float32(3.14), "3.14"},
{float64(3.14), "3.14"},
}
for _, test := range tests {
var result string
switch v := test.input.(type) {
case float32:
result = FloatToString(v)
case float64:
result = FloatToString(v)
default:
t.Errorf("Unsupported type %T", v)
continue
}
if result != test.expected {
t.Errorf("FloatToString(%v) = %q, want %q", test.input, result, test.expected)
}
}
}