forked from exercism/java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPovTest.java
More file actions
248 lines (212 loc) · 9.44 KB
/
PovTest.java
File metadata and controls
248 lines (212 loc) · 9.44 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
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
public class PovTest {
@Test
@DisplayName("Results in the same tree if the input tree is a singleton")
public void testFromPovGivenSingletonTree() {
Tree tree = Tree.of("x");
Tree expected = Tree.of("x");
assertThat(tree.fromPov("x")).isEqualTo(expected);
}
@Disabled("Remove to run test")
@Test
@DisplayName("Can reroot a tree with a parent and one sibling")
public void testFromPovGivenTreeWithParentAndOneSibling() {
Tree tree = Tree.of("parent",
List.of(Tree.of("x"),
Tree.of("sibling")));
Tree expected = Tree.of("x",
List.of(Tree.of("parent",
List.of(Tree.of("sibling")))));
assertThat(tree.fromPov("x")).isEqualTo(expected);
}
@Disabled("Remove to run test")
@Test
@DisplayName("Can reroot a tree with a parent and many siblings")
public void testFromPovGivenTreeWithParentAndManySibling() {
Tree tree = Tree.of("parent",
List.of(Tree.of("x"),
Tree.of("a"),
Tree.of("b"),
Tree.of("c")));
Tree expected = Tree.of("x",
List.of(Tree.of("parent",
List.of(Tree.of("a"),
Tree.of("b"),
Tree.of("c")))));
assertThat(tree.fromPov("x")).isEqualTo(expected);
}
@Disabled("Remove to run test")
@Test
@DisplayName("an reroot a tree with new root deeply nested in the tree")
public void testFromPovGivenTreeWithNewRootDeeplyNested() {
Tree tree = Tree.of("level-0",
List.of(Tree.of("level-1",
List.of(Tree.of("level-2",
List.of(Tree.of("level-3",
List.of(Tree.of("x")))))))));
Tree expected = Tree.of("x",
List.of(Tree.of("level-3",
List.of(Tree.of("level-2",
List.of(Tree.of("level-1",
List.of(Tree.of("level-0")))))))));
assertThat(tree.fromPov("x")).isEqualTo(expected);
}
@Disabled("Remove to run test")
@Test
@DisplayName("Moves children of the new root to same level as former parent")
public void testFromPovGivenMovesChildrenOfNewRootToSameLevelAsFormerParent() {
Tree tree = Tree.of("parent",
List.of(Tree.of("x",
List.of(Tree.of("kid-0"),
Tree.of("kid-1")))));
Tree expected = Tree.of("x",
List.of(Tree.of("kid-0"),
Tree.of("kid-1"),
Tree.of("parent")));
assertThat(tree.fromPov("x")).isEqualTo(expected);
}
@Disabled("Remove to run test")
@Test
@DisplayName("Can reroot a complex tree with cousins")
public void testFromPovGivenComplexTreeWithCousins() {
Tree tree = Tree.of("grandparent",
List.of(Tree.of("parent",
List.of(Tree.of("x",
List.of(Tree.of("kid-0"),
Tree.of("kid-1"))),
Tree.of("sibling-0"),
Tree.of("sibling-1"))),
Tree.of("uncle",
List.of(Tree.of("cousin-0"),
Tree.of("cousin-1")))));
Tree expected = Tree.of("x",
List.of(Tree.of("kid-1"),
Tree.of("kid-0"),
Tree.of("parent",
List.of(Tree.of("sibling-0"),
Tree.of("sibling-1"),
Tree.of("grandparent",
List.of(Tree.of("uncle",
List.of(Tree.of("cousin-0"),
Tree.of("cousin-1")))))))));
assertThat(tree.fromPov("x")).isEqualTo(expected);
}
@Disabled("Remove to run test")
@Test
@DisplayName("Errors if target does not exist in a singleton tree")
public void testFromPovGivenNonExistentTargetInSingletonTree() {
Tree tree = Tree.of("x");
assertThatExceptionOfType(UnsupportedOperationException.class)
.isThrownBy(() -> tree.fromPov("nonexistent"))
.withMessage("Tree could not be reoriented");
}
@Disabled("Remove to run test")
@Test
@DisplayName("Errors if target does not exist in a larger tree")
public void testFromPovGivenNonExistentTargetInLargeTree() {
Tree tree = Tree.of("parent",
List.of(Tree.of("x",
List.of(Tree.of("kid-0"),
Tree.of("kid-1"))),
Tree.of("sibling-0"),
Tree.of("sibling-1")));
assertThatExceptionOfType(UnsupportedOperationException.class)
.isThrownBy(() -> tree.fromPov("nonexistent"))
.withMessage("Tree could not be reoriented");
}
@Disabled("Remove to run test")
@Test
@DisplayName("Can find path to parent")
public void testPathToCanFindPathToParent() {
Tree tree = Tree.of("parent",
List.of(Tree.of("x"),
Tree.of("sibling")));
List<String> expected = List.of("x", "parent");
assertThat(tree.pathTo("x", "parent")).isEqualTo(expected);
}
@Disabled("Remove to run test")
@Test
public void testPathToCanFindPathToSibling() {
Tree tree = Tree.of("parent",
List.of(Tree.of("a"),
Tree.of("x"),
Tree.of("b"),
Tree.of("c")));
List<String> expected = List.of("x", "parent", "b");
assertThat(tree.pathTo("x", "b")).isEqualTo(expected);
}
@Disabled("Remove to run test")
@Test
@DisplayName("Can find path to cousin")
public void testPathToCanFindPathToCousin() {
Tree tree = Tree.of("grandparent",
List.of(Tree.of("parent",
List.of(Tree.of("x",
List.of(Tree.of("kid-0"),
Tree.of("kid-1"))),
Tree.of("sibling-0"),
Tree.of("sibling-1"))),
Tree.of("uncle",
List.of(Tree.of("cousin-0"),
Tree.of("cousin-1")))));
List<String> expected = List.of("x", "parent", "grandparent", "uncle", "cousin-1");
assertThat(tree.pathTo("x", "cousin-1")).isEqualTo(expected);
}
@Disabled("Remove to run test")
@Test
@DisplayName("Can find path not involving root")
public void testPathToCanFindPathNotEnvolvingRoot() {
Tree tree = Tree.of("grandparent",
List.of(Tree.of("parent",
List.of(Tree.of("x"),
Tree.of("sibling-0"),
Tree.of("sibling-1")))));
List<String> expected = List.of("x", "parent", "sibling-1");
assertThat(tree.pathTo("x", "sibling-1")).isEqualTo(expected);
}
@Disabled("Remove to run test")
@Test
@DisplayName("Can find path from nodes other than x")
public void testPathToCanFindPathFromNodesOtherThanX() {
Tree tree = Tree.of("parent",
List.of(Tree.of("a"),
Tree.of("x"),
Tree.of("b"),
Tree.of("c")));
List<String> expected = List.of("a", "parent", "c");
assertThat(tree.pathTo("a", "c")).isEqualTo(expected);
}
@Disabled("Remove to run test")
@Test
@DisplayName("Errors if destination does not exist")
public void testPathWhenDestinationDoesNotExist() {
Tree tree = Tree.of("parent",
List.of(Tree.of("x",
List.of(Tree.of("kid-0"),
Tree.of("kid-1"))),
Tree.of("sibling-0"),
Tree.of("sibling-1")));
assertThatExceptionOfType(UnsupportedOperationException.class)
.isThrownBy(() -> tree.pathTo("x", "nonexistent"))
.withMessage("No path found");
}
@Disabled("Remove to run test")
@Test
@DisplayName("Errors if source does not exist")
public void testPathWhenSourceDoesNotExist() {
Tree tree = Tree.of("parent",
List.of(Tree.of("x",
List.of(Tree.of("kid-0"),
Tree.of("kid-1"))),
Tree.of("sibling-0"),
Tree.of("sibling-1")));
assertThatExceptionOfType(UnsupportedOperationException.class)
.isThrownBy(() -> tree.pathTo("nonexistent", "x"))
.withMessage("No path found");
}
}