1
exercism fetch go nth-prime

cases_test.go

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
package prime

// Source: exercism/problem-specifications
// Commit: e82cbcd nth-prime: Use object instead of bool for invalid value (#969)
// Problem Specifications Version: 2.0.0

var tests = []struct {
	description string
	n           int
	p           int
	ok          bool
}{
	{
		"first prime",
		1,
		2,
		true,
	},
	{
		"second prime",
		2,
		3,
		true,
	},
	{
		"sixth prime",
		6,
		13,
		true,
	},
	{
		"big prime",
		10001,
		104743,
		true,
	},
	{
		"there is no zeroth prime",
		0,
		0,
		false,
	},
}

nth_prime_test.go

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
package prime

import "testing"

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

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