• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.net.apf
18 
19 import androidx.test.filters.SmallTest
20 import androidx.test.runner.AndroidJUnit4
21 import com.android.testutils.assertThrows
22 import java.util.NoSuchElementException
23 import java.util.concurrent.atomic.AtomicReference
24 import kotlin.test.assertEquals
25 import org.junit.Before
26 import org.junit.Test
27 import org.junit.runner.RunWith
28 import org.mockito.Mock
29 import org.mockito.Mockito.inOrder
30 import org.mockito.MockitoAnnotations
31 
32 @RunWith(AndroidJUnit4::class)
33 @SmallTest
34 class JumpTableTest {
35 
36     @Mock
37     lateinit var gen: ApfGenerator
38 
39     @Before
setUpnull40     fun setUp() {
41         MockitoAnnotations.initMocks(this)
42     }
43 
44     @Test(expected = NullPointerException::class)
testNullStartLabelnull45     fun testNullStartLabel() {
46         // Can't use "null" because the method is @NonNull.
47         JumpTable(AtomicReference<String>(null).get(), 10)
48     }
49 
50     @Test(expected = IllegalArgumentException::class)
testNegativeSlotnull51     fun testNegativeSlot() {
52         JumpTable("my_jump_table", -1)
53     }
54 
55     @Test(expected = IllegalArgumentException::class)
testSlotTooLargenull56     fun testSlotTooLarge() {
57         JumpTable("my_jump_table", 13)
58     }
59 
60     @Test
testValidSlotNumbersnull61     fun testValidSlotNumbers() {
62         JumpTable("my_jump_table", 1)
63         JumpTable("my_jump_table", 10)
64         JumpTable("my_jump_table", 12)
65     }
66 
67     @Test
testGetStartLabelnull68     fun testGetStartLabel() {
69         assertEquals("xyz", JumpTable("xyz", 3).startLabel)
70         assertEquals("abc", JumpTable("abc", 9).startLabel)
71     }
72 
73     @Test
testCodeGenerationnull74     fun testCodeGeneration() {
75         val name = "my_jump_table"
76         val slot = 7
77 
78         val j = JumpTable(name, slot)
79         j.addLabel("foo")
80         j.addLabel("bar")
81         j.addLabel("bar")
82         j.addLabel("baz")
83 
84         assertEquals(0, j.getIndex("foo"))
85         assertEquals(1, j.getIndex("bar"))
86         assertEquals(2, j.getIndex("baz"))
87 
88         assertThrows(NoSuchElementException::class.java) {
89             j.getIndex("nonexistent")
90         }
91 
92         val inOrder = inOrder(gen)
93 
94         j.generate(gen)
95 
96         inOrder.verify(gen).defineLabel(name)
97         inOrder.verify(gen).addLoadFromMemory(ApfGenerator.Register.R0, slot)
98         inOrder.verify(gen).addJumpIfR0Equals(0, "foo")
99         inOrder.verify(gen).addJumpIfR0Equals(1, "bar")
100         inOrder.verify(gen).addJumpIfR0Equals(2, "baz")
101         inOrder.verify(gen).addJump(ApfGenerator.PASS_LABEL)
102         inOrder.verifyNoMoreInteractions()
103     }
104 }
105