aboutsummaryrefslogtreecommitdiffstats
path: root/org.jacoco.core.test/src/org/jacoco/core/internal/analysis/InstructionsBuilderTest.java
blob: 32dea10ff87a76f56f9c5b95dfdaad15f6a90ce8 (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
/*******************************************************************************
 * 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.internal.analysis;

import static org.junit.Assert.assertEquals;

import java.util.Map;

import org.jacoco.core.analysis.ISourceFileCoverage;
import org.jacoco.core.internal.flow.LabelInfo;
import org.junit.Before;
import org.junit.Test;
import org.objectweb.asm.Label;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.tree.AbstractInsnNode;
import org.objectweb.asm.tree.InsnNode;

/**
 * Unit tests for {@link InstructionsBuilder}.
 */
public class InstructionsBuilderTest {

	private InstructionsBuilder builder;

	@Before
	public void setup() {
		builder = new InstructionsBuilder(new boolean[] { false, true });
	}

	@Test
	public void current_line_number_should_be_applied_to_instructions() {
		InsnNode i1 = new InsnNode(Opcodes.NOP);
		builder.addInstruction(i1);

		builder.setCurrentLine(10);
		InsnNode i2 = new InsnNode(Opcodes.NOP);
		builder.addInstruction(i2);
		InsnNode i3 = new InsnNode(Opcodes.NOP);
		builder.addInstruction(i3);

		builder.setCurrentLine(20);
		InsnNode i4 = new InsnNode(Opcodes.NOP);
		builder.addInstruction(i4);

		Map<AbstractInsnNode, Instruction> map = builder.getInstructions();
		assertEquals(ISourceFileCoverage.UNKNOWN_LINE, map.get(i1).getLine());
		assertEquals(10, map.get(i2).getLine());
		assertEquals(10, map.get(i3).getLine());
		assertEquals(20, map.get(i4).getLine());
	}

	@Test
	public void null_probearray_should_not_mark_instruction_as_covered() {
		builder = new InstructionsBuilder(null);

		InsnNode i1 = new InsnNode(Opcodes.NOP);
		builder.addInstruction(i1);
		builder.addProbe(5, 0);

		Map<AbstractInsnNode, Instruction> map = builder.getInstructions();
		assertEquals(CounterImpl.COUNTER_1_0,
				map.get(i1).getInstructionCounter());
	}

	@Test
	public void unexecuted_probe_should_not_mark_instruction_as_covered() {
		InsnNode i1 = new InsnNode(Opcodes.NOP);
		builder.addInstruction(i1);
		builder.addProbe(0, 0);

		Map<AbstractInsnNode, Instruction> map = builder.getInstructions();
		assertEquals(CounterImpl.COUNTER_1_0,
				map.get(i1).getInstructionCounter());
	}

	@Test
	public void executed_probe_should_mark_instruction_as_covered() {
		InsnNode i1 = new InsnNode(Opcodes.NOP);
		builder.addInstruction(i1);
		builder.addProbe(1, 0);

		Map<AbstractInsnNode, Instruction> map = builder.getInstructions();
		assertEquals(CounterImpl.COUNTER_0_1,
				map.get(i1).getInstructionCounter());
	}

	@Test
	public void subsequent_instructions_should_be_linked_by_default() {
		InsnNode i1 = new InsnNode(Opcodes.NOP);
		builder.addInstruction(i1);

		InsnNode i2 = new InsnNode(Opcodes.NOP);
		builder.addInstruction(i2);

		// mark i2 as covered
		builder.addProbe(1, 0);

		// coverage should be propagated to i1
		Map<AbstractInsnNode, Instruction> map = builder.getInstructions();
		assertEquals(CounterImpl.COUNTER_0_1,
				map.get(i1).getInstructionCounter());
	}

	@Test
	public void subsequent_instructions_should_not_be_linked_when_noSuccessor_was_called() {
		InsnNode i1 = new InsnNode(Opcodes.NOP);
		builder.addInstruction(i1);
		builder.noSuccessor();

		InsnNode i2 = new InsnNode(Opcodes.NOP);
		builder.addInstruction(i2);

		// mark i2 as covered
		builder.addProbe(1, 0);

		// coverage should not be propagated to i1
		Map<AbstractInsnNode, Instruction> map = builder.getInstructions();
		assertEquals(CounterImpl.COUNTER_1_0,
				map.get(i1).getInstructionCounter());
	}

	@Test
	public void subsequent_instructions_should_be_linked_after_label_marked_as_successor() {
		InsnNode i1 = new InsnNode(Opcodes.NOP);
		builder.addInstruction(i1);

		Label l = new Label();
		LabelInfo.setSuccessor(l);
		builder.addLabel(l);
		InsnNode i2 = new InsnNode(Opcodes.NOP);
		builder.addInstruction(i2);

		// mark i2 as covered
		builder.addProbe(1, 0);

		// coverage should be propagated to i1
		Map<AbstractInsnNode, Instruction> map = builder.getInstructions();
		assertEquals(CounterImpl.COUNTER_0_1,
				map.get(i1).getInstructionCounter());
	}

	@Test
	public void subsequent_instructions_should_not_be_linked_after_label_not_marked_as_successor() {
		InsnNode i1 = new InsnNode(Opcodes.NOP);
		builder.addInstruction(i1);

		builder.addLabel(new Label());
		InsnNode i2 = new InsnNode(Opcodes.NOP);
		builder.addInstruction(i2);

		// mark i2 as covered
		builder.addProbe(1, 0);

		// coverage should not be propagated to i1
		Map<AbstractInsnNode, Instruction> map = builder.getInstructions();
		assertEquals(CounterImpl.COUNTER_1_0,
				map.get(i1).getInstructionCounter());
	}

	@Test
	public void jumps_should_propagate_coverage_status() {
		InsnNode i1 = new InsnNode(Opcodes.NOP);
		builder.addInstruction(i1);
		Label l2 = new Label();
		builder.addJump(l2, 0);

		builder.addLabel(l2);
		InsnNode i2 = new InsnNode(Opcodes.NOP);
		builder.addInstruction(i2);

		// mark i2 as covered
		builder.addProbe(1, 0);

		// coverage should be propagated to i1
		Map<AbstractInsnNode, Instruction> map = builder.getInstructions();
		assertEquals(CounterImpl.COUNTER_0_1,
				map.get(i1).getInstructionCounter());
	}

}