1 // Copyright (C) 2023 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include <gtest/gtest.h>
16 #include <string>
17 #include "MonotonicMap.h"
18
19 class MagmaTest : public ::testing::Test {
20 protected:
SetUpTestSuite()21 static void SetUpTestSuite() {}
TearDownTestSuite()22 static void TearDownTestSuite() {}
SetUp()23 void SetUp() override {}
TearDown()24 void TearDown() override {}
25 };
26
TEST_F(MagmaTest,MonotonicMap)27 TEST_F(MagmaTest, MonotonicMap) {
28 struct MapTester{
29 MapTester(int i, std::string s) {
30 x = i + s.length();
31 }
32 uint64_t x;
33 };
34 gfxstream::magma::MonotonicMap<uint64_t, MapTester> m;
35
36 auto k1 = m.create(42, "hello");
37 EXPECT_EQ(k1, 1);
38 auto v1 = m.get(k1);
39 ASSERT_NE(v1, nullptr);
40 EXPECT_EQ(v1->x, 42 + 5);
41
42 auto k2 = m.create(5, "foo");
43 EXPECT_EQ(k2, 2);
44 auto v2 = m.get(k2);
45 ASSERT_NE(v2, nullptr);
46 EXPECT_EQ(v2->x, 5 + 3);
47
48 EXPECT_TRUE(m.erase(k1));
49 EXPECT_FALSE(m.erase(k1));
50
51 auto k3 = m.create(8, "bar");
52 EXPECT_EQ(k3, 3);
53 auto v3 = m.get(k3);
54 ASSERT_NE(v3, nullptr);
55 EXPECT_EQ(v3->x, 11);
56
57 auto v2b = m.get(k2);
58 EXPECT_EQ(v2, v2b);
59 }
60