1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.bcel.verifier.statics;
21
22 import static org.junit.jupiter.api.Assertions.assertEquals;
23 import static org.junit.jupiter.api.Assertions.assertNull;
24 import static org.junit.jupiter.api.Assertions.assertThrows;
25 import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively;
26
27 import java.time.Duration;
28
29 import org.apache.bcel.generic.Type;
30 import org.apache.bcel.verifier.exc.LocalVariableInfoInconsistentException;
31 import org.junit.jupiter.api.Test;
32
33
34
35
36 class LocalVariableInfoTest {
37
38 @Test
39 void testConflictingNameRejected() {
40 final LocalVariableInfo info = new LocalVariableInfo();
41 info.add("a", 0, 10, Type.INT);
42 assertThrows(LocalVariableInfoInconsistentException.class, () -> info.add("b", 5, 10, Type.INT));
43 }
44
45 @Test
46 void testConflictingTypeRejected() {
47 final LocalVariableInfo info = new LocalVariableInfo();
48 info.add("a", 0, 10, Type.INT);
49 assertThrows(LocalVariableInfoInconsistentException.class, () -> info.add("a", 10, 5, Type.FLOAT));
50 }
51
52 @Test
53 void testConsistentOverlapAccepted() {
54 final LocalVariableInfo info = new LocalVariableInfo();
55 info.add("a", 0, 10, Type.INT);
56 info.add("a", 5, 10, Type.INT);
57 assertEquals("a", info.getName(15));
58 assertNull(info.getName(16));
59 }
60
61 @Test
62 void testLookups() {
63 final LocalVariableInfo info = new LocalVariableInfo();
64 info.add("a", 0, 10, Type.INT);
65 info.add("b", 20, 5, Type.FLOAT);
66 assertEquals("a", info.getName(0));
67 assertEquals("a", info.getName(10));
68 assertEquals(Type.INT, info.getType(5));
69 assertNull(info.getName(11));
70 assertNull(info.getType(19));
71 assertEquals("b", info.getName(20));
72 assertEquals(Type.FLOAT, info.getType(25));
73 assertNull(info.getName(26));
74 }
75
76
77
78
79
80 @Test
81 void testManyMaximumLengthEntriesFinishQuickly() {
82 assertTimeoutPreemptively(Duration.ofSeconds(10), () -> {
83 final LocalVariableInfo info = new LocalVariableInfo();
84 for (int i = 0; i < 65_535; i++) {
85 info.add("dup", 0, 65_535, Type.INT);
86 }
87 assertEquals("dup", info.getName(65_535));
88 assertNull(info.getName(65_536));
89 });
90 }
91 }