Skip to content

Commit

Permalink
Add a few unit tests for SHP API
Browse files Browse the repository at this point in the history
  • Loading branch information
thbeu committed May 29, 2024
1 parent 011083c commit 4c3a219
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 1 deletion.
1 change: 1 addition & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ EXTRA_DIST = makefile.vc CMakeLists.txt autogen.sh \
tests/CMakeLists.txt \
tests/dbf_test.cc \
tests/sbn_test.cc \
tests/shp_test.cc \
tests/test1.sh tests/test2.sh tests/test3.sh \
tests/expect1.out tests/expect2.out tests/expect3.out \
tests/shape_eg_data/3dpoints.dbf \
Expand Down
2 changes: 1 addition & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ set(INSTALL_GTEST OFF CACHE BOOL "" FORCE)

FetchContent_MakeAvailable(googletest)

foreach(executable dbf_test sbn_test)
foreach(executable dbf_test sbn_test shp_test)
add_executable(${executable} ${PROJECT_SOURCE_DIR}/${executable}.cc)
target_link_libraries(${executable} PRIVATE ${PACKAGE} gtest)
add_test(
Expand Down
89 changes: 89 additions & 0 deletions tests/shp_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#include <cstring>
#include <filesystem>
#include <memory>
#include <string>
#include <string_view>

#include <gtest/gtest.h>
#include "shapefil.h"

namespace fs = std::filesystem;

namespace
{

static const auto kTestData = fs::path{"shape_eg_data"};

TEST(SHPOpenTest, OpenDoesNotExist_rb)
{
const auto handle = SHPOpen("/does/not/exist.shp", "rb");
ASSERT_EQ(nullptr, handle);
}

TEST(SHPOpenTest, OpenDoesNotExist_rb_plus)
{
const auto handle = SHPOpen("/does/not/exist2.shp", "rb+");
ASSERT_EQ(nullptr, handle);
}

TEST(SHPOpenTest, OpenUnexpectedFormat)
{
const auto filename = kTestData / "README.md";
const auto handle = SHPOpen(filename.string().c_str(), "rb");
ASSERT_EQ(nullptr, handle);
}

TEST(SHPOpenTest, OpenExisting)
{
const auto filename = kTestData / "anno.shp";
const auto handle = SHPOpen(filename.string().c_str(), "rb");
ASSERT_NE(nullptr, handle);
SHPClose(handle);
}

TEST(SHPOpenTest, OpenExistingWithRestoreSHX)
{
const auto filename_shx = kTestData / "anno.shx";
fs::remove(filename_shx);

const auto filename_shp = kTestData / "anno.shp";
const auto sHooks = std::make_unique<SAHooks>();
SASetupDefaultHooks(sHooks.get());
auto handle =
SHPOpenLLEx(filename_shp.string().c_str(), "rb", sHooks.get(), false);
ASSERT_EQ(nullptr, handle);
handle =
SHPOpenLLEx(filename_shp.string().c_str(), "rb", sHooks.get(), true);
ASSERT_NE(nullptr, handle);
SHPClose(handle);

const auto exists_shx = fs::exists(filename_shx);
EXPECT_TRUE(exists_shx);
}

TEST(SHPCreateTest, CreateDoesNotExist)
{
const auto handle = SHPCreate("/does/not/exist", 42);
EXPECT_EQ(nullptr, handle);
}

TEST(SHPCreateTest, CreateAndClose)
{
const auto filename = kTestData / "empty.shp";
const auto handle = SHPCreate(filename.string().c_str(), 1234);
SHPClose(handle);
for (const auto &_filename : {filename, kTestData / "empty.shx"})
{
const auto size = fs::file_size(_filename);
EXPECT_EQ(100, size);
fs::remove(_filename);
}
}

} // namespace

int main(int argc, char **argv)
{
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

0 comments on commit 4c3a219

Please sign in to comment.