Benjamin Kramer | b856d55 | 2012-04-25 18:01:58 +0000 | [diff] [blame] | 1 | //===- llvm/unittest/ADT/SmallMapTest.cpp ------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // SmallMap unit tests. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "gtest/gtest.h" |
| 15 | #include "llvm/ADT/SmallMap.h" |
| 16 | |
| 17 | using namespace llvm; |
| 18 | |
| 19 | // SmallMap test. |
| 20 | TEST(SmallMapTest, GeneralTest) { |
| 21 | |
| 22 | int buf[10]; |
| 23 | |
| 24 | SmallMap<int *, int, 3> a; |
| 25 | SmallMap<int *, int, 3> b; |
| 26 | SmallMap<int *, int, 3>::iterator found; |
| 27 | std::pair<SmallMap<int *, int, 3>::iterator, bool> insRes; |
| 28 | SmallMap<int *, int, 3>::const_iterator foundc; |
| 29 | |
| 30 | a.insert(std::make_pair(&buf[0], 0)); |
| 31 | insRes = a.insert(std::make_pair(&buf[1], 1)); |
| 32 | EXPECT_TRUE(insRes.second); |
| 33 | |
| 34 | // Check insertion, looking up, and data editing in small mode. |
| 35 | insRes = a.insert(std::make_pair(&buf[1], 6)); |
| 36 | EXPECT_FALSE(insRes.second); |
| 37 | EXPECT_EQ(insRes.first->second, 1); |
| 38 | insRes.first->second = 5; |
| 39 | found = a.find(&buf[1]); |
| 40 | EXPECT_NE(found, a.end()); |
| 41 | EXPECT_EQ(found->second, 5); |
| 42 | a[&buf[1]] = 10; |
| 43 | EXPECT_EQ(found->second, 10); |
| 44 | // Check "not found" case. |
| 45 | found = a.find(&buf[8]); |
| 46 | EXPECT_EQ(found, a.end()); |
| 47 | |
Benjamin Kramer | b856d55 | 2012-04-25 18:01:58 +0000 | [diff] [blame] | 48 | b.insert(std::make_pair(&buf[2], 2)); |
| 49 | |
| 50 | std::swap(a, b); |
| 51 | a.swap(b); |
| 52 | std::swap(a, b); |
| 53 | |
| 54 | EXPECT_EQ(1U, a.size()); |
| 55 | EXPECT_EQ(2U, b.size()); |
| 56 | EXPECT_TRUE(a.count(&buf[2])); |
| 57 | EXPECT_TRUE(b.count(&buf[0])); |
| 58 | EXPECT_TRUE(b.count(&buf[1])); |
| 59 | |
| 60 | insRes = b.insert(std::make_pair(&buf[3], 3)); |
| 61 | EXPECT_TRUE(insRes.second); |
| 62 | |
| 63 | // Check insertion, looking up, and data editing in big mode. |
| 64 | insRes = b.insert(std::make_pair(&buf[3], 6)); |
| 65 | EXPECT_FALSE(insRes.second); |
| 66 | EXPECT_EQ(insRes.first->second, 3); |
| 67 | insRes.first->second = 7; |
| 68 | found = b.find(&buf[3]); |
| 69 | EXPECT_EQ(found->second, 7); |
| 70 | b[&buf[3]] = 14; |
| 71 | EXPECT_EQ(found->second, 14); |
| 72 | // Check constant looking up. |
| 73 | foundc = b.find(&buf[3]); |
| 74 | EXPECT_EQ(foundc->first, &buf[3]); |
| 75 | EXPECT_EQ(foundc->second, 14); |
| 76 | // Check not found case. |
| 77 | found = b.find(&buf[8]); |
| 78 | EXPECT_EQ(found, b.end()); |
| 79 | |
Benjamin Kramer | b856d55 | 2012-04-25 18:01:58 +0000 | [diff] [blame] | 80 | std::swap(a, b); |
| 81 | a.swap(b); |
| 82 | std::swap(a, b); |
| 83 | |
| 84 | EXPECT_EQ(3U, a.size()); |
| 85 | EXPECT_EQ(1U, b.size()); |
| 86 | EXPECT_TRUE(a.count(&buf[0])); |
| 87 | EXPECT_TRUE(a.count(&buf[1])); |
| 88 | EXPECT_TRUE(a.count(&buf[3])); |
| 89 | EXPECT_TRUE(b.count(&buf[2])); |
| 90 | EXPECT_EQ(b.find(&buf[2])->second, 2); |
| 91 | |
| 92 | std::swap(a, b); |
| 93 | a.swap(b); |
| 94 | std::swap(a, b); |
| 95 | |
| 96 | EXPECT_EQ(1U, a.size()); |
| 97 | EXPECT_EQ(3U, b.size()); |
| 98 | EXPECT_TRUE(a.count(&buf[2])); |
| 99 | EXPECT_TRUE(b.count(&buf[0])); |
| 100 | EXPECT_TRUE(b.count(&buf[1])); |
| 101 | EXPECT_TRUE(b.count(&buf[3])); |
| 102 | |
| 103 | a.insert(std::make_pair(&buf[4], 4)); |
| 104 | a.insert(std::make_pair(&buf[5], 5)); |
| 105 | a.insert(std::make_pair(&buf[6], 6)); |
| 106 | |
| 107 | std::swap(b, a); |
| 108 | |
| 109 | EXPECT_EQ(3U, a.size()); |
| 110 | EXPECT_EQ(4U, b.size()); |
| 111 | EXPECT_TRUE(b.count(&buf[2])); |
| 112 | EXPECT_TRUE(b.count(&buf[4])); |
| 113 | EXPECT_TRUE(b.count(&buf[5])); |
| 114 | EXPECT_TRUE(b.count(&buf[6])); |
| 115 | EXPECT_TRUE(a.count(&buf[0])); |
| 116 | EXPECT_TRUE(a.count(&buf[1])); |
| 117 | EXPECT_TRUE(a.count(&buf[3])); |
| 118 | |
| 119 | // Check findAndConstruct |
| 120 | SmallMap<int *, int, 3>::value_type Buf7; |
| 121 | Buf7 = a.FindAndConstruct(&buf[7]); |
| 122 | EXPECT_EQ(Buf7.second, 0); |
Stepan Dyatkovskiy | 5c77bc2 | 2012-04-26 18:45:24 +0000 | [diff] [blame] | 123 | |
| 124 | // Check increments |
| 125 | |
| 126 | SmallMap<int *, int, 2> c; |
| 127 | c.insert(std::make_pair(&buf[0], 0)); |
| 128 | c.insert(std::make_pair(&buf[1], 1)); |
| 129 | |
| 130 | // For small mode we know that flat array map is used and we know the |
| 131 | // order of items. |
| 132 | unsigned ii = 0; |
| 133 | for (SmallMap<int *, int, 2>::iterator i = c.begin(), e = c.end(); |
| 134 | i != e; ++i, ++ii) { |
| 135 | EXPECT_TRUE((i->first == &buf[0] && i->second == 0 && ii == 0) || |
| 136 | (i->first == &buf[1] && i->second == 1 && ii == 1)); |
| 137 | } |
| 138 | |
| 139 | // For big mode DenseMap is used and final order of items is undefined. |
| 140 | c.insert(std::make_pair(&buf[2], 2)); |
| 141 | for (SmallMap<int *, int, 2>::iterator i = c.begin(), e = c.end(); |
| 142 | i != e; ++i) { |
| 143 | EXPECT_TRUE((i->first == &buf[0] && i->second == 0) || |
| 144 | (i->first == &buf[1] && i->second == 1) || |
| 145 | (i->first == &buf[2] && i->second == 2)); |
| 146 | } |
Benjamin Kramer | b856d55 | 2012-04-25 18:01:58 +0000 | [diff] [blame] | 147 | } |