-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathZipTest.java
More file actions
84 lines (68 loc) · 2.87 KB
/
Copy pathZipTest.java
File metadata and controls
84 lines (68 loc) · 2.87 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
package algorithms.sprint0;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.Arrays;
import java.util.List;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
@Tag("unit")
class ZipTest {
@Test
void interleavesEqualLengthLists() {
List<Integer> a = Arrays.asList(1, 3, 5);
List<Integer> b = Arrays.asList(2, 4, 6);
assertEquals(Arrays.asList(1, 2, 3, 4, 5, 6), Zip.zip(a, b, 3));
}
@Test
void clampsByRequestedLengthAndActualListSizes() {
assertEquals(Arrays.asList(10, 20, 30, 40),
Zip.zip(Arrays.asList(10, 30, 50), Arrays.asList(20, 40, 60), 2));
assertEquals(Arrays.asList(1, 2, 3, 4, 5, 6),
Zip.zip(Arrays.asList(1, 3, 5), Arrays.asList(2, 4, 6), 10));
assertEquals(Arrays.asList(1, 2), Zip.zip(List.of(1), Arrays.asList(2, 4, 6), 3));
}
@Test
void returnsEmptyListForZeroOrEmptyInputs() {
assertTrue(Zip.zip(Arrays.asList(1, 3, 5), Arrays.asList(2, 4, 6), 0).isEmpty());
assertTrue(Zip.zip(List.of(), List.of(), 5).isEmpty());
}
@Test
void rejectsNegativeLength() {
IllegalArgumentException exception =
assertThrows(IllegalArgumentException.class, () -> Zip.zip(List.of(1), List.of(2), -1));
assertTrue(exception.getMessage().contains("n >= 0"));
}
@Test
void rejectsNullLists() {
assertThrows(NullPointerException.class, () -> Zip.zip(null, List.of(2), 1));
assertThrows(NullPointerException.class, () -> Zip.zip(List.of(1), null, 1));
}
@Test
void processAcceptsValidInput() throws IOException {
StringWriter output = new StringWriter();
BufferedWriter writer = new BufferedWriter(output);
Zip.process(new BufferedReader(new StringReader("3\n1 5 6\n7 8 9\n")), writer);
writer.flush();
assertEquals("1 7 5 8 6 9 ", output.toString());
}
@Test
void processRejectsMissingOrShortLists() {
assertThrows(IOException.class, () -> process("3\n1 2 3\n"));
assertThrows(IllegalArgumentException.class, () -> process("3\n1 2\n4 5 6\n"));
}
@Test
void processRejectsUnboundedSizesAndLines() {
assertThrows(IllegalArgumentException.class, () -> process("100001\n1\n2\n"));
String oversizedLine = "1".repeat(1_200_002);
assertThrows(IllegalArgumentException.class, () -> process("1\n" + oversizedLine + "\n2\n"));
}
private static void process(String input) throws IOException {
Zip.process(new BufferedReader(new StringReader(input)), new BufferedWriter(new StringWriter()));
}
}