summaryrefslogtreecommitdiff
path: root/round_test.go
blob: 13a90517347235a7092695864d82ba7a2e70b267 (plain)
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
package float

import (
	"fmt"
	"testing"
)

func TestRound(t *testing.T) {
	testCases := []struct {
		in, out float64
	}{
		{0.0, 0.0},
		{0.3, 0.3},
		{0.333, 0.33},
		{0.334, 0.33},
		{0.335, 0.34},
		{-0.333, -0.33},
		{-0.334, -0.33},
		{-0.335, -0.34},
		{495.17999999999995, 495.18},
		{-495.17999999999995, -495.18},
		{0.115, 0.12},
		{-0.115, -0.12},
	}
	for _, tc := range testCases {
		t.Run(fmt.Sprint(tc.in), func(t *testing.T) {
			if r := Round(tc.in, 2); r != tc.out {
				t.Errorf("got %v, want %v", r, tc.out)
			}
		})
	}
}

func BenchmarkRound(b *testing.B) {
	for i := 0; i < b.N; i++ {
		Round(0.333, 2)
	}
}