summaryrefslogtreecommitdiff
path: root/go/grains/grains_test.go
blob: e37cdab780d4ebda8cfc789a3a7d3cb3a9e63e78 (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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package grains

import (
	"testing"
)

var squareTests = []struct {
	input       int
	expectedVal uint64
	expectError bool
}{
	{1, 1, false},
	{2, 2, false},
	{3, 4, false},
	{4, 8, false},
	{16, 32768, false},
	{32, 2147483648, false},
	{64, 9223372036854775808, false},
	{65, 0, true},
	{0, 0, true},
	{-1, 0, true},
}

func TestSquare(t *testing.T) {
	for _, test := range squareTests {
		actualVal, actualErr := Square(test.input)
		if actualVal != test.expectedVal {
			t.Errorf("Square(%d) expected %d, Actual %d", test.input, test.expectedVal, actualVal)
		}

		// if we expect an error and there isn't one
		if test.expectError && actualErr == nil {
			t.Errorf("Square(%d) expected an error, but error is nil", test.input)
		}
		// if we don't expect an error and there is one
		if !test.expectError && actualErr != nil {
			t.Errorf("Square(%d) expected no error, but error is: %s", test.input, actualErr)
		}
	}
}

func TestTotal(t *testing.T) {
	var expected uint64 = 18446744073709551615
	if actual := Total(); actual != expected {
		t.Errorf("Total() expected %d, Actual %d", expected, actual)
	}
}

func BenchmarkSquare(b *testing.B) {
	b.StopTimer()

	for _, test := range squareTests {
		b.StartTimer()

		for i := 0; i < b.N; i++ {
			Square(test.input)
		}

		b.StopTimer()
	}
}