|
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+import org.junit.Ignore;
|
|
|
2
|
+import org.junit.Test;
|
|
|
3
|
+import org.junit.Before;
|
|
|
4
|
+
|
|
|
5
|
+import static org.junit.Assert.assertEquals;
|
|
|
6
|
+
|
|
|
7
|
+public class ZipperTest {
|
|
|
8
|
+
|
|
|
9
|
+ Zipper zipper;
|
|
|
10
|
+ BinaryTree binaryTree;
|
|
|
11
|
+
|
|
|
12
|
+ @Before
|
|
|
13
|
+ public void setup() {
|
|
|
14
|
+ zipper = new Zipper(1);
|
|
|
15
|
+ binaryTree = new BinaryTree(zipper);
|
|
|
16
|
+
|
|
|
17
|
+ zipper.setLeft(new Zipper(2));
|
|
|
18
|
+ zipper.left.setRight(new Zipper(3));
|
|
|
19
|
+
|
|
|
20
|
+ zipper.setRight(new Zipper(4));
|
|
|
21
|
+ }
|
|
|
22
|
+
|
|
|
23
|
+ @Test
|
|
|
24
|
+ public void testToTree() {
|
|
|
25
|
+ assertEquals(binaryTree, zipper.toTree());
|
|
|
26
|
+ }
|
|
|
27
|
+
|
|
|
28
|
+ @Ignore("Remove to run test")
|
|
|
29
|
+ @Test
|
|
|
30
|
+ public void testLeftRightAndValue() {
|
|
|
31
|
+ zipper = binaryTree.getRoot();
|
|
|
32
|
+ assertEquals(3, zipper.left.right.getValue());
|
|
|
33
|
+ }
|
|
|
34
|
+
|
|
|
35
|
+ @Ignore("Remove to run test")
|
|
|
36
|
+ @Test
|
|
|
37
|
+ public void testDeadEnd() {
|
|
|
38
|
+ zipper = binaryTree.getRoot();
|
|
|
39
|
+ assertEquals(null, zipper.left.left);
|
|
|
40
|
+ }
|
|
|
41
|
+
|
|
|
42
|
+ @Ignore("Remove to run test")
|
|
|
43
|
+ @Test
|
|
|
44
|
+ public void testToTreeFromDeepFocus() {
|
|
|
45
|
+ zipper = binaryTree.getRoot();
|
|
|
46
|
+ assertEquals(binaryTree, zipper.left.right.toTree());
|
|
|
47
|
+ }
|
|
|
48
|
+
|
|
|
49
|
+ @Ignore("Remove to run test")
|
|
|
50
|
+ @Test
|
|
|
51
|
+ public void testTraversingUpFromTop() {
|
|
|
52
|
+ zipper = binaryTree.getRoot();
|
|
|
53
|
+ assertEquals(null, zipper.up);
|
|
|
54
|
+ }
|
|
|
55
|
+
|
|
|
56
|
+ @Ignore("Remove to run test")
|
|
|
57
|
+ @Test
|
|
|
58
|
+ public void testLeftRightAndUp() {
|
|
|
59
|
+ zipper = binaryTree.getRoot();
|
|
|
60
|
+ assertEquals(3, zipper.left.up.right.up.left.right.getValue());
|
|
|
61
|
+ }
|
|
|
62
|
+
|
|
|
63
|
+ @Ignore("Remove to run test")
|
|
|
64
|
+ @Test
|
|
|
65
|
+ public void testSetValue() {
|
|
|
66
|
+ zipper = binaryTree.getRoot();
|
|
|
67
|
+ zipper = zipper.left;
|
|
|
68
|
+ zipper.setValue(5);
|
|
|
69
|
+ String expected =
|
|
|
70
|
+ "value: 1, " +
|
|
|
71
|
+ "left: { " +
|
|
|
72
|
+ "value: 5, " +
|
|
|
73
|
+ "left: null, " +
|
|
|
74
|
+ "right: { " +
|
|
|
75
|
+ "value: 3, " +
|
|
|
76
|
+ "left: null, " +
|
|
|
77
|
+ "right: null } }, " +
|
|
|
78
|
+ "right: { " +
|
|
|
79
|
+ "value: 4, " +
|
|
|
80
|
+ "left: null, " +
|
|
|
81
|
+ "right: null }";
|
|
|
82
|
+ assertEquals(expected, zipper.toTree().printTree());
|
|
|
83
|
+ }
|
|
|
84
|
+
|
|
|
85
|
+ @Ignore("Remove to run test")
|
|
|
86
|
+ @Test
|
|
|
87
|
+ public void testSetValueAfterTraversingUp() {
|
|
|
88
|
+ zipper = binaryTree.getRoot();
|
|
|
89
|
+ zipper = zipper.left.right.up;
|
|
|
90
|
+ zipper.setValue(5);
|
|
|
91
|
+ String expected =
|
|
|
92
|
+ "value: 1, " +
|
|
|
93
|
+ "left: { " +
|
|
|
94
|
+ "value: 5, " +
|
|
|
95
|
+ "left: null, " +
|
|
|
96
|
+ "right: { " +
|
|
|
97
|
+ "value: 3, " +
|
|
|
98
|
+ "left: null, " +
|
|
|
99
|
+ "right: null } }, " +
|
|
|
100
|
+ "right: { " +
|
|
|
101
|
+ "value: 4, " +
|
|
|
102
|
+ "left: null, " +
|
|
|
103
|
+ "right: null }";
|
|
|
104
|
+ assertEquals(expected, zipper.toTree().printTree());
|
|
|
105
|
+ }
|
|
|
106
|
+
|
|
|
107
|
+ @Ignore("Remove to run test")
|
|
|
108
|
+ @Test
|
|
|
109
|
+ public void testSetLeftWithLeaf() {
|
|
|
110
|
+ zipper = binaryTree.getRoot();
|
|
|
111
|
+ zipper = zipper.left;
|
|
|
112
|
+ zipper.setLeft(new Zipper(5));
|
|
|
113
|
+ String expected =
|
|
|
114
|
+ "value: 1, " +
|
|
|
115
|
+ "left: { " +
|
|
|
116
|
+ "value: 2, " +
|
|
|
117
|
+ "left: { " +
|
|
|
118
|
+ "value: 5, " +
|
|
|
119
|
+ "left: null, " +
|
|
|
120
|
+ "right: null }, " +
|
|
|
121
|
+ "right: { " +
|
|
|
122
|
+ "value: 3, " +
|
|
|
123
|
+ "left: null, " +
|
|
|
124
|
+ "right: null } }, " +
|
|
|
125
|
+ "right: { " +
|
|
|
126
|
+ "value: 4, " +
|
|
|
127
|
+ "left: null, " +
|
|
|
128
|
+ "right: null }";
|
|
|
129
|
+ assertEquals(expected, zipper.toTree().printTree());
|
|
|
130
|
+ }
|
|
|
131
|
+
|
|
|
132
|
+ @Ignore("Remove to run test")
|
|
|
133
|
+ @Test
|
|
|
134
|
+ public void testSetRightWithNull() {
|
|
|
135
|
+ zipper = binaryTree.getRoot();
|
|
|
136
|
+ zipper = zipper.left;
|
|
|
137
|
+ zipper.setRight(null);
|
|
|
138
|
+ String expected =
|
|
|
139
|
+ "value: 1, " +
|
|
|
140
|
+ "left: { value: 2, left: null, right: null }, " +
|
|
|
141
|
+ "right: { value: 4, left: null, right: null }";
|
|
|
142
|
+ assertEquals(expected, zipper.toTree().printTree());
|
|
|
143
|
+ }
|
|
|
144
|
+
|
|
|
145
|
+ @Ignore("Remove to run test")
|
|
|
146
|
+ @Test
|
|
|
147
|
+ public void testSetRightWithSubtree() {
|
|
|
148
|
+ BinaryTree subtree = new BinaryTree(6);
|
|
|
149
|
+ subtree.getRoot().setLeft(new Zipper(7));
|
|
|
150
|
+ subtree.getRoot().setRight(new Zipper(8));
|
|
|
151
|
+ zipper = binaryTree.getRoot();
|
|
|
152
|
+ zipper.setRight(subtree.getRoot());
|
|
|
153
|
+ String expected =
|
|
|
154
|
+ "value: 1, " +
|
|
|
155
|
+ "left: { " +
|
|
|
156
|
+ "value: 2, " +
|
|
|
157
|
+ "left: null, " +
|
|
|
158
|
+ "right: { " +
|
|
|
159
|
+ "value: 3, " +
|
|
|
160
|
+ "left: null, " +
|
|
|
161
|
+ "right: null } }, " +
|
|
|
162
|
+ "right: { " +
|
|
|
163
|
+ "value: 6, " +
|
|
|
164
|
+ "left: { " +
|
|
|
165
|
+ "value: 7, " +
|
|
|
166
|
+ "left: null, " +
|
|
|
167
|
+ "right: null }, " +
|
|
|
168
|
+ "right: { " +
|
|
|
169
|
+ "value: 8, " +
|
|
|
170
|
+ "left: null, " +
|
|
|
171
|
+ "right: null } }";
|
|
|
172
|
+ assertEquals(expected, zipper.toTree().printTree());
|
|
|
173
|
+ }
|
|
|
174
|
+
|
|
|
175
|
+ @Ignore("Remove to run test")
|
|
|
176
|
+ @Test
|
|
|
177
|
+ public void testSetValueOnDeepFocus() {
|
|
|
178
|
+ zipper = binaryTree.getRoot();
|
|
|
179
|
+ zipper = zipper.left.right;
|
|
|
180
|
+ zipper.setValue(5);
|
|
|
181
|
+ String expected =
|
|
|
182
|
+ "value: 1, " +
|
|
|
183
|
+ "left: { " +
|
|
|
184
|
+ "value: 2, " +
|
|
|
185
|
+ "left: null, " +
|
|
|
186
|
+ "right: { " +
|
|
|
187
|
+ "value: 5, " +
|
|
|
188
|
+ "left: null, " +
|
|
|
189
|
+ "right: null } }, " +
|
|
|
190
|
+ "right: { " +
|
|
|
191
|
+ "value: 4, " +
|
|
|
192
|
+ "left: null, " +
|
|
|
193
|
+ "right: null }";
|
|
|
194
|
+ assertEquals(expected, zipper.toTree().printTree());
|
|
|
195
|
+ }
|
|
|
196
|
+
|
|
|
197
|
+ @Ignore("Remove to run test")
|
|
|
198
|
+ @Test
|
|
|
199
|
+ public void differentPathToSameZipper() {
|
|
|
200
|
+ Zipper zipper1 = binaryTree.getRoot().left.up.right;
|
|
|
201
|
+ Zipper zipper2 = binaryTree.getRoot().right;
|
|
|
202
|
+ assertEquals(zipper2, zipper1);
|
|
|
203
|
+ }
|
|
|
204
|
+}
|