blob: 4e1ef1248db0ab43579717fd64ef565e9564d9b5 [file] [log] [blame]
Mathieu Chartier9df89312016-11-23 13:28:16 -08001/*
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
Mathieu Chartier93764b82017-07-17 14:51:53 -070017#include "atomic_dex_ref_map-inl.h"
Mathieu Chartier9df89312016-11-23 13:28:16 -080018
19#include <memory>
20
21#include "common_runtime_test.h"
David Sehr9e734c72018-01-04 17:56:19 -080022#include "dex/dex_file-inl.h"
David Sehr312f3b22018-03-19 08:39:26 -070023#include "dex/method_reference.h"
Mathieu Chartier9df89312016-11-23 13:28:16 -080024#include "scoped_thread_state_change-inl.h"
25
26namespace art {
27
Mathieu Chartier93764b82017-07-17 14:51:53 -070028class AtomicDexRefMapTest : public CommonRuntimeTest {};
Mathieu Chartier9df89312016-11-23 13:28:16 -080029
Mathieu Chartier93764b82017-07-17 14:51:53 -070030TEST_F(AtomicDexRefMapTest, RunTests) {
Mathieu Chartier9df89312016-11-23 13:28:16 -080031 ScopedObjectAccess soa(Thread::Current());
32 std::unique_ptr<const DexFile> dex(OpenTestDexFile("Interfaces"));
33 ASSERT_TRUE(dex != nullptr);
Mathieu Chartierfc8b4222017-09-17 13:44:24 -070034 using Map = AtomicDexRefMap<MethodReference, int>;
Mathieu Chartier9df89312016-11-23 13:28:16 -080035 Map map;
36 int value = 123;
37 // Error case: Not already inserted.
Mathieu Chartierfc8b4222017-09-17 13:44:24 -070038 EXPECT_FALSE(map.Get(MethodReference(dex.get(), 1), &value));
Mathieu Chartieracab8d42016-11-23 13:45:58 -080039 EXPECT_FALSE(map.HaveDexFile(dex.get()));
Mathieu Chartier9df89312016-11-23 13:28:16 -080040 // Error case: Dex file not registered.
Mathieu Chartierfc8b4222017-09-17 13:44:24 -070041 EXPECT_TRUE(map.Insert(MethodReference(dex.get(), 1), 0, 1) == Map::kInsertResultInvalidDexFile);
42 map.AddDexFile(dex.get());
Mathieu Chartieracab8d42016-11-23 13:45:58 -080043 EXPECT_TRUE(map.HaveDexFile(dex.get()));
Mathieu Chartier9df89312016-11-23 13:28:16 -080044 EXPECT_GT(dex->NumMethodIds(), 10u);
45 // After we have added the get should succeed but return the default value.
Mathieu Chartierfc8b4222017-09-17 13:44:24 -070046 EXPECT_TRUE(map.Get(MethodReference(dex.get(), 1), &value));
Mathieu Chartier9df89312016-11-23 13:28:16 -080047 EXPECT_EQ(value, 0);
48 // Actually insert an item and make sure we can retreive it.
49 static const int kInsertValue = 44;
Mathieu Chartierfc8b4222017-09-17 13:44:24 -070050 EXPECT_TRUE(map.Insert(MethodReference(dex.get(), 1), 0, kInsertValue) ==
Mathieu Chartier9df89312016-11-23 13:28:16 -080051 Map::kInsertResultSuccess);
Mathieu Chartierfc8b4222017-09-17 13:44:24 -070052 EXPECT_TRUE(map.Get(MethodReference(dex.get(), 1), &value));
Mathieu Chartier9df89312016-11-23 13:28:16 -080053 EXPECT_EQ(value, kInsertValue);
54 static const int kInsertValue2 = 123;
Mathieu Chartierfc8b4222017-09-17 13:44:24 -070055 EXPECT_TRUE(map.Insert(MethodReference(dex.get(), 2), 0, kInsertValue2) ==
Mathieu Chartier9df89312016-11-23 13:28:16 -080056 Map::kInsertResultSuccess);
Mathieu Chartierfc8b4222017-09-17 13:44:24 -070057 EXPECT_TRUE(map.Get(MethodReference(dex.get(), 1), &value));
Mathieu Chartier9df89312016-11-23 13:28:16 -080058 EXPECT_EQ(value, kInsertValue);
Mathieu Chartierfc8b4222017-09-17 13:44:24 -070059 EXPECT_TRUE(map.Get(MethodReference(dex.get(), 2), &value));
Mathieu Chartier9df89312016-11-23 13:28:16 -080060 EXPECT_EQ(value, kInsertValue2);
61 // Error case: Incorrect expected value for CAS.
Mathieu Chartierfc8b4222017-09-17 13:44:24 -070062 EXPECT_TRUE(map.Insert(MethodReference(dex.get(), 1), 0, kInsertValue + 1) ==
Mathieu Chartier9df89312016-11-23 13:28:16 -080063 Map::kInsertResultCASFailure);
64 // Correctly overwrite the value and verify.
Mathieu Chartierfc8b4222017-09-17 13:44:24 -070065 EXPECT_TRUE(map.Insert(MethodReference(dex.get(), 1), kInsertValue, kInsertValue + 1) ==
Mathieu Chartier9df89312016-11-23 13:28:16 -080066 Map::kInsertResultSuccess);
Mathieu Chartierfc8b4222017-09-17 13:44:24 -070067 EXPECT_TRUE(map.Get(MethodReference(dex.get(), 1), &value));
Mathieu Chartier9df89312016-11-23 13:28:16 -080068 EXPECT_EQ(value, kInsertValue + 1);
69}
70
71} // namespace art