blob: b6ab6da78cc45adf655e5e13a4d001404f805d94 [file] [log] [blame]
Artem Udovichenkod9786b02015-10-14 16:36:55 +03001/*
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 Gampe8cf9cb32017-07-19 09:28:38 -070017#include "type_lookup_table.h"
Artem Udovichenkod9786b02015-10-14 16:36:55 +030018
19#include <memory>
20
21#include "common_runtime_test.h"
David Sehr9e734c72018-01-04 17:56:19 -080022#include "dex/dex_file-inl.h"
David Sehr0225f8e2018-01-31 08:52:24 +000023#include "dex/utf-inl.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070024#include "scoped_thread_state_change-inl.h"
Artem Udovichenkod9786b02015-10-14 16:36:55 +030025
26namespace art {
27
Mathieu Chartier91c91162016-01-15 09:48:15 -080028using DescriptorClassDefIdxPair = std::pair<const char*, uint32_t>;
29class TypeLookupTableTest : public CommonRuntimeTestWithParam<DescriptorClassDefIdxPair> {};
Artem Udovichenkod9786b02015-10-14 16:36:55 +030030
31TEST_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 Chartier91c91162016-01-15 09:48:15 -080040TEST_P(TypeLookupTableTest, Find) {
Artem Udovichenkod9786b02015-10-14 16:36:55 +030041 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 Chartier91c91162016-01-15 09:48:15 -080045 auto pair = GetParam();
46 const char* descriptor = pair.first;
Artem Udovichenkod9786b02015-10-14 16:36:55 +030047 size_t hash = ComputeModifiedUtf8Hash(descriptor);
48 uint32_t class_def_idx = table->Lookup(descriptor, hash);
Mathieu Chartier91c91162016-01-15 09:48:15 -080049 ASSERT_EQ(pair.second, class_def_idx);
Artem Udovichenkod9786b02015-10-14 16:36:55 +030050}
51
Mathieu Chartier91c91162016-01-15 09:48:15 -080052INSTANTIATE_TEST_CASE_P(FindNonExistingClassWithoutCollisions,
53 TypeLookupTableTest,
54 testing::Values(DescriptorClassDefIdxPair("LAB;", 1U)));
55INSTANTIATE_TEST_CASE_P(FindNonExistingClassWithCollisions,
56 TypeLookupTableTest,
Andreas Gampee2abbc62017-09-15 11:59:26 -070057 testing::Values(DescriptorClassDefIdxPair("LDA;", dex::kDexNoIndex)));
Mathieu Chartier91c91162016-01-15 09:48:15 -080058INSTANTIATE_TEST_CASE_P(FindClassNoCollisions,
59 TypeLookupTableTest,
60 testing::Values(DescriptorClassDefIdxPair("LC;", 2U)));
61INSTANTIATE_TEST_CASE_P(FindClassWithCollisions,
62 TypeLookupTableTest,
63 testing::Values(DescriptorClassDefIdxPair("LAB;", 1U)));
Artem Udovichenkod9786b02015-10-14 16:36:55 +030064} // namespace art