diff --git a/src/App/MappedName.cpp b/src/App/MappedName.cpp index f7620ff7c8..ba9b90c2ac 100644 --- a/src/App/MappedName.cpp +++ b/src/App/MappedName.cpp @@ -33,13 +33,12 @@ using namespace Data; -void MappedName::compact() const +void MappedName::compact() { - auto self = const_cast(this); if (this->raw) { - self->data = QByteArray(self->data.constData(), self->data.size()); - self->raw = false; + this->data = QByteArray(this->data.constData(), this->data.size()); + this->raw = false; } #if 0 diff --git a/src/App/MappedName.h b/src/App/MappedName.h index 90172b9c99..84a6ca6927 100644 --- a/src/App/MappedName.h +++ b/src/App/MappedName.h @@ -89,11 +89,12 @@ public: /// is appended as text to the MappedName. In that case the memory is *not* shared between the /// original IndexedName and the MappedName. explicit MappedName(const IndexedName& element) - : data(element.getType()), - raw(false) + : data(QByteArray::fromRawData(element.getType(), qstrlen(element.getType()))), + raw(true) { if (element.getIndex() > 0) { - data += QByteArray::number(element.getIndex()); + this->data += QByteArray::number(element.getIndex()); + this->raw = false; } } @@ -664,8 +665,13 @@ public: /// continuing through postfix. No bounds checking is performed when compiled in release mode. char operator[](int index) const { - // FIXME overflow underflow checks? + if (index < 0) { + index = 0; + } if (index >= this->data.size()) { + if (index - this->data.size() > this->postfix.size() - 1) { + index = this->postfix.size() - 1; + } return this->postfix[index - this->data.size()]; } return this->data[index]; @@ -708,7 +714,7 @@ public: } /// Ensure that this data is unshared, making a copy if necessary. - void compact() const; + void compact(); /// Boolean conversion is the inverse of empty(), returning true if there is data in either the /// data or postfix, and false if there is nothing in either. diff --git a/tests/src/App/MappedName.cpp b/tests/src/App/MappedName.cpp index feb16c63d0..99f833782c 100644 --- a/tests/src/App/MappedName.cpp +++ b/tests/src/App/MappedName.cpp @@ -103,6 +103,7 @@ TEST(MappedName, constructFromIndexedNameNoIndex) // Assert EXPECT_EQ(mappedName.dataBytes().constData(), indexedName.getType()); // shared memory + EXPECT_EQ(mappedName.isRaw(), true); } TEST(MappedName, constructFromIndexedNameWithIndex) @@ -115,6 +116,7 @@ TEST(MappedName, constructFromIndexedNameWithIndex) // Assert EXPECT_NE(mappedName.dataBytes().constData(), indexedName.getType()); // NOT shared memory + EXPECT_EQ(mappedName.isRaw(), false); EXPECT_EQ(mappedName.toString(), indexedName.toString()); } @@ -534,14 +536,69 @@ TEST(MappedName, toIndexedNameInvalid) EXPECT_TRUE(indexedName.isNull()); } -TEST(MappedName, toPrefixedString) +TEST(MappedName, appendToBuffer) { - // TODO Write this test + // Arrange + Data::MappedName mappedName(Data::MappedName("TEST"), "POSTFIXTEST"); + std::string buffer("STUFF"); + + // Act + mappedName.appendToBuffer(buffer); + + // Assert + EXPECT_EQ(buffer, std::string("STUFFTESTPOSTFIXTEST")); + + // Act + mappedName.appendToBuffer(buffer, 2, 7); + + // Assert + EXPECT_EQ(buffer, std::string("STUFFTESTPOSTFIXTESTSTPOSTF")); } TEST(MappedName, appendToBufferWithPrefix) { - // TODO Write this test + // Arrange + Data::MappedName mappedName(Data::MappedName("TEST"), "POSTFIXTEST"); + std::string buffer("STUFF"); + std::string elemMapPrefix = Data::ComplexGeoData::elementMapPrefix(); + + // Act + mappedName.appendToBufferWithPrefix(buffer); + + // Assert + EXPECT_EQ(buffer, std::string("STUFF") + elemMapPrefix + std::string("TESTPOSTFIXTEST")); + + // Arrange + Data::MappedName mappedName2("TEST"); //If mappedName does not have a postfix and is a valid indexedName: prefix is not added + + // Act + mappedName2.appendToBufferWithPrefix(buffer); + + // Assert + EXPECT_EQ(buffer, std::string("STUFF") + elemMapPrefix + std::string("TESTPOSTFIXTEST") + /*missing prefix*/ std::string("TEST")); +} + +TEST(MappedName, toPrefixedString) +{ + // Arrange + Data::MappedName mappedName(Data::MappedName("TEST"), "POSTFIXTEST"); + std::string buffer("STUFF"); + std::string elemMapPrefix = Data::ComplexGeoData::elementMapPrefix(); + + // Act + buffer += mappedName.toPrefixedString(); + + // Assert + EXPECT_EQ(buffer, std::string("STUFF") + elemMapPrefix + std::string("TESTPOSTFIXTEST")); + + // Arrange + Data::MappedName mappedName2("TEST"); //If mappedName does not have a postfix and is a valid indexedName: prefix is not added + + // Act + buffer += mappedName2.toPrefixedString(); + + // Assert + EXPECT_EQ(buffer, std::string("STUFF") + elemMapPrefix + std::string("TESTPOSTFIXTEST") + /*missing prefix*/ std::string("TEST")); } TEST(MappedName, toBytes) @@ -763,6 +820,13 @@ TEST(MappedName, startsWith) EXPECT_EQ(mappedName.startsWith("WASD"), false); } -//TODO test hash function +TEST(MappedName, hash) +{ + // Arrange + Data::MappedName mappedName(Data::MappedName("TEST"), "POSTFIXTEST"); + + // Act & Assert + EXPECT_EQ(mappedName.hash(), qHash(QByteArray("TEST"), qHash(QByteArray("POSTFIXTEST")))); +} // NOLINTEND(readability-magic-numbers)