File size: 9,501 Bytes
985c397 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 | // SPDX-License-Identifier: LGPL-2.1-or-later
#include <gtest/gtest.h>
#include <utility>
#include <map>
#include <string>
#include <vector>
#include "App/IndexedName.h"
#include "App/MappedElement.h"
class MappedElementTest: public ::testing::Test
{
protected:
// void SetUp() override {}
// void TearDown() override {}
// NOLINTNEXTLINE(bugprone-easily-swappable-parameters)
static Data::MappedElement givenMappedElement(const char* index, const char* name)
{
Data::IndexedName indexedName {index};
Data::MappedName mappedName {name};
return {indexedName, mappedName};
}
};
TEST_F(MappedElementTest, constructFromNameAndIndex)
{
// Arrange
Data::IndexedName indexedName {"EDGE1"};
Data::MappedName mappedName {"OTHER_NAME"};
// Act
Data::MappedElement mappedElement {indexedName, mappedName};
// Assert
EXPECT_EQ(mappedElement.index, indexedName);
EXPECT_EQ(mappedElement.name, mappedName);
}
TEST_F(MappedElementTest, moveConstructor)
{
// Arrange
auto originalMappedElement = givenMappedElement("EDGE1", "OTHER_NAME");
auto originalName = originalMappedElement.name;
auto originalIndex = originalMappedElement.index;
// Act
Data::MappedElement newMappedElement {std::move(originalMappedElement)};
// Assert
EXPECT_EQ(originalName, newMappedElement.name);
EXPECT_EQ(originalIndex, newMappedElement.index);
}
TEST_F(MappedElementTest, assignmentOperator)
{
// Arrange
auto mappedElementA = givenMappedElement("EDGE1", "OTHER_NAME");
auto mappedElementB = givenMappedElement("EDGE2", "ANOTHER_NAME");
EXPECT_NE(mappedElementA, mappedElementB); // Verify test setup
// Act
mappedElementA = mappedElementB;
// Assert
EXPECT_EQ(mappedElementA, mappedElementB);
}
TEST_F(MappedElementTest, moveAssignmentOperator)
{
// Arrange
auto mappedElementA = givenMappedElement("EDGE1", "OTHER_NAME");
auto mappedElementB = givenMappedElement("EDGE2", "ANOTHER_NAME");
EXPECT_NE(mappedElementA, mappedElementB); // Verify test setup
// Act
mappedElementA = std::move(mappedElementB);
// Assert
EXPECT_EQ(mappedElementA.name.toString(), "ANOTHER_NAME");
EXPECT_EQ(mappedElementA.index.toString(), "EDGE2");
}
TEST_F(MappedElementTest, equalityOperatorsWhenNotEqual)
{
// Arrange
auto mappedElementA = givenMappedElement("EDGE1", "OTHER_NAME");
auto mappedElementB = givenMappedElement("EDGE2", "ANOTHER_NAME");
// Act
bool aAndBAreEqual = mappedElementA == mappedElementB;
bool aAndBAreNotEqual = mappedElementA != mappedElementB;
// Assert
EXPECT_TRUE(aAndBAreNotEqual);
EXPECT_FALSE(aAndBAreEqual);
}
TEST_F(MappedElementTest, equalityOperatorsWhenEqual)
{
// Arrange
auto mappedElementA = givenMappedElement("EDGE1", "OTHER_NAME");
auto mappedElementB = givenMappedElement("EDGE1", "OTHER_NAME");
// Act
bool aAndBAreEqual = mappedElementA == mappedElementB;
bool aAndBAreNotEqual = mappedElementA != mappedElementB;
// Assert
EXPECT_FALSE(aAndBAreNotEqual);
EXPECT_TRUE(aAndBAreEqual);
}
TEST_F(MappedElementTest, lessThanOperator)
{
// Index is compared first, then mappedName
// Arrange
auto mappedElement1A = givenMappedElement("EDGE1", "A");
auto mappedElement1B = givenMappedElement("EDGE1", "B");
auto mappedElement2A = givenMappedElement("EDGE2", "A");
auto mappedElement2B = givenMappedElement("EDGE2", "B");
auto mappedElement2BDuplicate = givenMappedElement("EDGE2", "B");
// Act & Assert
EXPECT_TRUE(mappedElement1A < mappedElement1B);
EXPECT_TRUE(mappedElement1A < mappedElement2A);
EXPECT_TRUE(mappedElement1B < mappedElement2A);
EXPECT_FALSE(mappedElement2A < mappedElement1B);
EXPECT_FALSE(mappedElement2B < mappedElement2BDuplicate);
}
TEST_F(MappedElementTest, comparatorBothAreZeroSize)
{
// Arrange
Data::MappedName mappedName1 {""};
Data::MappedName mappedName2 {""};
auto comp = Data::ElementNameComparator();
// Act & Assert
EXPECT_FALSE(comp(mappedName1, mappedName2));
}
TEST_F(MappedElementTest, comparatorOneIsZeroSize)
{
// Arrange
Data::MappedName mappedName1 {""};
Data::MappedName mappedName2 {"#12345"};
auto comp = Data::ElementNameComparator();
// Act & Assert
EXPECT_TRUE(comp(mappedName1, mappedName2));
}
TEST_F(MappedElementTest, comparatorBothStartWithHexDigitsThatDiffer)
{
// Arrange
Data::MappedName mappedName1 {"#fed;B"};
Data::MappedName mappedName2 {"#abcdef;A"};
auto comp = Data::ElementNameComparator();
// Act & Assert
EXPECT_TRUE(comp(mappedName1, mappedName2));
}
TEST_F(MappedElementTest, comparatorBothStartWithTheSameHexDigits)
{
// Arrange
Data::MappedName mappedName1 {"#12345;B"};
Data::MappedName mappedName2 {"#12345;A"};
auto comp = Data::ElementNameComparator();
// Act & Assert
EXPECT_FALSE(comp(mappedName1, mappedName2));
}
TEST_F(MappedElementTest, comparatorHexWithoutTerminator)
{
// Arrange
Data::MappedName mappedName1 {"#fed"};
Data::MappedName mappedName2 {"#abcdef"};
auto comp = Data::ElementNameComparator();
// Act & Assert
EXPECT_TRUE(comp(mappedName1, mappedName2));
}
TEST_F(MappedElementTest, comparatorHexWithoutTerminatorSameLength)
{
// Arrange
Data::MappedName mappedName1 {"#fed"};
Data::MappedName mappedName2 {"#abc"};
auto comp = Data::ElementNameComparator();
// Act & Assert
EXPECT_FALSE(comp(mappedName1, mappedName2));
}
TEST_F(MappedElementTest, comparatorNoHexDigitsLexicalCompare)
{
// Arrange
Data::MappedName mappedName1 {"A"};
Data::MappedName mappedName2 {"B"};
auto comp = Data::ElementNameComparator();
// Act & Assert
EXPECT_TRUE(comp(mappedName1, mappedName2));
}
TEST_F(MappedElementTest, comparatorNoHexDigitsSameStringNumericCompare)
{
// Arrange
Data::MappedName mappedName1 {"Edge123456;"};
Data::MappedName mappedName2 {"Edge321;"};
auto comp = Data::ElementNameComparator();
// Act & Assert
EXPECT_FALSE(comp(mappedName1, mappedName2));
}
TEST_F(MappedElementTest, comparatorIntegerWithoutTerminatorIsBroken)
{
// Arrange
Data::MappedName mappedName1 {"Edge123456"};
Data::MappedName mappedName2 {"Edge321"};
auto comp = Data::ElementNameComparator();
// Act & Assert
EXPECT_FALSE(comp(mappedName1, mappedName2));
}
TEST_F(MappedElementTest, comparatorThreeComplexHexNamesInMap)
{
// Arrange
Data::MappedName name1("#19c9:e;:U;FUS;:Hce4:7,E");
Data::MappedName name2("#1dadb:11;:L#1061a;FUS;:H:d,E");
Data::MappedName name3("#1dae6:8;:L#1dae4;FUS;:H:d,E");
std::map<Data::MappedName, int, Data::ElementNameComparator> testMap;
testMap[name1] = 1;
testMap[name2] = 2;
testMap[name3] = 3;
// Assert: map should have 3 unique keys
EXPECT_EQ(testMap.size(), 3);
// Collect keys in order
std::vector<std::string> keys;
for (const auto& kv : testMap) {
keys.push_back(kv.first.toString());
}
// Print for debug (optional)
// for (const auto& k : keys) std::cout << k << std::endl;
// The expected order depends on your comparator logic.
// If you want to check the exact order, set it here:
// (Replace with the correct expected order if needed)
std::vector<std::string> expectedOrder = {
"#19c9:e;:U;FUS;:Hce4:7,E",
"#1dadb:11;:L#1061a;FUS;:H:d,E",
"#1dae6:8;:L#1dae4;FUS;:H:d,E"
};
EXPECT_EQ(keys, expectedOrder);
}
TEST_F(MappedElementTest, comparatorLargerWorkedExampleWithMap)
{
// Arrange
Data::MappedName name0("Edge123;:U;FUS;:Hce4:7,E");
Data::MappedName name1("#1dad:e;:U;FUS;:Hce4:7,E");
Data::MappedName name2("#1dadb:11;:L#1061a;FUS;:H:d,E");
Data::MappedName name3("#1dae6:8;:L#1dae4;FUS;:H:d,E");
Data::MappedName name4("Edge999;;:L#1dae4;FUS;:H:d,E");
Data::MappedName name5("g4v2;SKT;:H1234,F;:H5678:2,E;:G0(g1;SKT;:H9012,E);XTR;:H3456:2,F");
std::map<Data::MappedName, int, Data::ElementNameComparator> testMap;
testMap[name0] = 1;
testMap[name1] = 2;
testMap[name2] = 3;
testMap[name3] = 4;
testMap[name0] = 5; // Duplicate, should not affect size
testMap[name1] = 6; // Duplicate, should not affect size
testMap[name4] = 7; // New entry
testMap[name4] = 8; // Duplicate, should not affect size
testMap[name2] = 9; // Duplicate, should not affect size
testMap[name3] = 10; // Duplicate, should not affect size
testMap[name5] = 11;
// Assert: map should have 5 unique keys
EXPECT_EQ(testMap.size(), 6);
// Collect keys in order
std::vector<std::string> keys;
for (const auto& kv : testMap) {
keys.push_back(kv.first.toString());
}
// Print for debug (optional)
// for (const auto& k : keys) std::cout << k << std::endl;
// The expected order depends on your comparator logic.
// If you want to check the exact order, set it here:
// (Replace with the correct expected order if needed)
std::vector<std::string> expectedOrder = {
"Edge123;:U;FUS;:Hce4:7,E",
"Edge999;;:L#1dae4;FUS;:H:d,E",
"g4v2;SKT;:H1234,F;:H5678:2,E;:G0(g1;SKT;:H9012,E);XTR;:H3456:2,F",
"#1dad:e;:U;FUS;:Hce4:7,E",
"#1dadb:11;:L#1061a;FUS;:H:d,E",
"#1dae6:8;:L#1dae4;FUS;:H:d,E"
};
EXPECT_EQ(keys, expectedOrder);
}
|