1
exercism fetch go prime-factors

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
44
45
46
47
48
package prime

// Source: exercism/problem-specifications
// Commit: bf5bb2b prime-factors: Fix canonical-data.json formatting
// Problem Specifications Version: 1.0.0

var tests = []struct {
	description string
	input       int64
	expected    []int64
}{

	{
		"no factors",
		1,
		[]int64{},
	},
	{
		"prime number",
		2,
		[]int64{2},
	},
	{
		"square of a prime",
		9,
		[]int64{3, 3},
	},
	{
		"cube of a prime",
		8,
		[]int64{2, 2, 2},
	},
	{
		"product of primes and non-primes",
		12,
		[]int64{2, 2, 3},
	},
	{
		"product of primes",
		901255,
		[]int64{5, 17, 23, 461},
	},
	{
		"factors include a large prime",
		93819012551,
		[]int64{11, 9539, 894119},
	},
}

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

// Return prime factors in increasing order

import (
	"reflect"
	"testing"
)

func TestPrimeFactors(t *testing.T) {
	for _, test := range tests {
		actual := Factors(test.input)
		if !reflect.DeepEqual(actual, test.expected) {
			t.Fatalf("FAIL %s\nFactors(%d) = %#v;\nexpected %#v",
				test.description, test.input,
				actual, test.expected)
		}
		t.Logf("PASS %s", test.description)
	}
}

func BenchmarkPrimeFactors(b *testing.B) {
	for i := 0; i < b.N; i++ {
		for _, test := range tests {
			Factors(test.input)
		}
	}
}