Skip to content

Commit

Permalink
Add test/unit/test_grids.cpp to improve testing coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
rouault committed Jan 14, 2020
1 parent 5562552 commit b8b6f74
Show file tree
Hide file tree
Showing 6 changed files with 209 additions and 15 deletions.
29 changes: 29 additions & 0 deletions scripts/reference_exported_symbols.txt
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,26 @@ osgeo::proj::datum::TemporalDatum::temporalOrigin() const
osgeo::proj::datum::VerticalReferenceFrame::create(osgeo::proj::util::PropertyMap const&, osgeo::proj::util::optional<std::string> const&, osgeo::proj::util::optional<osgeo::proj::datum::RealizationMethod> const&)
osgeo::proj::datum::VerticalReferenceFrame::realizationMethod() const
osgeo::proj::datum::VerticalReferenceFrame::~VerticalReferenceFrame()
osgeo::proj::GenericShiftGrid::~GenericShiftGrid()
osgeo::proj::GenericShiftGrid::GenericShiftGrid(std::string const&, int, int, osgeo::proj::ExtentAndRes const&)
osgeo::proj::GenericShiftGrid::gridAt(double, double) const
osgeo::proj::GenericShiftGridSet::~GenericShiftGridSet()
osgeo::proj::GenericShiftGridSet::GenericShiftGridSet()
osgeo::proj::GenericShiftGridSet::gridAt(double, double) const
osgeo::proj::GenericShiftGridSet::open(projCtx_t*, std::string const&)
osgeo::proj::GenericShiftGridSet::reassign_context(projCtx_t*)
osgeo::proj::GenericShiftGridSet::reopen(projCtx_t*)
osgeo::proj::Grid::~Grid()
osgeo::proj::Grid::Grid(std::string const&, int, int, osgeo::proj::ExtentAndRes const&)
osgeo::proj::HorizontalShiftGrid::gridAt(double, double) const
osgeo::proj::HorizontalShiftGrid::~HorizontalShiftGrid()
osgeo::proj::HorizontalShiftGrid::HorizontalShiftGrid(std::string const&, int, int, osgeo::proj::ExtentAndRes const&)
osgeo::proj::HorizontalShiftGridSet::gridAt(double, double) const
osgeo::proj::HorizontalShiftGridSet::~HorizontalShiftGridSet()
osgeo::proj::HorizontalShiftGridSet::HorizontalShiftGridSet()
osgeo::proj::HorizontalShiftGridSet::open(projCtx_t*, std::string const&)
osgeo::proj::HorizontalShiftGridSet::reassign_context(projCtx_t*)
osgeo::proj::HorizontalShiftGridSet::reopen(projCtx_t*)
osgeo::proj::internal::ci_equal(std::string const&, char const*)
osgeo::proj::internal::ci_equal(std::string const&, std::string const&)
osgeo::proj::internal::ci_find(std::string const&, char const*)
Expand Down Expand Up @@ -696,6 +716,15 @@ osgeo::proj::util::PropertyMap::set(std::string const&, std::string const&)
osgeo::proj::util::PropertyMap::set(std::string const&, std::vector<std::string, std::allocator<std::string> > const&)
osgeo::proj::util::UnsupportedOperationException::~UnsupportedOperationException()
osgeo::proj::util::UnsupportedOperationException::UnsupportedOperationException(osgeo::proj::util::UnsupportedOperationException const&)
osgeo::proj::VerticalShiftGrid::gridAt(double, double) const
osgeo::proj::VerticalShiftGridSet::gridAt(double, double) const
osgeo::proj::VerticalShiftGridSet::open(projCtx_t*, std::string const&)
osgeo::proj::VerticalShiftGridSet::reassign_context(projCtx_t*)
osgeo::proj::VerticalShiftGridSet::reopen(projCtx_t*)
osgeo::proj::VerticalShiftGridSet::~VerticalShiftGridSet()
osgeo::proj::VerticalShiftGridSet::VerticalShiftGridSet()
osgeo::proj::VerticalShiftGrid::~VerticalShiftGrid()
osgeo::proj::VerticalShiftGrid::VerticalShiftGrid(std::string const&, int, int, osgeo::proj::ExtentAndRes const&)
pj_acquire_lock
pj_add_type_crs_if_needed(std::string const&)
pj_apply_gridshift
Expand Down
4 changes: 1 addition & 3 deletions src/grids.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,7 @@ VerticalShiftGrid::VerticalShiftGrid(const std::string &nameIn, int widthIn,

// ---------------------------------------------------------------------------

bool VerticalShiftGrid::isNodata(float /*val*/, double /* multiplier */) const {
return false;
}
VerticalShiftGrid::~VerticalShiftGrid() = default;

// ---------------------------------------------------------------------------

Expand Down
17 changes: 9 additions & 8 deletions src/grids.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ struct ExtentAndRes {

// ---------------------------------------------------------------------------

class Grid {
class PROJ_FOR_TEST Grid {
protected:
std::string m_name;
int m_width;
Expand All @@ -75,17 +75,18 @@ class Grid {

// ---------------------------------------------------------------------------

class VerticalShiftGrid : public Grid {
class PROJ_FOR_TEST VerticalShiftGrid : public Grid {
protected:
std::vector<std::unique_ptr<VerticalShiftGrid>> m_children{};

public:
VerticalShiftGrid(const std::string &nameIn, int widthIn, int heightIn,
const ExtentAndRes &extentIn);
~VerticalShiftGrid() override;

const VerticalShiftGrid *gridAt(double lon, double lat) const;

virtual bool isNodata(float /*val*/, double /* multiplier */) const;
virtual bool isNodata(float /*val*/, double /* multiplier */) const = 0;

// x = 0 is western-most column, y = 0 is southern-most line
virtual bool valueAt(int x, int y, float &out) const = 0;
Expand All @@ -95,7 +96,7 @@ class VerticalShiftGrid : public Grid {

// ---------------------------------------------------------------------------

class VerticalShiftGridSet {
class PROJ_FOR_TEST VerticalShiftGridSet {
protected:
std::string m_name{};
std::string m_format{};
Expand All @@ -122,7 +123,7 @@ class VerticalShiftGridSet {

// ---------------------------------------------------------------------------

class HorizontalShiftGrid : public Grid {
class PROJ_FOR_TEST HorizontalShiftGrid : public Grid {
protected:
std::vector<std::unique_ptr<HorizontalShiftGrid>> m_children{};

Expand All @@ -142,7 +143,7 @@ class HorizontalShiftGrid : public Grid {

// ---------------------------------------------------------------------------

class HorizontalShiftGridSet {
class PROJ_FOR_TEST HorizontalShiftGridSet {
protected:
std::string m_name{};
std::string m_format{};
Expand All @@ -169,7 +170,7 @@ class HorizontalShiftGridSet {

// ---------------------------------------------------------------------------

class GenericShiftGrid : public Grid {
class PROJ_FOR_TEST GenericShiftGrid : public Grid {
protected:
std::vector<std::unique_ptr<GenericShiftGrid>> m_children{};

Expand Down Expand Up @@ -198,7 +199,7 @@ class GenericShiftGrid : public Grid {

// ---------------------------------------------------------------------------

class GenericShiftGridSet {
class PROJ_FOR_TEST GenericShiftGridSet {
protected:
std::string m_name{};
std::string m_format{};
Expand Down
12 changes: 9 additions & 3 deletions test/unit/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,20 @@ add_executable(proj_test_cpp_api
test_operation.cpp
test_datum.cpp
test_factory.cpp
test_c_api.cpp)
test_c_api.cpp
test_grids.cpp)
target_link_libraries(proj_test_cpp_api
GTest::gtest
${PROJ_LIBRARIES}
${SQLITE3_LIBRARY})
add_test(NAME proj_test_cpp_api COMMAND proj_test_cpp_api)
set_property(TEST proj_test_cpp_api
PROPERTY ENVIRONMENT "PROJ_IGNORE_USER_WRITABLE_DIRECTORY=YES;PROJ_LIB=${PROJECT_BINARY_DIR}/data")
if(MSVC)
set_property(TEST proj_test_cpp_api
PROPERTY ENVIRONMENT "PROJ_IGNORE_USER_WRITABLE_DIRECTORY=YES;PROJ_LIB=${PROJECT_BINARY_DIR}/data\\;${PROJECT_SOURCE_DIR}/data")
else()
set_property(TEST proj_test_cpp_api
PROPERTY ENVIRONMENT "PROJ_IGNORE_USER_WRITABLE_DIRECTORY=YES;PROJ_LIB=${PROJECT_BINARY_DIR}/data:${PROJECT_SOURCE_DIR}/data")
endif()


add_executable(gie_self_tests
Expand Down
2 changes: 1 addition & 1 deletion test/unit/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ proj_context_test_LDADD = ../../src/libproj.la @GTEST_LIBS@
proj_context_test-check: proj_context_test
PROJ_IGNORE_USER_WRITABLE_DIRECTORY=YES ./proj_context_test

test_cpp_api_SOURCES = test_util.cpp test_common.cpp test_crs.cpp test_metadata.cpp test_io.cpp test_operation.cpp test_datum.cpp test_factory.cpp test_c_api.cpp main.cpp
test_cpp_api_SOURCES = test_util.cpp test_common.cpp test_crs.cpp test_metadata.cpp test_io.cpp test_operation.cpp test_datum.cpp test_factory.cpp test_c_api.cpp test_grids.cpp main.cpp
test_cpp_api_LDADD = ../../src/libproj.la @GTEST_LIBS@ @SQLITE3_LIBS@

test_cpp_api-check: test_cpp_api
Expand Down
160 changes: 160 additions & 0 deletions test/unit/test_grids.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
/******************************************************************************
*
* Project: PROJ
* Purpose: Test grids.hpp
* Author: Even Rouault <even dot rouault at spatialys dot com>
*
******************************************************************************
* Copyright (c) 2020, Even Rouault <even dot rouault at spatialys dot com>
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
****************************************************************************/

#include "gtest_include.h"

#include "grids.hpp"

#include "proj_internal.h" // M_PI

namespace {

// ---------------------------------------------------------------------------

class GridTest : public ::testing::Test {

static void DummyLogFunction(void *, int, const char *) {}

protected:
void SetUp() override {
m_ctxt = proj_context_create();
proj_log_func(m_ctxt, nullptr, DummyLogFunction);
m_ctxt2 = proj_context_create();
proj_log_func(m_ctxt2, nullptr, DummyLogFunction);
}

void TearDown() override {
proj_context_destroy(m_ctxt);
proj_context_destroy(m_ctxt2);
}

PJ_CONTEXT *m_ctxt = nullptr;
PJ_CONTEXT *m_ctxt2 = nullptr;
};

// ---------------------------------------------------------------------------

TEST_F(GridTest, VerticalShiftGridSet_null) {
auto gridSet = NS_PROJ::VerticalShiftGridSet::open(m_ctxt, "null");
ASSERT_NE(gridSet, nullptr);
auto grid = gridSet->gridAt(0.0, 0.0);
ASSERT_NE(grid, nullptr);
EXPECT_EQ(grid->width(), 3);
EXPECT_EQ(grid->height(), 3);
EXPECT_EQ(grid->extentAndRes().westLon, -M_PI);
EXPECT_TRUE(grid->isNullGrid());
EXPECT_FALSE(grid->hasChanged());
float out = -1.0f;
EXPECT_TRUE(grid->valueAt(0, 0, out));
EXPECT_EQ(out, 0.0f);
EXPECT_FALSE(grid->isNodata(0.0f, 0.0));
gridSet->reassign_context(m_ctxt2);
gridSet->reopen(m_ctxt2);
}

// ---------------------------------------------------------------------------

TEST_F(GridTest, VerticalShiftGridSet_regular) {
ASSERT_EQ(NS_PROJ::VerticalShiftGridSet::open(m_ctxt, "foobar"), nullptr);
auto gridSet =
NS_PROJ::VerticalShiftGridSet::open(m_ctxt, "tests/test_nodata.gtx");
ASSERT_NE(gridSet, nullptr);
ASSERT_EQ(gridSet->gridAt(-100, -100), nullptr);
auto grid = gridSet->gridAt(4.15 / 180 * M_PI, 52.15 / 180 * M_PI);
ASSERT_NE(grid, nullptr);
EXPECT_TRUE(grid->isNodata(-88.8888f, 1.0));
gridSet->reassign_context(m_ctxt2);
gridSet->reopen(m_ctxt2);
}

// ---------------------------------------------------------------------------

TEST_F(GridTest, HorizontalShiftGridSet_null) {
auto gridSet = NS_PROJ::HorizontalShiftGridSet::open(m_ctxt, "null");
ASSERT_NE(gridSet, nullptr);
auto grid = gridSet->gridAt(0.0, 0.0);
ASSERT_NE(grid, nullptr);
EXPECT_EQ(grid->width(), 3);
EXPECT_EQ(grid->height(), 3);
EXPECT_EQ(grid->extentAndRes().westLon, -M_PI);
EXPECT_TRUE(grid->isNullGrid());
EXPECT_FALSE(grid->hasChanged());
float out1 = -1.0f;
float out2 = -1.0f;
EXPECT_TRUE(grid->valueAt(0, 0, false, out1, out2));
EXPECT_EQ(out1, 0.0f);
EXPECT_EQ(out2, 0.0f);
gridSet->reassign_context(m_ctxt2);
gridSet->reopen(m_ctxt2);
}

// ---------------------------------------------------------------------------

TEST_F(GridTest, GenericShiftGridSet_null) {
auto gridSet = NS_PROJ::GenericShiftGridSet::open(m_ctxt, "null");
ASSERT_NE(gridSet, nullptr);
auto grid = gridSet->gridAt(0.0, 0.0);
ASSERT_NE(grid, nullptr);
EXPECT_EQ(grid->width(), 3);
EXPECT_EQ(grid->height(), 3);
EXPECT_EQ(grid->extentAndRes().westLon, -M_PI);
EXPECT_TRUE(grid->isNullGrid());
EXPECT_FALSE(grid->hasChanged());
float out = -1.0f;
EXPECT_TRUE(grid->valueAt(0, 0, 0, out));
EXPECT_EQ(out, 0.0f);
EXPECT_EQ(grid->unit(0), "");
EXPECT_EQ(grid->description(0), "");
EXPECT_EQ(grid->metadataItem("foo"), "");
EXPECT_EQ(grid->samplesPerPixel(), 0);
gridSet->reassign_context(m_ctxt2);
gridSet->reopen(m_ctxt2);
}

// ---------------------------------------------------------------------------

TEST_F(GridTest, GenericShiftGridSet_regular) {
ASSERT_EQ(NS_PROJ::GenericShiftGridSet::open(m_ctxt, "foobar"), nullptr);
auto gridSet = NS_PROJ::GenericShiftGridSet::open(
m_ctxt, "tests/nkgrf03vel_realigned_extract.tif");
ASSERT_NE(gridSet, nullptr);
ASSERT_EQ(gridSet->gridAt(-100, -100), nullptr);
auto grid = gridSet->gridAt(21.3333333 / 180 * M_PI, 63.0 / 180 * M_PI);
ASSERT_NE(grid, nullptr);
EXPECT_EQ(grid->width(), 5);
EXPECT_EQ(grid->height(), 5);
EXPECT_EQ(grid->extentAndRes().westLon, 21.0 / 180 * M_PI);
EXPECT_FALSE(grid->isNullGrid());
EXPECT_FALSE(grid->hasChanged());
float out = -1.0f;
EXPECT_FALSE(grid->valueAt(0, 0, grid->samplesPerPixel(), out));
gridSet->reassign_context(m_ctxt2);
gridSet->reopen(m_ctxt2);
}

} // namespace

0 comments on commit b8b6f74

Please sign in to comment.