view tests/libclient/color/main.cpp @ 76:858621081b95

Happy new year!
author David Demelier <markand@malikania.fr>
date Sun, 01 Jan 2017 13:35:37 +0100
parents fe7e3524e571
children ee850a6ab89e
line wrap: on
line source

/*
 * main.cpp -- test mlk::color
 *
 * Copyright (c) 2013-2017 Malikania Authors
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#define BOOST_TEST_MODULE "Color"
#include <boost/test/unit_test.hpp>

#include <malikania/color.hpp>

/*
 * Separate arguments.
 * ------------------------------------------------------------------
 */

BOOST_AUTO_TEST_SUITE(basic)

BOOST_AUTO_TEST_CASE(black)
{
    mlk::color c;

    BOOST_REQUIRE_EQUAL(0, c.red());
    BOOST_REQUIRE_EQUAL(0, c.green());
    BOOST_REQUIRE_EQUAL(0, c.blue());
    BOOST_REQUIRE_EQUAL(255, c.alpha());
}

BOOST_AUTO_TEST_CASE(white)
{
    mlk::color c{255, 255, 255, 255};

    BOOST_REQUIRE_EQUAL(255, c.red());
    BOOST_REQUIRE_EQUAL(255, c.green());
    BOOST_REQUIRE_EQUAL(255, c.blue());
    BOOST_REQUIRE_EQUAL(255, c.alpha());
}

BOOST_AUTO_TEST_CASE(red)
{
    mlk::color c{255, 0, 0, 255};

    BOOST_REQUIRE_EQUAL(255, c.red());
    BOOST_REQUIRE_EQUAL(0, c.green());
    BOOST_REQUIRE_EQUAL(0, c.blue());
    BOOST_REQUIRE_EQUAL(255, c.alpha());
}

BOOST_AUTO_TEST_CASE(green)
{
    mlk::color c{0, 255, 0, 255};

    BOOST_REQUIRE_EQUAL(0, c.red());
    BOOST_REQUIRE_EQUAL(255, c.green());
    BOOST_REQUIRE_EQUAL(0, c.blue());
    BOOST_REQUIRE_EQUAL(255, c.alpha());
}

BOOST_AUTO_TEST_CASE(blue)
{
    mlk::color c{0, 0, 255, 255};

    BOOST_REQUIRE_EQUAL(0, c.red());
    BOOST_REQUIRE_EQUAL(0, c.green());
    BOOST_REQUIRE_EQUAL(255, c.blue());
    BOOST_REQUIRE_EQUAL(255, c.alpha());
}

BOOST_AUTO_TEST_SUITE_END()

/*
 * Hexadecimal.
 * ------------------------------------------------------------------
 */

BOOST_AUTO_TEST_SUITE(hex)

BOOST_AUTO_TEST_CASE(black)
{
    mlk::color c{0xff000000};

    BOOST_REQUIRE_EQUAL(0, c.red());
    BOOST_REQUIRE_EQUAL(0, c.green());
    BOOST_REQUIRE_EQUAL(0, c.blue());
    BOOST_REQUIRE_EQUAL(255, c.alpha());
}

BOOST_AUTO_TEST_CASE(white)
{
    mlk::color c{0xffffffff};

    BOOST_REQUIRE_EQUAL(255, c.red());
    BOOST_REQUIRE_EQUAL(255, c.green());
    BOOST_REQUIRE_EQUAL(255, c.blue());
    BOOST_REQUIRE_EQUAL(255, c.alpha());
}

BOOST_AUTO_TEST_CASE(red)
{
    mlk::color c{0xffff0000};

    BOOST_REQUIRE_EQUAL(255, c.red());
    BOOST_REQUIRE_EQUAL(0, c.green());
    BOOST_REQUIRE_EQUAL(0, c.blue());
    BOOST_REQUIRE_EQUAL(255, c.alpha());
}

BOOST_AUTO_TEST_CASE(green)
{
    mlk::color c{0xff00ff00};

    BOOST_REQUIRE_EQUAL(0, c.red());
    BOOST_REQUIRE_EQUAL(255, c.green());
    BOOST_REQUIRE_EQUAL(0, c.blue());
    BOOST_REQUIRE_EQUAL(255, c.alpha());
}

BOOST_AUTO_TEST_CASE(blue)
{
    mlk::color c{0xff0000ff};

    BOOST_REQUIRE_EQUAL(0, c.red());
    BOOST_REQUIRE_EQUAL(0, c.green());
    BOOST_REQUIRE_EQUAL(255, c.blue());
    BOOST_REQUIRE_EQUAL(255, c.alpha());
}

BOOST_AUTO_TEST_SUITE_END()

/*
 * Names.
 * ------------------------------------------------------------------
 *
 * Test most famous.
 */

BOOST_AUTO_TEST_SUITE(named)

BOOST_AUTO_TEST_CASE(black)
{
    mlk::color c{"black"};

    BOOST_REQUIRE_EQUAL(0, c.red());
    BOOST_REQUIRE_EQUAL(0, c.green());
    BOOST_REQUIRE_EQUAL(0, c.blue());
    BOOST_REQUIRE_EQUAL(255, c.alpha());
}

BOOST_AUTO_TEST_CASE(white)
{
    mlk::color c{"white"};

    BOOST_REQUIRE_EQUAL(255, c.red());
    BOOST_REQUIRE_EQUAL(255, c.green());
    BOOST_REQUIRE_EQUAL(255, c.blue());
    BOOST_REQUIRE_EQUAL(255, c.alpha());
}

BOOST_AUTO_TEST_CASE(red)
{
    mlk::color c{"red"};

    BOOST_REQUIRE_EQUAL(255, c.red());
    BOOST_REQUIRE_EQUAL(0, c.green());
    BOOST_REQUIRE_EQUAL(0, c.blue());
    BOOST_REQUIRE_EQUAL(255, c.alpha());
}

BOOST_AUTO_TEST_CASE(green)
{
    mlk::color c{"green"};

    BOOST_REQUIRE_EQUAL(0, c.red());
    BOOST_REQUIRE_EQUAL(128, c.green());
    BOOST_REQUIRE_EQUAL(0, c.blue());
    BOOST_REQUIRE_EQUAL(255, c.alpha());
}

BOOST_AUTO_TEST_CASE(blue)
{
    mlk::color c{"blue"};

    BOOST_REQUIRE_EQUAL(0, c.red());
    BOOST_REQUIRE_EQUAL(0, c.green());
    BOOST_REQUIRE_EQUAL(255, c.blue());
    BOOST_REQUIRE_EQUAL(255, c.alpha());
}

BOOST_AUTO_TEST_CASE(wrong)
{
    BOOST_REQUIRE_THROW(mlk::color c{"does not exist"}, std::exception);
}

BOOST_AUTO_TEST_SUITE_END()

/*
 * #rrggbb.
 * ------------------------------------------------------------------
 *
 * Test most famous.
 */

BOOST_AUTO_TEST_SUITE(long_rgb)

BOOST_AUTO_TEST_CASE(black)
{
    mlk::color c{"#000000"};

    BOOST_REQUIRE_EQUAL(0, c.red());
    BOOST_REQUIRE_EQUAL(0, c.green());
    BOOST_REQUIRE_EQUAL(0, c.blue());
    BOOST_REQUIRE_EQUAL(255, c.alpha());
}

BOOST_AUTO_TEST_CASE(white)
{
    mlk::color c{"#ffffff"};

    BOOST_REQUIRE_EQUAL(255, c.red());
    BOOST_REQUIRE_EQUAL(255, c.green());
    BOOST_REQUIRE_EQUAL(255, c.blue());
    BOOST_REQUIRE_EQUAL(255, c.alpha());
}

BOOST_AUTO_TEST_CASE(red)
{
    mlk::color c{"#ff0000"};

    BOOST_REQUIRE_EQUAL(255, c.red());
    BOOST_REQUIRE_EQUAL(0, c.green());
    BOOST_REQUIRE_EQUAL(0, c.blue());
    BOOST_REQUIRE_EQUAL(255, c.alpha());
}

BOOST_AUTO_TEST_CASE(green)
{
    mlk::color c{"#00ff00"};

    BOOST_REQUIRE_EQUAL(0, c.red());
    BOOST_REQUIRE_EQUAL(255, c.green());
    BOOST_REQUIRE_EQUAL(0, c.blue());
    BOOST_REQUIRE_EQUAL(255, c.alpha());
}

BOOST_AUTO_TEST_CASE(blue)
{
    mlk::color c{"#0000ff"};

    BOOST_REQUIRE_EQUAL(0, c.red());
    BOOST_REQUIRE_EQUAL(0, c.green());
    BOOST_REQUIRE_EQUAL(255, c.blue());
    BOOST_REQUIRE_EQUAL(255, c.alpha());
}

BOOST_AUTO_TEST_CASE(wrong)
{
    BOOST_REQUIRE_THROW(mlk::color c{"#ghijkl"}, std::exception);
}

BOOST_AUTO_TEST_SUITE_END()

/*
 * #rgb.
 * ------------------------------------------------------------------
 *
 * Test most famous.
 */

BOOST_AUTO_TEST_SUITE(short_rgb)

BOOST_AUTO_TEST_CASE(black)
{
    mlk::color c{"#000"};

    BOOST_REQUIRE_EQUAL(0, c.red());
    BOOST_REQUIRE_EQUAL(0, c.green());
    BOOST_REQUIRE_EQUAL(0, c.blue());
    BOOST_REQUIRE_EQUAL(255, c.alpha());
}

BOOST_AUTO_TEST_CASE(white)
{
    mlk::color c{"#fff"};

    BOOST_REQUIRE_EQUAL(255, c.red());
    BOOST_REQUIRE_EQUAL(255, c.green());
    BOOST_REQUIRE_EQUAL(255, c.blue());
    BOOST_REQUIRE_EQUAL(255, c.alpha());
}

BOOST_AUTO_TEST_CASE(red)
{
    mlk::color c{"#f00"};

    BOOST_REQUIRE_EQUAL(255, c.red());
    BOOST_REQUIRE_EQUAL(0, c.green());
    BOOST_REQUIRE_EQUAL(0, c.blue());
    BOOST_REQUIRE_EQUAL(255, c.alpha());
}

BOOST_AUTO_TEST_CASE(green)
{
    mlk::color c{"#0f0"};

    BOOST_REQUIRE_EQUAL(0, c.red());
    BOOST_REQUIRE_EQUAL(255, c.green());
    BOOST_REQUIRE_EQUAL(0, c.blue());
    BOOST_REQUIRE_EQUAL(255, c.alpha());
}

BOOST_AUTO_TEST_CASE(blue)
{
    mlk::color c{"#00f"};

    BOOST_REQUIRE_EQUAL(0, c.red());
    BOOST_REQUIRE_EQUAL(0, c.green());
    BOOST_REQUIRE_EQUAL(255, c.blue());
    BOOST_REQUIRE_EQUAL(255, c.alpha());
}

BOOST_AUTO_TEST_CASE(combination)
{
    mlk::color c{"#123"};

    BOOST_REQUIRE_EQUAL(17, c.red());
    BOOST_REQUIRE_EQUAL(34, c.green());
    BOOST_REQUIRE_EQUAL(51, c.blue());
    BOOST_REQUIRE_EQUAL(255, c.alpha());
}

BOOST_AUTO_TEST_CASE(wrong)
{
    BOOST_REQUIRE_THROW(mlk::color c{"#ghi"}, std::exception);
}

BOOST_AUTO_TEST_SUITE_END()