-
-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathTreeBuildingTests.vb
More file actions
284 lines (248 loc) · 7.22 KB
/
Copy pathTreeBuildingTests.vb
File metadata and controls
284 lines (248 loc) · 7.22 KB
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
Public Class TreeBuildingTests
<Fact>
Public Sub One_node()
Dim records = {New TreeBuildingRecord With {
.RecordId = 0,
.ParentId = 0
}}
Dim tree = BuildTree(records)
AssertTreeIsLeaf(tree, id:=0)
End Sub
<Fact(Skip:="Remove this Skip property to run this test")>
Public Sub Three_nodes_in_order()
Dim records = {New TreeBuildingRecord With {
.RecordId = 0,
.ParentId = 0
}, New TreeBuildingRecord With {
.RecordId = 1,
.ParentId = 0
}, New TreeBuildingRecord With {
.RecordId = 2,
.ParentId = 0
}}
Dim tree = BuildTree(records)
AssertTreeIsBranch(tree, id:=0, childCount:=2)
AssertTreeIsLeaf(tree.Children(0), id:=1)
AssertTreeIsLeaf(tree.Children(1), id:=2)
End Sub
<Fact(Skip:="Remove this Skip property to run this test")>
Public Sub Three_nodes_in_reverse_order()
Dim records = {New TreeBuildingRecord With {
.RecordId = 2,
.ParentId = 0
}, New TreeBuildingRecord With {
.RecordId = 1,
.ParentId = 0
}, New TreeBuildingRecord With {
.RecordId = 0,
.ParentId = 0
}}
Dim tree = BuildTree(records)
AssertTreeIsBranch(tree, id:=0, childCount:=2)
AssertTreeIsLeaf(tree.Children(0), id:=1)
AssertTreeIsLeaf(tree.Children(1), id:=2)
End Sub
<Fact(Skip:="Remove this Skip property to run this test")>
Public Sub More_than_two_children()
Dim records = {New TreeBuildingRecord With {
.RecordId = 3,
.ParentId = 0
}, New TreeBuildingRecord With {
.RecordId = 2,
.ParentId = 0
}, New TreeBuildingRecord With {
.RecordId = 1,
.ParentId = 0
}, New TreeBuildingRecord With {
.RecordId = 0,
.ParentId = 0
}}
Dim tree = BuildTree(records)
AssertTreeIsBranch(tree, id:=0, childCount:=3)
AssertTreeIsLeaf(tree.Children(0), id:=1)
AssertTreeIsLeaf(tree.Children(1), id:=2)
AssertTreeIsLeaf(tree.Children(2), id:=3)
End Sub
<Fact(Skip:="Remove this Skip property to run this test")>
Public Sub Binary_tree()
Dim records = {New TreeBuildingRecord With {
.RecordId = 5,
.ParentId = 1
}, New TreeBuildingRecord With {
.RecordId = 3,
.ParentId = 2
}, New TreeBuildingRecord With {
.RecordId = 2,
.ParentId = 0
}, New TreeBuildingRecord With {
.RecordId = 4,
.ParentId = 1
}, New TreeBuildingRecord With {
.RecordId = 1,
.ParentId = 0
}, New TreeBuildingRecord With {
.RecordId = 0,
.ParentId = 0
}, New TreeBuildingRecord With {
.RecordId = 6,
.ParentId = 2
}}
Dim tree = BuildTree(records)
AssertTreeIsBranch(tree, id:=0, childCount:=2)
AssertTreeIsBranch(tree.Children(0), id:=1, childCount:=2)
AssertTreeIsBranch(tree.Children(1), id:=2, childCount:=2)
AssertTreeIsLeaf(tree.Children(0).Children(0), id:=4)
AssertTreeIsLeaf(tree.Children(0).Children(1), id:=5)
AssertTreeIsLeaf(tree.Children(1).Children(0), id:=3)
AssertTreeIsLeaf(tree.Children(1).Children(1), id:=6)
End Sub
<Fact(Skip:="Remove this Skip property to run this test")>
Public Sub Unbalanced_tree()
Dim records = {New TreeBuildingRecord With {
.RecordId = 5,
.ParentId = 2
}, New TreeBuildingRecord With {
.RecordId = 3,
.ParentId = 2
}, New TreeBuildingRecord With {
.RecordId = 2,
.ParentId = 0
}, New TreeBuildingRecord With {
.RecordId = 4,
.ParentId = 1
}, New TreeBuildingRecord With {
.RecordId = 1,
.ParentId = 0
}, New TreeBuildingRecord With {
.RecordId = 0,
.ParentId = 0
}, New TreeBuildingRecord With {
.RecordId = 6,
.ParentId = 2
}}
Dim tree = BuildTree(records)
AssertTreeIsBranch(tree, id:=0, childCount:=2)
AssertTreeIsBranch(tree.Children(0), id:=1, childCount:=1)
AssertTreeIsBranch(tree.Children(1), id:=2, childCount:=3)
AssertTreeIsLeaf(tree.Children(0).Children(0), id:=4)
AssertTreeIsLeaf(tree.Children(1).Children(0), id:=3)
AssertTreeIsLeaf(tree.Children(1).Children(1), id:=5)
AssertTreeIsLeaf(tree.Children(1).Children(2), id:=6)
End Sub
<Fact(Skip:="Remove this Skip property to run this test")>
Public Sub Empty_input()
Dim records = New TreeBuildingRecord(-1) {}
Assert.Throws(Of ArgumentException)(Function() BuildTree(records))
End Sub
<Fact(Skip:="Remove this Skip property to run this test")>
Public Sub Root_node_has_parent()
Dim records = {New TreeBuildingRecord With {
.RecordId = 0,
.ParentId = 1
}, New TreeBuildingRecord With {
.RecordId = 1,
.ParentId = 0
}}
Assert.Throws(Of ArgumentException)(Function() BuildTree(records))
End Sub
<Fact(Skip:="Remove this Skip property to run this test")>
Public Sub No_root_node()
Dim records = {New TreeBuildingRecord With {
.RecordId = 1,
.ParentId = 0
}}
Assert.Throws(Of ArgumentException)(Function() BuildTree(records))
End Sub
<Fact(Skip:="Remove this Skip property to run this test")>
Public Sub Non_continuous()
Dim records = {New TreeBuildingRecord With {
.RecordId = 2,
.ParentId = 0
}, New TreeBuildingRecord With {
.RecordId = 4,
.ParentId = 2
}, New TreeBuildingRecord With {
.RecordId = 1,
.ParentId = 0
}, New TreeBuildingRecord With {
.RecordId = 0,
.ParentId = 0
}}
Assert.Throws(Of ArgumentException)(Function() BuildTree(records))
End Sub
<Fact(Skip:="Remove this Skip property to run this test")>
Public Sub Cycle_directly()
Dim records = {New TreeBuildingRecord With {
.RecordId = 5,
.ParentId = 2
}, New TreeBuildingRecord With {
.RecordId = 3,
.ParentId = 2
}, New TreeBuildingRecord With {
.RecordId = 2,
.ParentId = 2
}, New TreeBuildingRecord With {
.RecordId = 4,
.ParentId = 1
}, New TreeBuildingRecord With {
.RecordId = 1,
.ParentId = 0
}, New TreeBuildingRecord With {
.RecordId = 0,
.ParentId = 0
}, New TreeBuildingRecord With {
.RecordId = 6,
.ParentId = 3
}}
Assert.Throws(Of ArgumentException)(Function() BuildTree(records))
End Sub
<Fact(Skip:="Remove this Skip property to run this test")>
Public Sub Cycle_indirectly()
Dim records = {New TreeBuildingRecord With {
.RecordId = 5,
.ParentId = 2
}, New TreeBuildingRecord With {
.RecordId = 3,
.ParentId = 2
}, New TreeBuildingRecord With {
.RecordId = 2,
.ParentId = 6
}, New TreeBuildingRecord With {
.RecordId = 4,
.ParentId = 1
}, New TreeBuildingRecord With {
.RecordId = 1,
.ParentId = 0
}, New TreeBuildingRecord With {
.RecordId = 0,
.ParentId = 0
}, New TreeBuildingRecord With {
.RecordId = 6,
.ParentId = 3
}}
Assert.Throws(Of ArgumentException)(Function() BuildTree(records))
End Sub
<Fact(Skip:="Remove this Skip property to run this test")>
Public Sub Higher_id_parent_of_lower_id()
Dim records = {New TreeBuildingRecord With {
.RecordId = 0,
.ParentId = 0
}, New TreeBuildingRecord With {
.RecordId = 2,
.ParentId = 0
}, New TreeBuildingRecord With {
.RecordId = 1,
.ParentId = 2
}}
Assert.Throws(Of ArgumentException)(Function() BuildTree(records))
End Sub
Private Sub AssertTreeIsBranch(ByVal tree As Tree, ByVal id As Integer, ByVal childCount As Integer)
Assert.Equal(id, tree.Id)
Assert.False(tree.IsLeaf)
Assert.Equal(childCount, tree.Children.Count)
End Sub
Private Sub AssertTreeIsLeaf(ByVal tree As Tree, ByVal id As Integer)
Assert.Equal(id, tree.Id)
Assert.True(tree.IsLeaf)
End Sub
End Class