summaryrefslogtreecommitdiff
path: root/go/pythagorean-triplet/pythagorean_triplet_test.go
blob: 0af626a6e21a0c56eae68b3639d2a84b6f674635 (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
62
63
64
65
66
67
68
69
70
71
72
73
package pythagorean

// Use this type definition,
//
//    type Triplet [3]int
//
// and implement two functions,
//
//    Range(min, max int) []Triplet
//    Sum(p int) []Triplet
//
// Range returns a list of all Pythagorean triplets with sides in the
// range min to max inclusive.
//
// Sum returns a list of all Pythagorean triplets where the sum a+b+c
// (the perimeter) is equal to p.
//
// The three elements of each returned triplet must be in order,
// t[0] <= t[1] <= t[2], and the list of triplets must be in lexicographic
// order.

import (
	"reflect"
	"testing"
)

var rangeTests = []struct {
	min, max int
	ts       []Triplet
}{
	{1, 10, []Triplet{{3, 4, 5}, {6, 8, 10}}},
	{11, 20, []Triplet{{12, 16, 20}}},
}

func TestRange(t *testing.T) {
	for _, test := range rangeTests {
		ts := Range(test.min, test.max)
		if !reflect.DeepEqual(ts, test.ts) {
			t.Fatalf("Range(%d, %d) = %v, want %v",
				test.min, test.max, ts, test.ts)
		}
	}
}

var sumTests = []struct {
	sum int
	ts  []Triplet
}{
	{180, []Triplet{{18, 80, 82}, {30, 72, 78}, {45, 60, 75}}},
	{1000, []Triplet{{200, 375, 425}}},
}

func TestSum(t *testing.T) {
	for _, test := range sumTests {
		ts := Sum(test.sum)
		if !reflect.DeepEqual(ts, test.ts) {
			t.Fatalf("Sum(%d) = %v, want %v",
				test.sum, ts, test.ts)
		}
	}
}

func BenchmarkRange(b *testing.B) {
	for i := 0; i < b.N; i++ {
		Range(1, 100)
	}
}

func BenchmarkSum(b *testing.B) {
	for i := 0; i < b.N; i++ {
		Sum(1000)
	}
}