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