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