From 1645f2e8dcc6f8a277a16478c0a782d6beef09eb Mon Sep 17 00:00:00 2001 From: Daniel Brunner <0xFEEDC0DE64@gmail.com> Date: Thu, 17 Aug 2017 23:13:06 +0200 Subject: [PATCH] Added existing sources --- QFixedSizeMatrix.pro | 32 ++++++++ fixedsizematrix.h | 133 ++++++++++++++++++++++++++++++++ tst_qfixedsizematrixtest.cpp | 142 +++++++++++++++++++++++++++++++++++ 3 files changed, 307 insertions(+) create mode 100644 QFixedSizeMatrix.pro create mode 100644 fixedsizematrix.h create mode 100644 tst_qfixedsizematrixtest.cpp diff --git a/QFixedSizeMatrix.pro b/QFixedSizeMatrix.pro new file mode 100644 index 0000000..02e0eda --- /dev/null +++ b/QFixedSizeMatrix.pro @@ -0,0 +1,32 @@ +#------------------------------------------------- +# +# Project created by QtCreator 2017-08-17T23:09:54 +# +#------------------------------------------------- + +QT += testlib + +QT -= gui + +TARGET = tst_qfixedsizematrixtest +CONFIG += console +CONFIG -= app_bundle + +TEMPLATE = app + +# The following define makes your compiler emit warnings if you use +# any feature of Qt which as been marked as deprecated (the exact warnings +# depend on your compiler). Please consult the documentation of the +# deprecated API in order to know how to port your code away from it. +DEFINES += QT_DEPRECATED_WARNINGS + +# You can also make your code fail to compile if you use deprecated APIs. +# In order to do so, uncomment the following line. +# You can also select to disable deprecated APIs only up to a certain version of Qt. +DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 + +HEADERS += fixedsizematrix.h + +SOURCES += tst_qfixedsizematrixtest.cpp + +DEFINES += SRCDIR=\\\"$$PWD/\\\" diff --git a/fixedsizematrix.h b/fixedsizematrix.h new file mode 100644 index 0000000..23c4b23 --- /dev/null +++ b/fixedsizematrix.h @@ -0,0 +1,133 @@ +#ifndef FIXEDSIZEMATRIX_H +#define FIXEDSIZEMATRIX_H + +#include +#include + +template +class FixedSizeMatrix +{ +public: + FixedSizeMatrix(); + FixedSizeMatrix(const std::array, height> &data); + FixedSizeMatrix(const FixedSizeMatrix &other); + + FixedSizeMatrix& operator=(const FixedSizeMatrix& other); + FixedSizeMatrix& operator=(const std::array, height> &data); + + bool operator==(const FixedSizeMatrix& other) const; + bool operator!=(const FixedSizeMatrix& other) const; + + T& operator()(unsigned int x, unsigned int y); + const T& 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); + + FixedSizeMatrix rotatedLeft() const; + FixedSizeMatrix rotatedRight() const; + FixedSizeMatrix rotated180() const; + +private: + std::array, height> m_data; +}; + +template +FixedSizeMatrix::FixedSizeMatrix() {} + +template +FixedSizeMatrix::FixedSizeMatrix(const std::array, height> &data) : m_data(data) {} + +template +FixedSizeMatrix::FixedSizeMatrix(const FixedSizeMatrix &other) : m_data(other.m_data) {} + +template +FixedSizeMatrix &FixedSizeMatrix::operator=(const FixedSizeMatrix &other) +{ + if(&other != this) + m_data = other.m_data; + return *this; +} + +template +FixedSizeMatrix &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; } + +template +bool FixedSizeMatrix::operator!=(const FixedSizeMatrix &other) const { return m_data != other.m_data; } + +template +T &FixedSizeMatrix::operator()(unsigned int x, unsigned int y) +{ + assert(x < width); + assert(y < height); + return m_data[y][x]; +} + +template +const T &FixedSizeMatrix::operator()(unsigned int x, unsigned int y) const +{ + assert(x < width); + assert(y < height); + return m_data[y][x]; +} + +template +const T &FixedSizeMatrix::at(unsigned int x, unsigned int y) +{ + assert(x < width); + assert(y < height); + return m_data[y][x]; +} + +template +void FixedSizeMatrix::set(unsigned int x, unsigned int y, const T &value) +{ + assert(x < width); + assert(y < height); + m_data[y][x] = value; +} + +template +FixedSizeMatrix FixedSizeMatrix::rotatedLeft() const +{ + std::array, width> data; + + for(unsigned int y = 0; y < height; y++) + for(unsigned int x = 0; x < width; x++) + data[width-x-1][y] = m_data[y][x]; + + return FixedSizeMatrix(data); +} + +template +FixedSizeMatrix FixedSizeMatrix::rotatedRight() const +{ + std::array, width> data; + + for(unsigned int y = 0; y < height; y++) + for(unsigned int x = 0; x < width; x++) + data[x][height-y-1] = m_data[y][x]; + + return FixedSizeMatrix(data); +} + +template +FixedSizeMatrix FixedSizeMatrix::rotated180() const +{ + std::array, height> data; + + for(unsigned int y = 0; y < height; y++) + for(unsigned int x = 0; x < width; x++) + data[height-y-1][width-x-1] = m_data[y][x]; + + return FixedSizeMatrix(data); +} + +#endif // FIXEDSIZEMATRIX_H diff --git a/tst_qfixedsizematrixtest.cpp b/tst_qfixedsizematrixtest.cpp new file mode 100644 index 0000000..b4ee0bb --- /dev/null +++ b/tst_qfixedsizematrixtest.cpp @@ -0,0 +1,142 @@ +#include + +#include + +#include "fixedsizematrix.h" + +class QFixedSizeMatrixTest : public QObject +{ + Q_OBJECT + +public: + enum class TestEnum { ONE, TWO, THREE, FOUR }; + Q_ENUM(TestEnum) + +private Q_SLOTS: + void constructorTest(); + void assignmentTest(); + void comparisonTest(); + void rotateLeftTest(); + void rotateRightTest(); + 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; + + template + void verifyToDemoData(const FixedSizeMatrix& matrix, + const std::array, height> &data); +}; + +const std::array, 4> QFixedSizeMatrixTest::demoData { + 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 { + 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 { + 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 { + std::array { TestEnum::FOUR, TestEnum::FOUR }, + std::array { TestEnum::THREE, TestEnum::THREE }, + std::array { TestEnum::TWO, TestEnum::TWO }, + std::array { TestEnum::ONE, TestEnum::ONE } +}; + +template +void QFixedSizeMatrixTest::verifyToDemoData(const FixedSizeMatrix &matrix, + const std::array, height> &data) +{ + for(unsigned int y = 0; y < height; y++) + for(unsigned int x = 0; x < width; x++) + QCOMPARE(matrix(x, y), data[y][x]); +} + +void QFixedSizeMatrixTest::constructorTest() +{ + FixedSizeMatrix matrix(demoData); + verifyToDemoData(matrix, demoData); +} + +void QFixedSizeMatrixTest::assignmentTest() +{ + FixedSizeMatrix matrix(demoData); + FixedSizeMatrix other; + other = matrix; + verifyToDemoData(other, demoData); +} + +void QFixedSizeMatrixTest::comparisonTest() +{ + FixedSizeMatrix matrix0(demoData); + FixedSizeMatrix matrix1; + + QVERIFY2(matrix0 != matrix1, ""); + + matrix1 = FixedSizeMatrix { demoData }; + + QVERIFY2(matrix0 == matrix1, ""); +} + +void QFixedSizeMatrixTest::rotateLeftTest() +{ + FixedSizeMatrix matrix(demoData); + FixedSizeMatrix rotatedContainer; + + rotatedContainer = matrix.rotatedLeft(); + verifyToDemoData(rotatedContainer, demoDataRotatedLeft); + + matrix = rotatedContainer.rotatedLeft(); + verifyToDemoData(matrix, demoDataRotated180); + + rotatedContainer = matrix.rotatedLeft(); + verifyToDemoData(rotatedContainer, demoDataRotatedRight); + + matrix = rotatedContainer.rotatedLeft(); + verifyToDemoData(matrix, demoData); +} + +void QFixedSizeMatrixTest::rotateRightTest() +{ + FixedSizeMatrix matrix(demoData); + FixedSizeMatrix rotatedContainer; + + rotatedContainer = matrix.rotatedRight(); + verifyToDemoData(rotatedContainer, demoDataRotatedRight); + + matrix = rotatedContainer.rotatedRight(); + verifyToDemoData(matrix, demoDataRotated180); + + rotatedContainer = matrix.rotatedRight(); + verifyToDemoData(rotatedContainer, demoDataRotatedLeft); + + matrix = rotatedContainer.rotatedRight(); + verifyToDemoData(matrix, demoData); +} + +void QFixedSizeMatrixTest::rotate180Test() +{ + FixedSizeMatrix matrix(demoData); + + matrix = matrix.rotated180(); + verifyToDemoData(matrix, demoDataRotated180); + + matrix = matrix.rotated180(); + verifyToDemoData(matrix, demoData); +} + +QTEST_APPLESS_MAIN(QFixedSizeMatrixTest) + +#include "tst_qfixedsizematrixtest.moc"