diff --git a/include/gz/math/Color.hh b/include/gz/math/Color.hh index 22d8c92f..6412aa60 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 c79b52b4..8af1ffbd 100644 --- a/src/Color.cc +++ b/src/Color.cc @@ -16,6 +16,7 @@ */ #include #include +#include #include "gz/math/Color.hh" @@ -187,13 +188,13 @@ 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]; + return (*static_cast(this))[_index]; } ////////////////////////////////////////////////// -float Color::operator[](const unsigned int _index) const +const float& Color::operator[](const unsigned int _index) const { switch (_index) { diff --git a/src/Color_TEST.cc b/src/Color_TEST.cc index 33b1d8d3..fa1b5117 100644 --- a/src/Color_TEST.cc +++ b/src/Color_TEST.cc @@ -433,3 +433,19 @@ TEST(Color, HSV) EXPECT_NEAR(clr.B(), 0.3f, 1e-3); EXPECT_NEAR(clr.A(), 1.0, 1e-3); } + +TEST(Color, OperatorIndex){ + math::Color clr; + clr[0] = 0.1f; + clr[1] = 0.2f; + clr[2] = 0.3f; + clr[3] = 0.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); + EXPECT_TRUE(std::isnan(clr[4])); +} + + diff --git a/src/Vector3_TEST.cc b/src/Vector3_TEST.cc index fdda6895..ab4d9247 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); +}