| | |
| |
|
| | #include <gtest/gtest.h> |
| |
|
| | #include "App/IndexedName.h" |
| |
|
| | #include <sstream> |
| |
|
| | |
| |
|
| | class IndexedNameTest: public ::testing::Test |
| | { |
| | protected: |
| | |
| |
|
| | |
| |
|
| | |
| | static std::vector<Data::IndexedName> givenInvalidIndexedNames() |
| | { |
| | return std::vector<Data::IndexedName> { |
| | Data::IndexedName(), |
| | Data::IndexedName("", 1), |
| | Data::IndexedName("INVALID42NAME", 1), |
| | Data::IndexedName(".EDGE", 1) |
| | }; |
| | } |
| |
|
| | |
| | static std::vector<Data::IndexedName> givenValidIndexedNames() |
| | { |
| | return std::vector<Data::IndexedName> { |
| | Data::IndexedName("NAME"), |
| | Data::IndexedName("NAME1"), |
| | Data::IndexedName("NAME", 1), |
| | Data::IndexedName("NAME_WITH_UNDERSCORES12345") |
| | }; |
| | } |
| |
|
| | |
| | |
| | std::vector<const char*> allowedTypes {"VERTEX", "EDGE", "FACE", "WIRE"}; |
| | }; |
| |
|
| | TEST_F(IndexedNameTest, defaultConstruction) |
| | { |
| | |
| | auto indexedName = Data::IndexedName(); |
| |
|
| | |
| | EXPECT_STREQ(indexedName.getType(), ""); |
| | EXPECT_EQ(indexedName.getIndex(), 0); |
| | } |
| |
|
| | TEST_F(IndexedNameTest, nameOnlyConstruction) |
| | { |
| | |
| | auto indexedName = Data::IndexedName("TestName"); |
| |
|
| | |
| | EXPECT_STREQ(indexedName.getType(), "TestName"); |
| | EXPECT_EQ(indexedName.getIndex(), 0); |
| | } |
| |
|
| | TEST_F(IndexedNameTest, nameAndIndexConstruction) |
| | { |
| | |
| | const int testIndex {42}; |
| |
|
| | |
| | auto indexedName = Data::IndexedName("TestName", testIndex); |
| |
|
| | |
| | EXPECT_STREQ(indexedName.getType(), "TestName"); |
| | EXPECT_EQ(indexedName.getIndex(), testIndex); |
| | } |
| |
|
| | TEST_F(IndexedNameTest, nameAndIndexConstructionWithOverride) |
| | { |
| | |
| | const int testIndex {42}; |
| |
|
| | |
| | auto indexedName = Data::IndexedName("TestName17", testIndex); |
| |
|
| | |
| | EXPECT_STREQ(indexedName.getType(), "TestName"); |
| | EXPECT_EQ(indexedName.getIndex(), testIndex); |
| | } |
| |
|
| | |
| | TEST_F(IndexedNameTest, constructionInvalidCharInName) |
| | { |
| | |
| | constexpr int lastASCIICode {127}; |
| | std::vector<char> illegalCharacters = {}; |
| | for (int code = 1; code <= lastASCIICode; ++code) { |
| | if ((std::isalnum(code) == 0) && code != '_') { |
| | illegalCharacters.push_back(char(code)); |
| | } |
| | } |
| | for (auto illegalChar : illegalCharacters) { |
| | std::string testName {"TestName"}; |
| | testName += illegalChar; |
| |
|
| | |
| | auto indexedName = Data::IndexedName(testName.c_str(), 1); |
| |
|
| | |
| | EXPECT_STREQ(indexedName.getType(), "") << "Expected empty name when given " << testName; |
| | } |
| | } |
| |
|
| | |
| | TEST_F(IndexedNameTest, constructionNumberInName) |
| | { |
| | |
| | const int testIndex {42}; |
| | std::string testName; |
| | testName += "Test" + std::to_string(testIndex) + "Name"; |
| |
|
| | |
| | auto indexedName = Data::IndexedName(testName.c_str(), testIndex); |
| |
|
| | |
| | EXPECT_STREQ(indexedName.getType(), ""); |
| | } |
| |
|
| | TEST_F(IndexedNameTest, nameAndTypeListConstructionWithoutAllowOthers) |
| | { |
| | |
| | auto indexedName = Data::IndexedName("EDGE19", allowedTypes, false); |
| |
|
| | |
| | EXPECT_STREQ(indexedName.getType(), "EDGE"); |
| | EXPECT_EQ(indexedName.getIndex(), 19); |
| |
|
| | |
| | indexedName = Data::IndexedName("EDGES_ARE_REALLY_GREAT19", allowedTypes, false); |
| |
|
| | |
| | EXPECT_STREQ(indexedName.getType(), ""); |
| | EXPECT_EQ(indexedName.getIndex(), 19); |
| |
|
| | |
| | indexedName = Data::IndexedName("NOT_IN_THE_LIST42", allowedTypes, false); |
| |
|
| | |
| | EXPECT_STREQ(indexedName.getType(), ""); |
| | } |
| |
|
| | TEST_F(IndexedNameTest, nameAndTypeListConstructionWithAllowOthers) |
| | { |
| | |
| | auto indexedName = Data::IndexedName("NOT_IN_THE_LIST42", allowedTypes, true); |
| |
|
| | |
| | EXPECT_STREQ(indexedName.getType(), "NOT_IN_THE_LIST"); |
| | EXPECT_EQ(indexedName.getIndex(), 42); |
| | } |
| |
|
| | |
| | TEST_F(IndexedNameTest, nameAndTypeListConstructionReusedMemoryCheck) |
| | { |
| | |
| | auto indexedName1 = Data::IndexedName("NOT_IN_THE_LIST42", allowedTypes, true); |
| | auto indexedName2 = Data::IndexedName("NOT_IN_THE_LIST43", allowedTypes, true); |
| |
|
| | |
| | EXPECT_EQ(indexedName1.getType(), indexedName2.getType()); |
| | } |
| |
|
| | TEST_F(IndexedNameTest, byteArrayConstruction) |
| | { |
| | |
| | QByteArray qba {"EDGE42"}; |
| |
|
| | |
| | auto indexedName = Data::IndexedName(qba); |
| |
|
| | |
| | EXPECT_STREQ(indexedName.getType(), "EDGE"); |
| | EXPECT_EQ(indexedName.getIndex(), 42); |
| | } |
| |
|
| | TEST_F(IndexedNameTest, copyConstruction) |
| | { |
| | |
| | auto indexedName = Data::IndexedName("EDGE42"); |
| |
|
| | |
| | auto indexedNameCopy {indexedName}; |
| |
|
| | |
| | EXPECT_EQ(indexedName, indexedNameCopy); |
| | } |
| |
|
| | TEST_F(IndexedNameTest, streamInsertionOperator) |
| | { |
| | |
| | auto indexedName = Data::IndexedName("EDGE42"); |
| | std::stringstream ss; |
| |
|
| | |
| | ss << indexedName; |
| |
|
| | |
| | EXPECT_EQ(ss.str(), std::string {"EDGE42"}); |
| | } |
| |
|
| | TEST_F(IndexedNameTest, compoundAssignmentOperator) |
| | { |
| | |
| |
|
| | |
| | constexpr int base {42}; |
| | constexpr int offset {10}; |
| | auto indexedName = Data::IndexedName("EDGE", base); |
| |
|
| | |
| | indexedName += offset; |
| |
|
| | |
| | EXPECT_EQ(indexedName.getIndex(), 52); |
| | } |
| |
|
| | TEST_F(IndexedNameTest, preincrementOperator) |
| | { |
| | |
| | auto indexedName = Data::IndexedName("EDGE42"); |
| |
|
| | |
| | ++indexedName; |
| |
|
| | |
| | EXPECT_EQ(indexedName.getIndex(), 43); |
| | } |
| |
|
| | TEST_F(IndexedNameTest, predecrementOperator) |
| | { |
| | |
| | auto indexedName = Data::IndexedName("EDGE42"); |
| |
|
| | |
| | --indexedName; |
| |
|
| | |
| | EXPECT_EQ(indexedName.getIndex(), 41); |
| | } |
| |
|
| | TEST_F(IndexedNameTest, comparisonOperators) |
| | { |
| | |
| | auto indexedName1 = Data::IndexedName("EDGE42"); |
| | auto indexedName2 = Data::IndexedName("EDGE42"); |
| |
|
| | |
| | EXPECT_EQ(indexedName1.compare(indexedName2), 0); |
| | EXPECT_TRUE(indexedName1 == indexedName2); |
| | EXPECT_FALSE(indexedName1 != indexedName2); |
| | EXPECT_FALSE(indexedName1 < indexedName2); |
| |
|
| | |
| | auto indexedName3 = Data::IndexedName("EDGE42"); |
| | auto indexedName4 = Data::IndexedName("FACE42"); |
| |
|
| | |
| | EXPECT_LT(indexedName3.compare(indexedName4), 0); |
| | EXPECT_FALSE(indexedName3 == indexedName4); |
| | EXPECT_TRUE(indexedName3 != indexedName4); |
| | EXPECT_TRUE(indexedName3 < indexedName4); |
| |
|
| | |
| | auto indexedName5 = Data::IndexedName("FACE42"); |
| | auto indexedName6 = Data::IndexedName("EDGE42"); |
| |
|
| | |
| | EXPECT_GT(indexedName5.compare(indexedName6), 0); |
| | EXPECT_FALSE(indexedName5 == indexedName6); |
| | EXPECT_TRUE(indexedName5 != indexedName6); |
| | EXPECT_FALSE(indexedName5 < indexedName6); |
| |
|
| | |
| | auto indexedName7 = Data::IndexedName("EDGE41"); |
| | auto indexedName8 = Data::IndexedName("EDGE42"); |
| |
|
| | |
| | EXPECT_LT(indexedName7.compare(indexedName8), 0); |
| | EXPECT_FALSE(indexedName7 == indexedName8); |
| | EXPECT_TRUE(indexedName7 != indexedName8); |
| | EXPECT_TRUE(indexedName7 < indexedName8); |
| |
|
| | |
| | auto indexedName9 = Data::IndexedName("EDGE43"); |
| | auto indexedName10 = Data::IndexedName("EDGE42"); |
| |
|
| | |
| | EXPECT_GT(indexedName9.compare(indexedName10), 0); |
| | EXPECT_FALSE(indexedName9 == indexedName10); |
| | EXPECT_TRUE(indexedName9 != indexedName10); |
| | EXPECT_FALSE(indexedName9 < indexedName10); |
| |
|
| | |
| | auto indexedName11 = Data::IndexedName("EDGE2"); |
| | auto indexedName12 = Data::IndexedName("EDGE12"); |
| |
|
| | |
| | EXPECT_LT(indexedName11.compare(indexedName12), 0); |
| | EXPECT_FALSE(indexedName11 == indexedName12); |
| | EXPECT_TRUE(indexedName11 != indexedName12); |
| | EXPECT_TRUE(indexedName11 < indexedName12); |
| | } |
| |
|
| | TEST_F(IndexedNameTest, subscriptOperator) |
| | { |
| | |
| | auto indexedName = Data::IndexedName("EDGE42"); |
| |
|
| | |
| | EXPECT_EQ(indexedName[0], 'E'); |
| | EXPECT_EQ(indexedName[1], 'D'); |
| | EXPECT_EQ(indexedName[2], 'G'); |
| | EXPECT_EQ(indexedName[3], 'E'); |
| | } |
| |
|
| | TEST_F(IndexedNameTest, getType) |
| | { |
| | |
| | auto indexedName = Data::IndexedName("EDGE42"); |
| |
|
| | |
| | EXPECT_STREQ(indexedName.getType(), "EDGE"); |
| | } |
| |
|
| | TEST_F(IndexedNameTest, setIndex) |
| | { |
| | |
| | auto indexedName = Data::IndexedName("EDGE42"); |
| | EXPECT_EQ(indexedName.getIndex(), 42); |
| |
|
| | |
| | indexedName.setIndex(1); |
| |
|
| | |
| | EXPECT_EQ(indexedName.getIndex(), 1); |
| | } |
| |
|
| | TEST_F(IndexedNameTest, isNullTrue) |
| | { |
| | |
| | auto invalidNames = givenInvalidIndexedNames(); |
| | for (const auto& name : invalidNames) { |
| |
|
| | |
| | EXPECT_TRUE(name.isNull()); |
| | } |
| | } |
| |
|
| | TEST_F(IndexedNameTest, isNullFalse) |
| | { |
| | |
| | auto validNames = givenValidIndexedNames(); |
| | for (const auto& name : validNames) { |
| |
|
| | |
| | EXPECT_FALSE(name.isNull()); |
| | } |
| | } |
| |
|
| | TEST_F(IndexedNameTest, booleanConversionFalse) |
| | { |
| | |
| | auto invalidNames = givenInvalidIndexedNames(); |
| | for (const auto& name : invalidNames) { |
| |
|
| | |
| | EXPECT_FALSE(static_cast<bool>(name)); |
| | } |
| |
|
| | |
| | auto indexedName = Data::IndexedName(".EDGE", 1); |
| | if (indexedName) { |
| | FAIL() << "indexedName as a boolean should have been false for an invalid name"; |
| | } |
| | } |
| |
|
| | TEST_F(IndexedNameTest, booleanConversionTrue) |
| | { |
| | |
| | auto validNames = givenValidIndexedNames(); |
| | for (const auto& name : validNames) { |
| |
|
| | |
| | EXPECT_TRUE(static_cast<bool>(name)); |
| | } |
| | } |
| |
|
| | TEST_F(IndexedNameTest, fromConst) |
| | { |
| | |
| | const int testIndex {42}; |
| |
|
| | |
| | auto indexedName = Data::IndexedName::fromConst("TestName", testIndex); |
| |
|
| | |
| | EXPECT_STREQ(indexedName.getType(), "TestName"); |
| | EXPECT_EQ(indexedName.getIndex(), testIndex); |
| | } |
| |
|
| | TEST_F(IndexedNameTest, appendToStringBufferEmptyBuffer) |
| | { |
| | |
| | std::string bufferStartedEmpty; |
| | Data::IndexedName testName("TEST_NAME", 1); |
| |
|
| | |
| | testName.appendToStringBuffer(bufferStartedEmpty); |
| |
|
| | |
| | EXPECT_EQ(bufferStartedEmpty, "TEST_NAME1"); |
| | } |
| |
|
| | TEST_F(IndexedNameTest, appendToStringBufferNonEmptyBuffer) |
| | { |
| | |
| | std::string bufferWithData {"DATA"}; |
| | Data::IndexedName testName("TEST_NAME", 1); |
| |
|
| | |
| | testName.appendToStringBuffer(bufferWithData); |
| |
|
| | |
| | EXPECT_EQ(bufferWithData, "DATATEST_NAME1"); |
| | } |
| |
|
| | TEST_F(IndexedNameTest, appendToStringBufferZeroIndex) |
| | { |
| | |
| | std::string bufferStartedEmpty; |
| | Data::IndexedName testName("TEST_NAME", 0); |
| |
|
| | |
| | testName.appendToStringBuffer(bufferStartedEmpty); |
| |
|
| | |
| | EXPECT_EQ(bufferStartedEmpty, "TEST_NAME"); |
| | } |
| |
|
| | TEST_F(IndexedNameTest, toString) |
| | { |
| | |
| | Data::IndexedName testName("TEST_NAME", 1); |
| |
|
| | |
| | auto result = testName.toString(); |
| |
|
| | |
| | EXPECT_EQ(result, "TEST_NAME1"); |
| | } |
| |
|
| | TEST_F(IndexedNameTest, toStringNoIndex) |
| | { |
| | |
| | Data::IndexedName testName("TEST_NAME", 0); |
| |
|
| | |
| | auto result = testName.toString(); |
| |
|
| | |
| | EXPECT_EQ(result, "TEST_NAME"); |
| | } |
| |
|
| | TEST_F(IndexedNameTest, assignmentOperator) |
| | { |
| | |
| | const int testIndex1 {42}; |
| | const int testIndex2 {24}; |
| | auto indexedName1 = Data::IndexedName::fromConst("TestName", testIndex1); |
| | auto indexedName2 = Data::IndexedName::fromConst("TestName2", testIndex2); |
| | EXPECT_NE(indexedName1, indexedName2); |
| |
|
| | |
| | indexedName1 = indexedName2; |
| |
|
| | |
| | EXPECT_EQ(indexedName1, indexedName2); |
| | } |
| |
|
| |
|
| | class ByteArrayTest: public ::testing::Test |
| | { |
| | protected: |
| | |
| |
|
| | |
| | }; |
| |
|
| | TEST_F(ByteArrayTest, QByteArrayConstruction) |
| | { |
| | |
| | QByteArray testQBA("Data in a QByteArray"); |
| |
|
| | |
| | Data::ByteArray testByteArray(testQBA); |
| |
|
| | |
| | EXPECT_EQ(testQBA, testByteArray.bytes); |
| | } |
| |
|
| | TEST_F(ByteArrayTest, CopyConstruction) |
| | { |
| | |
| | QByteArray testQBA("Data in a QByteArray"); |
| | Data::ByteArray originalByteArray(testQBA); |
| |
|
| | |
| | |
| | Data::ByteArray copiedByteArray(originalByteArray); |
| |
|
| | |
| | EXPECT_EQ(originalByteArray, copiedByteArray); |
| | } |
| |
|
| | TEST_F(ByteArrayTest, MoveConstruction) |
| | { |
| | |
| | QByteArray testQBA("Data in a QByteArray"); |
| | Data::ByteArray originalByteArray(testQBA); |
| | const auto* originalDataLocation = originalByteArray.bytes.constData(); |
| |
|
| | |
| | Data::ByteArray copiedByteArray(std::move(originalByteArray)); |
| |
|
| | |
| | EXPECT_EQ(testQBA, copiedByteArray.bytes); |
| | EXPECT_EQ(originalDataLocation, copiedByteArray.bytes.constData()); |
| | } |
| |
|
| | TEST_F(ByteArrayTest, ensureUnshared) |
| | { |
| | |
| | QByteArray testQBA("Data in a QByteArray"); |
| | Data::ByteArray originalByteArray(testQBA); |
| | const auto* originalDataLocation = originalByteArray.bytes.constData(); |
| | Data::ByteArray copiedByteArray(originalByteArray); |
| |
|
| | |
| | copiedByteArray.ensureUnshared(); |
| |
|
| | |
| | EXPECT_EQ(testQBA, copiedByteArray.bytes); |
| | EXPECT_NE(originalDataLocation, copiedByteArray.bytes.constData()); |
| | } |
| |
|
| | TEST_F(ByteArrayTest, equalityOperator) |
| | { |
| | |
| | QByteArray testQBA1("Data in a QByteArray"); |
| | QByteArray testQBA2("Data in a QByteArray"); |
| | QByteArray testQBA3("Not the same data in a QByteArray"); |
| | Data::ByteArray byteArray1(testQBA1); |
| | Data::ByteArray byteArray2(testQBA2); |
| | Data::ByteArray byteArray3(testQBA3); |
| |
|
| | |
| | EXPECT_TRUE(byteArray1 == byteArray2); |
| | EXPECT_FALSE(byteArray1 == byteArray3); |
| | } |
| |
|
| | TEST_F(ByteArrayTest, assignmentOperator) |
| | { |
| | |
| | QByteArray testQBA1("Data in a QByteArray"); |
| | QByteArray testQBA2("Different data in a QByteArray"); |
| | Data::ByteArray originalByteArray(testQBA1); |
| | Data::ByteArray newByteArray(testQBA2); |
| | ASSERT_FALSE(originalByteArray == newByteArray); |
| |
|
| | |
| | newByteArray = originalByteArray; |
| |
|
| | |
| | EXPECT_TRUE(originalByteArray == newByteArray); |
| | } |
| |
|
| | TEST_F(ByteArrayTest, moveAssignmentOperator) |
| | { |
| | |
| | QByteArray testQBA1("Data in a QByteArray"); |
| | QByteArray testQBA2("Different data in a QByteArray"); |
| | Data::ByteArray originalByteArray(testQBA1); |
| | const auto* originalByteArrayLocation = originalByteArray.bytes.constData(); |
| | Data::ByteArray newByteArray(testQBA2); |
| | ASSERT_FALSE(originalByteArray == newByteArray); |
| |
|
| | |
| | newByteArray = std::move(originalByteArray); |
| |
|
| | |
| | EXPECT_EQ(originalByteArrayLocation, newByteArray.bytes.constData()); |
| | } |
| |
|
| | |
| |
|