-
Notifications
You must be signed in to change notification settings - Fork 478
Expand file tree
/
Copy pathWordCountTest.java
More file actions
72 lines (63 loc) · 2.5 KB
/
Copy pathWordCountTest.java
File metadata and controls
72 lines (63 loc) · 2.5 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
/*
* Copyright 2013-2025 chronicle.software; SPDX-License-Identifier: Apache-2.0
*/
package eg;
import com.google.common.io.ByteStreams;
import net.openhft.chronicle.core.io.Closeable;
import net.openhft.chronicle.core.values.IntValue;
import net.openhft.chronicle.map.ChronicleMap;
import net.openhft.chronicle.values.Values;
import org.junit.jupiter.api.Test;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.zip.GZIPInputStream;
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.stream.Collectors.groupingBy;
import static java.util.stream.Collectors.reducing;
import static org.junit.jupiter.api.Assertions.*;
class WordCountTest {
static final String[] words;
static final Map<CharSequence, Integer> expectedMap;
static {
// english version of war and peace -> ascii
ClassLoader cl = Thread.currentThread().getContextClassLoader();
try (InputStream zippedIS = Objects.requireNonNull(cl.getResourceAsStream("war_and_peace.txt.gz"));
GZIPInputStream binaryIS = new GZIPInputStream(zippedIS)) {
String fullText =
new String(ByteStreams.toByteArray(binaryIS), UTF_8);
words = fullText.split("\\s+");
expectedMap = Arrays.stream(words)
.map(CharSequence.class::cast)
.collect(groupingBy(
Function.identity(),
reducing(0, e -> 1, Integer::sum))
);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
@Test
void wordCountTest() {
try (ChronicleMap<CharSequence, IntValue> map = ChronicleMap
.of(CharSequence.class, IntValue.class)
.averageKeySize(7) // average word is 7 ascii bytes long (text in english)
.entries(expectedMap.size())
.create()) {
IntValue v = Values.newNativeReference(IntValue.class);
for (String word : words) {
try (Closeable ignored = map.acquireContext(word, v)) {
assertNotNull(ignored);
v.addValue(1);
}
}
assertEquals(expectedMap.size(), map.size());
expectedMap.forEach((key, value) ->
assertEquals((int) value, map.get(key).getValue())
);
}
}
}