Home | History | Annotate | Download | only in runtime
      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 #include "type_lookup_table.h"
     18 
     19 #include <memory>
     20 
     21 #include "common_runtime_test.h"
     22 #include "dex/dex_file-inl.h"
     23 #include "dex/utf-inl.h"
     24 #include "scoped_thread_state_change-inl.h"
     25 
     26 namespace art {
     27 
     28 using DescriptorClassDefIdxPair = std::pair<const char*, uint32_t>;
     29 class TypeLookupTableTest : public CommonRuntimeTestWithParam<DescriptorClassDefIdxPair> {};
     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 
     40 TEST_P(TypeLookupTableTest, Find) {
     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());
     45   auto pair = GetParam();
     46   const char* descriptor = pair.first;
     47   size_t hash = ComputeModifiedUtf8Hash(descriptor);
     48   uint32_t class_def_idx = table->Lookup(descriptor, hash);
     49   ASSERT_EQ(pair.second, class_def_idx);
     50 }
     51 
     52 INSTANTIATE_TEST_CASE_P(FindNonExistingClassWithoutCollisions,
     53                         TypeLookupTableTest,
     54                         testing::Values(DescriptorClassDefIdxPair("LAB;", 1U)));
     55 INSTANTIATE_TEST_CASE_P(FindNonExistingClassWithCollisions,
     56                         TypeLookupTableTest,
     57                         testing::Values(DescriptorClassDefIdxPair("LDA;", dex::kDexNoIndex)));
     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)));
     64 }  // namespace art
     65