1/*******************************************************************************
2 * Copyright (c) 2009, 2015 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 *******************************************************************************/
12package org.jacoco.core.internal.flow;
13
14import static org.junit.Assert.assertEquals;
15
16import org.jacoco.core.instr.MethodRecorder;
17import org.junit.After;
18import org.junit.Before;
19import org.junit.Test;
20import org.objectweb.asm.Label;
21import org.objectweb.asm.MethodVisitor;
22import org.objectweb.asm.Opcodes;
23import org.objectweb.asm.commons.AnalyzerAdapter;
24
25/**
26 * Unit tests for {@link FrameSnapshot}.
27 */
28public class FrameSnapshotTest {
29
30	private AnalyzerAdapter analyzer;
31	private IFrame frame;
32
33	private MethodRecorder expected;
34
35	private MethodVisitor expectedVisitor;
36
37	@Before
38	public void setup() {
39		analyzer = new AnalyzerAdapter("Foo", 0, "doit", "()V", null);
40		expected = new MethodRecorder();
41		expectedVisitor = expected.getVisitor();
42	}
43
44	@After
45	public void teardown() {
46		MethodRecorder actual = new MethodRecorder();
47		frame.accept(actual.getVisitor());
48		assertEquals(expected, actual);
49	}
50
51	@Test
52	public void testNullAnalyzer() {
53		frame = FrameSnapshot.create(null, 0);
54	}
55
56	@Test
57	public void testNoFrame() {
58		analyzer.visitJumpInsn(Opcodes.GOTO, new Label());
59		frame = FrameSnapshot.create(analyzer, 0);
60	}
61
62	@Test
63	public void testFrame() {
64		analyzer.visitInsn(Opcodes.ICONST_0);
65		frame = FrameSnapshot.create(analyzer, 0);
66
67		expectedVisitor.visitFrame(Opcodes.F_FULL, 1, arr("Foo"), 1,
68				arr(Opcodes.INTEGER));
69	}
70
71	@Test
72	public void testReduce() {
73		analyzer.visitInsn(Opcodes.ICONST_0);
74		analyzer.visitInsn(Opcodes.LCONST_0);
75		analyzer.visitInsn(Opcodes.ICONST_0);
76		analyzer.visitInsn(Opcodes.DCONST_0);
77		frame = FrameSnapshot.create(analyzer, 0);
78
79		final Object[] stack = arr(Opcodes.INTEGER, Opcodes.LONG,
80				Opcodes.INTEGER, Opcodes.DOUBLE);
81		expectedVisitor.visitFrame(Opcodes.F_FULL, 1, arr("Foo"), 4, stack);
82	}
83
84	@Test
85	public void testPop() {
86		analyzer.visitInsn(Opcodes.ICONST_0);
87		analyzer.visitInsn(Opcodes.LCONST_0);
88		analyzer.visitInsn(Opcodes.ICONST_0);
89		analyzer.visitInsn(Opcodes.ICONST_0);
90		frame = FrameSnapshot.create(analyzer, 2);
91
92		final Object[] stack = arr(Opcodes.INTEGER, Opcodes.LONG);
93		expectedVisitor.visitFrame(Opcodes.F_FULL, 1, arr("Foo"), 2, stack);
94	}
95
96	private Object[] arr(Object... elements) {
97		return elements;
98	}
99}
100