Commit bafed64e authored by Mathias Goldau's avatar Mathias Goldau
Browse files

[ADD #132] Missing unit tests

parent 035596b6
......@@ -35,6 +35,11 @@ WColor::WColor( float red, float green, float blue, float alpha )
m_blue( blue ),
m_alpha( alpha )
{
// check if the given values are correct in range
assert( m_green <= 1.0 && m_green >= 0.0 && "WColor comopnent out of range" );
assert( m_blue <= 1.0 && m_blue >= 0.0 && "WColor comopnent out of range" );
assert( m_red <= 1.0 && m_red >= 0.0 && "WColor comopnent out of range" );
assert( m_alpha <= 1.0 && m_alpha >= 0.0 && "WColor comopnent out of range" );
}
void WColor::setGreen( float green )
......@@ -99,15 +104,16 @@ std::ostream& operator<<( std::ostream& out, const WColor& c )
std::istream& operator>>( std::istream& in, WColor& c )
{
std::vector<std::string> tokens;
std::string str;
in >> str;
std::vector<std::string> tokens;
tokens = string_utils::tokenize( str, ";" );
assert( tokens.size() == 3 && "There weren't 3 color values for a WColor" );
assert( tokens.size() == 4 && "There weren't 4 color values for a WColor" );
c.setRed( boost::lexical_cast< float >( tokens[0] ) );
c.setGreen( boost::lexical_cast< float >( tokens[1] ) );
c.setBlue( boost::lexical_cast< float >( tokens[2] ) );
c.setAlpha( boost::lexical_cast< float >( tokens[3] ) );
return in;
}
......@@ -92,11 +92,6 @@ public:
*/
osg::Vec4 getOSGColor() const;
void tokenize( const std::string& str, std::vector<std::string>& tokens, const std::string& delimiters = " " );
friend std::ostream& operator<<( std::ostream& out, const WColor& c );
friend std::istream& operator>>( std::istream& in, WColor& c );
protected:
private:
float m_red;
......@@ -105,4 +100,7 @@ private:
float m_alpha;
};
std::ostream& operator<<( std::ostream& out, const WColor& c );
std::istream& operator>>( std::istream& in, WColor& c );
#endif // WCOLOR_H
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
// For more information see http://www.openwalnut.org/copying
//
// This file is part of OpenWalnut.
//
// OpenWalnut is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// OpenWalnut is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------
#ifndef WCOLOR_TEST_H
#define WCOLOR_TEST_H
#include <string>
#include <sstream>
#include <cxxtest/TestSuite.h>
#include "../WColor.h"
/**
* Unit tests the WColor class
*/
class WColorTest : public CxxTest::TestSuite
{
public:
/**
* To write a WColor object to an output stream we must ensure that every
* component (red, green, blue and alpha) are written to that stream and
* that we have a special delimiter char.
*/
void testOutputOperator( void )
{
std::stringstream ss;
WColor c;
c.setBlue( 0.4711 );
ss << c;
std::string expected = "0;0;0.4711;1";
TS_ASSERT_EQUALS( ss.str(), expected );
}
/**
* When given a string separated by semicolons then it should be handled
* as red green blue and alpha values. There has to be 4 such values.
*/
void testInputOperator( void )
{
WColor c( 1.0, 0.5, 0.25 );
std::stringstream ss( "1.0;0.5;0.25;0.0" );
ss >> c;
TS_ASSERT_EQUALS( c.getRed(), 1.0 );
TS_ASSERT_EQUALS( c.getGreen(), 0.5 );
TS_ASSERT_EQUALS( c.getBlue(), 0.25 );
TS_ASSERT_EQUALS( c.getAlpha(), 0.0 );
}
};
#endif // WCOLOR_TEST_H
......@@ -45,18 +45,18 @@ CXXTEST_TEMPLATE_INSTANTIATION
class ValueTraits< std::streampos >
{
private:
char _s[256];
std::string _s;
public:
explicit ValueTraits( const std::streampos &pos )
{
std::stringstream ss;
ss << pos;
snprintf( _s, ss.str().size(), "%s", ss.str().c_str() );
_s = ss.str();
}
const char *asString() const
{
return _s;
return _s.c_str();
}
};
}
......@@ -70,18 +70,18 @@ CXXTEST_TEMPLATE_INSTANTIATION
class ValueTraits< wmath::WFiber >
{
private:
char _s[10240];
std::string _s;
public:
explicit ValueTraits( const wmath::WFiber &fib )
{
std::stringstream ss;
ss << fib;
snprintf( _s, ss.str().size(), "%s", ss.str().c_str() );
_s = ss.str();
}
const char *asString() const
{
return _s;
return _s.c_str();
}
};
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment