1 | //===----------- ImmutableMapTest.cpp - ImmutableMap unit tests ------------===// |
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | |
9 | #include "llvm/ADT/ImmutableMap.h" |
10 | #include "gtest/gtest.h" |
11 | |
12 | using namespace llvm; |
13 | |
14 | namespace { |
15 | |
16 | TEST(ImmutableMapTest, EmptyIntMapTest) { |
17 | ImmutableMap<int, int>::Factory f; |
18 | |
19 | EXPECT_TRUE(f.getEmptyMap() == f.getEmptyMap()); |
20 | EXPECT_FALSE(f.getEmptyMap() != f.getEmptyMap()); |
21 | EXPECT_TRUE(f.getEmptyMap().isEmpty()); |
22 | |
23 | ImmutableMap<int, int> S = f.getEmptyMap(); |
24 | EXPECT_EQ(0u, S.getHeight()); |
25 | EXPECT_TRUE(S.begin() == S.end()); |
26 | EXPECT_FALSE(S.begin() != S.end()); |
27 | } |
28 | |
29 | TEST(ImmutableMapTest, MultiElemIntMapTest) { |
30 | ImmutableMap<int, int>::Factory f; |
31 | ImmutableMap<int, int> S = f.getEmptyMap(); |
32 | |
33 | ImmutableMap<int, int> S2 = f.add(Old: f.add(Old: f.add(Old: S, K: 3, D: 10), K: 4, D: 11), K: 5, D: 12); |
34 | |
35 | EXPECT_TRUE(S.isEmpty()); |
36 | EXPECT_FALSE(S2.isEmpty()); |
37 | |
38 | EXPECT_EQ(nullptr, S.lookup(3)); |
39 | EXPECT_EQ(nullptr, S.lookup(9)); |
40 | |
41 | EXPECT_EQ(10, *S2.lookup(3)); |
42 | EXPECT_EQ(11, *S2.lookup(4)); |
43 | EXPECT_EQ(12, *S2.lookup(5)); |
44 | |
45 | EXPECT_EQ(5, S2.getMaxElement()->first); |
46 | EXPECT_EQ(3U, S2.getHeight()); |
47 | } |
48 | |
49 | TEST(ImmutableMapTest, EmptyIntMapRefTest) { |
50 | using int_int_map = ImmutableMapRef<int, int>; |
51 | ImmutableMapRef<int, int>::FactoryTy f; |
52 | |
53 | EXPECT_TRUE(int_int_map::getEmptyMap(&f) == int_int_map::getEmptyMap(&f)); |
54 | EXPECT_FALSE(int_int_map::getEmptyMap(&f) != int_int_map::getEmptyMap(&f)); |
55 | EXPECT_TRUE(int_int_map::getEmptyMap(&f).isEmpty()); |
56 | |
57 | int_int_map S = int_int_map::getEmptyMap(F: &f); |
58 | EXPECT_EQ(0u, S.getHeight()); |
59 | EXPECT_TRUE(S.begin() == S.end()); |
60 | EXPECT_FALSE(S.begin() != S.end()); |
61 | } |
62 | |
63 | TEST(ImmutableMapTest, MultiElemIntMapRefTest) { |
64 | ImmutableMapRef<int, int>::FactoryTy f; |
65 | |
66 | ImmutableMapRef<int, int> S = ImmutableMapRef<int, int>::getEmptyMap(F: &f); |
67 | |
68 | ImmutableMapRef<int, int> S2 = S.add(K: 3, D: 10).add(K: 4, D: 11).add(K: 5, D: 12); |
69 | |
70 | EXPECT_TRUE(S.isEmpty()); |
71 | EXPECT_FALSE(S2.isEmpty()); |
72 | |
73 | EXPECT_EQ(nullptr, S.lookup(3)); |
74 | EXPECT_EQ(nullptr, S.lookup(9)); |
75 | |
76 | EXPECT_EQ(10, *S2.lookup(3)); |
77 | EXPECT_EQ(11, *S2.lookup(4)); |
78 | EXPECT_EQ(12, *S2.lookup(5)); |
79 | |
80 | EXPECT_EQ(5, S2.getMaxElement()->first); |
81 | EXPECT_EQ(3U, S2.getHeight()); |
82 | } |
83 | |
84 | TEST(ImmutableMapTest, MapOfMapRefsTest) { |
85 | ImmutableMap<int, ImmutableMapRef<int, int>>::Factory f; |
86 | |
87 | EXPECT_TRUE(f.getEmptyMap() == f.getEmptyMap()); |
88 | } |
89 | |
90 | } |
91 | |