aboutsummaryrefslogtreecommitdiff
path: root/org.jacoco.core.test/src/org/jacoco/core/internal/analysis/InstructionTest.java
blob: e7e167e21d8a503c9dd83ca42f2527caf2dcb67c (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
/*******************************************************************************
 * 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 org.junit.Before;
import org.junit.Test;

/**
 * Unit tests for {@link Instruction}.
 */
public class InstructionTest {

	private Instruction instruction;

	@Before
	public void setup() {
		instruction = new Instruction(123);
	}

	@Test
	public void getLine_should_return_line_number() {
		assertEquals(123, instruction.getLine());
	}

	@Test
	public void new_instance_should_have_no_coverage_and_no_branches() {
		assertEquals(CounterImpl.COUNTER_1_0,
				instruction.getInstructionCounter());
		assertEquals(CounterImpl.COUNTER_0_0, instruction.getBranchCounter());
	}

	@Test
	public void addBranchWithInstruction_should_not_increment_branches_when_only_one_branch_is_added() {
		instruction.addBranch(new Instruction(122), 0);

		assertEquals(CounterImpl.COUNTER_0_0, instruction.getBranchCounter());
	}

	@Test
	public void addBranchWithInstruction_should_increment_branches_when_two_branches_are_added() {
		instruction.addBranch(new Instruction(122), 0);
		instruction.addBranch(new Instruction(123), 1);

		assertEquals(CounterImpl.getInstance(2, 0),
				instruction.getBranchCounter());
	}

	@Test
	public void addBranchWithInstruction_should_propagate_existing_coverage_status() {
		final Instruction target = new Instruction(122);
		target.addBranch(true, 0);

		instruction.addBranch(target, 0);

		assertEquals(CounterImpl.COUNTER_0_1,
				instruction.getInstructionCounter());
	}

	@Test
	public void addBranchWithProbe_should_increment_branches_when_covered() {
		instruction.addBranch(true, 0);
		instruction.addBranch(true, 1);

		assertEquals(CounterImpl.getInstance(0, 1),
				instruction.getInstructionCounter());
		assertEquals(CounterImpl.getInstance(0, 2),
				instruction.getBranchCounter());
	}

	@Test
	public void addBranchWithProbe_should_increment_branches_when_not_covered() {
		instruction.addBranch(false, 0);
		instruction.addBranch(false, 1);

		assertEquals(CounterImpl.getInstance(1, 0),
				instruction.getInstructionCounter());
		assertEquals(CounterImpl.getInstance(2, 0),
				instruction.getBranchCounter());
	}

	@Test
	public void addBranchWithProbe_should_increment_branches_when_partly_covered() {
		instruction.addBranch(false, 0);
		instruction.addBranch(true, 1);

		assertEquals(CounterImpl.getInstance(0, 1),
				instruction.getInstructionCounter());
		assertEquals(CounterImpl.getInstance(1, 1),
				instruction.getBranchCounter());
	}

	@Test
	public void addBranchWithProbe_should_propagate_coverage_status_to_existing_predecessors() {
		final Instruction i1 = new Instruction(124);
		final Instruction i2 = new Instruction(125);
		instruction.addBranch(i1, 3);
		i1.addBranch(i2, 5);

		i2.addBranch(true, 8);

		assertEquals(CounterImpl.COUNTER_0_1,
				instruction.getInstructionCounter());
	}

	@Test
	public void addBranch_should_count_large_number_of_branches() {
		for (int branch = 0; branch < 0x1000; branch++) {
			instruction.addBranch(true, branch);
		}

		assertEquals(CounterImpl.getInstance(0, 0x1000),
				instruction.getBranchCounter());
	}

	@Test
	public void addBranch_should_propagate_coverage_status_over_very_long_sequence() {
		Instruction next = instruction;
		for (int i = 0; i < 0x10000; i++) {
			final Instruction insn = new Instruction(i);
			next.addBranch(insn, 0);
			next = insn;
		}
		next.addBranch(true, 0);

		assertEquals(CounterImpl.COUNTER_0_1,
				instruction.getInstructionCounter());
	}

	@Test
	public void merge_should_calculate_superset_of_covered_branches() {
		final Instruction i1 = new Instruction(124);
		i1.addBranch(false, 1);
		i1.addBranch(false, 2);
		i1.addBranch(true, 3);
		i1.addBranch(true, 4);
		final Instruction i2 = new Instruction(124);
		i2.addBranch(false, 1);
		i2.addBranch(true, 2);
		i2.addBranch(false, 3);
		i2.addBranch(true, 4);

		instruction = i1.merge(i2);

		assertEquals(CounterImpl.getInstance(1, 3),
				instruction.getBranchCounter());
	}

	@Test
	public void replaceBranches_should_calculate_coverage_on_new_branches() {
		Instruction i1 = new Instruction(1);
		Instruction i2 = new Instruction(2);
		Instruction i3 = new Instruction(3);
		i3.addBranch(true, 0);

		instruction = instruction.replaceBranches(Arrays.asList(i1, i2, i3));

		assertEquals(CounterImpl.getInstance(2, 1),
				instruction.getBranchCounter());
	}
}