34 lines
616 B
Go
34 lines
616 B
Go
package number
|
|
|
|
import (
|
|
"fmt"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestFixed(t *testing.T) {
|
|
for _, tc := range []struct {
|
|
precision int
|
|
source, expected float64
|
|
}{
|
|
{3, 0, 0},
|
|
{3, -1, -1},
|
|
{3, 1.5, 1.5},
|
|
{3, 2.25, 2.25},
|
|
{3, 3.275, 3.275},
|
|
{3, 4.0004, 4.000},
|
|
{5, 5.000005, 5.00001},
|
|
{4, -6.00004, -6.},
|
|
{4, -6.00005, -6.0001},
|
|
} {
|
|
t.Run(fmt.Sprintf("%f", tc.source), func(t *testing.T) {
|
|
if tc.precision == 3 {
|
|
assert.Equal(t, tc.expected, Fixed3(tc.source))
|
|
} else {
|
|
assert.Equal(t, tc.expected, fixed(tc.source, tc.precision))
|
|
}
|
|
})
|
|
}
|
|
}
|