From 3d25e562be032c8cd84dfc6b3e02b875c58669d5 Mon Sep 17 00:00:00 2001 From: Rylan Dmello Date: Fri, 18 Aug 2017 21:23:39 -0400 Subject: [PATCH] Adding improved InMemDriver tests --- src/core/driver/InMemDriver.cpp | 16 ++--- src/core/driver/InMemDriver.hpp | 18 +++-- test/core/driver/testInMemDriver.cpp | 98 ++++++++++++++++++++++------ 3 files changed, 97 insertions(+), 35 deletions(-) diff --git a/src/core/driver/InMemDriver.cpp b/src/core/driver/InMemDriver.cpp index 06bfbbb..6805f99 100644 --- a/src/core/driver/InMemDriver.cpp +++ b/src/core/driver/InMemDriver.cpp @@ -6,10 +6,9 @@ /* * build the InMemImage */ -PixelSort::InMemDriver::InMemDriver(const Coordinate& maxExtent, const BoxCoordinate& box): -pixels{maxExtent.y, std::vector{maxExtent.x, RGBColor(0.5, 0.6, 0.7)}}, -boxExtent{maxExtent}, -box{box, maxExtent.x, maxExtent.y} {} +PixelSort::InMemDriver::InMemDriver(std::vector>& pixels, const BoxCoordinate& box): +pixels{pixels}, +box{box, unsigned(pixels[0].size()), unsigned(pixels.size())} {} void PixelSort::InMemDriver::readPrep (const BoxCoordinate& box) @@ -17,11 +16,10 @@ PixelSort::InMemDriver::readPrep (const BoxCoordinate& box) /* do nothing */ } - void PixelSort::InMemDriver::readRGBFromXY (Pixel& p) const { - RGBColor rgbColor = pixels[p.y][p.x]; + RGBColor rgbColor = pixels[p.y + box.y][p.x + box.x]; p.red(rgbColor.red()); p.green(rgbColor.green()); @@ -44,7 +42,7 @@ PixelSort::InMemDriver::writePrep (const BoxCoordinate& box) void PixelSort::InMemDriver::writeRGBFromXY (const Pixel& p) { - pixels[p.y][p.x] = RGBColor(p.red(), p.green(), p.blue()); + pixels[p.y + box.y][p.x + box.x] = RGBColor(p.red(), p.green(), p.blue()); } void @@ -56,13 +54,13 @@ PixelSort::InMemDriver::writeFinish (void) unsigned int PixelSort::InMemDriver::columns (void) const { - return pixels[0].size(); + return box.width; } unsigned int PixelSort::InMemDriver::rows (void) const { - return pixels.size(); + return box.height; } void diff --git a/src/core/driver/InMemDriver.hpp b/src/core/driver/InMemDriver.hpp index 68d7aa6..dcd537e 100644 --- a/src/core/driver/InMemDriver.hpp +++ b/src/core/driver/InMemDriver.hpp @@ -1,4 +1,10 @@ +/* + * InMemDriver is an interface for in-memory image objects + * this class allows us to use vector> objects with + * BlockPixelSort, AsendorfSort, etc. + */ + #include "../Image.hpp" #ifndef _PIXELSORT_DRIVER_INMEM_HPP_ @@ -10,17 +16,15 @@ namespace PixelSort { class InMemDriver : public Image { + public: /* image object */ - std::vector> pixels; - - /* Actual extent of in-memory image */ - Coordinate boxExtent; + std::vector>& pixels; - /* Pointer to ROI being sorted */ + /* ROI being sorted */ BoundedCoordinate box; - - InMemDriver(const Coordinate& maxExtent, const BoxCoordinate& box); + + InMemDriver(std::vector>& pixels, const BoxCoordinate& box); virtual unsigned int columns (void) const; virtual unsigned int rows (void) const; diff --git a/test/core/driver/testInMemDriver.cpp b/test/core/driver/testInMemDriver.cpp index 0ee81db..48bbc09 100644 --- a/test/core/driver/testInMemDriver.cpp +++ b/test/core/driver/testInMemDriver.cpp @@ -6,41 +6,100 @@ namespace PS = PixelSort; -/* Size of test image being created */ -PS::Coordinate extent{10, 20}; +class TestInMemDriver: public ::testing::Test +{ +public: + const PS::Coordinate size; + const PS::BoxCoordinate extent; + std::vector> pixels; + + TestInMemDriver(): + size(15, 25), + extent(0, 0, 10, 20), + pixels(size.y, std::vector(size.x, + PS::RGBColor(0.5, 0.6, 0.7))) {}; +}; -TEST(testInMemDriver, Constructor) +TEST_F(TestInMemDriver, Constructor) { - PS::InMemDriver image(extent, PS::BoxCoordinate(0, 0, 15, 25)); - image.print(); + // construct InMemDriver object + PS::InMemDriver image(pixels, extent); - /* Check that red component is correctly set */ - EXPECT_EQ(0.5, image.pixels[0][0].red()); + /* Check that the rgb components are correctly set */ + for (auto& row: pixels) { + for (auto& element: row) { + EXPECT_EQ(0.5, element.red()); + EXPECT_EQ(0.6, element.green()); + EXPECT_EQ(0.7, element.blue()); + } + } + +} +TEST_F(TestInMemDriver, Dimensions) +{ + // construct InMemDriver object + PS::InMemDriver image(pixels, extent); + /* Verify that image size is correct */ - EXPECT_EQ(extent.y, image.pixels.size()); - EXPECT_EQ(extent.x, image.pixels[0].size()); + EXPECT_EQ(size.y, image.pixels.size()); + EXPECT_EQ(size.x, image.pixels[0].size()); /* Verify that columns and rows are being reported correctly */ - EXPECT_EQ(extent.y, image.rows()); - EXPECT_EQ(extent.x, image.columns()); + EXPECT_EQ(extent.height, image.rows()); + EXPECT_EQ(extent.width, image.columns()); /* Verify that coordinates are correctly bounded when they exceed * the image dimensions */ - EXPECT_EQ(extent.y, image.box.height); - EXPECT_EQ(extent.x, image.box.width); + EXPECT_EQ(extent.height, image.box.height); + EXPECT_EQ(extent.width, image.box.width); +} + +TEST_F(TestInMemDriver, ReadRGBFromXY) +{ + // construct InMemDriver object + PS::InMemDriver image(pixels, extent); + + /* Check that the rgb components are correctly being read */ + for (unsigned i = 0; i < pixels.size(); ++i) { + for (unsigned j = 0; j < pixels[0].size(); ++j) { + PS::Pixel color(PS::Coordinate(j, i), PS::RGBColor(0, 0, 0)); + image.readRGBFromXY(color); + EXPECT_EQ(pixels[i][j].red(), color.red()); + EXPECT_EQ(pixels[i][j].green(), color.green()); + EXPECT_EQ(pixels[i][j].blue(), color.blue()); + } + } } -TEST(testInMemDriver, BlockSort) +TEST_F(TestInMemDriver, WriteRGBFromXY) { - /* construct image driver object */ - PS::InMemDriver image(extent, PS::BoxCoordinate(0, 0, 10, 20)); - + // construct InMemDriver object + PS::InMemDriver image(pixels, extent); + + /* Check that the rgb components are correctly being read */ + for (unsigned i = 0; i < pixels.size(); ++i) { + for (unsigned j = 0; j < pixels[0].size(); ++j) { + PS::Pixel color(PS::Coordinate(j, i), PS::RGBColor(0, 0, 0)); + image.writeRGBFromXY(color); + EXPECT_EQ(pixels[i][j].red(), color.red()); + EXPECT_EQ(pixels[i][j].green(), color.green()); + EXPECT_EQ(pixels[i][j].blue(), color.blue()); + } + } +} + + +TEST_F(TestInMemDriver, BlockSort) +{ + // construct InMemDriver object + PS::InMemDriver image(pixels, extent); + double i = 1; /* Populate first row of object */ for (PS::RGBColor& c: image.pixels[0]) { c.red(i); - i -= (1.0/extent.x); + i -= (1.0/extent.width); } /* verify that last red color on first row has been set correctly */ @@ -49,6 +108,7 @@ TEST(testInMemDriver, BlockSort) * so we cannot directly use ASSERT_DOUBLE_EQ. Using EXPECT_NEAR * with an arbitrary abs_error for now. */ - EXPECT_NEAR(1.0/extent.x, image.pixels[0][extent.x-1].red(), 0.00001); + EXPECT_NEAR(1.0/extent.width, image.pixels[0][extent.width-1].red(), 0.00001); + }