Skip to content

Commit 2d5093e

Browse files
idodeclaretarzanek
authored andcommitted
Add PhraseHighlightComparatorTest
1 parent 38e3c8e commit 2d5093e

File tree

2 files changed

+166
-3
lines changed

2 files changed

+166
-3
lines changed

src/org/opensolaris/opengrok/search/context/PhraseHighlightComparator.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,21 +33,36 @@ public class PhraseHighlightComparator implements Comparator<PhraseHighlight> {
3333
public static final PhraseHighlightComparator INSTANCE =
3434
new PhraseHighlightComparator();
3535

36+
/**
37+
* Compares two {@link PhraseHighlight} instances for order by first
38+
* comparing using {@link Integer#compare(int, int)} the
39+
* {@link PhraseHighlight#lineStart} values of {@code o1} and {@code o2} and
40+
* subsequently, if identical, comparing the {@link PhraseHighlight#lineEnd}
41+
* values of {@code o2} and {@code o1} (i.e. inverted).
42+
* <p>The ordering allows to iterate through a collection afterward and
43+
* easily subsume where necessary a {@link PhraseHighlight} instance into
44+
* its immediate predecessor.
45+
* @param o1 a required instance
46+
* @param o2 a required instance
47+
* @return a negative integer, zero, or a positive integer as the first
48+
* argument is less than, equal to, or greater than the second
49+
*/
3650
@Override
3751
public int compare(PhraseHighlight o1, PhraseHighlight o2) {
38-
// ASC by lineStart, with -1 == -Inf.
52+
int cmp;
3953
if (o1.getLineStart() < 0) {
4054
if (o2.getLineStart() >= 0) {
4155
return -1;
4256
}
57+
cmp = 0;
4358
} else if (o2.getLineStart() < 0) {
4459
return 1;
60+
} else {
61+
cmp = Integer.compare(o1.getLineStart(), o2.getLineStart());
4562
}
46-
int cmp = Integer.compare(o1.getLineStart(), o2.getLineStart());
4763
if (cmp != 0) {
4864
return cmp;
4965
}
50-
// DESC by lineEnd, with -1 == Inf.
5166
cmp = Integer.compare(o2.getLineEnd(), o1.getLineEnd());
5267
return cmp;
5368
}
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
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.search.context;
25+
26+
import static org.junit.Assert.assertEquals;
27+
import org.junit.Test;
28+
29+
/**
30+
* Represents a container of tests of {@link PhraseHighlightComparator}.
31+
*/
32+
public class PhraseHighlightComparatorTest {
33+
34+
private PhraseHighlight o1;
35+
private PhraseHighlight o2;
36+
37+
@Test
38+
public void testEqualBoundedInstances() {
39+
o1 = PhraseHighlight.create(0, 1);
40+
o2 = PhraseHighlight.create(0, 1);
41+
int result = PhraseHighlightComparator.INSTANCE.compare(o1, o2);
42+
assertEquals("o1[0,1] should be == o2[0,1]", 0, result);
43+
}
44+
45+
@Test
46+
public void testEqualUnboundedInstances() {
47+
o1 = PhraseHighlight.createEntire();
48+
o2 = PhraseHighlight.createEntire();
49+
int result = PhraseHighlightComparator.INSTANCE.compare(o1, o2);
50+
assertEquals("o1[,] should be == o2[,]", 0, result);
51+
}
52+
53+
@Test
54+
public void testEqualMixedBoundedInstances1() {
55+
o1 = PhraseHighlight.createStarter(5);
56+
o2 = PhraseHighlight.createStarter(5);
57+
int result = PhraseHighlightComparator.INSTANCE.compare(o1, o2);
58+
assertEquals("o1[5,] should be == o2[5,]", 0, result);
59+
}
60+
61+
@Test
62+
public void testEqualMixedBoundedInstances2() {
63+
o1 = PhraseHighlight.createEnder(5);
64+
o2 = PhraseHighlight.createEnder(5);
65+
int result = PhraseHighlightComparator.INSTANCE.compare(o1, o2);
66+
assertEquals("o1[,5] should be == o2[,5]", 0, result);
67+
}
68+
69+
@Test
70+
public void testDisjointInstances1() {
71+
o1 = PhraseHighlight.create(0, 10);
72+
o2 = PhraseHighlight.create(100, 110);
73+
int result = PhraseHighlightComparator.INSTANCE.compare(o1, o2);
74+
assertEquals("o1[0,10] should be < o2[100,110]", -1, result);
75+
}
76+
77+
@Test
78+
public void testDisjointInstances2() {
79+
o1 = PhraseHighlight.create(2, 3);
80+
o2 = PhraseHighlight.create(0, 2);
81+
int result = PhraseHighlightComparator.INSTANCE.compare(o1, o2);
82+
assertEquals("o1[2,3] should be > o2[0,2]", 1, result);
83+
}
84+
85+
@Test
86+
public void testBoundedOverlappingUnequalInstances1() {
87+
o1 = PhraseHighlight.create(0, 10);
88+
o2 = PhraseHighlight.create(1, 3);
89+
int result = PhraseHighlightComparator.INSTANCE.compare(o1, o2);
90+
assertEquals("o1[0,10] should be < o2[1,3]", -1, result);
91+
}
92+
93+
@Test
94+
public void testBoundedOverlappingUnequalInstances2() {
95+
o1 = PhraseHighlight.create(0, 5);
96+
o2 = PhraseHighlight.create(0, 10);
97+
int result = PhraseHighlightComparator.INSTANCE.compare(o1, o2);
98+
assertEquals("o1[0,5] should be > o2[0,10]", 1, result);
99+
}
100+
101+
@Test
102+
public void testBoundedOverlappingUnequalInstances3() {
103+
o1 = PhraseHighlight.create(5, 15);
104+
o2 = PhraseHighlight.create(0, 10);
105+
int result = PhraseHighlightComparator.INSTANCE.compare(o1, o2);
106+
assertEquals("o1[5,15] should be > o2[0,10]", 1, result);
107+
}
108+
109+
@Test
110+
public void testBoundedOverlappingUnequalInstances4() {
111+
o1 = PhraseHighlight.create(0, 10);
112+
o2 = PhraseHighlight.create(5, 10);
113+
int result = PhraseHighlightComparator.INSTANCE.compare(o1, o2);
114+
assertEquals("o1[0,10] should be < o2[5,10]", -1, result);
115+
}
116+
117+
@Test
118+
public void testMixedBoundedNonOverlappingInstances1() {
119+
o1 = PhraseHighlight.createEnder(10);
120+
o2 = PhraseHighlight.create(15, 30);
121+
int result = PhraseHighlightComparator.INSTANCE.compare(o1, o2);
122+
assertEquals("o1[,10] should be < o2[15,30]", -1, result);
123+
}
124+
125+
@Test
126+
public void testMixedBoundedNonOverlappingInstances2() {
127+
o1 = PhraseHighlight.createStarter(15);
128+
o2 = PhraseHighlight.create(0, 10);
129+
int result = PhraseHighlightComparator.INSTANCE.compare(o1, o2);
130+
assertEquals("o1[15,] should be > o2[0,10]", 1, result);
131+
}
132+
133+
@Test
134+
public void testMixedBoundedOverlappingInstances1() {
135+
o1 = PhraseHighlight.createEnder(20);
136+
o2 = PhraseHighlight.create(15, 30);
137+
int result = PhraseHighlightComparator.INSTANCE.compare(o1, o2);
138+
assertEquals("o1[,20] should be < o2[15,30]", -1, result);
139+
}
140+
141+
@Test
142+
public void testMixedBoundedOverlappingInstances2() {
143+
o1 = PhraseHighlight.createStarter(20);
144+
o2 = PhraseHighlight.create(15, 30);
145+
int result = PhraseHighlightComparator.INSTANCE.compare(o1, o2);
146+
assertEquals("o1[20,] should be > o2[15,30]", 1, result);
147+
}
148+
}

0 commit comments

Comments
 (0)