package float import "testing" func cmp(a, b []string) bool { if len(a) != len(b) { return false } for i := 0; i < len(a); i++ { if a[i] != b[i] { return false } } return true } func TestSplit(t *testing.T) { testCases := []struct { s string v []string }{ {"", []string{}}, {"1", []string{"1"}}, {"12", []string{"12"}}, {"123", []string{"123"}}, {"1234", []string{"1", "234"}}, {"12345", []string{"12", "345"}}, {"123456", []string{"123", "456"}}, {"1234567", []string{"1", "234", "567"}}, } for _, tc := range testCases { t.Run(tc.s, func(t *testing.T) { v := split(tc.s, 3) if !cmp(v, tc.v) { t.Errorf("got %q, want %q", v, tc.v) } }) } } func TestFill(t *testing.T) { testCases := []struct { s string v string }{ {"", "00"}, {"1", "10"}, {"01", "01"}, {"123", "12"}, } for _, tc := range testCases { t.Run(tc.s, func(t *testing.T) { v := fill(tc.s, 2) if v != tc.v { t.Errorf("got %q, want %q", v, tc.v) } }) } } func TestFormat(t *testing.T) { testCases := []struct { f float64 s string }{ {0.0, "0,00"}, {1.0, "1,00"}, {1000.0, "1 000,00"}, {10000.0, "10 000,00"}, {10000.10, "10 000,10"}, {1234567.89, "1 234 567,89"}, {-1234567.89, "-1 234 567,89"}, {-1000.0, "-1 000,00"}, {-100.0, "-100,00"}, {-10.0, "-10,00"}, {1.0 / 3, "0,33"}, {0.555, "0,55"}, } for _, tc := range testCases { t.Run(tc.s, func(t *testing.T) { s := Format(tc.f) if s != tc.s { t.Errorf("got %q, want %q", s, tc.s) } }) } } func BenchmarkFormat(b *testing.B) { for i := 0; i < b.N; i++ { Format(1234567.89) } } func TestCountry(t *testing.T) { f := 1234567.89 testCases := []struct { f Country v string }{ {EN, "1 234 567.89"}, {FR, "1 234 567,89"}, {US, "1,234,567.89"}, {DE, "1.234.567,89"}, {IR, "1 234 567·89"}, {CH, "1'234'567.89"}, {IT, "1˙234˙567,89"}, {CN, "123,4567.89"}, } for _, tc := range testCases { t.Run(tc.v, func(t *testing.T) { v := tc.f.Format(f) if v != tc.v { t.Errorf("got %q, want %q", v, tc.v) } }) } }