blob: ec38b4154ee129312fa386692d858efe52dd897c [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
17
18#include <memory>
19
20#include "common_runtime_test.h"
21#include "dex_file-inl.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070022#include "scoped_thread_state_change-inl.h"
Artem Udovichenkod9786b02015-10-14 16:36:55 +030023#include "type_lookup_table.h"
24#include "utf-inl.h"
25
26namespace art {
27
Mathieu Chartier91c91162016-01-15 09:48:15 -080028static const size_t kDexNoIndex = DexFile::kDexNoIndex; // Make copy to prevent linking errors.
29
30using DescriptorClassDefIdxPair = std::pair<const char*, uint32_t>;
31class TypeLookupTableTest : public CommonRuntimeTestWithParam<DescriptorClassDefIdxPair> {};
Artem Udovichenkod9786b02015-10-14 16:36:55 +030032
33TEST_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 Chartier91c91162016-01-15 09:48:15 -080042TEST_P(TypeLookupTableTest, Find) {
Artem Udovichenkod9786b02015-10-14 16:36:55 +030043 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 Chartier91c91162016-01-15 09:48:15 -080047 auto pair = GetParam();
48 const char* descriptor = pair.first;
Artem Udovichenkod9786b02015-10-14 16:36:55 +030049 size_t hash = ComputeModifiedUtf8Hash(descriptor);
50 uint32_t class_def_idx = table->Lookup(descriptor, hash);
Mathieu Chartier91c91162016-01-15 09:48:15 -080051 ASSERT_EQ(pair.second, class_def_idx);
Artem Udovichenkod9786b02015-10-14 16:36:55 +030052}
53
Mathieu Chartier91c91162016-01-15 09:48:15 -080054INSTANTIATE_TEST_CASE_P(FindNonExistingClassWithoutCollisions,
55 TypeLookupTableTest,
56 testing::Values(DescriptorClassDefIdxPair("LAB;", 1U)));
57INSTANTIATE_TEST_CASE_P(FindNonExistingClassWithCollisions,
58 TypeLookupTableTest,
59 testing::Values(DescriptorClassDefIdxPair("LDA;", kDexNoIndex)));
60INSTANTIATE_TEST_CASE_P(FindClassNoCollisions,
61 TypeLookupTableTest,
62 testing::Values(DescriptorClassDefIdxPair("LC;", 2U)));
63INSTANTIATE_TEST_CASE_P(FindClassWithCollisions,
64 TypeLookupTableTest,
65 testing::Values(DescriptorClassDefIdxPair("LAB;", 1U)));
Artem Udovichenkod9786b02015-10-14 16:36:55 +030066} // namespace art