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(f.add(f.add(S, 3, 10), 4, 11), 5, 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 new ImmutableMapRef<int, int>::FactoryTy(); 53 54 EXPECT_TRUE(int_int_map::getEmptyMap(f) == int_int_map::getEmptyMap(f)); 55 EXPECT_FALSE(int_int_map::getEmptyMap(f) != int_int_map::getEmptyMap(f)); 56 EXPECT_TRUE(int_int_map::getEmptyMap(f).isEmpty()); 57 58 int_int_map S = int_int_map::getEmptyMap(f); 59 EXPECT_EQ(0u, S.getHeight()); 60 EXPECT_TRUE(S.begin() == S.end()); 61 EXPECT_FALSE(S.begin() != S.end()); 62 } 63 64 TEST(ImmutableMapTest, MultiElemIntMapRefTest) { 65 ImmutableMapRef<int, int>::FactoryTy *f = 66 new ImmutableMapRef<int, int>::FactoryTy(); 67 68 ImmutableMapRef<int, int> S = ImmutableMapRef<int, int>::getEmptyMap(f); 69 70 ImmutableMapRef<int, int> S2 = S.add(3, 10).add(4, 11).add(5, 12); 71 72 EXPECT_TRUE(S.isEmpty()); 73 EXPECT_FALSE(S2.isEmpty()); 74 75 EXPECT_EQ(nullptr, S.lookup(3)); 76 EXPECT_EQ(nullptr, S.lookup(9)); 77 78 EXPECT_EQ(10, *S2.lookup(3)); 79 EXPECT_EQ(11, *S2.lookup(4)); 80 EXPECT_EQ(12, *S2.lookup(5)); 81 82 EXPECT_EQ(5, S2.getMaxElement()->first); 83 EXPECT_EQ(3U, S2.getHeight()); 84 } 85 86 TEST(ImmutableMapTest, MapOfMapRefsTest) { 87 ImmutableMap<int, ImmutableMapRef<int, int>>::Factory f; 88 89 EXPECT_TRUE(f.getEmptyMap() == f.getEmptyMap()); 90 } 91 92 } 93