Skip to content

Commit

Permalink
Updated pixelator tests (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
niosus authored Aug 15, 2023
1 parent d1bbd8b commit 3f34841
Show file tree
Hide file tree
Showing 8 changed files with 134 additions and 87 deletions.
4 changes: 2 additions & 2 deletions homework.yml
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ homeworks:
folder: homework_5
tasks:
- name: Pixelator build
folder: tui_pixelator
folder: pixelator
tests:
- name: Configure CMake
cmd: cmake -D UPDATE_SUBMODULES=NO -D CMAKE_CXX_FLAGS="-Wall -Wextra -Wpedantic" -S . -B build
Expand All @@ -146,4 +146,4 @@ homeworks:
- name: Run your and validation tests
cmd: ctest --test-dir build --output-on-failure -j 2
- name: Pixelate an image
cmd: ./build/examples/pixelate tui_pixelator/test_data/test.png
cmd: ./build/examples/pixelate pixelator/test_data/test.png
6 changes: 3 additions & 3 deletions inject/homework_5/validation_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ include(GoogleTest)
set(VALIDATION_TEST validation_tests)
add_executable(${VALIDATION_TEST}
drawer_test.cpp
pixelated_image_test.cpp
stb_image_test.cpp)
target_link_libraries(${VALIDATION_TEST} drawer stb_image pixelated_image GTest::gtest_main)
image_test.cpp
stb_image_data_view_test.cpp)
target_link_libraries(${VALIDATION_TEST} drawer stb_image_data_view image GTest::gtest_main)
gtest_discover_tests(${VALIDATION_TEST} WORKING_DIRECTORY ${PROJECT_DIR})
9 changes: 4 additions & 5 deletions inject/homework_5/validation_tests/drawer_test.cpp
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
#include "tui_pixelator/drawer.hpp"
#include "pixelator/drawer.hpp"

#include <gtest/gtest.h>

#include <ftxui/screen/color.hpp>
#include <ftxui/screen/screen.hpp>

namespace {
using pixelator::Drawer;
using pixelator::PixelatedImage;
using pixelator::Image;
using pixelator::Size;
} // namespace

TEST(DrawerValidationTest, Initialization) {
TEST(DrawerValidationTest, InitializeWithFixedSize) {
Drawer drawer{ftxui::Dimension::Fixed(42)};
EXPECT_EQ(drawer.size().rows, 42);
EXPECT_EQ(drawer.size().cols, 84);
Expand All @@ -20,7 +19,7 @@ TEST(DrawerValidationTest, Initialization) {
}

TEST(DrawerValidationTest, SetFromImage) {
PixelatedImage image{Size{1, 2}};
Image image{Size{1, 2}};
image.at(0, 0) = ftxui::Color::Black;
image.at(0, 1) = ftxui::Color::Red;
Drawer drawer{ftxui::Dimension::Fixed(1)};
Expand Down
62 changes: 62 additions & 0 deletions inject/homework_5/validation_tests/image_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#include "pixelator/image.hpp"

#include "ftxui/screen/color.hpp"
#include "gtest/gtest.h"

namespace {
using pixelator::Image;
using pixelator::Size;
} // namespace

TEST(ImageValidationTest, DefaultConstructorCreatesAnEmptyImage) {
const Image image;
EXPECT_TRUE(image.empty());
EXPECT_EQ(image.size().cols, 0);
EXPECT_EQ(image.size().rows, 0);
EXPECT_EQ(image.cols(), 0);
EXPECT_EQ(image.rows(), 0);
}

TEST(ImageValidationTest, ImageCreatedWithSizeHasCorrectSize) {
const Image image{Size{10, 20}};
EXPECT_EQ(image.size().rows, 10);
EXPECT_EQ(image.size().cols, 20);
EXPECT_EQ(image.cols(), 20);
EXPECT_EQ(image.rows(), 10);
}

TEST(ImageValidationTest, ImageCreatedWithSizeHasCorrectDefaultColor) {
const Image image{Size{10, 20}};
EXPECT_FALSE(image.empty());
EXPECT_EQ(image.cols(), 20);
EXPECT_EQ(image.rows(), 10);
for (auto row = 0; row < image.rows(); ++row) {
for (auto col = 0; col < image.cols(); ++col) {
EXPECT_EQ(image.at(row, col), ftxui::Color{});
}
}
}

TEST(ImageTest, AtMethodSetsAndReturnsCorrectColor) {
Image image{Size{10, 20}};
image.at(0, 0) = ftxui::Color::Red;
EXPECT_EQ(image.at(0, 0), ftxui::Color::Red);
}

TEST(ImageTest, CanBeConstructedByCopyingAnExistingImage) {
Image image{Size{10, 20}};
image.at(0, 0) = ftxui::Color::Red;
EXPECT_EQ(image.at(0, 0), ftxui::Color::Red);

const Image image_copy{image};
EXPECT_EQ(image_copy.at(0, 0), ftxui::Color::Red);
}

TEST(ImageTest, CanBeConstructedByMovingAnExistingImage) {
Image image{Size{10, 20}};
image.at(0, 0) = ftxui::Color::Red;
EXPECT_EQ(image.at(0, 0), ftxui::Color::Red);

const Image image_copy{std::move(image)};
EXPECT_EQ(image_copy.at(0, 0), ftxui::Color::Red);
}
50 changes: 50 additions & 0 deletions inject/homework_5/validation_tests/pixelate_image_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include "pixelator/pixelate_image.hpp"

#include "pixelator/image.hpp"
#include "pixelator/size.hpp"
#include "pixelator/stb_image_data_view.hpp"

#include "gtest/gtest.h"

namespace {
using pixelator::Image;
using pixelator::PixelateImage;
using pixelator::Size;
using pixelator::StbImageDataView;

const auto kImagePath{"../../pixelator/test_data/test.png"};
const auto kBlack{ftxui::Color::RGB(0, 0, 0)};
const auto kWhite{ftxui::Color::RGB(255, 255, 255)};
} // namespace

TEST(PixelateImageValidationTest, PassingSameOrBiggerSizeReturnsSameSizedImage) {
const StbImageDataView image{kImagePath};
const auto size = image.size();
const auto pixelated_image = PixelateImage(image, size);
const Size bigger_size{size.rows + 10, size.cols + 20};
const auto pixelated_image_bigger = PixelateImage(image, bigger_size);
ASSERT_FALSE(pixelated_image.empty());
ASSERT_FALSE(pixelated_image_bigger.empty());
ASSERT_EQ(image.rows(), pixelated_image.rows());
ASSERT_EQ(image.cols(), pixelated_image.cols());
ASSERT_EQ(image.rows(), pixelated_image_bigger.rows());
ASSERT_EQ(image.cols(), pixelated_image_bigger.cols());
for (auto row = 0; row < image.rows(); ++row) {
for (auto col = 0; col < image.cols(); ++col) {
EXPECT_EQ(image.at(row, col), pixelated_image.at(row, col));
EXPECT_EQ(image.at(row, col), pixelated_image_bigger.at(row, col));
}
}
}

TEST(PixelateImageValidationTest, PixelateSimpleTest6x4Image) {
const StbImageDataView image{kImagePath};
const auto pixelated_image = PixelateImage(image, Size{3, 2});
EXPECT_EQ(pixelated_image.rows(), 3) << "Pixelated image has wrong size.";
EXPECT_EQ(pixelated_image.cols(), 2) << "Pixelated image has wrong size.";

ASSERT_EQ(pixelated_image.at(0, 0), kBlack)
<< "Pixelated image color is wrong.";
ASSERT_EQ(pixelated_image.at(2, 1), kWhite)
<< "Pixelated image color is wrong.";
}
33 changes: 0 additions & 33 deletions inject/homework_5/validation_tests/pixelate_test.cpp

This file was deleted.

30 changes: 0 additions & 30 deletions inject/homework_5/validation_tests/pixelated_image_test.cpp

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
#include "tui_pixelator/stb_image.hpp"
#include "pixelator/stb_image_data_view.hpp"

#include <gtest/gtest.h>

#include <ftxui/screen/color.hpp>
#include "ftxui/screen/color.hpp"
#include "gtest/gtest.h"

namespace {
using pixelator::StbImage;
const auto kImagePath{"../../tui_pixelator/test_data/test.png"};
using pixelator::StbImageDataView;
const auto kImagePath{"../../pixelator/test_data/test.png"};
const auto kBlack{ftxui::Color::RGB(0, 0, 0)};
const auto kWhite{ftxui::Color::RGB(255, 255, 255)};
} // namespace

TEST(StbImageValidationTest, Initialization) {
const StbImage image{kImagePath};
TEST(StbImageDataViewValidationTest, Initialization) {
const StbImageDataView image{kImagePath};
ASSERT_FALSE(image.empty())
<< "Make sure the image is available at " << kImagePath
<< " with respect to the root of the project.";
Expand All @@ -28,12 +27,12 @@ TEST(StbImageValidationTest, Initialization) {
ASSERT_EQ(image.at(5, 2), kWhite);
}

TEST(StbImageValidationTest, MoveConstructor) {
StbImage image{kImagePath};
TEST(StbImageDataViewValidationTest, MoveConstructor) {
StbImageDataView image{kImagePath};
ASSERT_FALSE(image.empty())
<< "Make sure the image is available at " << kImagePath
<< " with respect to the root of the project.";
const StbImage image_other{std::move(image)};
const StbImageDataView image_other{std::move(image)};
ASSERT_TRUE(image.empty());
ASSERT_FALSE(image_other.empty());
ASSERT_EQ(image_other.rows(), 6);
Expand All @@ -48,10 +47,10 @@ TEST(StbImageValidationTest, MoveConstructor) {
ASSERT_EQ(image_other.at(5, 2), kWhite);
}

TEST(StbImageValidationTest, MoveAssignmentOperator) {
StbImage image_to_move_to{};
TEST(StbImageDataViewValidationTest, MoveAssignmentOperator) {
StbImageDataView image_to_move_to{};
ASSERT_TRUE(image_to_move_to.empty());
StbImage image{kImagePath};
StbImageDataView image{kImagePath};
image_to_move_to = std::move(image);
ASSERT_TRUE(image.empty());
ASSERT_FALSE(image_to_move_to.empty());
Expand Down

0 comments on commit 3f34841

Please sign in to comment.