Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdexcept>
- #include <iostream>
- #include <sstream>
- #include <string>
- class Color{
- public:
- Color(int r, int g, int b, int a = 255);
- Color(int g);
- int get_red();
- int get_green();
- int get_blue();
- int get_alpha();
- Color operator+(Color const& rhs);
- friend std::ostream& operator<<(std::ostream& lhs, Color const&);
- private:
- int red;
- int green;
- int blue;
- int alpha;
- };
- //------------------------------------------------------------------//
- Color::Color(int r, int g, int b, int a):
- red{r}, green{g}, blue{b}, alpha{a}
- {
- if(r > 255 || r<0 || g > 255 || g<0 ||b > 255 || b<0 || a > 255 || a<0){
- throw std::invalid_argument("Glöggen is lodium");
- }
- }
- Color::Color(int g):
- red{g}, green{g}, blue{g}, alpha{255}
- {
- }
- int Color::get_red(){
- return red;
- }
- int Color::get_green(){
- return green;
- }
- int Color::get_blue(){
- return blue;
- }
- int Color::get_alpha(){
- return alpha;
- }
- Color Color::operator+(Color const& rhs){
- Color tmp{1,2,5};
- std:: cout << this -> red;
- return tmp;
- }
- std::ostream& operator<<(std::ostream& lhs, Color const& rhs) {
- lhs << "{" << rhs.red << ", " << rhs.green << ", " << rhs.blue << "}";
- return lhs;
- }
- //------------------------------------------------------------------//
- std::string str(Color const& c)
- {
- std::ostringstream oss;
- oss << c;
- return oss.str();
- }
- // add your Color class here
- // you may but are not required to add a header file
- // do not modify anything else
- #define CATCH_CONFIG_MAIN
- #include "catch.hpp"
- #include <sstream>
- TEST_CASE("Constructor")
- {
- Color a{100,200,0,3};
- CHECK( a.get_red() == 100 );
- CHECK( a.get_green() == 200 );
- CHECK( a.get_blue() == 0 );
- CHECK( a.get_alpha() == 3 );
- Color b{100,200,0};
- CHECK( b.get_red() == 100 );
- CHECK( b.get_green() == 200 );
- CHECK( b.get_blue() == 0 );
- CHECK( b.get_alpha() == 255 );
- a+b;
- Color c{100};
- CHECK( c.get_red() == 100 );
- CHECK( c.get_green() == 100 );
- CHECK( c.get_blue() == 100 );
- CHECK( c.get_alpha() == 255 );
- }
- TEST_CASE("construction")
- {
- CHECK_NOTHROW( (Color{1,255,0,255}) );
- CHECK_NOTHROW( (Color{100,200,0}) );
- CHECK_NOTHROW( (Color{75,140,60}) );
- CHECK_NOTHROW( (Color{128}) );
- CHECK_THROWS( (Color{256,-1,256,-1}) );
- CHECK_THROWS( (Color{-256,1,256,1}) );
- CHECK_THROWS( (Color{256,1,-256,1}) );
- }
- TEST_CASE("print")
- {
- CHECK( str(Color{50,80,20,128}) == "{50, 80, 20}");
- }
- /*
- TEST_CASE("add color")
- {
- Color a{100,200,0};
- Color b{50,80,120};
- Color c{75,140,60};
- REQUIRE( str(Color{50,80,20}) == "{50, 80, 20}");
- CHECK( str(a+b) == str(c) );
- CHECK( str(a+c) != str(b) );
- CHECK( str(b+c) != str(a) );
- }
- TEST_CASE("add grayscale")
- {
- Color a{100,200,0};
- Color b{100,150,50};
- REQUIRE( str(Color{50,80,20}) == "{50, 80, 20}");
- CHECK( str(a+100) == str(b) );
- CHECK( str(100+a) == str(b) );
- }
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement