-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtils.java
More file actions
150 lines (124 loc) · 4.49 KB
/
Copy pathUtils.java
File metadata and controls
150 lines (124 loc) · 4.49 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
package algorithms.sprint0;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.junit.jupiter.api.Test;
import java.io.*;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import static org.junit.jupiter.api.Assertions.*;
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class Utils {
public static List<Integer> readList(BufferedReader reader) throws IOException {
String line = reader.readLine();
if (line == null) throw new NumberFormatException("EOF");
return Arrays.stream(line.trim().split("\\s+"))
.map(Integer::parseInt)
.collect(Collectors.toList());
}
public static <T> void printList(List<T> list, Writer writer) {
for (T elem : list) {
try {
writer.write(String.valueOf(elem));
writer.write(" ");
} catch (IOException ignored) {
}
}
}
public static int readInt(BufferedReader reader) throws IOException {
String line = reader.readLine();
if (line == null) throw new NumberFormatException("EOF");
return Integer.parseInt(line);
}
@Test
public void readListbasic() throws Exception {
BufferedReader br = new BufferedReader(new StringReader("1 2 3"));
List<Integer> got = readList(br);
assertEquals(Arrays.asList(1, 2, 3), got);
}
@Test
void readList_mixedWhitespace_and_signs() throws Exception {
BufferedReader br = new BufferedReader(new StringReader(" -1\t0 5 "));
List<Integer> got = readList(br);
assertEquals(Arrays.asList(-1, 0, 5), got);
}
@Test
void readList_emptyLine_throwsNumberFormat() {
BufferedReader br = new BufferedReader(new StringReader("\\n"));
assertThrows(NumberFormatException.class, () -> readList(br));
}
@Test
void readList_EOF_throwsNumberFormat() {
BufferedReader br = new BufferedReader(new StringReader(""));
assertThrows(NumberFormatException.class, () -> readList(br));
}
@Test
void readList_nonIntegerToken_throwsNumberFormat() {
BufferedReader br = new BufferedReader(new StringReader("1 a 3"));
assertThrows(NumberFormatException.class, () -> readList(br));
}
@Test
void readList_overflow_throwsNumberFormat() {
BufferedReader br = new BufferedReader(new StringReader("2147483648"));
assertThrows(NumberFormatException.class, () -> readList(br));
}
// --- readInt ---
@Test
void readInt_basic() throws Exception {
BufferedReader br = new BufferedReader(new StringReader("42"));
assertEquals(42, readInt(br));
}
@Test
void readInt_negative() throws Exception {
BufferedReader br = new BufferedReader(new StringReader("-7"));
assertEquals(-7, readInt(br));
}
@Test
void readInt_withSpaces_throwsNumberFormat() {
BufferedReader br = new BufferedReader(new StringReader(" 42 "));
assertThrows(NumberFormatException.class, () -> readInt(br));
}
// --- printList ---
@Test
void printList_integers_trailingSpace_noNewline() throws Exception {
StringWriter sw = new StringWriter();
printList(Arrays.asList(1, 2, 3), sw);
assertEquals("1 2 3 ", sw.toString());
}
@Test
void printList_strings_usesToString() throws Exception {
StringWriter sw = new StringWriter();
printList(Arrays.asList("a", "b"), sw);
assertEquals("a b ", sw.toString());
}
@Test
void printList_empty_writesNothing() throws Exception {
StringWriter sw = new StringWriter();
printList(List.of(), sw);
assertEquals("", sw.toString());
}
@Test
void printList_ioExceptions_areSwallowed() {
Writer throwing = new Writer() {
@Override
public void write(char[] cbuf, int off, int len) throws IOException {
throw new IOException("boom");
}
@Override
public void flush() {
}
@Override
public void close() {
}
@Override
public void write(String str) throws IOException {
throw new IOException("boom");
}
@Override
public void write(int c) throws IOException {
throw new IOException("boom");
}
};
assertDoesNotThrow(() -> printList(Arrays.asList(1, 2, 3), throwing));
}
}