1
exercism fetch go pov

pov_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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
package pov

import (
	"reflect"
	"sort"
	"testing"
)

// POV / reparent / change root of a tree
//
// API:
// type Graph
// func New() *Graph
// func (*Graph) AddNode(nodeLabel string)
// func (*Graph) AddArc(from, to string)
// func (*Graph) ArcList() []string
// func (*Graph) ChangeRoot(oldRoot, newRoot string) *Graph
//
// The type name is Graph because you'll probably be implementing a general
// directed graph representation, although the test program will only use
// it to create a tree.  The term "arc" is used here to mean a directed edge.
//
// The test program will create a graph with New, then use AddNode to add
// leaf nodes.  After that it will use AddArc to construct the rest of the tree
// from the bottom up.  That is, the `to` argument will always specify a node
// that has already been added.
//
// ArcList is a dump method to let the test program see your graph.  It must
// return a list of all arcs in the graph.  Format each arc as a single string
// like "from -> to". The test program can then easily sort the list and
// compare it to an expected result.  You do not need to bother with sorting
// the list yourself.
//
// All this graph construction and dumping must be working before you start
// on the interesting part of the exercise, so it is tested separately as
// a first test.
//
// API function ChangeRoot does the interesting part of the exercise.
// OldRoot is passed (as a convenience) and you must return a graph with
// newRoot as the root.  You can modify the original graph in place and
// return it or create a new graph and return that.  If you return a new
// graph you are free to consume or destroy the original graph.  Of course
// it's nice to leave it unmodified.

type arc struct{ fr, to string }

type testCase struct {
	description string
	leaves      []string
	arcPairs    []arc
	root        string
	arcStrings  []string
	reRooted    []string
}

var testCases = []testCase{
	{
		description: "singleton",
		leaves:      []string{"x"},
		arcPairs:    nil,
		root:        "x",
		arcStrings:  nil,
		reRooted:    nil,
	},
	{
		description: "simple tree",
		leaves:      []string{"sibling", "x"},
		arcPairs: []arc{
			{"parent", "sibling"},
			{"parent", "x"},
		},
		root: "parent",
		arcStrings: []string{
			"parent -> sibling",
			"parent -> x",
		},
		reRooted: []string{
			"parent -> sibling",
			"x -> parent",
		},
	},
	{
		description: "large flat",
		leaves:      []string{"sib-a", "sib-b", "x", "sib-c", "sib-d"},
		arcPairs: []arc{
			{"parent", "sib-a"},
			{"parent", "sib-b"},
			{"parent", "x"},
			{"parent", "sib-c"},
			{"parent", "sib-d"},
		},
		root: "parent",
		arcStrings: []string{
			"parent -> sib-a",
			"parent -> sib-b",
			"parent -> sib-c",
			"parent -> sib-d",
			"parent -> x",
		},
		reRooted: []string{
			"parent -> sib-a",
			"parent -> sib-b",
			"parent -> sib-c",
			"parent -> sib-d",
			"x -> parent",
		},
	},
	{
		description: "deeply nested",
		leaves:      []string{"x"},
		arcPairs: []arc{
			{"level-4", "x"},
			{"level-3", "level-4"},
			{"level-2", "level-3"},
			{"level-1", "level-2"},
			{"level-0", "level-1"},
		},
		root: "level-0",
		arcStrings: []string{
			"level-0 -> level-1",
			"level-1 -> level-2",
			"level-2 -> level-3",
			"level-3 -> level-4",
			"level-4 -> x",
		},
		reRooted: []string{
			"level-1 -> level-0",
			"level-2 -> level-1",
			"level-3 -> level-2",
			"level-4 -> level-3",
			"x -> level-4",
		},
	},
	{
		description: "cousins",
		leaves:      []string{"sib-1", "x", "sib-2", "cousin-1", "cousin-2"},
		arcPairs: []arc{
			{"parent", "sib-1"},
			{"parent", "x"},
			{"parent", "sib-2"},
			{"aunt", "cousin-1"},
			{"aunt", "cousin-2"},
			{"grand-parent", "parent"},
			{"grand-parent", "aunt"},
		},
		root: "grand-parent",
		arcStrings: []string{
			"aunt -> cousin-1",
			"aunt -> cousin-2",
			"grand-parent -> aunt",
			"grand-parent -> parent",
			"parent -> sib-1",
			"parent -> sib-2",
			"parent -> x",
		},
		reRooted: []string{
			"aunt -> cousin-1",
			"aunt -> cousin-2",
			"grand-parent -> aunt",
			"parent -> grand-parent",
			"parent -> sib-1",
			"parent -> sib-2",
			"x -> parent",
		},
	},
	{
		description: "target with children",
		leaves: []string{"child-1", "child-2", "nephew", "niece",
			"2nd-cousin-1", "2nd-cousin-2", "2nd-cousin-3", "2nd-cousin-4"},
		arcPairs: []arc{
			{"x", "child-1"},
			{"x", "child-2"},
			{"sibling", "nephew"},
			{"sibling", "niece"},
			{"cousin-1", "2nd-cousin-1"},
			{"cousin-1", "2nd-cousin-2"},
			{"cousin-2", "2nd-cousin-3"},
			{"cousin-2", "2nd-cousin-4"},
			{"parent", "x"},
			{"parent", "sibling"},
			{"aunt", "cousin-1"},
			{"aunt", "cousin-2"},
			{"grand-parent", "parent"},
			{"grand-parent", "aunt"},
		},
		root: "grand-parent",
		arcStrings: []string{
			"aunt -> cousin-1",
			"aunt -> cousin-2",
			"cousin-1 -> 2nd-cousin-1",
			"cousin-1 -> 2nd-cousin-2",
			"cousin-2 -> 2nd-cousin-3",
			"cousin-2 -> 2nd-cousin-4",
			"grand-parent -> aunt",
			"grand-parent -> parent",
			"parent -> sibling",
			"parent -> x",
			"sibling -> nephew",
			"sibling -> niece",
			"x -> child-1",
			"x -> child-2",
		},
		reRooted: []string{
			"aunt -> cousin-1",
			"aunt -> cousin-2",
			"cousin-1 -> 2nd-cousin-1",
			"cousin-1 -> 2nd-cousin-2",
			"cousin-2 -> 2nd-cousin-3",
			"cousin-2 -> 2nd-cousin-4",
			"grand-parent -> aunt",
			"parent -> grand-parent",
			"parent -> sibling",
			"sibling -> nephew",
			"sibling -> niece",
			"x -> child-1",
			"x -> child-2",
			"x -> parent",
		},
	},
}

func (tc testCase) graph() *Graph {
	g := New()
	for _, l := range tc.leaves {
		g.AddNode(l)
	}
	for _, a := range tc.arcPairs {
		g.AddArc(a.fr, a.to)
	}
	return g
}

func (tc testCase) testResult(got, want []string, msg string, t *testing.T) {
	if len(got)+len(want) == 0 {
		return
	}
	gs := append([]string{}, got...)
	sort.Strings(gs)
	if reflect.DeepEqual(gs, want) {
		return
	}
	// test has failed
	t.Log(tc.description, "test case")
	t.Log(msg)
	t.Logf("got %d arcs:", len(got))
	for _, s := range got {
		t.Log(" ", s)
	}
	t.Logf("that result sorted:")
	for _, s := range gs {
		t.Log(" ", s)
	}
	t.Logf("want %d arcs:", len(want))
	for _, s := range want {
		t.Log(" ", s)
	}
	t.FailNow()
}

func TestConstruction(t *testing.T) {
	for _, tc := range testCases {
		got := tc.graph().ArcList()
		want := tc.arcStrings
		tc.testResult(got, want, "incorrect graph construction", t)
	}
}

func TestChangeRoot(t *testing.T) {
	for _, tc := range testCases {
		got := tc.graph().ChangeRoot(tc.root, "x").ArcList()
		want := tc.reRooted
		tc.testResult(got, want, "incorrect root change", t)
	}
}

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

func BenchmarkConstructAndChangeRoot(b *testing.B) {
	for i := 0; i < b.N; i++ {
		for _, tc := range testCases {
			tc.graph().ChangeRoot(tc.root, "x")
		}
	}
}