Add lib.AbsInt() helper function

This commit is contained in:
Sean Chittenden 2016-03-30 11:47:37 -07:00
parent 12f1a71f00
commit 9f74944f71
2 changed files with 24 additions and 8 deletions

View File

@ -1,5 +1,12 @@
package lib package lib
func AbsInt(a int) int {
if a > 0 {
return a
}
return a * -1
}
func MaxInt(a, b int) int { func MaxInt(a, b int) int {
if a > b { if a > b {
return a return a

View File

@ -6,24 +6,33 @@ import (
"github.com/hashicorp/consul/lib" "github.com/hashicorp/consul/lib"
) )
func TestMathAbsInt(t *testing.T) {
tests := [][3]int{{1, 1}, {-1, 1}, {0, 0}}
for _, test := range tests {
if test[1] != lib.AbsInt(test[0]) {
t.Fatalf("expected %d, got %d", test[1], test[0])
}
}
}
func TestMathMaxInt(t *testing.T) { func TestMathMaxInt(t *testing.T) {
tests := [][3]int{{1, 2, 2}, {-1, 1, 1}, {2, 0, 2}} tests := [][3]int{{1, 2, 2}, {-1, 1, 1}, {2, 0, 2}}
for i, _ := range tests { for _, test := range tests {
expected := tests[i][2] expected := test[2]
actual := lib.MaxInt(tests[i][0], tests[i][1]) actual := lib.MaxInt(test[0], test[1])
if expected != actual { if expected != actual {
t.Fatalf("in iteration %d expected %d, got %d", i, expected, actual) t.Fatalf("expected %d, got %d", expected, actual)
} }
} }
} }
func TestMathMinInt(t *testing.T) { func TestMathMinInt(t *testing.T) {
tests := [][3]int{{1, 2, 1}, {-1, 1, -1}, {2, 0, 0}} tests := [][3]int{{1, 2, 1}, {-1, 1, -1}, {2, 0, 0}}
for i, _ := range tests { for _, test := range tests {
expected := tests[i][2] expected := test[2]
actual := lib.MinInt(tests[i][0], tests[i][1]) actual := lib.MinInt(test[0], test[1])
if expected != actual { if expected != actual {
t.Fatalf("in iteration %d expected %d, got %d", i, expected, actual) t.Fatalf("expected %d, got %d", expected, actual)
} }
} }
} }