xref: /OpenGrok/opengrok-indexer/src/test/java/org/opengrok/indexer/index/NumLinesLOCAggregatorTest.java (revision fb4fc4bcc2970158c11a79b4070ebcd0797d96d6)
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) 2020, Chris Fraire <cfraire@me.com>.
22  */
23 package org.opengrok.indexer.index;
24 
25 import org.junit.jupiter.api.Test;
26 
27 import org.opengrok.indexer.analysis.AccumulatedNumLinesLOC;
28 import org.opengrok.indexer.analysis.NumLinesLOC;
29 
30 import java.util.ArrayList;
31 import java.util.Comparator;
32 import java.util.List;
33 
34 import static org.junit.jupiter.api.Assertions.assertEquals;
35 
36 public class NumLinesLOCAggregatorTest {
37 
38     @Test
shouldEnumerateToRoot()39     public void shouldEnumerateToRoot() {
40         NumLinesLOCAggregator aggtor = new NumLinesLOCAggregator();
41         final String PATH = "/a/b/c/f0";
42         aggtor.register(new NumLinesLOC(PATH, 2, 1));
43         List<AccumulatedNumLinesLOC> counts = new ArrayList<>();
44         aggtor.iterator().forEachRemaining(counts::add);
45         counts.sort(Comparator.comparingInt(o -> o.getPath().length()));
46 
47         assertEquals(4, counts.size(), "agg count");
48 
49         AccumulatedNumLinesLOC entry = counts.get(0);
50         assertEquals("/", entry.getPath(), "counts[0] path");
51         assertEquals(2, entry.getNumLines(), "counts[0] numLines");
52         assertEquals(1, entry.getLOC(), "counts[0] LOC");
53 
54         entry = counts.get(1);
55         assertEquals("/a", entry.getPath(), "counts[1] path");
56         assertEquals(2, entry.getNumLines(), "counts[1] numLines");
57         assertEquals(1, entry.getLOC(), "counts[1] LOC");
58 
59         entry = counts.get(2);
60         assertEquals("/a/b", entry.getPath(), "counts[2] path");
61         assertEquals(2, entry.getNumLines(), "counts[2] numLines");
62         assertEquals(1, entry.getLOC(), "counts[2] LOC");
63 
64         entry = counts.get(3);
65         assertEquals("/a/b/c", entry.getPath(), "counts[2] path");
66         assertEquals(2, entry.getNumLines(), "counts[2] numLines");
67         assertEquals(1, entry.getLOC(), "counts[2] LOC");
68     }
69 
70     @Test
shouldAggregateToRoot()71     public void shouldAggregateToRoot() {
72         NumLinesLOCAggregator aggtor = new NumLinesLOCAggregator();
73         aggtor.register(new NumLinesLOC("/a/b/f0", 2, 1));
74         aggtor.register(new NumLinesLOC("/a/c/f1", 5, 3));
75         aggtor.register(new NumLinesLOC("/a/f2", 11, 7));
76         List<AccumulatedNumLinesLOC> counts = new ArrayList<>();
77         aggtor.iterator().forEachRemaining(counts::add);
78         counts.sort(Comparator.comparingInt((AccumulatedNumLinesLOC o) ->
79                 o.getPath().length()).thenComparing(AccumulatedNumLinesLOC::getPath));
80 
81         assertEquals(4, counts.size(), "agg count");
82 
83         AccumulatedNumLinesLOC entry = counts.get(0);
84         assertEquals("/", entry.getPath(), "counts[0] path");
85         assertEquals(18, entry.getNumLines(), "counts[0] numLines");
86         assertEquals(11, entry.getLOC(), "counts[0] LOC");
87 
88         entry = counts.get(1);
89         assertEquals("/a", entry.getPath(), "counts[1] path");
90         assertEquals(18, entry.getNumLines(), "counts[1] numLines");
91         assertEquals(11, entry.getLOC(), "counts[1] LOC");
92 
93         entry = counts.get(2);
94         assertEquals("/a/b", entry.getPath(), "counts[2] path");
95         assertEquals(2, entry.getNumLines(), "counts[2] numLines");
96         assertEquals(1, entry.getLOC(), "counts[2] LOC");
97 
98         entry = counts.get(3);
99         assertEquals("/a/c", entry.getPath(), "counts[2] path");
100         assertEquals(5, entry.getNumLines(), "counts[2] numLines");
101         assertEquals(3, entry.getLOC(), "counts[2] LOC");
102     }
103 }
104