1
exercism fetch go sublist

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
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package sublist

// Source: exercism/problem-specifications
// Commit: 71e24b5 sublist: Fix canonical-data.json formatting
// Problem Specifications Version: 1.0.0

var testCases = []struct {
	description string
	listOne     []int
	listTwo     []int
	expected    Relation
}{
	{
		description: "empty lists",
		listOne:     []int{},
		listTwo:     []int{},
		expected:    "equal",
	},
	{
		description: "empty list within non empty list",
		listOne:     []int{},
		listTwo:     []int{1, 2, 3},
		expected:    "sublist",
	},
	{
		description: "non empty list contains empty list",
		listOne:     []int{1, 2, 3},
		listTwo:     []int{},
		expected:    "superlist",
	},
	{
		description: "list equals itself",
		listOne:     []int{1, 2, 3},
		listTwo:     []int{1, 2, 3},
		expected:    "equal",
	},
	{
		description: "different lists",
		listOne:     []int{1, 2, 3},
		listTwo:     []int{2, 3, 4},
		expected:    "unequal",
	},
	{
		description: "false start",
		listOne:     []int{1, 2, 5},
		listTwo:     []int{0, 1, 2, 3, 1, 2, 5, 6},
		expected:    "sublist",
	},
	{
		description: "consecutive",
		listOne:     []int{1, 1, 2},
		listTwo:     []int{0, 1, 1, 1, 2, 1, 2},
		expected:    "sublist",
	},
	{
		description: "sublist at start",
		listOne:     []int{0, 1, 2},
		listTwo:     []int{0, 1, 2, 3, 4, 5},
		expected:    "sublist",
	},
	{
		description: "sublist in middle",
		listOne:     []int{2, 3, 4},
		listTwo:     []int{0, 1, 2, 3, 4, 5},
		expected:    "sublist",
	},
	{
		description: "sublist at end",
		listOne:     []int{3, 4, 5},
		listTwo:     []int{0, 1, 2, 3, 4, 5},
		expected:    "sublist",
	},
	{
		description: "at start of superlist",
		listOne:     []int{0, 1, 2, 3, 4, 5},
		listTwo:     []int{0, 1, 2},
		expected:    "superlist",
	},
	{
		description: "in middle of superlist",
		listOne:     []int{0, 1, 2, 3, 4, 5},
		listTwo:     []int{2, 3},
		expected:    "superlist",
	},
	{
		description: "at end of superlist",
		listOne:     []int{0, 1, 2, 3, 4, 5},
		listTwo:     []int{3, 4, 5},
		expected:    "superlist",
	},
	{
		description: "first list missing element from second list",
		listOne:     []int{1, 3},
		listTwo:     []int{1, 2, 3},
		expected:    "unequal",
	},
	{
		description: "second list missing element from first list",
		listOne:     []int{1, 2, 3},
		listTwo:     []int{1, 3},
		expected:    "unequal",
	},
	{
		description: "order matters to a list",
		listOne:     []int{1, 2, 3},
		listTwo:     []int{3, 2, 1},
		expected:    "unequal",
	},
	{
		description: "same digits but different numbers",
		listOne:     []int{1, 0, 1},
		listTwo:     []int{10, 1},
		expected:    "unequal",
	},
}

sublist_test.go

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package sublist

import (
	"testing"
)

func TestSublist(t *testing.T) {
	for _, tc := range testCases {
		if actual := Sublist(tc.listOne, tc.listTwo); actual != tc.expected {
			t.Fatalf("FAIL: %s\nExpected: %#v\nActual: %#v", tc.description, tc.expected, actual)
		}
		t.Logf("PASS: %s", tc.description)
	}
}

func BenchmarkSublist(b *testing.B) {
	for i := 0; i < b.N; i++ {
		for _, tc := range testCases {
			Sublist(tc.listOne, tc.listTwo)
		}
	}
}