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 | |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 17 | #include "type_lookup_table.h" |
Artem Udovichenko | d9786b0 | 2015-10-14 16:36:55 +0300 | [diff] [blame] | 18 | |
| 19 | #include <memory> |
| 20 | |
| 21 | #include "common_runtime_test.h" |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 22 | #include "dex/dex_file-inl.h" |
David Sehr | 0225f8e | 2018-01-31 08:52:24 +0000 | [diff] [blame] | 23 | #include "dex/utf-inl.h" |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 24 | #include "scoped_thread_state_change-inl.h" |
Artem Udovichenko | d9786b0 | 2015-10-14 16:36:55 +0300 | [diff] [blame] | 25 | |
| 26 | namespace art { |
| 27 | |
Mathieu Chartier | 91c9116 | 2016-01-15 09:48:15 -0800 | [diff] [blame] | 28 | using DescriptorClassDefIdxPair = std::pair<const char*, uint32_t>; |
| 29 | class TypeLookupTableTest : public CommonRuntimeTestWithParam<DescriptorClassDefIdxPair> {}; |
Artem Udovichenko | d9786b0 | 2015-10-14 16:36:55 +0300 | [diff] [blame] | 30 | |
| 31 | TEST_F(TypeLookupTableTest, CreateLookupTable) { |
| 32 | ScopedObjectAccess soa(Thread::Current()); |
| 33 | std::unique_ptr<const DexFile> dex_file(OpenTestDexFile("Lookup")); |
| 34 | std::unique_ptr<TypeLookupTable> table(TypeLookupTable::Create(*dex_file)); |
| 35 | ASSERT_NE(nullptr, table.get()); |
| 36 | ASSERT_NE(nullptr, table->RawData()); |
| 37 | ASSERT_EQ(32U, table->RawDataLength()); |
| 38 | } |
| 39 | |
Mathieu Chartier | 91c9116 | 2016-01-15 09:48:15 -0800 | [diff] [blame] | 40 | TEST_P(TypeLookupTableTest, Find) { |
Artem Udovichenko | d9786b0 | 2015-10-14 16:36:55 +0300 | [diff] [blame] | 41 | ScopedObjectAccess soa(Thread::Current()); |
| 42 | std::unique_ptr<const DexFile> dex_file(OpenTestDexFile("Lookup")); |
| 43 | std::unique_ptr<TypeLookupTable> table(TypeLookupTable::Create(*dex_file)); |
| 44 | ASSERT_NE(nullptr, table.get()); |
Mathieu Chartier | 91c9116 | 2016-01-15 09:48:15 -0800 | [diff] [blame] | 45 | auto pair = GetParam(); |
| 46 | const char* descriptor = pair.first; |
Artem Udovichenko | d9786b0 | 2015-10-14 16:36:55 +0300 | [diff] [blame] | 47 | size_t hash = ComputeModifiedUtf8Hash(descriptor); |
| 48 | uint32_t class_def_idx = table->Lookup(descriptor, hash); |
Mathieu Chartier | 91c9116 | 2016-01-15 09:48:15 -0800 | [diff] [blame] | 49 | ASSERT_EQ(pair.second, class_def_idx); |
Artem Udovichenko | d9786b0 | 2015-10-14 16:36:55 +0300 | [diff] [blame] | 50 | } |
| 51 | |
Mathieu Chartier | 91c9116 | 2016-01-15 09:48:15 -0800 | [diff] [blame] | 52 | INSTANTIATE_TEST_CASE_P(FindNonExistingClassWithoutCollisions, |
| 53 | TypeLookupTableTest, |
| 54 | testing::Values(DescriptorClassDefIdxPair("LAB;", 1U))); |
| 55 | INSTANTIATE_TEST_CASE_P(FindNonExistingClassWithCollisions, |
| 56 | TypeLookupTableTest, |
Andreas Gampe | e2abbc6 | 2017-09-15 11:59:26 -0700 | [diff] [blame] | 57 | testing::Values(DescriptorClassDefIdxPair("LDA;", dex::kDexNoIndex))); |
Mathieu Chartier | 91c9116 | 2016-01-15 09:48:15 -0800 | [diff] [blame] | 58 | INSTANTIATE_TEST_CASE_P(FindClassNoCollisions, |
| 59 | TypeLookupTableTest, |
| 60 | testing::Values(DescriptorClassDefIdxPair("LC;", 2U))); |
| 61 | INSTANTIATE_TEST_CASE_P(FindClassWithCollisions, |
| 62 | TypeLookupTableTest, |
| 63 | testing::Values(DescriptorClassDefIdxPair("LAB;", 1U))); |
Artem Udovichenko | d9786b0 | 2015-10-14 16:36:55 +0300 | [diff] [blame] | 64 | } // namespace art |