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 
26 namespace art {
27 
28 class AtomicDexRefMapTest : public CommonRuntimeTest {};
29 
TEST_F(AtomicDexRefMapTest,RunTests)30 TEST_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   std::vector<const DexFile*> registered_dex_files = map.GetDexFiles();
45   EXPECT_EQ(1u, registered_dex_files.size());
46   EXPECT_TRUE(registered_dex_files[0] == dex.get());
47   EXPECT_GT(dex->NumMethodIds(), 10u);
48   // After we have added the get should succeed but return the default value.
49   EXPECT_TRUE(map.Get(MethodReference(dex.get(), 1), &value));
50   EXPECT_EQ(value, 0);
51   // Actually insert an item and make sure we can retreive it.
52   static const int kInsertValue = 44;
53   EXPECT_TRUE(map.Insert(MethodReference(dex.get(), 1), 0, kInsertValue) ==
54               Map::kInsertResultSuccess);
55   EXPECT_TRUE(map.Get(MethodReference(dex.get(), 1), &value));
56   EXPECT_EQ(value, kInsertValue);
57   static const int kInsertValue2 = 123;
58   EXPECT_TRUE(map.Insert(MethodReference(dex.get(), 2), 0, kInsertValue2) ==
59               Map::kInsertResultSuccess);
60   EXPECT_TRUE(map.Get(MethodReference(dex.get(), 1), &value));
61   EXPECT_EQ(value, kInsertValue);
62   EXPECT_TRUE(map.Get(MethodReference(dex.get(), 2), &value));
63   EXPECT_EQ(value, kInsertValue2);
64   // Error case: Incorrect expected value for CAS.
65   EXPECT_TRUE(map.Insert(MethodReference(dex.get(), 1), 0, kInsertValue + 1) ==
66       Map::kInsertResultCASFailure);
67   // Correctly overwrite the value and verify.
68   EXPECT_TRUE(map.Insert(MethodReference(dex.get(), 1), kInsertValue, kInsertValue + 1) ==
69       Map::kInsertResultSuccess);
70   EXPECT_TRUE(map.Get(MethodReference(dex.get(), 1), &value));
71   EXPECT_EQ(value, kInsertValue + 1);
72 }
73 
74 }  // namespace art
75