diff --git a/QFixedSizeMatrix.pro b/QFixedSizeMatrix.pro index e4fdca0..f7881d3 100644 --- a/QFixedSizeMatrix.pro +++ b/QFixedSizeMatrix.pro @@ -12,6 +12,12 @@ TARGET = tst_qfixedsizematrixtest CONFIG += console CONFIG -= app_bundle +CONFIG += c++14 +win32-g++ { + QMAKE_CXXFLAGS_CXX14 = -std=c++14 + QMAKE_CXXFLAGS_GNUCXX14 = -std=c++14 +} + TEMPLATE = app # The following define makes your compiler emit warnings if you use diff --git a/fixedsizematrix.h b/fixedsizematrix.h index 23c4b23..3bdd287 100644 --- a/fixedsizematrix.h +++ b/fixedsizematrix.h @@ -9,24 +9,29 @@ class FixedSizeMatrix { public: FixedSizeMatrix(); - FixedSizeMatrix(const std::array, height> &data); + FixedSizeMatrix(const std::array, height>& data); FixedSizeMatrix(const FixedSizeMatrix &other); - FixedSizeMatrix& operator=(const FixedSizeMatrix& other); - FixedSizeMatrix& operator=(const std::array, height> &data); + auto& operator=(const FixedSizeMatrix& other); + auto& operator=(const std::array, height>& data); - bool operator==(const FixedSizeMatrix& other) const; - bool operator!=(const FixedSizeMatrix& other) const; + auto operator==(const FixedSizeMatrix& other) const; + auto operator!=(const FixedSizeMatrix& other) const; - T& operator()(unsigned int x, unsigned int y); - const T& operator()(unsigned int x, unsigned int y) const; + auto& operator()(unsigned int x, unsigned int y); + const auto& operator()(unsigned int x, unsigned int y) const; - const T& at(unsigned int x, unsigned int y); - void set(unsigned int x, unsigned int y, const T &value); + void rotateLeft(); + void rotateRight(); + void rotate180(); + void mirrorHorizontally(); + void mirrorVertically(); - FixedSizeMatrix rotatedLeft() const; - FixedSizeMatrix rotatedRight() const; - FixedSizeMatrix rotated180() const; + auto asRotatedLeft() const; + auto asRotatedRight() const; + auto asRotated180() const; + auto asMirroredHorizontally() const; + auto asMirroredVertically() const; private: std::array, height> m_data; @@ -36,13 +41,13 @@ template FixedSizeMatrix::FixedSizeMatrix() {} template -FixedSizeMatrix::FixedSizeMatrix(const std::array, height> &data) : m_data(data) {} +FixedSizeMatrix::FixedSizeMatrix(const std::array, height>& data) : m_data(data) {} template -FixedSizeMatrix::FixedSizeMatrix(const FixedSizeMatrix &other) : m_data(other.m_data) {} +FixedSizeMatrix::FixedSizeMatrix(const FixedSizeMatrix& other) : m_data(other.m_data) {} template -FixedSizeMatrix &FixedSizeMatrix::operator=(const FixedSizeMatrix &other) +auto& FixedSizeMatrix::operator=(const FixedSizeMatrix& other) { if(&other != this) m_data = other.m_data; @@ -50,20 +55,26 @@ FixedSizeMatrix &FixedSizeMatrix::operator=( } template -FixedSizeMatrix &FixedSizeMatrix::operator=(const std::array, height> &data) +auto& FixedSizeMatrix::operator=(const std::array, height>& data) { m_data = data; return *this; } template -bool FixedSizeMatrix::operator==(const FixedSizeMatrix &other) const { return m_data == other.m_data; } +auto FixedSizeMatrix::operator==(const FixedSizeMatrix& other) const +{ + return m_data == other.m_data; +} template -bool FixedSizeMatrix::operator!=(const FixedSizeMatrix &other) const { return m_data != other.m_data; } +auto FixedSizeMatrix::operator!=(const FixedSizeMatrix& other) const +{ + return m_data != other.m_data; +} template -T &FixedSizeMatrix::operator()(unsigned int x, unsigned int y) +auto& FixedSizeMatrix::operator()(unsigned int x, unsigned int y) { assert(x < width); assert(y < height); @@ -71,7 +82,7 @@ T &FixedSizeMatrix::operator()(unsigned int x, unsigned int y) } template -const T &FixedSizeMatrix::operator()(unsigned int x, unsigned int y) const +const auto& FixedSizeMatrix::operator()(unsigned int x, unsigned int y) const { assert(x < width); assert(y < height); @@ -79,23 +90,41 @@ const T &FixedSizeMatrix::operator()(unsigned int x, unsigned } template -const T &FixedSizeMatrix::at(unsigned int x, unsigned int y) +void FixedSizeMatrix::rotateLeft() { - assert(x < width); - assert(y < height); - return m_data[y][x]; + static_assert(width == height, "Rotating in place only works when width matches height"); + + //TODO } template -void FixedSizeMatrix::set(unsigned int x, unsigned int y, const T &value) +void FixedSizeMatrix::rotateRight() { - assert(x < width); - assert(y < height); - m_data[y][x] = value; + static_assert(width == height, "Rotating in place only works when width matches height"); + + //TODO } template -FixedSizeMatrix FixedSizeMatrix::rotatedLeft() const +void FixedSizeMatrix::rotate180() +{ + //TODO +} + +template +void FixedSizeMatrix::mirrorHorizontally() +{ + //TODO +} + +template +void FixedSizeMatrix::mirrorVertically() +{ + //TODO +} + +template +auto FixedSizeMatrix::asRotatedLeft() const { std::array, width> data; @@ -107,7 +136,7 @@ FixedSizeMatrix FixedSizeMatrix::rotatedLeft } template -FixedSizeMatrix FixedSizeMatrix::rotatedRight() const +auto FixedSizeMatrix::asRotatedRight() const { std::array, width> data; @@ -119,7 +148,7 @@ FixedSizeMatrix FixedSizeMatrix::rotatedRigh } template -FixedSizeMatrix FixedSizeMatrix::rotated180() const +auto FixedSizeMatrix::asRotated180() const { std::array, height> data; @@ -130,4 +159,16 @@ FixedSizeMatrix FixedSizeMatrix::rotated180( return FixedSizeMatrix(data); } +template +auto FixedSizeMatrix::asMirroredHorizontally() const +{ + //TODO +} + +template +auto FixedSizeMatrix::asMirroredVertically() const +{ + //TODO +} + #endif // FIXEDSIZEMATRIX_H diff --git a/tst_qfixedsizematrixtest.cpp b/tst_qfixedsizematrixtest.cpp index b4ee0bb..a660cf3 100644 --- a/tst_qfixedsizematrixtest.cpp +++ b/tst_qfixedsizematrixtest.cpp @@ -21,43 +21,76 @@ private Q_SLOTS: void rotate180Test(); private: - static const std::array, 4> demoData; - static const std::array, 2> demoDataRotatedLeft; - static const std::array, 2> demoDataRotatedRight; - static const std::array, 4> demoDataRotated180; + static const std::array, 4> demoData2x4; + static const std::array, 2> demoData2x4RotatedLeft; + static const std::array, 2> demoData2x4RotatedRight; + static const std::array, 4> demoData2x4Rotated180; + + static const std::array, 4> demoData4x4; + static const std::array, 4> demoData4x4RotatedLeft; + static const std::array, 4> demoData4x4RotatedRight; + static const std::array, 4> demoData4x4Rotated180; template - void verifyToDemoData(const FixedSizeMatrix& matrix, + void verifyData(const FixedSizeMatrix& matrix, const std::array, height> &data); }; -const std::array, 4> QFixedSizeMatrixTest::demoData { +const std::array, 4> QFixedSizeMatrixTest::demoData2x4 { std::array { TestEnum::ONE, TestEnum::ONE }, std::array { TestEnum::TWO, TestEnum::TWO }, std::array { TestEnum::THREE, TestEnum::THREE }, std::array { TestEnum::FOUR, TestEnum::FOUR } }; -const std::array, 2> QFixedSizeMatrixTest::demoDataRotatedLeft { +const std::array, 2> QFixedSizeMatrixTest::demoData2x4RotatedLeft { std::array { TestEnum::ONE, TestEnum::TWO, TestEnum::THREE, TestEnum::FOUR }, std::array { TestEnum::ONE, TestEnum::TWO, TestEnum::THREE, TestEnum::FOUR } }; -const std::array, 2> QFixedSizeMatrixTest::demoDataRotatedRight { +const std::array, 2> QFixedSizeMatrixTest::demoData2x4RotatedRight { std::array { TestEnum::FOUR, TestEnum::THREE, TestEnum::TWO, TestEnum::ONE }, std::array { TestEnum::FOUR, TestEnum::THREE, TestEnum::TWO, TestEnum::ONE } }; -const std::array, 4> QFixedSizeMatrixTest::demoDataRotated180 { +const std::array, 4> QFixedSizeMatrixTest::demoData2x4Rotated180 { std::array { TestEnum::FOUR, TestEnum::FOUR }, std::array { TestEnum::THREE, TestEnum::THREE }, std::array { TestEnum::TWO, TestEnum::TWO }, std::array { TestEnum::ONE, TestEnum::ONE } }; +const std::array, 4> QFixedSizeMatrixTest::demoData4x4 { + std::array { TestEnum::ONE, TestEnum::ONE, TestEnum::ONE, TestEnum::ONE }, + std::array { TestEnum::TWO, TestEnum::TWO, TestEnum::TWO, TestEnum::TWO }, + std::array { TestEnum::THREE, TestEnum::THREE, TestEnum::THREE, TestEnum::THREE }, + std::array { TestEnum::FOUR, TestEnum::FOUR, TestEnum::FOUR, TestEnum::FOUR } +}; + +const std::array, 4> QFixedSizeMatrixTest::demoData4x4RotatedLeft { + std::array { TestEnum::ONE, TestEnum::TWO, TestEnum::THREE, TestEnum::FOUR }, + std::array { TestEnum::ONE, TestEnum::TWO, TestEnum::THREE, TestEnum::FOUR }, + std::array { TestEnum::ONE, TestEnum::TWO, TestEnum::THREE, TestEnum::FOUR }, + std::array { TestEnum::ONE, TestEnum::TWO, TestEnum::THREE, TestEnum::FOUR } +}; + +const std::array, 4> QFixedSizeMatrixTest::demoData4x4RotatedRight { + std::array { TestEnum::FOUR, TestEnum::THREE, TestEnum::TWO, TestEnum::ONE }, + std::array { TestEnum::FOUR, TestEnum::THREE, TestEnum::TWO, TestEnum::ONE }, + std::array { TestEnum::FOUR, TestEnum::THREE, TestEnum::TWO, TestEnum::ONE }, + std::array { TestEnum::FOUR, TestEnum::THREE, TestEnum::TWO, TestEnum::ONE } +}; + +const std::array, 4> QFixedSizeMatrixTest::demoData4x4Rotated180 { + std::array { TestEnum::FOUR, TestEnum::FOUR, TestEnum::FOUR, TestEnum::FOUR }, + std::array { TestEnum::THREE, TestEnum::THREE, TestEnum::THREE, TestEnum::THREE }, + std::array { TestEnum::TWO, TestEnum::TWO, TestEnum::TWO, TestEnum::TWO }, + std::array { TestEnum::ONE, TestEnum::ONE, TestEnum::ONE, TestEnum::ONE } +}; + template -void QFixedSizeMatrixTest::verifyToDemoData(const FixedSizeMatrix &matrix, - const std::array, height> &data) +void QFixedSizeMatrixTest::verifyData(const FixedSizeMatrix &matrix, + const std::array, height> &data) { for(unsigned int y = 0; y < height; y++) for(unsigned int x = 0; x < width; x++) @@ -66,75 +99,75 @@ void QFixedSizeMatrixTest::verifyToDemoData(const FixedSizeMatrix matrix(demoData); - verifyToDemoData(matrix, demoData); + FixedSizeMatrix matrix(demoData2x4); + verifyData(matrix, demoData2x4); } void QFixedSizeMatrixTest::assignmentTest() { - FixedSizeMatrix matrix(demoData); + FixedSizeMatrix matrix(demoData2x4); FixedSizeMatrix other; other = matrix; - verifyToDemoData(other, demoData); + verifyData(other, demoData2x4); } void QFixedSizeMatrixTest::comparisonTest() { - FixedSizeMatrix matrix0(demoData); + FixedSizeMatrix matrix0(demoData2x4); FixedSizeMatrix matrix1; QVERIFY2(matrix0 != matrix1, ""); - matrix1 = FixedSizeMatrix { demoData }; + matrix1 = FixedSizeMatrix { demoData2x4 }; QVERIFY2(matrix0 == matrix1, ""); } void QFixedSizeMatrixTest::rotateLeftTest() { - FixedSizeMatrix matrix(demoData); + FixedSizeMatrix matrix(demoData2x4); FixedSizeMatrix rotatedContainer; - rotatedContainer = matrix.rotatedLeft(); - verifyToDemoData(rotatedContainer, demoDataRotatedLeft); + rotatedContainer = matrix.asRotatedLeft(); + verifyData(rotatedContainer, demoData2x4RotatedLeft); - matrix = rotatedContainer.rotatedLeft(); - verifyToDemoData(matrix, demoDataRotated180); + matrix = rotatedContainer.asRotatedLeft(); + verifyData(matrix, demoData2x4Rotated180); - rotatedContainer = matrix.rotatedLeft(); - verifyToDemoData(rotatedContainer, demoDataRotatedRight); + rotatedContainer = matrix.asRotatedLeft(); + verifyData(rotatedContainer, demoData2x4RotatedRight); - matrix = rotatedContainer.rotatedLeft(); - verifyToDemoData(matrix, demoData); + matrix = rotatedContainer.asRotatedLeft(); + verifyData(matrix, demoData2x4); } void QFixedSizeMatrixTest::rotateRightTest() { - FixedSizeMatrix matrix(demoData); + FixedSizeMatrix matrix(demoData2x4); FixedSizeMatrix rotatedContainer; - rotatedContainer = matrix.rotatedRight(); - verifyToDemoData(rotatedContainer, demoDataRotatedRight); + rotatedContainer = matrix.asRotatedRight(); + verifyData(rotatedContainer, demoData2x4RotatedRight); - matrix = rotatedContainer.rotatedRight(); - verifyToDemoData(matrix, demoDataRotated180); + matrix = rotatedContainer.asRotatedRight(); + verifyData(matrix, demoData2x4Rotated180); - rotatedContainer = matrix.rotatedRight(); - verifyToDemoData(rotatedContainer, demoDataRotatedLeft); + rotatedContainer = matrix.asRotatedRight(); + verifyData(rotatedContainer, demoData2x4RotatedLeft); - matrix = rotatedContainer.rotatedRight(); - verifyToDemoData(matrix, demoData); + matrix = rotatedContainer.asRotatedRight(); + verifyData(matrix, demoData2x4); } void QFixedSizeMatrixTest::rotate180Test() { - FixedSizeMatrix matrix(demoData); + FixedSizeMatrix matrix(demoData2x4); - matrix = matrix.rotated180(); - verifyToDemoData(matrix, demoDataRotated180); + matrix = matrix.asRotated180(); + verifyData(matrix, demoData2x4Rotated180); - matrix = matrix.rotated180(); - verifyToDemoData(matrix, demoData); + matrix = matrix.asRotated180(); + verifyData(matrix, demoData2x4); } QTEST_APPLESS_MAIN(QFixedSizeMatrixTest)