Added existing sources
This commit is contained in:
32
QFixedSizeMatrix.pro
Normal file
32
QFixedSizeMatrix.pro
Normal file
@@ -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/\\\"
|
133
fixedsizematrix.h
Normal file
133
fixedsizematrix.h
Normal file
@@ -0,0 +1,133 @@
|
||||
#ifndef FIXEDSIZEMATRIX_H
|
||||
#define FIXEDSIZEMATRIX_H
|
||||
|
||||
#include <array>
|
||||
#include <assert.h>
|
||||
|
||||
template<typename T, unsigned int width, unsigned int height>
|
||||
class FixedSizeMatrix
|
||||
{
|
||||
public:
|
||||
FixedSizeMatrix();
|
||||
FixedSizeMatrix(const std::array<std::array<T, width>, height> &data);
|
||||
FixedSizeMatrix(const FixedSizeMatrix<T, width, height> &other);
|
||||
|
||||
FixedSizeMatrix<T, width, height>& operator=(const FixedSizeMatrix<T, width, height>& other);
|
||||
FixedSizeMatrix<T, width, height>& operator=(const std::array<std::array<T, width>, height> &data);
|
||||
|
||||
bool operator==(const FixedSizeMatrix<T, width, height>& other) const;
|
||||
bool operator!=(const FixedSizeMatrix<T, width, height>& 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<T, height, width> rotatedLeft() const;
|
||||
FixedSizeMatrix<T, height, width> rotatedRight() const;
|
||||
FixedSizeMatrix<T, width, height> rotated180() const;
|
||||
|
||||
private:
|
||||
std::array<std::array<T, width>, height> m_data;
|
||||
};
|
||||
|
||||
template<typename T, unsigned int width, unsigned int height>
|
||||
FixedSizeMatrix<T, width, height>::FixedSizeMatrix() {}
|
||||
|
||||
template<typename T, unsigned int width, unsigned int height>
|
||||
FixedSizeMatrix<T, width, height>::FixedSizeMatrix(const std::array<std::array<T, width>, height> &data) : m_data(data) {}
|
||||
|
||||
template<typename T, unsigned int width, unsigned int height>
|
||||
FixedSizeMatrix<T, width, height>::FixedSizeMatrix(const FixedSizeMatrix<T, width, height> &other) : m_data(other.m_data) {}
|
||||
|
||||
template<typename T, unsigned int width, unsigned int height>
|
||||
FixedSizeMatrix<T, width, height> &FixedSizeMatrix<T, width, height>::operator=(const FixedSizeMatrix<T, width, height> &other)
|
||||
{
|
||||
if(&other != this)
|
||||
m_data = other.m_data;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename T, unsigned int width, unsigned int height>
|
||||
FixedSizeMatrix<T, width, height> &FixedSizeMatrix<T, width, height>::operator=(const std::array<std::array<T, width>, height> &data)
|
||||
{
|
||||
m_data = data;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename T, unsigned int width, unsigned int height>
|
||||
bool FixedSizeMatrix<T, width, height>::operator==(const FixedSizeMatrix<T, width, height> &other) const { return m_data == other.m_data; }
|
||||
|
||||
template<typename T, unsigned int width, unsigned int height>
|
||||
bool FixedSizeMatrix<T, width, height>::operator!=(const FixedSizeMatrix<T, width, height> &other) const { return m_data != other.m_data; }
|
||||
|
||||
template<typename T, unsigned int width, unsigned int height>
|
||||
T &FixedSizeMatrix<T, width, height>::operator()(unsigned int x, unsigned int y)
|
||||
{
|
||||
assert(x < width);
|
||||
assert(y < height);
|
||||
return m_data[y][x];
|
||||
}
|
||||
|
||||
template<typename T, unsigned int width, unsigned int height>
|
||||
const T &FixedSizeMatrix<T, width, height>::operator()(unsigned int x, unsigned int y) const
|
||||
{
|
||||
assert(x < width);
|
||||
assert(y < height);
|
||||
return m_data[y][x];
|
||||
}
|
||||
|
||||
template<typename T, unsigned int width, unsigned int height>
|
||||
const T &FixedSizeMatrix<T, width, height>::at(unsigned int x, unsigned int y)
|
||||
{
|
||||
assert(x < width);
|
||||
assert(y < height);
|
||||
return m_data[y][x];
|
||||
}
|
||||
|
||||
template<typename T, unsigned int width, unsigned int height>
|
||||
void FixedSizeMatrix<T, width, height>::set(unsigned int x, unsigned int y, const T &value)
|
||||
{
|
||||
assert(x < width);
|
||||
assert(y < height);
|
||||
m_data[y][x] = value;
|
||||
}
|
||||
|
||||
template<typename T, unsigned int width, unsigned int height>
|
||||
FixedSizeMatrix<T, height, width> FixedSizeMatrix<T, width, height>::rotatedLeft() const
|
||||
{
|
||||
std::array<std::array<T, height>, 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<T, height, width>(data);
|
||||
}
|
||||
|
||||
template<typename T, unsigned int width, unsigned int height>
|
||||
FixedSizeMatrix<T, height, width> FixedSizeMatrix<T, width, height>::rotatedRight() const
|
||||
{
|
||||
std::array<std::array<T, height>, 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<T, height, width>(data);
|
||||
}
|
||||
|
||||
template<typename T, unsigned int width, unsigned int height>
|
||||
FixedSizeMatrix<T, width, height> FixedSizeMatrix<T, width, height>::rotated180() const
|
||||
{
|
||||
std::array<std::array<T, width>, 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<T, width, height>(data);
|
||||
}
|
||||
|
||||
#endif // FIXEDSIZEMATRIX_H
|
142
tst_qfixedsizematrixtest.cpp
Normal file
142
tst_qfixedsizematrixtest.cpp
Normal file
@@ -0,0 +1,142 @@
|
||||
#include <QtTest>
|
||||
|
||||
#include <array>
|
||||
|
||||
#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<std::array<TestEnum, 2>, 4> demoData;
|
||||
static const std::array<std::array<TestEnum, 4>, 2> demoDataRotatedLeft;
|
||||
static const std::array<std::array<TestEnum, 4>, 2> demoDataRotatedRight;
|
||||
static const std::array<std::array<TestEnum, 2>, 4> demoDataRotated180;
|
||||
|
||||
template<unsigned int width, unsigned int height>
|
||||
void verifyToDemoData(const FixedSizeMatrix<TestEnum, width, height>& matrix,
|
||||
const std::array<std::array<QFixedSizeMatrixTest::TestEnum, width>, height> &data);
|
||||
};
|
||||
|
||||
const std::array<std::array<QFixedSizeMatrixTest::TestEnum, 2>, 4> QFixedSizeMatrixTest::demoData {
|
||||
std::array<TestEnum, 2> { TestEnum::ONE, TestEnum::ONE },
|
||||
std::array<TestEnum, 2> { TestEnum::TWO, TestEnum::TWO },
|
||||
std::array<TestEnum, 2> { TestEnum::THREE, TestEnum::THREE },
|
||||
std::array<TestEnum, 2> { TestEnum::FOUR, TestEnum::FOUR }
|
||||
};
|
||||
|
||||
const std::array<std::array<QFixedSizeMatrixTest::TestEnum, 4>, 2> QFixedSizeMatrixTest::demoDataRotatedLeft {
|
||||
std::array<TestEnum, 4> { TestEnum::ONE, TestEnum::TWO, TestEnum::THREE, TestEnum::FOUR },
|
||||
std::array<TestEnum, 4> { TestEnum::ONE, TestEnum::TWO, TestEnum::THREE, TestEnum::FOUR }
|
||||
};
|
||||
|
||||
const std::array<std::array<QFixedSizeMatrixTest::TestEnum, 4>, 2> QFixedSizeMatrixTest::demoDataRotatedRight {
|
||||
std::array<TestEnum, 4> { TestEnum::FOUR, TestEnum::THREE, TestEnum::TWO, TestEnum::ONE },
|
||||
std::array<TestEnum, 4> { TestEnum::FOUR, TestEnum::THREE, TestEnum::TWO, TestEnum::ONE }
|
||||
};
|
||||
|
||||
const std::array<std::array<QFixedSizeMatrixTest::TestEnum, 2>, 4> QFixedSizeMatrixTest::demoDataRotated180 {
|
||||
std::array<TestEnum, 2> { TestEnum::FOUR, TestEnum::FOUR },
|
||||
std::array<TestEnum, 2> { TestEnum::THREE, TestEnum::THREE },
|
||||
std::array<TestEnum, 2> { TestEnum::TWO, TestEnum::TWO },
|
||||
std::array<TestEnum, 2> { TestEnum::ONE, TestEnum::ONE }
|
||||
};
|
||||
|
||||
template<unsigned int width, unsigned int height>
|
||||
void QFixedSizeMatrixTest::verifyToDemoData(const FixedSizeMatrix<QFixedSizeMatrixTest::TestEnum, width, height> &matrix,
|
||||
const std::array<std::array<QFixedSizeMatrixTest::TestEnum, width>, 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<TestEnum, 2, 4> matrix(demoData);
|
||||
verifyToDemoData(matrix, demoData);
|
||||
}
|
||||
|
||||
void QFixedSizeMatrixTest::assignmentTest()
|
||||
{
|
||||
FixedSizeMatrix<TestEnum, 2, 4> matrix(demoData);
|
||||
FixedSizeMatrix<TestEnum, 2, 4> other;
|
||||
other = matrix;
|
||||
verifyToDemoData(other, demoData);
|
||||
}
|
||||
|
||||
void QFixedSizeMatrixTest::comparisonTest()
|
||||
{
|
||||
FixedSizeMatrix<TestEnum, 2, 4> matrix0(demoData);
|
||||
FixedSizeMatrix<TestEnum, 2, 4> matrix1;
|
||||
|
||||
QVERIFY2(matrix0 != matrix1, "");
|
||||
|
||||
matrix1 = FixedSizeMatrix<TestEnum, 2, 4> { demoData };
|
||||
|
||||
QVERIFY2(matrix0 == matrix1, "");
|
||||
}
|
||||
|
||||
void QFixedSizeMatrixTest::rotateLeftTest()
|
||||
{
|
||||
FixedSizeMatrix<TestEnum, 2, 4> matrix(demoData);
|
||||
FixedSizeMatrix<TestEnum, 4, 2> 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<TestEnum, 2, 4> matrix(demoData);
|
||||
FixedSizeMatrix<TestEnum, 4, 2> 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<TestEnum, 2, 4> matrix(demoData);
|
||||
|
||||
matrix = matrix.rotated180();
|
||||
verifyToDemoData(matrix, demoDataRotated180);
|
||||
|
||||
matrix = matrix.rotated180();
|
||||
verifyToDemoData(matrix, demoData);
|
||||
}
|
||||
|
||||
QTEST_APPLESS_MAIN(QFixedSizeMatrixTest)
|
||||
|
||||
#include "tst_qfixedsizematrixtest.moc"
|
Reference in New Issue
Block a user