61 lines
1.5 KiB
Go
61 lines
1.5 KiB
Go
|
|
package libgofunc
|
||
|
|
|
||
|
|
import (
|
||
|
|
"math"
|
||
|
|
"testing"
|
||
|
|
)
|
||
|
|
|
||
|
|
func TestDiv(t *testing.T) {
|
||
|
|
tests := []struct {
|
||
|
|
name string
|
||
|
|
a any
|
||
|
|
b any
|
||
|
|
want float64
|
||
|
|
}{
|
||
|
|
{"Float division", 10.5, 2.0, 5.25},
|
||
|
|
{"Float division", 4975, 1000, 4.975},
|
||
|
|
{"Integer division", 10, 4, 2.5},
|
||
|
|
{"Mixed types", int64(100), float64(4.0), 25.0},
|
||
|
|
{"Division by zero (float)", 10.0, 0.0, 0.0},
|
||
|
|
{"Division by zero (int)", 5, 0, 0.0},
|
||
|
|
{"Unsupported type defaults to zero", "string", 2, 0.0},
|
||
|
|
}
|
||
|
|
|
||
|
|
for _, tt := range tests {
|
||
|
|
t.Run(tt.name, func(t *testing.T) {
|
||
|
|
got := Div(tt.a, tt.b)
|
||
|
|
if got != tt.want {
|
||
|
|
t.Errorf("TemplateDiv() = %v, want %v", got, tt.want)
|
||
|
|
}
|
||
|
|
})
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestFormatNumber(t *testing.T) {
|
||
|
|
tests := []struct {
|
||
|
|
name string
|
||
|
|
precision int
|
||
|
|
val any
|
||
|
|
want string
|
||
|
|
}{
|
||
|
|
{"Standard float with commas", 2, 1234567.891, "1,234,567.89"},
|
||
|
|
{"Standard float with commas", 3, 4.975, "4.975"},
|
||
|
|
{"Integer input with commas", 0, 5000000, "5,000,000"},
|
||
|
|
{"Integer input forced decimals", 2, 5000, "5,000.00"},
|
||
|
|
{"Negative float commas", 2, -9876543.21, "-9,876,543.21"},
|
||
|
|
{"Small float rounding up", 2, 0.128, "0.13"},
|
||
|
|
{"Small float rounding down", 2, 0.123, "0.12"},
|
||
|
|
{"Handling NaN", 2, math.NaN(), ""},
|
||
|
|
{"Handling Inf", 2, math.Inf(1), ""},
|
||
|
|
}
|
||
|
|
|
||
|
|
for _, tt := range tests {
|
||
|
|
t.Run(tt.name, func(t *testing.T) {
|
||
|
|
got := FormatNumber(tt.precision, tt.val)
|
||
|
|
if got != tt.want {
|
||
|
|
t.Errorf("TemplateFloat() = %q, want %q", got, tt.want)
|
||
|
|
}
|
||
|
|
})
|
||
|
|
}
|
||
|
|
}
|