Artem Udovichenko | d9786b0 | 2015-10-14 16:36:55 +0300 | [diff] [blame] | 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" |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 22 | #include "scoped_thread_state_change-inl.h" |
Artem Udovichenko | d9786b0 | 2015-10-14 16:36:55 +0300 | [diff] [blame] | 23 | #include "type_lookup_table.h" |
| 24 | #include "utf-inl.h" |
| 25 | |
| 26 | namespace art { |
| 27 | |
Mathieu Chartier | 91c9116 | 2016-01-15 09:48:15 -0800 | [diff] [blame] | 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> {}; |
Artem Udovichenko | d9786b0 | 2015-10-14 16:36:55 +0300 | [diff] [blame] | 32 | |
| 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 | |
Mathieu Chartier | 91c9116 | 2016-01-15 09:48:15 -0800 | [diff] [blame] | 42 | TEST_P(TypeLookupTableTest, Find) { |
Artem Udovichenko | d9786b0 | 2015-10-14 16:36:55 +0300 | [diff] [blame] | 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()); |
Mathieu Chartier | 91c9116 | 2016-01-15 09:48:15 -0800 | [diff] [blame] | 47 | auto pair = GetParam(); |
| 48 | const char* descriptor = pair.first; |
Artem Udovichenko | d9786b0 | 2015-10-14 16:36:55 +0300 | [diff] [blame] | 49 | size_t hash = ComputeModifiedUtf8Hash(descriptor); |
| 50 | uint32_t class_def_idx = table->Lookup(descriptor, hash); |
Mathieu Chartier | 91c9116 | 2016-01-15 09:48:15 -0800 | [diff] [blame] | 51 | ASSERT_EQ(pair.second, class_def_idx); |
Artem Udovichenko | d9786b0 | 2015-10-14 16:36:55 +0300 | [diff] [blame] | 52 | } |
| 53 | |
Mathieu Chartier | 91c9116 | 2016-01-15 09:48:15 -0800 | [diff] [blame] | 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))); |
Artem Udovichenko | d9786b0 | 2015-10-14 16:36:55 +0300 | [diff] [blame] | 66 | } // namespace art |