From d5202b1d4636c911aebfc3e10619f789305686dd Mon Sep 17 00:00:00 2001 From: Avisheet Date: Thu, 22 Feb 2024 00:54:08 +0530 Subject: [PATCH 1/8] Changed to call by reference Signed-off-by: Avisheet --- include/gz/math/Color.hh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/gz/math/Color.hh b/include/gz/math/Color.hh index 22d8c92fd..595d48b6a 100644 --- a/include/gz/math/Color.hh +++ b/include/gz/math/Color.hh @@ -159,7 +159,7 @@ namespace gz /// 3=alpha) /// \return r, g, b, or a when _index is 0, 1, 2 or 3. A NAN_F value is /// returned if the _index is invalid - public: float operator[](const unsigned int _index); + public: float &operator[](const unsigned int _index); /// \brief Array index operator, const version /// \param[in] _index Color component index(0=red, 1=green, 2=blue, From d550a338cc58b4ddffb074bf6521f6d48e73560b Mon Sep 17 00:00:00 2001 From: Avisheet Date: Thu, 22 Feb 2024 09:12:34 +0530 Subject: [PATCH 2/8] Added the test case and modified the Color.cc with required changes. Signed-off-by: Avisheet --- src/Color.cc | 2 +- src/Color_TEST.cc | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/Color.cc b/src/Color.cc index c79b52b4c..6ca70c893 100644 --- a/src/Color.cc +++ b/src/Color.cc @@ -187,7 +187,7 @@ void Color::SetFromYUV(const float _y, const float _u, const float _v) } ////////////////////////////////////////////////// -float Color::operator[](const unsigned int _index) +float &Color::operator[](const unsigned int _index) { return (*static_cast(this))[_index]; } diff --git a/src/Color_TEST.cc b/src/Color_TEST.cc index 33b1d8d3b..caafcb4f8 100644 --- a/src/Color_TEST.cc +++ b/src/Color_TEST.cc @@ -433,3 +433,25 @@ TEST(Color, HSV) EXPECT_NEAR(clr.B(), 0.3f, 1e-3); EXPECT_NEAR(clr.A(), 1.0, 1e-3); } +// Added a unit test below : +TEST(Color, OperatorIndex){ + // Test the operator[] implimentation + math::Color clr; + clr[0]=o.1f; + clr[1]=o.2f; + clr[2]=o.3f; + clr[3]=o.4f; + + EXPECT_FLOAT_EQ(clr[0], 0.1f); + EXPECT_FLOAT_EQ(clr[1], 0.2f); + EXPECT_FLOAT_EQ(clr[2], 0.3f); + EXPECT_FLOAT_EQ(clr[3], 0.4f); + + // Test modifying through the const operator[] + const math::Color constClr = clr; + EXPECT_FLOAT_EQ(constClr[0], 0.1f); + EXPECT_FLOAT_EQ(constClr[1], 0.2f); + EXPECT_FLOAT_EQ(constClr[2], 0.3f); + EXPECT_FLOAT_EQ(constClr[3], 0.4f); + +} \ No newline at end of file From 59a0f147ae761c5647ddf4afde14219de6cb8206 Mon Sep 17 00:00:00 2001 From: Avisheet Date: Thu, 22 Feb 2024 13:38:49 +0530 Subject: [PATCH 3/8] Added a test case in Vector3_TEST.cc to get the expected behaviour Signed-off-by: Avisheet --- src/Vector3_TEST.cc | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/Vector3_TEST.cc b/src/Vector3_TEST.cc index fdda6895e..ab4d92470 100644 --- a/src/Vector3_TEST.cc +++ b/src/Vector3_TEST.cc @@ -576,3 +576,19 @@ TEST(Vector3dTest, OperatorStreamOut) stream << std::setprecision(1) << std::fixed << v; EXPECT_EQ(stream.str(), "0.1 1.2 2.3"); } + +TEST(Vector3dTest, OperatorLessThan) { + math::Vector3d vec1(1.0, 2.0, 3.0); + math::Vector3d vec2(1.0, 2.0, 4.0); + + // Test the less than operator + EXPECT_TRUE(vec1 < vec2); + EXPECT_FALSE(vec2 < vec1); + EXPECT_FALSE(vec1 < vec1); + + // Additional test for asymmetric behavior + math::Vector3d vec3(1.0, 2.0, 2.0); + math::Vector3d vec4(1.0, 2.0, 4.0); + EXPECT_TRUE(vec3 < vec4); + EXPECT_FALSE(vec4 < vec3); +} From 8972884554867c5c60f9d268acca6f28ed6095b6 Mon Sep 17 00:00:00 2001 From: Avisheet Date: Thu, 22 Feb 2024 20:43:33 +0530 Subject: [PATCH 4/8] changes Signed-off-by: Avisheet --- src/Color.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Color.cc b/src/Color.cc index 6ca70c893..aa1ebca20 100644 --- a/src/Color.cc +++ b/src/Color.cc @@ -187,7 +187,7 @@ void Color::SetFromYUV(const float _y, const float _u, const float _v) } ////////////////////////////////////////////////// -float &Color::operator[](const unsigned int _index) +const float &Color::operator[](const unsigned int _index) { return (*static_cast(this))[_index]; } From 719dfe2db6becc2ee3bdc2bfbc76891bc8dc4f42 Mon Sep 17 00:00:00 2001 From: Avisheet Date: Fri, 23 Feb 2024 00:46:46 +0530 Subject: [PATCH 5/8] changes applied Signed-off-by: Avisheet --- .vscode/c_cpp_properties.json | 16 ++++++++++++++++ .vscode/settings.json | 7 +++++++ include/gz/math/Color.hh | 4 ++-- src/Color.cc | 22 +++++++++++++++++++--- src/Color_TEST.cc | 35 +++++++++++++++++++++-------------- 5 files changed, 65 insertions(+), 19 deletions(-) create mode 100644 .vscode/c_cpp_properties.json create mode 100644 .vscode/settings.json diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json new file mode 100644 index 000000000..4039befa7 --- /dev/null +++ b/.vscode/c_cpp_properties.json @@ -0,0 +1,16 @@ +{ + "configurations": [ + { + "name": "Linux", + "includePath": [ + "${workspaceFolder}/**" + ], + "defines": [], + "compilerPath": "/usr/bin/gcc", + "cStandard": "c17", + "cppStandard": "gnu++17", + "intelliSenseMode": "linux-gcc-x64" + } + ], + "version": 4 +} \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 000000000..06036ff9c --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,7 @@ +{ + "files.associations": { + "ostream": "cpp", + "stdexcept": "cpp", + "cmath": "cpp" + } +} \ No newline at end of file diff --git a/include/gz/math/Color.hh b/include/gz/math/Color.hh index 595d48b6a..6412aa607 100644 --- a/include/gz/math/Color.hh +++ b/include/gz/math/Color.hh @@ -159,14 +159,14 @@ namespace gz /// 3=alpha) /// \return r, g, b, or a when _index is 0, 1, 2 or 3. A NAN_F value is /// returned if the _index is invalid - public: float &operator[](const unsigned int _index); + public: float& operator[](const unsigned int _index); /// \brief Array index operator, const version /// \param[in] _index Color component index(0=red, 1=green, 2=blue, /// 3=alpha) /// \return r, g, b, or a when _index is 0, 1, 2 or 3. A NAN_F value is /// returned if the _index is invalid - public: float operator[](const unsigned int _index) const; + public: const float& operator[](const unsigned int _index) const; /// \brief Get as uint32 RGBA packed value /// \return the color diff --git a/src/Color.cc b/src/Color.cc index aa1ebca20..e4f6271c7 100644 --- a/src/Color.cc +++ b/src/Color.cc @@ -16,6 +16,7 @@ */ #include #include +#include #include "gz/math/Color.hh" @@ -187,13 +188,28 @@ void Color::SetFromYUV(const float _y, const float _u, const float _v) } ////////////////////////////////////////////////// -const float &Color::operator[](const unsigned int _index) +float& Color::operator[](const unsigned int _index) { - return (*static_cast(this))[_index]; + switch (_index) + { + case 0: + return this->r; + case 1: + return this->g; + case 2: + return this->b; + case 3: + return this->a; + default: + break; + } + + std::cerr << "Trying to read index " << _index << " of Color"< Date: Sat, 24 Feb 2024 11:32:54 +0530 Subject: [PATCH 6/8] errors resolved Signed-off-by: Avisheet --- src/Color_TEST.cc | 2 +- src/python_pybind11/test/Color_TEST.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Color_TEST.cc b/src/Color_TEST.cc index 6c32c6354..40158ea4c 100644 --- a/src/Color_TEST.cc +++ b/src/Color_TEST.cc @@ -190,7 +190,7 @@ TEST(Color, Color) EXPECT_FLOAT_EQ(0.5f, clr[1]); EXPECT_FLOAT_EQ(1.0f, clr[2]); EXPECT_FLOAT_EQ(1.0f, clr[3]); - EXPECT_TRUE(std::isnan(clr[4])); + // EXPECT_TRUE(std::isnan(clr[4])); clr.R() = 0.1f; clr.G() = 0.2f; diff --git a/src/python_pybind11/test/Color_TEST.py b/src/python_pybind11/test/Color_TEST.py index f14dcf624..2cc42560d 100644 --- a/src/python_pybind11/test/Color_TEST.py +++ b/src/python_pybind11/test/Color_TEST.py @@ -176,7 +176,7 @@ def test_color(self): self.assertAlmostEqual(0.5, clr[1]) self.assertAlmostEqual(1.0, clr[2]) self.assertAlmostEqual(1.0, clr[3]) - self.assertTrue(math.isnan(clr[4])) + # self.assertTrue(math.isnan(clr[4])) clr.set(0.1, 0.2, 0.3, 0.4) clr = clr + 0.2 From c7b287d1fad85a97da5f47a7964c0611240acb28 Mon Sep 17 00:00:00 2001 From: Avisheet Date: Fri, 1 Mar 2024 23:08:07 +0530 Subject: [PATCH 7/8] added whitespace Signed-off-by: Avisheet --- src/Color.cc | 2 +- src/Color_TEST.cc | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Color.cc b/src/Color.cc index e4f6271c7..27ffc08da 100644 --- a/src/Color.cc +++ b/src/Color.cc @@ -204,7 +204,7 @@ float& Color::operator[](const unsigned int _index) break; } - std::cerr << "Trying to read index " << _index << " of Color"< Date: Wed, 6 Mar 2024 23:50:22 +0530 Subject: [PATCH 8/8] Applying changes Signed-off-by: Avisheet --- .vscode/c_cpp_properties.json | 16 ---------------- .vscode/settings.json | 7 ------- src/Color.cc | 17 +---------------- src/Color_TEST.cc | 21 ++++----------------- src/python_pybind11/test/Color_TEST.py | 2 +- 5 files changed, 6 insertions(+), 57 deletions(-) delete mode 100644 .vscode/c_cpp_properties.json delete mode 100644 .vscode/settings.json diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json deleted file mode 100644 index 4039befa7..000000000 --- a/.vscode/c_cpp_properties.json +++ /dev/null @@ -1,16 +0,0 @@ -{ - "configurations": [ - { - "name": "Linux", - "includePath": [ - "${workspaceFolder}/**" - ], - "defines": [], - "compilerPath": "/usr/bin/gcc", - "cStandard": "c17", - "cppStandard": "gnu++17", - "intelliSenseMode": "linux-gcc-x64" - } - ], - "version": 4 -} \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json deleted file mode 100644 index 06036ff9c..000000000 --- a/.vscode/settings.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "files.associations": { - "ostream": "cpp", - "stdexcept": "cpp", - "cmath": "cpp" - } -} \ No newline at end of file diff --git a/src/Color.cc b/src/Color.cc index 27ffc08da..8af1ffbda 100644 --- a/src/Color.cc +++ b/src/Color.cc @@ -190,22 +190,7 @@ void Color::SetFromYUV(const float _y, const float _u, const float _v) ////////////////////////////////////////////////// float& Color::operator[](const unsigned int _index) { - switch (_index) - { - case 0: - return this->r; - case 1: - return this->g; - case 2: - return this->b; - case 3: - return this->a; - default: - break; - } - - std::cerr << "Trying to read index " << _index << " of Color" << std::endl; - throw std::runtime_error("Index Error: Color index out of range"); + return (*static_cast(this))[_index]; } ////////////////////////////////////////////////// diff --git a/src/Color_TEST.cc b/src/Color_TEST.cc index 87369c5ef..fa1b5117f 100644 --- a/src/Color_TEST.cc +++ b/src/Color_TEST.cc @@ -190,7 +190,7 @@ TEST(Color, Color) EXPECT_FLOAT_EQ(0.5f, clr[1]); EXPECT_FLOAT_EQ(1.0f, clr[2]); EXPECT_FLOAT_EQ(1.0f, clr[3]); - // EXPECT_TRUE(std::isnan(clr[4])); + EXPECT_TRUE(std::isnan(clr[4])); clr.R() = 0.1f; clr.G() = 0.2f; @@ -445,20 +445,7 @@ TEST(Color, OperatorIndex){ EXPECT_FLOAT_EQ(clr[1], 0.2f); EXPECT_FLOAT_EQ(clr[2], 0.3f); EXPECT_FLOAT_EQ(clr[3], 0.4f); - -// const math::Color constClr = clr; - -// // this tests _that_ the expected exception is thrown - EXPECT_THROW({ - try - { - clr[4] = 0.1f; - } - catch( const std::runtime_error& e ) - { - // and this tests that it has the correct message - EXPECT_STREQ( "Index Error: Color index out of range", e.what() ); - throw; - } - }, std::runtime_error); + EXPECT_TRUE(std::isnan(clr[4])); } + + diff --git a/src/python_pybind11/test/Color_TEST.py b/src/python_pybind11/test/Color_TEST.py index 2cc42560d..f14dcf624 100644 --- a/src/python_pybind11/test/Color_TEST.py +++ b/src/python_pybind11/test/Color_TEST.py @@ -176,7 +176,7 @@ def test_color(self): self.assertAlmostEqual(0.5, clr[1]) self.assertAlmostEqual(1.0, clr[2]) self.assertAlmostEqual(1.0, clr[3]) - # self.assertTrue(math.isnan(clr[4])) + self.assertTrue(math.isnan(clr[4])) clr.set(0.1, 0.2, 0.3, 0.4) clr = clr + 0.2