1/* 2 * Copyright (C) 2016 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 "atomic_dex_ref_map-inl.h" 18 19#include <memory> 20 21#include "common_runtime_test.h" 22#include "dex/dex_file-inl.h" 23#include "dex/method_reference.h" 24#include "scoped_thread_state_change-inl.h" 25 26namespace art { 27 28class AtomicDexRefMapTest : public CommonRuntimeTest {}; 29 30TEST_F(AtomicDexRefMapTest, RunTests) { 31 ScopedObjectAccess soa(Thread::Current()); 32 std::unique_ptr<const DexFile> dex(OpenTestDexFile("Interfaces")); 33 ASSERT_TRUE(dex != nullptr); 34 using Map = AtomicDexRefMap<MethodReference, int>; 35 Map map; 36 int value = 123; 37 // Error case: Not already inserted. 38 EXPECT_FALSE(map.Get(MethodReference(dex.get(), 1), &value)); 39 EXPECT_FALSE(map.HaveDexFile(dex.get())); 40 // Error case: Dex file not registered. 41 EXPECT_TRUE(map.Insert(MethodReference(dex.get(), 1), 0, 1) == Map::kInsertResultInvalidDexFile); 42 map.AddDexFile(dex.get()); 43 EXPECT_TRUE(map.HaveDexFile(dex.get())); 44 EXPECT_GT(dex->NumMethodIds(), 10u); 45 // After we have added the get should succeed but return the default value. 46 EXPECT_TRUE(map.Get(MethodReference(dex.get(), 1), &value)); 47 EXPECT_EQ(value, 0); 48 // Actually insert an item and make sure we can retreive it. 49 static const int kInsertValue = 44; 50 EXPECT_TRUE(map.Insert(MethodReference(dex.get(), 1), 0, kInsertValue) == 51 Map::kInsertResultSuccess); 52 EXPECT_TRUE(map.Get(MethodReference(dex.get(), 1), &value)); 53 EXPECT_EQ(value, kInsertValue); 54 static const int kInsertValue2 = 123; 55 EXPECT_TRUE(map.Insert(MethodReference(dex.get(), 2), 0, kInsertValue2) == 56 Map::kInsertResultSuccess); 57 EXPECT_TRUE(map.Get(MethodReference(dex.get(), 1), &value)); 58 EXPECT_EQ(value, kInsertValue); 59 EXPECT_TRUE(map.Get(MethodReference(dex.get(), 2), &value)); 60 EXPECT_EQ(value, kInsertValue2); 61 // Error case: Incorrect expected value for CAS. 62 EXPECT_TRUE(map.Insert(MethodReference(dex.get(), 1), 0, kInsertValue + 1) == 63 Map::kInsertResultCASFailure); 64 // Correctly overwrite the value and verify. 65 EXPECT_TRUE(map.Insert(MethodReference(dex.get(), 1), kInsertValue, kInsertValue + 1) == 66 Map::kInsertResultSuccess); 67 EXPECT_TRUE(map.Get(MethodReference(dex.get(), 1), &value)); 68 EXPECT_EQ(value, kInsertValue + 1); 69} 70 71} // namespace art 72