aboutsummaryrefslogtreecommitdiff
path: root/org.jacoco.core.test/src/org/jacoco/core/internal/analysis/MethodCoverageCalculatorTest.java
blob: 306bc792717e7672d124bf37e9ce10261c4e27fa (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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/*******************************************************************************
 * Copyright (c) 2009, 2019 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.internal.analysis;

import static org.junit.Assert.assertEquals;

import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;

import org.jacoco.core.analysis.ISourceFileCoverage;
import org.junit.Before;
import org.junit.Test;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.tree.AbstractInsnNode;
import org.objectweb.asm.tree.InsnList;
import org.objectweb.asm.tree.InsnNode;

/**
 * Unit tests for {@link MethodCoverageCalculator}.
 */
public class MethodCoverageCalculatorTest {

	private Map<AbstractInsnNode, Instruction> instructions;

	// The purpose of this list is to link instruction nodes
	private InsnList list;

	private MethodCoverageImpl coverage;

	@Before
	public void setup() {
		instructions = new HashMap<AbstractInsnNode, Instruction>();
		coverage = new MethodCoverageImpl("run", "()V", null);
		list = new InsnList();
	}

	@Test
	public void should_report_instructions() {
		addInsn(1, true);
		addInsn(2, true);
		addInsn(2, false);
		addInsn(3, false);

		MethodCoverageCalculator c = new MethodCoverageCalculator(instructions);
		c.calculate(coverage);

		assertLine(1, 0, 1, 0, 0);
		assertLine(2, 1, 1, 0, 0);
		assertLine(3, 1, 0, 0, 0);
	}

	@Test
	public void should_report_instructions_with_branches() {
		addInsn(1, false, false);
		addInsn(2, false, false, true);
		addInsn(3, false, true, true);

		MethodCoverageCalculator c = new MethodCoverageCalculator(instructions);
		c.calculate(coverage);

		assertLine(1, 1, 0, 2, 0);
		assertLine(2, 0, 1, 2, 1);
		assertLine(3, 0, 1, 1, 2);
	}

	@Test
	public void should_ignore_single_instruction() {
		addInsn(1, true);
		InsnNode i1 = addInsn(1, false);
		addInsn(2, true);

		MethodCoverageCalculator c = new MethodCoverageCalculator(instructions);
		c.ignore(i1, i1);
		c.calculate(coverage);

		assertLine(1, 0, 1, 0, 0); // only one instruction not filtered
		assertLine(2, 0, 1, 0, 0);
	}

	@Test
	public void should_ignore_instruction_range() {
		addInsn(1, true);
		InsnNode i1 = addInsn(2, false);
		addInsn(2, false);
		addInsn(2, false);
		addInsn(2, false);
		InsnNode i2 = addInsn(2, false);
		addInsn(3, true);

		MethodCoverageCalculator c = new MethodCoverageCalculator(instructions);
		c.ignore(i1, i2);
		c.calculate(coverage);

		assertLine(1, 0, 1, 0, 0);
		assertLine(2, 0, 0, 0, 0); // all instructions filtered in line 2
		assertLine(3, 0, 1, 0, 0);
	}

	@Test
	public void should_exclude_ignored_instructions_from_computation_of_first_and_last_lines() {
		InsnNode i1 = addInsn(1, false);
		addInsn(2, false);
		InsnNode i3 = addInsn(3, false);

		MethodCoverageCalculator c = new MethodCoverageCalculator(instructions);
		c.ignore(i1, i1);
		c.ignore(i3, i3);
		c.calculate(coverage);

		assertEquals(2, coverage.getFirstLine());
		assertEquals(2, coverage.getLastLine());
	}

	@Test
	public void should_merge_instructions() {
		addInsn(1, true);
		InsnNode i1 = addInsn(2, false, true);
		InsnNode i2 = addInsn(2, true, false);
		addInsn(3, true);

		MethodCoverageCalculator c = new MethodCoverageCalculator(instructions);
		c.merge(i1, i2);
		c.calculate(coverage);

		assertLine(1, 0, 1, 0, 0);
		assertLine(2, 0, 1, 0, 2); // one fully covered instruction left
		assertLine(3, 0, 1, 0, 0);
	}

	@Test
	public void should_merge_multiple_instructions() {
		InsnNode i1 = addInsn(1, true, false, false);
		InsnNode i2 = addInsn(1, false, true, false);
		InsnNode i3 = addInsn(1, false, false, true);

		MethodCoverageCalculator c = new MethodCoverageCalculator(instructions);
		c.merge(i1, i2);
		c.merge(i2, i3);
		c.calculate(coverage);

		assertLine(1, 0, 1, 0, 3); // one fully covered instruction left
	}

	@Test
	public void should_merge_instructions_redundant() {
		addInsn(1, true);
		InsnNode i1 = addInsn(2, false, true);
		InsnNode i2 = addInsn(2, true, false);
		addInsn(3, true);

		MethodCoverageCalculator c = new MethodCoverageCalculator(instructions);
		c.merge(i1, i2);
		c.merge(i2, i1);
		c.calculate(coverage);

		assertLine(1, 0, 1, 0, 0);
		assertLine(2, 0, 1, 0, 2); // one fully covered instruction left
		assertLine(3, 0, 1, 0, 0);
	}

	@Test
	public void should_replace_branches() {
		InsnNode i1 = addInsn(1);
		InsnNode i2 = addInsn(2, true);
		InsnNode i3 = addInsn(2, true);
		InsnNode i4 = addInsn(2, false);

		MethodCoverageCalculator c = new MethodCoverageCalculator(instructions);
		c.replaceBranches(i1,
				new HashSet<AbstractInsnNode>(Arrays.asList(i2, i3, i4)));
		c.calculate(coverage);

		assertLine(1, 0, 1, 1, 2); // branches coverage status replaced
		assertLine(2, 1, 2, 0, 0); // still in place
	}

	@Test
	public void should_replace_branches_with_merged_instructions() {
		InsnNode i1 = addInsn(1, false, false, false);
		InsnNode i2 = addInsn(2, true);
		InsnNode i3 = addInsn(2, false);
		InsnNode i4 = addInsn(2, false);

		MethodCoverageCalculator c = new MethodCoverageCalculator(instructions);
		c.merge(i4, i3);
		c.merge(i3, i2);
		c.replaceBranches(i1,
				new HashSet<AbstractInsnNode>(Arrays.asList(i2, i3, i4)));
		c.calculate(coverage);

		assertLine(1, 0, 1, 0, 3);
	}

	@Test
	public void should_work_without_lines() {
		addInsn(ISourceFileCoverage.UNKNOWN_LINE, false);
		addInsn(ISourceFileCoverage.UNKNOWN_LINE, false);
		addInsn(ISourceFileCoverage.UNKNOWN_LINE, true);

		MethodCoverageCalculator c = new MethodCoverageCalculator(instructions);
		c.calculate(coverage);

		assertEquals(ISourceFileCoverage.UNKNOWN_LINE, coverage.getFirstLine());
		assertEquals(ISourceFileCoverage.UNKNOWN_LINE, coverage.getLastLine());
		assertEquals(CounterImpl.getInstance(2, 1),
				coverage.getInstructionCounter());
	}

	private void assertLine(int idx, int mi, int ci, int mb, int cb) {
		assertEquals("instructions", CounterImpl.getInstance(mi, ci),
				coverage.getLine(idx).getInstructionCounter());
		assertEquals("branches", CounterImpl.getInstance(mb, cb),
				coverage.getLine(idx).getBranchCounter());
	}

	private InsnNode addInsn(int line, boolean... branches) {
		Instruction i = new Instruction(line);
		int idx = 0;
		for (boolean covered : branches) {
			i.addBranch(covered, idx++);
		}
		InsnNode node = new InsnNode(Opcodes.NOP);
		list.add(node);
		instructions.put(node, i);
		return node;
	}

}