Skip to content

Commit

Permalink
Adding improved InMemDriver tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rdmello committed Aug 19, 2017
1 parent 0cdf138 commit 3d25e56
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 35 deletions.
16 changes: 7 additions & 9 deletions src/core/driver/InMemDriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,20 @@
/*
* build the InMemImage
*/
PixelSort::InMemDriver::InMemDriver(const Coordinate& maxExtent, const BoxCoordinate& box):
pixels{maxExtent.y, std::vector<RGBColor>{maxExtent.x, RGBColor(0.5, 0.6, 0.7)}},
boxExtent{maxExtent},
box{box, maxExtent.x, maxExtent.y} {}
PixelSort::InMemDriver::InMemDriver(std::vector<std::vector<RGBColor>>& pixels, const BoxCoordinate& box):
pixels{pixels},
box{box, unsigned(pixels[0].size()), unsigned(pixels.size())} {}

void
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());
Expand All @@ -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
Expand All @@ -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
Expand Down
18 changes: 11 additions & 7 deletions src/core/driver/InMemDriver.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@

/*
* InMemDriver is an interface for in-memory image objects
* this class allows us to use vector<vector<RGBColor>> objects with
* BlockPixelSort, AsendorfSort, etc.
*/

#include "../Image.hpp"

#ifndef _PIXELSORT_DRIVER_INMEM_HPP_
Expand All @@ -10,17 +16,15 @@ namespace PixelSort {

class InMemDriver : public Image
{

public:
/* image object */
std::vector<std::vector<RGBColor>> pixels;

/* Actual extent of in-memory image */
Coordinate boxExtent;
std::vector<std::vector<RGBColor>>& pixels;

/* Pointer to ROI being sorted */
/* ROI being sorted */
BoundedCoordinate box;
InMemDriver(const Coordinate& maxExtent, const BoxCoordinate& box);

InMemDriver(std::vector<std::vector<RGBColor>>& pixels, const BoxCoordinate& box);
virtual unsigned int columns (void) const;
virtual unsigned int rows (void) const;

Expand Down
98 changes: 79 additions & 19 deletions test/core/driver/testInMemDriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::vector<PS::RGBColor>> pixels;

TestInMemDriver():
size(15, 25),
extent(0, 0, 10, 20),
pixels(size.y, std::vector<PS::RGBColor>(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 */
Expand All @@ -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);

}

0 comments on commit 3d25e56

Please sign in to comment.