Home | History | Annotate | Download | only in analysis
      1 /*******************************************************************************
      2  * Copyright (c) 2009, 2018 Mountainminds GmbH & Co. KG and Contributors
      3  * All rights reserved. This program and the accompanying materials
      4  * are made available under the terms of the Eclipse Public License v1.0
      5  * which accompanies this distribution, and is available at
      6  * http://www.eclipse.org/legal/epl-v10.html
      7  *
      8  * Contributors:
      9  *    Marc R. Hoffmann - initial API and implementation
     10  *
     11  *******************************************************************************/
     12 package org.jacoco.core.internal.analysis;
     13 
     14 import static org.junit.Assert.assertEquals;
     15 
     16 import org.jacoco.core.analysis.ICoverageNode.ElementType;
     17 import org.jacoco.core.analysis.ISourceNode;
     18 import org.junit.Test;
     19 
     20 /**
     21  * Unit tests for {@link SourceNodeImpl}.
     22  */
     23 public class SourceNodeImplTest {
     24 
     25 	@Test
     26 	public void testInit() {
     27 		final SourceNodeImpl node = new SourceNodeImpl(ElementType.CLASS, "Foo");
     28 		assertEquals(ElementType.CLASS, node.getElementType());
     29 		assertEquals("Foo", node.getName());
     30 		assertEquals(ISourceNode.UNKNOWN_LINE, node.getFirstLine());
     31 		assertEquals(ISourceNode.UNKNOWN_LINE, node.getLastLine());
     32 		assertEquals(LineImpl.EMPTY, node.getLine(123));
     33 	}
     34 
     35 	@Test
     36 	public void testGetLine() {
     37 		final SourceNodeImpl node = new SourceNodeImpl(ElementType.CLASS, "Foo");
     38 		node.ensureCapacity(10, 20);
     39 		assertEquals(LineImpl.EMPTY, node.getLine(5));
     40 		assertEquals(LineImpl.EMPTY, node.getLine(15));
     41 		assertEquals(LineImpl.EMPTY, node.getLine(25));
     42 	}
     43 
     44 	@Test
     45 	public void testEnsureCapacityUnknown1() {
     46 		final SourceNodeImpl node = new SourceNodeImpl(ElementType.CLASS, "Foo");
     47 		node.ensureCapacity(10, ISourceNode.UNKNOWN_LINE);
     48 		assertEquals(LineImpl.EMPTY, node.getLine(10));
     49 	}
     50 
     51 	@Test
     52 	public void testEnsureCapacityUnknown2() {
     53 		final SourceNodeImpl node = new SourceNodeImpl(ElementType.CLASS, "Foo");
     54 		node.ensureCapacity(ISourceNode.UNKNOWN_LINE, 10);
     55 		assertEquals(LineImpl.EMPTY, node.getLine(10));
     56 	}
     57 
     58 	@Test
     59 	public void testIncrementLineUnknown() {
     60 		final SourceNodeImpl node = new SourceNodeImpl(ElementType.CLASS, "Foo");
     61 		node.increment(CounterImpl.getInstance(1, 2),
     62 				CounterImpl.getInstance(3, 4), ISourceNode.UNKNOWN_LINE);
     63 		assertEquals(CounterImpl.getInstance(1, 2),
     64 				node.getInstructionCounter());
     65 		assertEquals(CounterImpl.getInstance(3, 4), node.getBranchCounter());
     66 		assertEquals(CounterImpl.COUNTER_0_0, node.getLineCounter());
     67 	}
     68 
     69 	@Test
     70 	public void testIncrementLines() {
     71 		final SourceNodeImpl node = new SourceNodeImpl(ElementType.CLASS, "Foo");
     72 		node.increment(CounterImpl.getInstance(1, 1), CounterImpl.COUNTER_0_0,
     73 				10);
     74 		node.increment(CounterImpl.getInstance(2, 2), CounterImpl.COUNTER_0_0,
     75 				12);
     76 
     77 		assertEquals(CounterImpl.getInstance(1, 1), node.getLine(10)
     78 				.getInstructionCounter());
     79 		assertEquals(CounterImpl.COUNTER_0_0, node.getLine(11)
     80 				.getInstructionCounter());
     81 		assertEquals(CounterImpl.getInstance(2, 2), node.getLine(12)
     82 				.getInstructionCounter());
     83 	}
     84 
     85 	@Test
     86 	public void testIncrementLine1_1() {
     87 		testIncrementLine(0, 0, 0, 0, 0, 0);
     88 	}
     89 
     90 	@Test
     91 	public void testIncrementLine1_2() {
     92 		testIncrementLine(0, 0, 5, 0, 1, 0);
     93 	}
     94 
     95 	@Test
     96 	public void testIncrementLine1_3() {
     97 		testIncrementLine(0, 0, 0, 5, 0, 1);
     98 	}
     99 
    100 	@Test
    101 	public void testIncrementLine1_4() {
    102 		testIncrementLine(0, 0, 5, 5, 0, 1);
    103 	}
    104 
    105 	@Test
    106 	public void testIncrementLine2_1() {
    107 		testIncrementLine(3, 0, 0, 0, 1, 0);
    108 	}
    109 
    110 	@Test
    111 	public void testIncrementLine2_2() {
    112 		testIncrementLine(3, 0, 5, 0, 1, 0);
    113 	}
    114 
    115 	@Test
    116 	public void testIncrementLine2_3() {
    117 		testIncrementLine(3, 0, 0, 5, 0, 1);
    118 	}
    119 
    120 	@Test
    121 	public void testIncrementLine2_4() {
    122 		testIncrementLine(3, 0, 5, 5, 0, 1);
    123 	}
    124 
    125 	@Test
    126 	public void testIncrementLine3_1() {
    127 		testIncrementLine(0, 3, 0, 0, 0, 1);
    128 	}
    129 
    130 	@Test
    131 	public void testIncrementLine3_2() {
    132 		testIncrementLine(0, 3, 5, 0, 0, 1);
    133 	}
    134 
    135 	@Test
    136 	public void testIncrementLine3_3() {
    137 		testIncrementLine(0, 3, 0, 5, 0, 1);
    138 	}
    139 
    140 	@Test
    141 	public void testIncrementLine3_4() {
    142 		testIncrementLine(0, 3, 5, 5, 0, 1);
    143 	}
    144 
    145 	@Test
    146 	public void testIncrementLine4_1() {
    147 		testIncrementLine(3, 3, 0, 0, 0, 1);
    148 	}
    149 
    150 	@Test
    151 	public void testIncrementLine4_2() {
    152 		testIncrementLine(3, 3, 5, 0, 0, 1);
    153 	}
    154 
    155 	@Test
    156 	public void testIncrementLine4_3() {
    157 		testIncrementLine(3, 3, 0, 5, 0, 1);
    158 	}
    159 
    160 	@Test
    161 	public void testIncrementLine4_4() {
    162 		testIncrementLine(3, 3, 5, 5, 0, 1);
    163 	}
    164 
    165 	private void testIncrementLine(int mi1, int ci1, int mi2, int ci2,
    166 			int expectedMissedLines, int expectedCoveredLines) {
    167 		final SourceNodeImpl node = new SourceNodeImpl(ElementType.CLASS, "Foo");
    168 		node.increment(CounterImpl.getInstance(mi1, ci1),
    169 				CounterImpl.COUNTER_0_0, 33);
    170 		node.increment(CounterImpl.getInstance(mi2, ci2),
    171 				CounterImpl.COUNTER_0_0, 33);
    172 		assertEquals(CounterImpl.getInstance(expectedMissedLines,
    173 				expectedCoveredLines), node.getLineCounter());
    174 		assertEquals(CounterImpl.getInstance(mi1 + mi2, ci1 + ci2), node
    175 				.getLine(33).getInstructionCounter());
    176 	}
    177 
    178 	@Test
    179 	public void testIncrementChildNoLines() {
    180 		final SourceNodeImpl node = new SourceNodeImpl(ElementType.CLASS, "Foo");
    181 		final SourceNodeImpl child = new SourceNodeImpl(ElementType.CLASS,
    182 				"Foo") {
    183 			{
    184 				this.instructionCounter = CounterImpl.getInstance(1, 11);
    185 				this.branchCounter = CounterImpl.getInstance(2, 22);
    186 				this.methodCounter = CounterImpl.getInstance(3, 33);
    187 				this.classCounter = CounterImpl.getInstance(4, 44);
    188 			}
    189 		};
    190 		node.increment(child);
    191 		assertEquals(CounterImpl.getInstance(1, 11),
    192 				node.getInstructionCounter());
    193 		assertEquals(CounterImpl.getInstance(2, 22), node.getBranchCounter());
    194 		assertEquals(CounterImpl.getInstance(3, 33), node.getMethodCounter());
    195 		assertEquals(CounterImpl.getInstance(4, 44), node.getClassCounter());
    196 	}
    197 
    198 	@Test
    199 	public void testIncrementChildWithLines() {
    200 		final SourceNodeImpl node = new SourceNodeImpl(ElementType.CLASS, "Foo");
    201 
    202 		final SourceNodeImpl child = new SourceNodeImpl(ElementType.CLASS,
    203 				"Foo");
    204 		child.increment(CounterImpl.getInstance(1, 11),
    205 				CounterImpl.getInstance(3, 33), 5);
    206 
    207 		node.increment(child);
    208 		node.increment(child);
    209 
    210 		assertEquals(CounterImpl.getInstance(2, 22),
    211 				node.getInstructionCounter());
    212 		assertEquals(CounterImpl.getInstance(6, 66), node.getBranchCounter());
    213 		assertEquals(CounterImpl.getInstance(0, 1), node.getLineCounter());
    214 	}
    215 
    216 }
    217