File size: 19,622 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 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 | // SPDX-License-Identifier: LGPL-2.1-or-later
#include <gtest/gtest.h>
#include <App/Application.h>
#include <App/ElementMap.h>
#include <src/App/InitApplication.h>
// NOLINTBEGIN(readability-magic-numbers)
// this is a "holder" class used for simpler testing of ElementMap in the context of a class
class LessComplexPart
{
public:
LessComplexPart(long tag, const std::string& nameStr, App::StringHasherRef hasher)
: elementMapPtr(std::make_shared<Data::ElementMap>())
, Tag(tag)
, name(nameStr)
{
// object also have Vertexes etc and the face count varies; but that is not important
// here since we are not testing a real model
// the "MappedName" is left blank for now
Data::IndexedName face1("Face", 1);
Data::IndexedName face2("Face", 2);
Data::IndexedName face3("Face", 3);
Data::IndexedName face4("Face", 4);
Data::IndexedName face5("Face", 5);
Data::IndexedName face6("Face", 6);
elementMapPtr->hasher = hasher;
elementMapPtr->setElementName(face1, Data::MappedName(face1), Tag);
elementMapPtr->setElementName(face2, Data::MappedName(face2), Tag);
elementMapPtr->setElementName(face3, Data::MappedName(face3), Tag);
elementMapPtr->setElementName(face4, Data::MappedName(face4), Tag);
elementMapPtr->setElementName(face5, Data::MappedName(face5), Tag);
elementMapPtr->setElementName(face6, Data::MappedName(face6), Tag);
}
Data::ElementMapPtr elementMapPtr;
mutable long Tag;
Data::MappedName name;
};
class ElementMapTest: public ::testing::Test
{
protected:
static void SetUpTestSuite()
{
tests::initApplication();
}
void SetUp() override
{
_docName = App::GetApplication().getUniqueDocumentName("test");
App::GetApplication().newDocument(_docName.c_str(), "testUser");
_sids = &_sid;
_hasher = Base::Reference<App::StringHasher>(new App::StringHasher);
ASSERT_EQ(_hasher.getRefCount(), 1);
}
void TearDown() override
{
App::GetApplication().closeDocument(_docName.c_str());
}
std::string _docName;
Data::ElementIDRefs _sid;
QVector<App::StringIDRef>* _sids;
App::StringHasherRef _hasher;
};
TEST_F(ElementMapTest, defaultConstruction)
{
// Act
Data::ElementMap elementMap = Data::ElementMap();
// Assert
EXPECT_EQ(elementMap.size(), 0);
}
TEST_F(ElementMapTest, setElementNameDefaults)
{
// Arrange
Data::ElementMap elementMap;
Data::IndexedName element("Edge", 1);
Data::MappedName mappedName("TEST");
// Act
auto resultName = elementMap.setElementName(element, mappedName, 0);
auto mappedToElement = elementMap.find(element);
// Assert
EXPECT_EQ(resultName, mappedName);
EXPECT_EQ(mappedToElement, mappedName);
}
TEST_F(ElementMapTest, setElementNameNoOverwrite)
{
// Arrange
Data::ElementMap elementMap;
Data::IndexedName element("Edge", 1);
Data::MappedName mappedName("TEST");
Data::MappedName anotherMappedName("ANOTHERTEST");
// Act
auto resultName = elementMap.setElementName(element, mappedName, 0);
auto resultName2 = elementMap.setElementName(element, anotherMappedName, 0, _sids, false);
auto mappedToElement = elementMap.find(element);
auto findAllResult = elementMap.findAll(element);
// Assert
EXPECT_EQ(resultName, mappedName);
EXPECT_EQ(resultName2, anotherMappedName);
EXPECT_EQ(mappedToElement, mappedName);
EXPECT_EQ(findAllResult.size(), 2);
EXPECT_EQ(findAllResult[0].first, mappedName);
EXPECT_EQ(findAllResult[1].first, anotherMappedName);
}
TEST_F(ElementMapTest, setElementNameWithOverwrite)
{
// Arrange
Data::ElementMap elementMap;
Data::IndexedName element("Edge", 1);
Data::MappedName mappedName("TEST");
Data::MappedName anotherMappedName("ANOTHERTEST");
// Act
auto resultName = elementMap.setElementName(element, mappedName, 0);
auto resultName2 = elementMap.setElementName(element, anotherMappedName, 0, _sids, true);
auto mappedToElement = elementMap.find(element);
auto findAllResult = elementMap.findAll(element);
// Assert
EXPECT_EQ(resultName, mappedName);
EXPECT_EQ(resultName2, anotherMappedName);
EXPECT_EQ(mappedToElement, anotherMappedName);
EXPECT_EQ(findAllResult.size(), 1);
EXPECT_EQ(findAllResult[0].first, anotherMappedName);
}
TEST_F(ElementMapTest, setElementNameWithHashing)
{
// Arrange
Data::ElementMap elementMap;
std::ostringstream ss;
Data::IndexedName element("Edge", 1);
Data::MappedName elementNameHolder(element); // Will get modified by the encoder
const Data::MappedName expectedName(element);
// Act
elementMap.encodeElementName(element.getType()[0], elementNameHolder, ss, nullptr, 0, nullptr, 0);
auto resultName = elementMap.setElementName(element, elementNameHolder, 0, _sids);
auto mappedToElement = elementMap.find(element);
// Assert
EXPECT_EQ(resultName, expectedName);
EXPECT_EQ(mappedToElement, expectedName);
}
TEST_F(ElementMapTest, eraseMappedName)
{
// Arrange
Data::ElementMap elementMap;
Data::IndexedName element("Edge", 1);
Data::MappedName mappedName("TEST");
Data::MappedName anotherMappedName("ANOTHERTEST");
elementMap.setElementName(element, mappedName, 0);
elementMap.setElementName(element, anotherMappedName, 0);
// Act
auto sizeBefore = elementMap.size();
auto findAllBefore = elementMap.findAll(element);
elementMap.erase(anotherMappedName);
auto sizeAfter = elementMap.size();
auto findAllAfter = elementMap.findAll(element);
elementMap.erase(anotherMappedName);
auto sizeAfterRepeat = elementMap.size();
auto findAllAfterRepeat = elementMap.findAll(element);
// Assert
EXPECT_EQ(sizeBefore, 2);
EXPECT_EQ(findAllBefore.size(), 2);
EXPECT_EQ(findAllBefore[0].first, mappedName);
EXPECT_EQ(findAllBefore[1].first, anotherMappedName);
EXPECT_EQ(sizeAfter, 1);
EXPECT_EQ(findAllAfter.size(), 1);
EXPECT_EQ(findAllAfter[0].first, mappedName);
EXPECT_EQ(sizeAfterRepeat, 1);
EXPECT_EQ(findAllAfterRepeat.size(), 1);
EXPECT_EQ(findAllAfterRepeat[0].first, mappedName);
}
TEST_F(ElementMapTest, eraseIndexedName)
{
// Arrange
// Create two elements, edge1 and edge2, that have two mapped names each.
Data::ElementMap elementMap;
Data::IndexedName element("Edge", 1);
Data::MappedName mappedName("TEST");
Data::MappedName anotherMappedName("ANOTHERTEST");
elementMap.setElementName(element, mappedName, 0);
elementMap.setElementName(element, anotherMappedName, 0);
Data::IndexedName element2("Edge", 2);
Data::MappedName mappedName2("TEST2");
Data::MappedName anotherMappedName2("ANOTHERTEST2");
elementMap.setElementName(element2, mappedName2, 0);
elementMap.setElementName(element2, anotherMappedName2, 0);
// Act
auto sizeBefore = elementMap.size();
auto findAllBefore = elementMap.findAll(element2);
elementMap.erase(element2);
auto sizeAfter = elementMap.size();
auto findAllAfter = elementMap.findAll(element2);
elementMap.erase(element2);
auto sizeAfterRepeat = elementMap.size();
auto findAllAfterRepeat = elementMap.findAll(element2);
// Assert
EXPECT_EQ(sizeBefore, 4);
EXPECT_EQ(findAllBefore.size(), 2);
EXPECT_EQ(findAllBefore[0].first, mappedName2);
EXPECT_EQ(findAllBefore[1].first, anotherMappedName2);
EXPECT_EQ(sizeAfter, 2);
EXPECT_EQ(findAllAfter.size(), 0);
EXPECT_EQ(sizeAfterRepeat, 2);
EXPECT_EQ(findAllAfterRepeat.size(), 0);
}
TEST_F(ElementMapTest, findMappedName)
{
// Arrange
// Create two elements, edge1 and edge2, that have two mapped names each.
Data::ElementMap elementMap;
Data::IndexedName element("Edge", 1);
Data::MappedName mappedName("TEST");
Data::MappedName anotherMappedName("ANOTHERTEST");
elementMap.setElementName(element, mappedName, 0);
elementMap.setElementName(element, anotherMappedName, 0);
Data::IndexedName element2("Edge", 2);
Data::MappedName mappedName2("TEST2");
Data::MappedName anotherMappedName2("ANOTHERTEST2");
elementMap.setElementName(element2, mappedName2, 0);
elementMap.setElementName(element2, anotherMappedName2, 0);
// Act
auto findResult = elementMap.find(mappedName);
auto findResult2 = elementMap.find(mappedName2);
// Assert
EXPECT_EQ(findResult, element);
EXPECT_EQ(findResult2, element2);
}
TEST_F(ElementMapTest, findIndexedName)
{
// Arrange
// Create two elements, edge1 and edge2, that have two mapped names each.
Data::ElementMap elementMap;
Data::IndexedName element("Edge", 1);
Data::MappedName mappedName("TEST");
Data::MappedName anotherMappedName("ANOTHERTEST");
elementMap.setElementName(element, mappedName, 0);
elementMap.setElementName(element, anotherMappedName, 0);
Data::IndexedName element2("Edge", 2);
Data::MappedName mappedName2("TEST2");
Data::MappedName anotherMappedName2("ANOTHERTEST2");
elementMap.setElementName(element2, mappedName2, 0);
elementMap.setElementName(element2, anotherMappedName2, 0);
// Act
// they return the first mapped name
auto findResult = elementMap.find(element);
auto findResult2 = elementMap.find(element2);
// Assert
EXPECT_EQ(findResult, mappedName);
EXPECT_EQ(findResult2, mappedName2);
}
TEST_F(ElementMapTest, findAll)
{
// Arrange
// Create two elements, edge1 and edge2, that have two mapped names each.
Data::ElementMap elementMap;
Data::IndexedName element("Edge", 1);
Data::MappedName mappedName("TEST");
Data::MappedName anotherMappedName("ANOTHERTEST");
elementMap.setElementName(element, mappedName, 0);
elementMap.setElementName(element, anotherMappedName, 0);
Data::IndexedName element2("Edge", 2);
Data::MappedName mappedName2("TEST2");
Data::MappedName anotherMappedName2("ANOTHERTEST2");
elementMap.setElementName(element2, mappedName2, 0);
elementMap.setElementName(element2, anotherMappedName2, 0);
// Act
// they return the first mapped name
auto findResult = elementMap.findAll(element);
auto findResult2 = elementMap.findAll(element2);
// Assert
EXPECT_EQ(findResult.size(), 2);
EXPECT_EQ(findResult[0].first, mappedName);
EXPECT_EQ(findResult[1].first, anotherMappedName);
EXPECT_EQ(findResult2.size(), 2);
EXPECT_EQ(findResult2[0].first, mappedName2);
EXPECT_EQ(findResult2[1].first, anotherMappedName2);
}
TEST_F(ElementMapTest, mimicOnePart)
{
// Arrange
// pattern: new doc, create Cube
// for a single part, there is no "naming algo" to speak of
std::ostringstream ss;
auto docName = "Unnamed";
LessComplexPart cube(1L, "Box", _hasher);
// Act
auto children = cube.elementMapPtr->getAll();
ss << docName << "#" << cube.name << "."
<< cube.elementMapPtr->find(Data::IndexedName("Face", 6));
// Assert
EXPECT_EQ(children.size(), 6);
EXPECT_EQ(children[0].index.toString(), "Face1");
EXPECT_EQ(children[0].name.toString(), "Face1");
EXPECT_EQ(children[1].index.toString(), "Face2");
EXPECT_EQ(children[1].name.toString(), "Face2");
EXPECT_EQ(children[2].index.toString(), "Face3");
EXPECT_EQ(children[2].name.toString(), "Face3");
EXPECT_EQ(children[3].index.toString(), "Face4");
EXPECT_EQ(children[3].name.toString(), "Face4");
EXPECT_EQ(children[4].index.toString(), "Face5");
EXPECT_EQ(children[4].name.toString(), "Face5");
EXPECT_EQ(children[5].index.toString(), "Face6");
EXPECT_EQ(children[5].name.toString(), "Face6");
EXPECT_EQ(ss.str(), "Unnamed#Box.Face6");
}
TEST_F(ElementMapTest, mimicSimpleUnion)
{
// Arrange
// pattern: new doc, create Cube, create Cylinder, Union of both (Cube first)
std::ostringstream ss;
std::ostringstream finalSs;
const char* docName = "Unnamed";
LessComplexPart cube(1L, "Box", _hasher);
LessComplexPart cylinder(2L, "Cylinder", _hasher);
// Union (Fusion) operation via the Part Workbench
LessComplexPart unionPart(3L, "Fusion", _hasher);
// we are only going to simulate one face for testing purpose
Data::IndexedName uface3("Face", 3);
auto PartOp = "FUS"; // Part::OpCodes::Fuse;
// Act
// act: simulate a union/fuse operation
auto parent = cube.elementMapPtr->getAll()[5];
Data::MappedName postfixHolder(std::string(Data::POSTFIX_MOD) + "2");
unionPart.elementMapPtr->encodeElementName(
postfixHolder[0],
postfixHolder,
ss,
nullptr,
unionPart.Tag,
nullptr,
unionPart.Tag
);
auto postfixStr = postfixHolder.toString() + Data::ELEMENT_MAP_PREFIX + PartOp;
// act: with the fuse op, name against the cube's Face6
Data::MappedName uface3Holder(parent.index);
// we will invoke the encoder for face 3
unionPart.elementMapPtr->encodeElementName(
uface3Holder[0],
uface3Holder,
ss,
nullptr,
unionPart.Tag,
postfixStr.c_str(),
cube.Tag
);
unionPart.elementMapPtr->setElementName(uface3, uface3Holder, unionPart.Tag, nullptr, true);
// act: generate a full toponame string for testing purposes
finalSs << docName << "#" << unionPart.name;
finalSs << ".";
finalSs << Data::ELEMENT_MAP_PREFIX + unionPart.elementMapPtr->find(uface3).toString();
finalSs << ".";
finalSs << uface3;
// Assert
EXPECT_EQ(postfixStr, ":M2;FUS");
EXPECT_EQ(unionPart.elementMapPtr->find(uface3).toString(), "Face6;:M2;FUS;:H1:8,F");
EXPECT_EQ(finalSs.str(), "Unnamed#Fusion.;Face6;:M2;FUS;:H1:8,F.Face3");
// explanation of "Fusion.;Face6;:M2;FUS;:H2:3,F.Face3" toponame
// Note: every postfix is prefixed by semicolon
// Note: the start/middle/end are separated by periods
//
// "Fusion" means that we are on the "Fusion" object.
// "." we are done with the first part
// ";Face6" means default inheritance comes from face 6 of the parent (which is a cube)
// ";:M2" means that a Workbench op has happened. "M" is the "Mod" directory in the source tree?
// ";FUS" means that a Fusion operation has happened. Notice the lack of a colon.
// ";:H2" means the subtending object (cylinder) has a tag of 2
// ":3" means the writing position is 3; literally how far into the current postfix we are
// ",F" means are of type "F" which is short for "Face" of Face3 of Fusion.
// "." we are done with the second part
// "Face3" is the localized name
}
TEST_F(ElementMapTest, mimicOperationAgainstSelf)
{
// Arrange
// pattern: new doc, create Cube, Mystery Op with self as target
std::ostringstream ss;
LessComplexPart finalPart(99L, "MysteryOp", _hasher);
// we are only going to simulate one face for testing purpose
Data::IndexedName uface3("Face", 3);
auto PartOp = "MYS";
auto ownFace6 = finalPart.elementMapPtr->getAll()[5];
Data::MappedName uface3Holder(ownFace6.index);
auto workbenchId = std::string(Data::POSTFIX_MOD) + "9999";
// Act
// act: with the mystery op, name against its own Face6 for some reason
Data::MappedName postfixHolder(workbenchId);
finalPart.elementMapPtr->encodeElementName(
postfixHolder[0],
postfixHolder,
ss,
nullptr,
finalPart.Tag,
nullptr,
finalPart.Tag
);
auto postfixStr = postfixHolder.toString() + Data::ELEMENT_MAP_PREFIX + PartOp;
// we will invoke the encoder for face 3
finalPart.elementMapPtr->encodeElementName(
uface3Holder[0],
uface3Holder,
ss,
nullptr,
finalPart.Tag,
postfixStr.c_str(),
finalPart.Tag
);
// override not forced
finalPart.elementMapPtr->setElementName(uface3, uface3Holder, finalPart.Tag, nullptr, false);
// Assert
EXPECT_EQ(postfixStr, ":M9999;MYS");
EXPECT_EQ(finalPart.elementMapPtr->find(uface3).toString(), "Face3"); // override not forced
EXPECT_EQ(uface3Holder.toString(), "Face6;:M9999;MYS;:H63:b,F");
// explaining ";Face6;:M2;MYS;:H2:3,F" name:
//
// ";Face6" means default inheritance comes from face 6 of the ownFace6 (which is itself)
// ";:M9999" means that a Workbench op happened. "M" is the "Mod" directory in the source tree?
// ";MYS" means that a "Mystery" operation has happened. Notice the lack of a colon.
// ";:H63" means the subtending object (cylinder) has a tag of 99 (63 in hex)
// ":b" means the writing position is b (hex); literally how far into the current postfix we are
// ",F" means are of type "F" which is short for "Face" of Face3 of Fusion.
}
TEST_F(ElementMapTest, hasChildElementMapTest)
{
// Arrange
Data::ElementMap::MappedChildElements child
= {Data::IndexedName("face", 1), 2, 7, 4L, Data::ElementMapPtr(), QByteArray(""), _sid};
std::vector<Data::ElementMap::MappedChildElements> children = {child};
LessComplexPart cubeFull(3L, "FullBox", _hasher);
cubeFull.elementMapPtr->addChildElements(cubeFull.Tag, children);
//
LessComplexPart cubeWithoutChildren(2L, "EmptyBox", _hasher);
// Act
bool resultFull = cubeFull.elementMapPtr->hasChildElementMap();
bool resultWhenEmpty = cubeWithoutChildren.elementMapPtr->hasChildElementMap();
// Assert
EXPECT_TRUE(resultFull);
EXPECT_FALSE(resultWhenEmpty);
}
TEST_F(ElementMapTest, hashChildMapsTest)
{
// Arrange
LessComplexPart cube(1L, "Box", _hasher);
auto childOneName = Data::IndexedName("Ping", 1);
Data::ElementMap::MappedChildElements childOne = {
childOneName,
2,
7,
3L,
Data::ElementMapPtr(),
QByteArray("abcdefghij"), // postfix must be 10 or more bytes to invoke hasher
_sid
};
std::vector<Data::ElementMap::MappedChildElements> children = {childOne};
cube.elementMapPtr->addChildElements(cube.Tag, children);
auto before = _hasher->getIDMap();
// Act
cube.elementMapPtr->hashChildMaps(cube.Tag);
// Assert
auto after = _hasher->getIDMap();
EXPECT_EQ(before.size(), 0);
EXPECT_EQ(after.size(), 1);
}
TEST_F(ElementMapTest, addAndGetChildElementsTest)
{
// Arrange
LessComplexPart cube(1L, "Box", _hasher);
Data::ElementMap::MappedChildElements childOne = {
Data::IndexedName("Ping", 1),
2,
7,
3L,
Data::ElementMapPtr(),
QByteArray("abcdefghij"), // postfix must be 10 or more bytes to invoke hasher
_sid
};
Data::ElementMap::MappedChildElements childTwo
= {Data::IndexedName("Pong", 2), 2, 7, 4L, Data::ElementMapPtr(), QByteArray("abc"), _sid};
std::vector<Data::ElementMap::MappedChildElements> children = {childOne, childTwo};
// Act
cube.elementMapPtr->addChildElements(cube.Tag, children);
auto result = cube.elementMapPtr->getChildElements();
// Assert
EXPECT_EQ(result.size(), 2);
EXPECT_TRUE(std::any_of(result.begin(), result.end(), [](Data::ElementMap::MappedChildElements e) {
return e.indexedName.toString() == "Ping1";
}));
EXPECT_TRUE(std::any_of(result.begin(), result.end(), [](Data::ElementMap::MappedChildElements e) {
return e.indexedName.toString() == "Pong2";
}));
}
// NOLINTEND(readability-magic-numbers)
|