Skip to content

Commit 93dc5b4

Browse files
idodeclaretarzanek
authored andcommitted
Add SourceSplitterTest from next commit but meant to be here
1 parent 70f3b72 commit 93dc5b4

File tree

1 file changed

+136
-0
lines changed

1 file changed

+136
-0
lines changed
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
/*
2+
* CDDL HEADER START
3+
*
4+
* The contents of this file are subject to the terms of the
5+
* Common Development and Distribution License (the "License").
6+
* You may not use this file except in compliance with the License.
7+
*
8+
* See LICENSE.txt included in this distribution for the specific
9+
* language governing permissions and limitations under the License.
10+
*
11+
* When distributing Covered Code, include this CDDL HEADER in each
12+
* file and include the License file at LICENSE.txt.
13+
* If applicable, add the following below this CDDL HEADER, with the
14+
* fields enclosed by brackets "[]" replaced with your own identifying
15+
* information: Portions Copyright [yyyy] [name of copyright owner]
16+
*
17+
* CDDL HEADER END
18+
*/
19+
20+
/*
21+
* Copyright (c) 2018, Chris Fraire <[email protected]>.
22+
*/
23+
24+
package org.opensolaris.opengrok.util;
25+
26+
import java.io.IOException;
27+
import static org.junit.Assert.assertArrayEquals;
28+
import static org.junit.Assert.assertEquals;
29+
import org.junit.Test;
30+
import org.opensolaris.opengrok.analysis.StreamSource;
31+
32+
/**
33+
* Represents a container for tests of {@link SourceSplitter}.
34+
*/
35+
public class SourceSplitterTest {
36+
37+
@Test
38+
public void shouldSplitEmptyStringIntoOneLine() {
39+
SourceSplitter splitter = new SourceSplitter();
40+
splitter.reset("");
41+
assertEquals("split count", 1, splitter.count());
42+
assertEquals("split position", 0, splitter.getPosition(0));
43+
assertEquals("split position", 0, splitter.getPosition(1));
44+
45+
assertEquals("split find-offset", 0, splitter.findLineOffset(0));
46+
assertEquals("split find-offset", -1, splitter.findLineOffset(1));
47+
}
48+
49+
@Test
50+
public void shouldSplitEndingLFsIntoOneMoreLine() {
51+
SourceSplitter splitter = new SourceSplitter();
52+
splitter.reset("abc\ndef\n");
53+
assertEquals("split count", 3, splitter.count());
54+
assertEquals("split position", 0, splitter.getPosition(0));
55+
assertEquals("split position", 4, splitter.getPosition(1));
56+
assertEquals("split position", 8, splitter.getPosition(2));
57+
assertEquals("split position", 8, splitter.getPosition(3));
58+
}
59+
60+
@Test
61+
public void shouldSplitDocsWithNoLastLF() {
62+
SourceSplitter splitter = new SourceSplitter();
63+
splitter.reset("abc\r\ndef");
64+
assertEquals("split count", 2, splitter.count());
65+
assertEquals("split position", 0, splitter.getPosition(0));
66+
assertEquals("split position", 5, splitter.getPosition(1));
67+
assertEquals("split position", 8, splitter.getPosition(2));
68+
69+
assertEquals("split find-offset", 0, splitter.findLineOffset(0));
70+
assertEquals("split find-offset", 0, splitter.findLineOffset(1));
71+
assertEquals("split find-offset", 0, splitter.findLineOffset(4));
72+
assertEquals("split find-offset", 1, splitter.findLineOffset(5));
73+
assertEquals("split find-offset", 1, splitter.findLineOffset(6));
74+
}
75+
76+
@Test
77+
public void shouldHandleDocsOfLongerLength() {
78+
// 0 0
79+
// 0-- - 5-- - -1--- - 5--- - 2-
80+
final String INPUT = "ab\r\ncde\r\nefgh\r\nijk\r\nlm";
81+
82+
SourceSplitter splitter = new SourceSplitter();
83+
splitter.reset(INPUT);
84+
assertEquals("split count", 5, splitter.count());
85+
assertEquals("split position", 0, splitter.getPosition(0));
86+
assertEquals("split position", 4, splitter.getPosition(1));
87+
assertEquals("split position", 9, splitter.getPosition(2));
88+
assertEquals("split position", 15, splitter.getPosition(3));
89+
assertEquals("split position", 20, splitter.getPosition(4));
90+
assertEquals("split position", 22, splitter.getPosition(5));
91+
92+
/*
93+
* Test findLineOffset() for every character with an alternate
94+
* computation that counts every LFs.
95+
*/
96+
for (int i = 0; i < splitter.originalLength(); ++i) {
97+
char c = INPUT.charAt(i);
98+
int off = splitter.findLineOffset(i);
99+
long numLF = INPUT.substring(0, i + 1).chars().filter(ch ->
100+
ch == '\n').count();
101+
long exp = numLF - (c == '\n' ? 1 : 0);
102+
assertEquals("split find-offset of " + i, exp, off);
103+
}
104+
}
105+
106+
@Test
107+
public void shouldHandleStreamedDocsOfLongerLength() throws IOException {
108+
// 0 0
109+
// 0-- - 5-- - -1--- - 5--- - 2-
110+
final String INPUT = "ab\r\ncde\r\nefgh\r\nijk\r\nlm";
111+
StreamSource src = StreamSource.fromString(INPUT);
112+
113+
SourceSplitter splitter = new SourceSplitter();
114+
splitter.reset(src);
115+
assertEquals("split count", 5, splitter.count());
116+
assertEquals("split position", 0, splitter.getPosition(0));
117+
assertEquals("split position", 4, splitter.getPosition(1));
118+
assertEquals("split position", 9, splitter.getPosition(2));
119+
assertEquals("split position", 15, splitter.getPosition(3));
120+
assertEquals("split position", 20, splitter.getPosition(4));
121+
assertEquals("split position", 22, splitter.getPosition(5));
122+
123+
/*
124+
* Test findLineOffset() for every character with an alternate
125+
* computation that counts every LFs.
126+
*/
127+
for (int i = 0; i < splitter.originalLength(); ++i) {
128+
char c = INPUT.charAt(i);
129+
int off = splitter.findLineOffset(i);
130+
long numLF = INPUT.substring(0, i + 1).chars().filter(ch ->
131+
ch == '\n').count();
132+
long exp = numLF - (c == '\n' ? 1 : 0);
133+
assertEquals("split find-offset of " + i, exp, off);
134+
}
135+
}
136+
}

0 commit comments

Comments
 (0)