-
Notifications
You must be signed in to change notification settings - Fork 21k
Expand file tree
/
Copy pathBufferedReaderTest.java
More file actions
99 lines (80 loc) · 3.12 KB
/
BufferedReaderTest.java
File metadata and controls
99 lines (80 loc) · 3.12 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
package com.thealgorithms.io;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import org.junit.jupiter.api.Test;
class BufferedReaderTest {
@Test
public void testPeeks() throws IOException {
String text = "Hello!\nWorld!";
int len = text.length();
byte[] bytes = text.getBytes();
ByteArrayInputStream input = new ByteArrayInputStream(bytes);
BufferedReader reader = new BufferedReader(input);
// read the first letter
assertEquals('H', reader.read());
len--;
assertEquals(len, reader.available());
// position: H[e]llo!\nWorld!
// reader.read() will be == 'e'
assertEquals('l', reader.peek(1));
assertEquals('l', reader.peek(2)); // second l
assertEquals('o', reader.peek(3));
}
@Test
public void testMixes() throws IOException {
String text = "Hello!\nWorld!";
int len = text.length();
byte[] bytes = text.getBytes();
ByteArrayInputStream input = new ByteArrayInputStream(bytes);
BufferedReader reader = new BufferedReader(input);
// read the first letter
assertEquals('H', reader.read()); // first letter
len--;
assertEquals('l', reader.peek(1)); // third later (second letter after 'H')
assertEquals('e', reader.read()); // second letter
len--;
assertEquals(len, reader.available());
// position: H[e]llo!\nWorld!
assertEquals('o', reader.peek(2)); // second l
assertEquals('!', reader.peek(3));
assertEquals('\n', reader.peek(4));
assertEquals('l', reader.read()); // third letter
assertEquals('o', reader.peek(1)); // fourth letter
for (int i = 0; i < 6; i++) {
reader.read();
}
try {
System.out.println((char) reader.peek(4));
} catch (Exception ignored) {
System.out.println("[cached intentional error]");
// intentional, for testing purpose
}
}
@Test
public void testBlockPractical() throws IOException {
String text = "!Hello\nWorld!";
byte[] bytes = text.getBytes();
int len = bytes.length;
ByteArrayInputStream input = new ByteArrayInputStream(bytes);
BufferedReader reader = new BufferedReader(input);
assertEquals('H', reader.peek());
assertEquals('!', reader.read()); // read the first letter
len--;
// this only reads the next 5 bytes (Hello) because
// the default buffer size = 5
assertEquals("Hello", new String(reader.readBlock()));
len -= 5;
assertEquals(reader.available(), len);
// maybe kind of a practical demonstration / use case
if (reader.read() == '\n') {
assertEquals('W', reader.read());
assertEquals('o', reader.read());
// the rest of the blocks
assertEquals("rld!", new String(reader.readBlock()));
} else {
// should not reach
throw new IOException("Something not right");
}
}
}