aboutsummaryrefslogtreecommitdiff
path: root/org.jacoco.core.test/src/org/jacoco/core/analysis/NodeComparatorTest.java
blob: ec4928a53c07bb8f8cc88859a87bf07bc591dc47 (plain)
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
/*******************************************************************************
 * Copyright (c) 2009, 2018 Mountainminds GmbH & Co. KG and Contributors
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Marc R. Hoffmann - initial API and implementation
 *    
 *******************************************************************************/
package org.jacoco.core.analysis;

import static org.jacoco.core.analysis.ICoverageNode.ElementType.GROUP;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.util.Arrays;
import java.util.List;

import org.jacoco.core.analysis.ICoverageNode.CounterEntity;
import org.jacoco.core.internal.analysis.CounterImpl;
import org.junit.Test;

/**
 * Unit test for {@link NodeComparator}.
 */
public class NodeComparatorTest {

	@Test
	public void testSort() {
		ICoverageNode d1 = new MockNode(18);
		ICoverageNode d2 = new MockNode(21);
		ICoverageNode d3 = new MockNode(30);
		ICoverageNode d4 = new MockNode(60);
		ICoverageNode d5 = new MockNode(99);
		final List<ICoverageNode> result = CounterComparator.TOTALITEMS.on(
				CounterEntity.INSTRUCTION).sort(
				Arrays.asList(d3, d5, d1, d4, d2));
		assertEquals(Arrays.asList(d1, d2, d3, d4, d5), result);
	}

	@Test
	public void testSecond1() {
		ICoverageNode d1 = new MockLineData(5, 30);
		ICoverageNode d2 = new MockLineData(3, 80);
		final NodeComparator c1 = CounterComparator.TOTALITEMS
				.on(CounterEntity.INSTRUCTION);
		final NodeComparator c2 = CounterComparator.TOTALITEMS
				.on(CounterEntity.LINE);
		assertTrue(c1.second(c2).compare(d1, d2) > 0);
	}

	@Test
	public void testSecond2() {
		ICoverageNode d1 = new MockLineData(5, 30);
		ICoverageNode d2 = new MockLineData(5, 80);
		final NodeComparator c1 = CounterComparator.TOTALITEMS
				.on(CounterEntity.INSTRUCTION);
		final NodeComparator c2 = CounterComparator.TOTALITEMS
				.on(CounterEntity.LINE);
		assertTrue(c1.second(c2).compare(d1, d2) < 0);
	}

	private static final class MockNode extends CoverageNodeImpl {
		MockNode(int total) {
			super(GROUP, "mock");
			instructionCounter = CounterImpl.getInstance(total, 0);
		}
	}

	private static final class MockLineData extends CoverageNodeImpl {
		MockLineData(int totalInstruction, int totalLine) {
			super(GROUP, "mock");
			instructionCounter = CounterImpl.getInstance(totalInstruction, 0);
			lineCounter = CounterImpl.getInstance(totalLine, 0);
		}
	}
}