Added all tests and minor fixes

This commit is contained in:
Pesc0
2023-03-19 02:55:24 +01:00
parent 88a06ce6d5
commit dce458d1e8
3 changed files with 82 additions and 13 deletions

View File

@@ -33,13 +33,12 @@
using namespace Data;
void MappedName::compact() const
void MappedName::compact()
{
auto self = const_cast<MappedName*>(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

View File

@@ -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.

View File

@@ -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)