From 87d2c11ab6243ebdf82d874676261d45ad2c3eea Mon Sep 17 00:00:00 2001 From: Pascal Serrarens Date: Fri, 15 Mar 2024 09:27:23 +0100 Subject: [PATCH] Set strict warnings --- CMakeLists.txt | 7 +++++++ DiscreteAngle.cpp | 8 ++++++-- DiscreteAngle.h | 21 ++++++++++++++++----- Matrix.h | 4 ++-- test/Matrix_test.cc | 3 ++- test/Vector2_test.cc | 12 ------------ test/Vector3_test.cc | 12 ------------ 7 files changed, 33 insertions(+), 34 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 372b7c0..ef430ee 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -52,6 +52,13 @@ else() VectorAlgebra ) + if(MSVC) + target_compile_options(VectorAlgebraTest PRIVATE /W4 /WX) + else() + target_compile_options(VectorAlgebraTest PRIVATE -Wall -Wextra -Wpedantic -Werror) + endif() + + include(GoogleTest) gtest_discover_tests(VectorAlgebraTest) endif() diff --git a/DiscreteAngle.cpp b/DiscreteAngle.cpp index 0a0e4cd..6cff5f8 100644 --- a/DiscreteAngle.cpp +++ b/DiscreteAngle.cpp @@ -7,6 +7,8 @@ #include "Angle.h" #include +// 1-byte angle + template <> AngleUsing::AngleUsing(unsigned char angle) { if (!isfinite(angle)) { value = 0; @@ -21,11 +23,13 @@ template <> AngleUsing::AngleUsing(unsigned char angle) { this->value = (unsigned char)((angle + 180.0F) / 360.0F * 255.0F); } -template <> float AngleUsing::ToFloat() { +template <> float AngleUsing::ToFloat() const { float f = ((float)this->value / 255.0F) * 360.0F - 180.0F; return f; } +// 2-byte angle + template <> AngleUsing::AngleUsing(unsigned short angle) { if (!isfinite(angle)) { value = 0; @@ -40,7 +44,7 @@ template <> AngleUsing::AngleUsing(unsigned short angle) { this->value = (unsigned short)((angle + 180.0F) / 360.0F * 65535.0F); } -template <> float AngleUsing::ToFloat() { +template <> float AngleUsing::ToFloat() const { float f = ((float)this->value / 65535.0F) * 360.0F - 180.0F; return f; } \ No newline at end of file diff --git a/DiscreteAngle.h b/DiscreteAngle.h index 4297c20..8f7454a 100644 --- a/DiscreteAngle.h +++ b/DiscreteAngle.h @@ -8,17 +8,28 @@ template class AngleUsing { public: AngleUsing(T sourceValue); - float ToFloat(); - inline T GetValue() { return this->value; } + float ToFloat() const; + inline T GetValue() const { return this->value; } - inline AngleUsing operator-(AngleUsing a) { + inline AngleUsing operator+(const AngleUsing a) { + return this->value + a.value; + } + + inline AngleUsing operator+=(const AngleUsing a &) { + return this->value + a.value; + } + + inline AngleUsing operator-(const AngleUsing a) { return this->value - a.value; } + inline AngleUsing operator-() { return -this->value; } + inline bool operator==(const AngleUsing a, const AngleUsing b) { + return a.value == b.value; + } + protected: T value; }; #endif - -// template inline float AngleUsing::ToFloat() { return 0.0f; } diff --git a/Matrix.h b/Matrix.h index 5ef0588..7eb80a9 100644 --- a/Matrix.h +++ b/Matrix.h @@ -33,7 +33,7 @@ public: // Return a null matrix; // We dont set data to nullptr because it is allocated memory // Instead we write all zeros - for (int dataIx = 0; dataIx < resultSize; dataIx++) + for (unsigned int dataIx = 0; dataIx < resultSize; dataIx++) r->data[dataIx] = 0.0f; r->rows = 0; r->cols = 0; @@ -43,7 +43,7 @@ public: r->cols = this->rows; r->rows = this->cols; - for (int rDataIx = 0; rDataIx < matrixSize; rDataIx++) { + for (unsigned int rDataIx = 0; rDataIx < matrixSize; rDataIx++) { unsigned int rowIx = rDataIx / this->rows; unsigned int colIx = rDataIx % this->rows; unsigned int mDataIx = this->cols * colIx + rowIx; diff --git a/test/Matrix_test.cc b/test/Matrix_test.cc index dd358f9..a1267cf 100644 --- a/test/Matrix_test.cc +++ b/test/Matrix_test.cc @@ -18,7 +18,8 @@ TEST(MatrixSingle, Init) { MatrixOf m2 = MatrixOf(2, 2, data2); // negative - MatrixOf m_1 = MatrixOf(-1, -1); + // MatrixOf m_1 = MatrixOf(-1, -1); + // parameters are unsigned } TEST(MatrixSingle, Transpose) { diff --git a/test/Vector2_test.cc b/test/Vector2_test.cc index 88b8c97..ba16470 100644 --- a/test/Vector2_test.cc +++ b/test/Vector2_test.cc @@ -99,8 +99,6 @@ TEST(Vector2, Normalize) { } TEST(Vector2, Negate) { - bool r = false; - Vector2 v1 = Vector2(4, 5); Vector2 v = Vector2::zero; @@ -129,8 +127,6 @@ TEST(Vector2, Negate) { } TEST(Vector2, Subtract) { - bool r = false; - Vector2 v1 = Vector2(4, 5); Vector2 v2 = Vector2(1, 2); Vector2 v = Vector2::zero; @@ -164,11 +160,9 @@ TEST(Vector2, Subtract) { } TEST(Vector2, Addition) { - Vector2 v1 = Vector2(4, 5); Vector2 v2 = Vector2(1, 2); Vector2 v = Vector2::zero; - bool r = false; v = v1 + v2; EXPECT_TRUE(v == Vector2(5, 7)) << "4 5 + 1 2"; @@ -195,8 +189,6 @@ TEST(Vector2, Addition) { } TEST(Vector2, Scale) { - bool r = false; - Vector2 v1 = Vector2(4, 5); Vector2 v2 = Vector2(1, 2); Vector2 v = Vector2::zero; @@ -226,8 +218,6 @@ TEST(Vector2, Scale) { } TEST(Vector2, Multiply) { - bool r = false; - Vector2 v1 = Vector2(4, 5); float f = 3; Vector2 v = Vector2::zero; @@ -257,8 +247,6 @@ TEST(Vector2, Multiply) { } TEST(Vector2, Divide) { - bool r = false; - Vector2 v1 = Vector2(4, 5); float f = 2; Vector2 v = Vector2::zero; diff --git a/test/Vector3_test.cc b/test/Vector3_test.cc index e8f61f8..2148937 100644 --- a/test/Vector3_test.cc +++ b/test/Vector3_test.cc @@ -103,8 +103,6 @@ TEST(Vector3, Normalize) { } TEST(Vector3, Negate) { - bool r = false; - Vector3 v1 = Vector3(4, 5, 6); Vector3 v = Vector3::zero; @@ -133,8 +131,6 @@ TEST(Vector3, Negate) { } TEST(Vector3, Subtract) { - bool r = false; - Vector3 v1 = Vector3(4, 5, 6); Vector3 v2 = Vector3(1, 2, 3); Vector3 v = Vector3::zero; @@ -168,11 +164,9 @@ TEST(Vector3, Subtract) { } TEST(Vector3, Addition) { - Vector3 v1 = Vector3(4, 5, 6); Vector3 v2 = Vector3(1, 2, 3); Vector3 v = Vector3::zero; - bool r = false; v = v1 + v2; EXPECT_TRUE(v == Vector3(5, 7, 9)) << "4 5 6 + 1 2 3"; @@ -199,8 +193,6 @@ TEST(Vector3, Addition) { } TEST(Vector3, Scale) { - bool r = false; - Vector3 v1 = Vector3(4, 5, 6); Vector3 v2 = Vector3(1, 2, 3); Vector3 v = Vector3::zero; @@ -230,8 +222,6 @@ TEST(Vector3, Scale) { } TEST(Vector3, Multiply) { - bool r = false; - Vector3 v1 = Vector3(4, 5, 6); float f = 3; Vector3 v = Vector3::zero; @@ -261,8 +251,6 @@ TEST(Vector3, Multiply) { } TEST(Vector3, Divide) { - bool r = false; - Vector3 v1 = Vector3(4, 5, 6); float f = 2; Vector3 v = Vector3::zero;