From 903de5f9062dad721e4e4d44b0f6bd50d76dd67e Mon Sep 17 00:00:00 2001 From: tastytea Date: Tue, 1 Jan 2019 16:46:17 +0100 Subject: [PATCH] Added first tests. --- .drone.yml | 3 +- CMakeLists.txt | 1 + README.md | 3 ++ src/tests/main.cpp | 2 + src/tests/test_ltr_asymmetric.cpp | 88 +++++++++++++++++++++++++++++++ src/tests/test_ltr_symmetric.cpp | 88 +++++++++++++++++++++++++++++++ src/tests/test_sigil.cpp | 88 +++++++++++++++++++++++++++++++ tests.CMakeLists.txt | 12 +++++ 8 files changed, 284 insertions(+), 1 deletion(-) create mode 100644 src/tests/main.cpp create mode 100644 src/tests/test_ltr_asymmetric.cpp create mode 100644 src/tests/test_ltr_symmetric.cpp create mode 100644 src/tests/test_sigil.cpp create mode 100644 tests.CMakeLists.txt diff --git a/.drone.yml b/.drone.yml index aba2445..25c1b1a 100644 --- a/.drone.yml +++ b/.drone.yml @@ -19,10 +19,11 @@ pipeline: - apt-get update -q - apt-get install -qy -t xenial g++-5 - apt-get install -qy cmake pkg-config - - apt-get install -qy libmagick++-dev + - apt-get install -qy libmagick++-dev catch - rm -rf build && mkdir -p build && cd build - cmake -DCMAKE_INSTALL_PREFIX=/usr .. - make VERBOSE=1 + - ctest .. volumes: - /var/cache/debian-package-cache:/var/cache/apt/archives diff --git a/CMakeLists.txt b/CMakeLists.txt index 4a2c6a4..e28c2c3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -41,3 +41,4 @@ install(FILES src/${CMAKE_PROJECT_NAME}.hpp DESTINATION ${CMAKE_INSTALL_INCLUDED install(FILES README.md DESTINATION ${CMAKE_INSTALL_DOCDIR}) include(packages.CMakeLists.txt) +include(tests.CMakeLists.txt) diff --git a/README.md b/README.md index 232afc4..e63d3ca 100644 --- a/README.md +++ b/README.md @@ -79,6 +79,8 @@ They are generated on Debian Stretch 64 bit and signed with my [clang](https://llvm.org/) 5/6) * [cmake](https://cmake.org/) (at least 3.2) * [imagemagick](https://www.imagemagick.org/) (tested: 7.0 / 6.7) +* Optional: + * [Catch](https://github.com/catchorg/Catch2) (tested: 2.3.0) On a Debian system, install the packages: `build-essential cmake libmagick++-dev`. @@ -96,6 +98,7 @@ make install ##### cmake options * `-DCMAKE_BUILD_TYPE=Debug` for a debug build +* `-DWITH_TESTS=Debug` to build tests * One of: * `-DWITH_DEB=YES` to generate a deb-package * `-DWITH_RPM=YES` to generate an rpm-package diff --git a/src/tests/main.cpp b/src/tests/main.cpp new file mode 100644 index 0000000..4ed06df --- /dev/null +++ b/src/tests/main.cpp @@ -0,0 +1,2 @@ +#define CATCH_CONFIG_MAIN +#include diff --git a/src/tests/test_ltr_asymmetric.cpp b/src/tests/test_ltr_asymmetric.cpp new file mode 100644 index 0000000..e72c5d4 --- /dev/null +++ b/src/tests/test_ltr_asymmetric.cpp @@ -0,0 +1,88 @@ +#include +#include +#include +#include "identiconpp.hpp" + +using std::string; +#include + +SCENARIO("ltr_asymmetric") +{ + GIVEN("An identicon instance with 2x2 dots") + { + Identiconpp identicon(2, 2, Identiconpp::algorithm::ltr_asymmetric); + Magick::Image img; + Magick::Color black("#000000ff"); + Magick::Color white("#ffffffff"); + + WHEN("Digest is \"00\"") + { + img = identicon.generate("00", 2); + THEN("Is completely white") + { + REQUIRE(img.pixelColor(0, 0) == white); + REQUIRE(img.pixelColor(0, 1) == white); + REQUIRE(img.pixelColor(1, 0) == white); + REQUIRE(img.pixelColor(1, 1) == white); + } + } + + WHEN("Digest is \"ff\"") + { + img = identicon.generate("ff", 2); + THEN("Is completely black") + { + REQUIRE(img.pixelColor(0, 0) == black); + REQUIRE(img.pixelColor(0, 1) == black); + REQUIRE(img.pixelColor(1, 0) == black); + REQUIRE(img.pixelColor(1, 1) == black); + } + } + + WHEN("Digest is \"70\"") + { + // 0111 0000 = 01 + // 11 + img = identicon.generate("70", 2); + THEN("Produces white pixel at 0x0") + { + REQUIRE(img.pixelColor(0, 0) == white); + } + THEN("Produces black pixel at 1x0") + { + REQUIRE(img.pixelColor(1, 0) == black); + } + THEN("Produces black pixel at 1x0") + { + REQUIRE(img.pixelColor(0, 1) == black); + } + THEN("Produces black pixel at 1x1") + { + REQUIRE(img.pixelColor(1, 1) == black); + } + } + + WHEN("Digest is \"80\"") + { + // 1000 0000 = 10 + // 00 + img = identicon.generate("80", 2); + THEN("Produces black pixel at 0x0") + { + REQUIRE(img.pixelColor(0, 0) == black); + } + THEN("Produces white pixel at 1x0") + { + REQUIRE(img.pixelColor(1, 0) == white); + } + THEN("Produces white pixel at 1x0") + { + REQUIRE(img.pixelColor(0, 1) == white); + } + THEN("Produces white pixel at 1x1") + { + REQUIRE(img.pixelColor(1, 1) == white); + } + } + } +} diff --git a/src/tests/test_ltr_symmetric.cpp b/src/tests/test_ltr_symmetric.cpp new file mode 100644 index 0000000..58dff59 --- /dev/null +++ b/src/tests/test_ltr_symmetric.cpp @@ -0,0 +1,88 @@ +#include +#include +#include +#include "identiconpp.hpp" + +using std::string; +#include + +SCENARIO("ltr_symmetric") +{ + GIVEN("An identicon instance with 2x2 dots") + { + Identiconpp identicon(2, 2, Identiconpp::algorithm::ltr_symmetric); + Magick::Image img; + Magick::Color black("#000000ff"); + Magick::Color white("#ffffffff"); + + WHEN("Digest is \"0\"") + { + img = identicon.generate("0", 2); + THEN("Is completely white") + { + REQUIRE(img.pixelColor(0, 0) == white); + REQUIRE(img.pixelColor(0, 1) == white); + REQUIRE(img.pixelColor(1, 0) == white); + REQUIRE(img.pixelColor(1, 1) == white); + } + } + + WHEN("Digest is \"f\"") + { + img = identicon.generate("f", 2); + THEN("Is completely black") + { + REQUIRE(img.pixelColor(0, 0) == black); + REQUIRE(img.pixelColor(0, 1) == black); + REQUIRE(img.pixelColor(1, 0) == black); + REQUIRE(img.pixelColor(1, 1) == black); + } + } + + WHEN("Digest is \"7\"") + { + // 0111 = 00 + // 11 + img = identicon.generate("7", 2); + THEN("Produces white pixel at 0x0") + { + REQUIRE(img.pixelColor(0, 0) == white); + } + THEN("Produces white pixel at 1x0") + { + REQUIRE(img.pixelColor(1, 0) == white); + } + THEN("Produces black pixel at 1x0") + { + REQUIRE(img.pixelColor(0, 1) == black); + } + THEN("Produces black pixel at 1x1") + { + REQUIRE(img.pixelColor(1, 1) == black); + } + } + + WHEN("Digest is \"8\"") + { + // 1000 = 11 + // 00 + img = identicon.generate("8", 2); + THEN("Produces black pixel at 0x0") + { + REQUIRE(img.pixelColor(0, 0) == black); + } + THEN("Produces black pixel at 1x0") + { + REQUIRE(img.pixelColor(1, 0) == black); + } + THEN("Produces white pixel at 1x0") + { + REQUIRE(img.pixelColor(0, 1) == white); + } + THEN("Produces white pixel at 1x1") + { + REQUIRE(img.pixelColor(1, 1) == white); + } + } + } +} diff --git a/src/tests/test_sigil.cpp b/src/tests/test_sigil.cpp new file mode 100644 index 0000000..0f987c1 --- /dev/null +++ b/src/tests/test_sigil.cpp @@ -0,0 +1,88 @@ +#include +#include +#include +#include "identiconpp.hpp" + +using std::string; +#include + +SCENARIO("sigil") +{ + GIVEN("An identicon instance with 2x2 dots") + { + Identiconpp identicon(2, 2, Identiconpp::algorithm::sigil); + Magick::Image img; + Magick::Color black("#000000ff"); + Magick::Color white("#ffffffff"); + + WHEN("Digest is \"000\"") + { + img = identicon.generate("000", 2); + THEN("Is completely white") + { + REQUIRE(img.pixelColor(0, 0) == white); + REQUIRE(img.pixelColor(0, 1) == white); + REQUIRE(img.pixelColor(1, 0) == white); + REQUIRE(img.pixelColor(1, 1) == white); + } + } + + WHEN("Digest is \"fff\"") + { + img = identicon.generate("fff", 2); + THEN("Is completely black") + { + REQUIRE(img.pixelColor(0, 0) == black); + REQUIRE(img.pixelColor(0, 1) == black); + REQUIRE(img.pixelColor(1, 0) == black); + REQUIRE(img.pixelColor(1, 1) == black); + } + } + + WHEN("Digest is \"007\"") + { + // 0000 0000 0111 = 00 + // 11 + img = identicon.generate("007", 2); + THEN("Produces white pixel at 0x0") + { + REQUIRE(img.pixelColor(0, 0) == white); + } + THEN("Produces white pixel at 1x0") + { + REQUIRE(img.pixelColor(1, 0) == white); + } + THEN("Produces black pixel at 1x0") + { + REQUIRE(img.pixelColor(0, 1) == black); + } + THEN("Produces black pixel at 1x1") + { + REQUIRE(img.pixelColor(1, 1) == black); + } + } + + WHEN("Digest is \"008\"") + { + // 0000 0000 1000 = 11 + // 00 + img = identicon.generate("008", 2); + THEN("Produces black pixel at 0x0") + { + REQUIRE(img.pixelColor(0, 0) == black); + } + THEN("Produces black pixel at 1x0") + { + REQUIRE(img.pixelColor(1, 0) == black); + } + THEN("Produces white pixel at 1x0") + { + REQUIRE(img.pixelColor(0, 1) == white); + } + THEN("Produces white pixel at 1x1") + { + REQUIRE(img.pixelColor(1, 1) == white); + } + } + } +} diff --git a/tests.CMakeLists.txt b/tests.CMakeLists.txt new file mode 100644 index 0000000..5eda443 --- /dev/null +++ b/tests.CMakeLists.txt @@ -0,0 +1,12 @@ +if(WITH_TESTS) + find_package(Catch2 REQUIRED) + include(CTest) + + file(GLOB sources_tests src/tests/test_*.cpp) + foreach(src ${sources_tests}) + get_filename_component(bin ${src} NAME_WE) + add_executable(${bin} src/tests/main.cpp ${src}) + target_link_libraries(${bin} ${CMAKE_PROJECT_NAME} Catch2::Catch2) + add_test(${bin} ${bin}) + endforeach() +endif()