Skip to content

Commit

Permalink
changes applied
Browse files Browse the repository at this point in the history
Signed-off-by: Avisheet <[email protected]>
  • Loading branch information
Avisheet committed Feb 22, 2024
1 parent 66df55e commit 82a2bff
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 19 deletions.
16 changes: 16 additions & 0 deletions .vscode/c_cpp_properties.json
Original file line number Diff line number Diff line change
@@ -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
}
7 changes: 7 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"files.associations": {
"ostream": "cpp",
"stdexcept": "cpp",
"cmath": "cpp"
}
}
4 changes: 2 additions & 2 deletions include/gz/math/Color.hh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 19 additions & 3 deletions src/Color.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
#include <cmath>
#include <algorithm>
#include<iostream>

#include "gz/math/Color.hh"

Expand Down Expand Up @@ -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<const Color *>(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"<<std::endl;
throw std::runtime_error("Index Error: Color index out of range");
}

//////////////////////////////////////////////////
float Color::operator[](const unsigned int _index) const
const float& Color::operator[](const unsigned int _index) const
{
switch (_index)
{
Expand Down
35 changes: 21 additions & 14 deletions src/Color_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -433,25 +433,32 @@ 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;
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);

// 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);

}
// 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);
}

0 comments on commit 82a2bff

Please sign in to comment.