summaryrefslogtreecommitdiff
path: root/go/nth-prime/nth_prime_test.go
blob: 03f23e80778d79722c919a43bd9eeac1d925d848 (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
package prime

import "testing"

var tests = []struct {
	n  int
	p  int
	ok bool
}{
	{1, 2, true},
	{2, 3, true},
	{3, 5, true},
	{4, 7, true},
	{5, 11, true},
	{6, 13, true},
	{10001, 104743, true},
	{0, 0, false},
}

func TestNth(t *testing.T) {
	for _, test := range tests {
		switch p, ok := Nth(test.n); {
		case !ok:
			if test.ok {
				t.Fatalf("Nth(%d) returned !ok.  Expecting ok.", test.n)
			}
		case !test.ok:
			t.Fatalf("Nth(%d) = %d, ok = %t.  Expecting !ok.", test.n, p, ok)
		case p != test.p:
			t.Fatalf("Nth(%d) = %d, want %d.", test.n, p, test.p)
		}
	}
}

func BenchmarkNth(b *testing.B) {
	for i := 0; i < b.N; i++ {
		Nth(10001)
	}
}