1 /*
2  * Copyright (C) 2011 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 #include "type_lookup_table.h"
18 
19 #include <memory>
20 
21 #include "base/common_art_test.h"
22 #include "dex/dex_file-inl.h"
23 #include "dex/utf-inl.h"
24 #include "scoped_thread_state_change-inl.h"
25 
26 namespace art {
27 
28 using DescriptorClassDefIdxPair = std::pair<const char*, uint32_t>;
29 class TypeLookupTableTest : public CommonArtTestWithParam<DescriptorClassDefIdxPair> {};
30 
TEST_F(TypeLookupTableTest,CreateLookupTable)31 TEST_F(TypeLookupTableTest, CreateLookupTable) {
32   std::unique_ptr<const DexFile> dex_file(OpenTestDexFile("Lookup"));
33   TypeLookupTable table = TypeLookupTable::Create(*dex_file);
34   ASSERT_TRUE(table.Valid());
35   ASSERT_NE(nullptr, table.RawData());
36   ASSERT_EQ(32U, table.RawDataLength());
37 }
38 
TEST_P(TypeLookupTableTest,Find)39 TEST_P(TypeLookupTableTest, Find) {
40   std::unique_ptr<const DexFile> dex_file(OpenTestDexFile("Lookup"));
41   TypeLookupTable table(TypeLookupTable::Create(*dex_file));
42   ASSERT_TRUE(table.Valid());
43   auto pair = GetParam();
44   const char* descriptor = pair.first;
45   size_t hash = ComputeModifiedUtf8Hash(descriptor);
46   uint32_t class_def_idx = table.Lookup(descriptor, hash);
47   ASSERT_EQ(pair.second, class_def_idx);
48 }
49 
50 INSTANTIATE_TEST_CASE_P(FindNonExistingClassWithoutCollisions,
51                         TypeLookupTableTest,
52                         testing::Values(DescriptorClassDefIdxPair("LAB;", 1U)));
53 INSTANTIATE_TEST_CASE_P(FindNonExistingClassWithCollisions,
54                         TypeLookupTableTest,
55                         testing::Values(DescriptorClassDefIdxPair("LDA;", dex::kDexNoIndex)));
56 INSTANTIATE_TEST_CASE_P(FindClassNoCollisions,
57                         TypeLookupTableTest,
58                         testing::Values(DescriptorClassDefIdxPair("LC;", 2U)));
59 INSTANTIATE_TEST_CASE_P(FindClassWithCollisions,
60                         TypeLookupTableTest,
61                         testing::Values(DescriptorClassDefIdxPair("LAB;", 1U)));
62 }  // namespace art
63