-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay07Tests.java
155 lines (130 loc) · 4.7 KB
/
Day07Tests.java
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
package de.zogan.aoc2022;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import org.junit.jupiter.api.Test;
import de.zogan.aoc2022.Day07.Directory;
import de.zogan.aoc2022.Day07.File;
class Day07Tests {
private static ArrayList<String> lines = new ArrayList<String>(List.of(
"$ cd /",
"$ ls",
"dir a",
"8504156 c.dat",
"14848514 b.txt",
"dir d",
"$ cd a",
"$ ls",
"dir e",
"29116 f",
"2557 g",
"62596 h.lst",
"$ cd e",
"$ ls",
"584 i",
"$ cd ..",
"$ cd ..",
"$ cd d",
"$ ls",
"4060174 j",
"8033020 d.log",
"5626152 d.ext",
"7214296 k"));
@Test
void parseTerminalOutput_FillsFileSystem() {
var root = new Directory();
Day07.parseTerminalOutput(lines, root);
// /
assertSubDirs(Set.of("d", "a"), root.subDirs);
var expectedRootDirFiles = Set.of(
new File("c.dat", 8504156),
new File("b.txt", 14848514));
assertFiles(expectedRootDirFiles, root.files);
assertEquals(48381165, root.getSize());
// /a/
var subDirA = root.subDirs.get("a");
assertSubDirs(Set.of("e"), subDirA.subDirs);
var expectedDirAFiles = Set.of(
new File("f", 29116),
new File("g", 2557),
new File("h.lst", 62596));
assertFiles(expectedDirAFiles, subDirA.files);
assertEquals(94853, subDirA.getSize());
// /a/e/
var subDirE = subDirA.subDirs.get("e");
assertSubDirs(Set.of(), subDirE.subDirs);
assertFiles(Set.of(new File("i", 584)), subDirE.files);
assertEquals(584, subDirE.getSize());
// /d/
var subDirD = root.subDirs.get("d");
assertSubDirs(Set.of(), subDirD.subDirs);
var expectedDirDFiles = Set.of(
new File("j", 4060174),
new File("d.log", 8033020),
new File("d.ext", 5626152),
new File("k", 7214296));
assertFiles(expectedDirDFiles, subDirD.files);
assertEquals(24933642, subDirD.getSize());
}
@Test
void findDirectoriesWithMaxSize_HaveCorrectSize() {
var root = new Directory();
Day07.parseTerminalOutput(lines, root);
var directories = root.findDirectoriesWithMaxSize(100000);
assertDirs(Set.of("a", "e"), Set.of(94853, 584), directories);
}
@Test
void getTotalSize_ReturnsCorrectSize() {
var root = new Directory();
Day07.parseTerminalOutput(lines, root);
assertEquals(95437, Day07.getTotalSize(root));
}
@Test
void getSpaceToBeDeleted_ReturnsCorrectValue() {
var root = new Directory();
Day07.parseTerminalOutput(lines, root);
assertEquals(8381165, Day07.getSpaceToBeDeleted(root));
}
@Test
void sizeOfSmallestDirToDelete_ReturnsCorrectValue() {
var root = new Directory();
Day07.parseTerminalOutput(lines, root);
assertEquals(24933642, Day07.sizeOfSmallestDirToDelete(root));
}
private void assertDirs(
Set<String> expectedNames,
Set<Integer> expectedSizes,
Set<Directory> actual) {
var actualNames = actual.stream()
.map(Directory::getName)
.collect(Collectors.toSet());
assertEquals(expectedNames, actualNames);
var actualSizes = actual.stream()
.map(Directory::getSize)
.collect(Collectors.toSet());
assertEquals(expectedSizes, actualSizes);
}
private void assertSubDirs(Set<String> expectedDirNames,
HashMap<String, Directory> actual) {
assertEquals(expectedDirNames, actual.keySet());
}
private void assertFiles(Set<File> expected, Set<File> actual) {
var expectedFileNames = expected.stream()
.map(File::getFilename)
.collect(Collectors.toSet());
var actualFileNames = actual.stream()
.map(File::getFilename)
.collect(Collectors.toSet());
assertEquals(expectedFileNames, actualFileNames);
var expectedFileSizes = expected.stream()
.map(File::getSize)
.collect(Collectors.toSet());
var actualFileSizes = actual.stream()
.map(File::getSize)
.collect(Collectors.toSet());
assertEquals(expectedFileSizes, actualFileSizes);
}
}