blob: 7f500cc9a1668805de3415eb765e4a882fe2afc8 [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"
22#include "scoped_thread_state_change.h"
23#include "type_lookup_table.h"
24#include "utf-inl.h"
25
26namespace art {
27
28class TypeLookupTableTest : public CommonRuntimeTest {
29 public:
30 size_t kDexNoIndex = DexFile::kDexNoIndex; // Make copy to prevent linking errors.
31};
32
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
42TEST_F(TypeLookupTableTest, FindNonExistingClassWithoutCollisions) {
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 const char* descriptor = "LBA;";
48 size_t hash = ComputeModifiedUtf8Hash(descriptor);
49 uint32_t class_def_idx = table->Lookup(descriptor, hash);
50 ASSERT_EQ(kDexNoIndex, class_def_idx);
51}
52
53TEST_F(TypeLookupTableTest, FindNonExistingClassWithCollisions) {
54 ScopedObjectAccess soa(Thread::Current());
55 std::unique_ptr<const DexFile> dex_file(OpenTestDexFile("Lookup"));
56 std::unique_ptr<TypeLookupTable> table(TypeLookupTable::Create(*dex_file));
57 ASSERT_NE(nullptr, table.get());
58 const char* descriptor = "LDA;";
59 size_t hash = ComputeModifiedUtf8Hash(descriptor);
60 uint32_t class_def_idx = table->Lookup(descriptor, hash);
61 ASSERT_EQ(kDexNoIndex, class_def_idx);
62}
63
64TEST_F(TypeLookupTableTest, FindClassNoCollisions) {
65 ScopedObjectAccess soa(Thread::Current());
66 std::unique_ptr<const DexFile> dex_file(OpenTestDexFile("Lookup"));
67 std::unique_ptr<TypeLookupTable> table(TypeLookupTable::Create(*dex_file));
68 ASSERT_NE(nullptr, table.get());
69 const char* descriptor = "LC;";
70 size_t hash = ComputeModifiedUtf8Hash(descriptor);
71 uint32_t class_def_idx = table->Lookup(descriptor, hash);
72 ASSERT_EQ(2U, class_def_idx);
73}
74
75TEST_F(TypeLookupTableTest, FindClassWithCollisions) {
76 ScopedObjectAccess soa(Thread::Current());
77 std::unique_ptr<const DexFile> dex_file(OpenTestDexFile("Lookup"));
78 std::unique_ptr<TypeLookupTable> table(TypeLookupTable::Create(*dex_file));
79 ASSERT_NE(nullptr, table.get());
80 const char* descriptor = "LAB;";
81 size_t hash = ComputeModifiedUtf8Hash(descriptor);
82 uint32_t class_def_idx = table->Lookup(descriptor, hash);
83 ASSERT_EQ(1U, class_def_idx);
84}
85
86} // namespace art