Switched to c++14 and extended API (rotating in place and mirroring)

This commit is contained in:
Daniel Brunner
2017-08-18 23:55:28 +02:00
parent bbbeac6061
commit 719bb8d98b
3 changed files with 151 additions and 71 deletions

View File

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

View File

@@ -9,24 +9,29 @@ class FixedSizeMatrix
{
public:
FixedSizeMatrix();
FixedSizeMatrix(const std::array<std::array<T, width>, height> &data);
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);
auto& operator=(const FixedSizeMatrix<T, width, height>& other);
auto& 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;
auto operator==(const FixedSizeMatrix<T, width, height>& other) const;
auto 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;
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<T, height, width> rotatedLeft() const;
FixedSizeMatrix<T, height, width> rotatedRight() const;
FixedSizeMatrix<T, width, height> rotated180() const;
auto asRotatedLeft() const;
auto asRotatedRight() const;
auto asRotated180() const;
auto asMirroredHorizontally() const;
auto asMirroredVertically() const;
private:
std::array<std::array<T, width>, height> m_data;
@@ -36,13 +41,13 @@ 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) {}
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) {}
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)
auto& FixedSizeMatrix<T, width, height>::operator=(const FixedSizeMatrix<T, width, height>& other)
{
if(&other != this)
m_data = other.m_data;
@@ -50,20 +55,26 @@ FixedSizeMatrix<T, width, height> &FixedSizeMatrix<T, width, height>::operator=(
}
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)
auto& 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; }
auto 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; }
auto 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)
auto& FixedSizeMatrix<T, width, height>::operator()(unsigned int x, unsigned int y)
{
assert(x < width);
assert(y < height);
@@ -71,7 +82,7 @@ T &FixedSizeMatrix<T, width, height>::operator()(unsigned int x, unsigned int y)
}
template<typename T, unsigned int width, unsigned int height>
const T &FixedSizeMatrix<T, width, height>::operator()(unsigned int x, unsigned int y) const
const auto& FixedSizeMatrix<T, width, height>::operator()(unsigned int x, unsigned int y) const
{
assert(x < width);
assert(y < height);
@@ -79,23 +90,41 @@ const T &FixedSizeMatrix<T, width, height>::operator()(unsigned int x, unsigned
}
template<typename T, unsigned int width, unsigned int height>
const T &FixedSizeMatrix<T, width, height>::at(unsigned int x, unsigned int y)
void FixedSizeMatrix<T, width, height>::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<typename T, unsigned int width, unsigned int height>
void FixedSizeMatrix<T, width, height>::set(unsigned int x, unsigned int y, const T &value)
void FixedSizeMatrix<T, width, height>::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<typename T, unsigned int width, unsigned int height>
FixedSizeMatrix<T, height, width> FixedSizeMatrix<T, width, height>::rotatedLeft() const
void FixedSizeMatrix<T, width, height>::rotate180()
{
//TODO
}
template<typename T, unsigned int width, unsigned int height>
void FixedSizeMatrix<T, width, height>::mirrorHorizontally()
{
//TODO
}
template<typename T, unsigned int width, unsigned int height>
void FixedSizeMatrix<T, width, height>::mirrorVertically()
{
//TODO
}
template<typename T, unsigned int width, unsigned int height>
auto FixedSizeMatrix<T, width, height>::asRotatedLeft() const
{
std::array<std::array<T, height>, width> data;
@@ -107,7 +136,7 @@ FixedSizeMatrix<T, height, width> FixedSizeMatrix<T, width, height>::rotatedLeft
}
template<typename T, unsigned int width, unsigned int height>
FixedSizeMatrix<T, height, width> FixedSizeMatrix<T, width, height>::rotatedRight() const
auto FixedSizeMatrix<T, width, height>::asRotatedRight() const
{
std::array<std::array<T, height>, width> data;
@@ -119,7 +148,7 @@ FixedSizeMatrix<T, height, width> FixedSizeMatrix<T, width, height>::rotatedRigh
}
template<typename T, unsigned int width, unsigned int height>
FixedSizeMatrix<T, width, height> FixedSizeMatrix<T, width, height>::rotated180() const
auto FixedSizeMatrix<T, width, height>::asRotated180() const
{
std::array<std::array<T, width>, height> data;
@@ -130,4 +159,16 @@ FixedSizeMatrix<T, width, height> FixedSizeMatrix<T, width, height>::rotated180(
return FixedSizeMatrix<T, width, height>(data);
}
template<typename T, unsigned int width, unsigned int height>
auto FixedSizeMatrix<T, width, height>::asMirroredHorizontally() const
{
//TODO
}
template<typename T, unsigned int width, unsigned int height>
auto FixedSizeMatrix<T, width, height>::asMirroredVertically() const
{
//TODO
}
#endif // FIXEDSIZEMATRIX_H

View File

@@ -21,43 +21,76 @@ private Q_SLOTS:
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;
static const std::array<std::array<TestEnum, 2>, 4> demoData2x4;
static const std::array<std::array<TestEnum, 4>, 2> demoData2x4RotatedLeft;
static const std::array<std::array<TestEnum, 4>, 2> demoData2x4RotatedRight;
static const std::array<std::array<TestEnum, 2>, 4> demoData2x4Rotated180;
static const std::array<std::array<TestEnum, 4>, 4> demoData4x4;
static const std::array<std::array<TestEnum, 4>, 4> demoData4x4RotatedLeft;
static const std::array<std::array<TestEnum, 4>, 4> demoData4x4RotatedRight;
static const std::array<std::array<TestEnum, 4>, 4> demoData4x4Rotated180;
template<unsigned int width, unsigned int height>
void verifyToDemoData(const FixedSizeMatrix<TestEnum, width, height>& matrix,
void verifyData(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 {
const std::array<std::array<QFixedSizeMatrixTest::TestEnum, 2>, 4> QFixedSizeMatrixTest::demoData2x4 {
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 {
const std::array<std::array<QFixedSizeMatrixTest::TestEnum, 4>, 2> QFixedSizeMatrixTest::demoData2x4RotatedLeft {
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 {
const std::array<std::array<QFixedSizeMatrixTest::TestEnum, 4>, 2> QFixedSizeMatrixTest::demoData2x4RotatedRight {
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 {
const std::array<std::array<QFixedSizeMatrixTest::TestEnum, 2>, 4> QFixedSizeMatrixTest::demoData2x4Rotated180 {
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 }
};
const std::array<std::array<QFixedSizeMatrixTest::TestEnum, 4>, 4> QFixedSizeMatrixTest::demoData4x4 {
std::array<TestEnum, 4> { TestEnum::ONE, TestEnum::ONE, TestEnum::ONE, TestEnum::ONE },
std::array<TestEnum, 4> { TestEnum::TWO, TestEnum::TWO, TestEnum::TWO, TestEnum::TWO },
std::array<TestEnum, 4> { TestEnum::THREE, TestEnum::THREE, TestEnum::THREE, TestEnum::THREE },
std::array<TestEnum, 4> { TestEnum::FOUR, TestEnum::FOUR, TestEnum::FOUR, TestEnum::FOUR }
};
const std::array<std::array<QFixedSizeMatrixTest::TestEnum, 4>, 4> QFixedSizeMatrixTest::demoData4x4RotatedLeft {
std::array<TestEnum, 4> { TestEnum::ONE, TestEnum::TWO, TestEnum::THREE, TestEnum::FOUR },
std::array<TestEnum, 4> { TestEnum::ONE, TestEnum::TWO, TestEnum::THREE, TestEnum::FOUR },
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>, 4> QFixedSizeMatrixTest::demoData4x4RotatedRight {
std::array<TestEnum, 4> { TestEnum::FOUR, TestEnum::THREE, TestEnum::TWO, TestEnum::ONE },
std::array<TestEnum, 4> { TestEnum::FOUR, TestEnum::THREE, TestEnum::TWO, TestEnum::ONE },
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, 4>, 4> QFixedSizeMatrixTest::demoData4x4Rotated180 {
std::array<TestEnum, 4> { TestEnum::FOUR, TestEnum::FOUR, TestEnum::FOUR, TestEnum::FOUR },
std::array<TestEnum, 4> { TestEnum::THREE, TestEnum::THREE, TestEnum::THREE, TestEnum::THREE },
std::array<TestEnum, 4> { TestEnum::TWO, TestEnum::TWO, TestEnum::TWO, TestEnum::TWO },
std::array<TestEnum, 4> { TestEnum::ONE, TestEnum::ONE, 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)
void QFixedSizeMatrixTest::verifyData(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++)
@@ -66,75 +99,75 @@ void QFixedSizeMatrixTest::verifyToDemoData(const FixedSizeMatrix<QFixedSizeMatr
void QFixedSizeMatrixTest::constructorTest()
{
FixedSizeMatrix<TestEnum, 2, 4> matrix(demoData);
verifyToDemoData(matrix, demoData);
FixedSizeMatrix<TestEnum, 2, 4> matrix(demoData2x4);
verifyData(matrix, demoData2x4);
}
void QFixedSizeMatrixTest::assignmentTest()
{
FixedSizeMatrix<TestEnum, 2, 4> matrix(demoData);
FixedSizeMatrix<TestEnum, 2, 4> matrix(demoData2x4);
FixedSizeMatrix<TestEnum, 2, 4> other;
other = matrix;
verifyToDemoData(other, demoData);
verifyData(other, demoData2x4);
}
void QFixedSizeMatrixTest::comparisonTest()
{
FixedSizeMatrix<TestEnum, 2, 4> matrix0(demoData);
FixedSizeMatrix<TestEnum, 2, 4> matrix0(demoData2x4);
FixedSizeMatrix<TestEnum, 2, 4> matrix1;
QVERIFY2(matrix0 != matrix1, "");
matrix1 = FixedSizeMatrix<TestEnum, 2, 4> { demoData };
matrix1 = FixedSizeMatrix<TestEnum, 2, 4> { demoData2x4 };
QVERIFY2(matrix0 == matrix1, "");
}
void QFixedSizeMatrixTest::rotateLeftTest()
{
FixedSizeMatrix<TestEnum, 2, 4> matrix(demoData);
FixedSizeMatrix<TestEnum, 2, 4> matrix(demoData2x4);
FixedSizeMatrix<TestEnum, 4, 2> 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<TestEnum, 2, 4> matrix(demoData);
FixedSizeMatrix<TestEnum, 2, 4> matrix(demoData2x4);
FixedSizeMatrix<TestEnum, 4, 2> 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<TestEnum, 2, 4> matrix(demoData);
FixedSizeMatrix<TestEnum, 2, 4> 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)