From 1d0423c78a40c560d671c4bc8c95f05759b687f1 Mon Sep 17 00:00:00 2001 From: Thomas Beutlich Date: Wed, 29 May 2024 19:01:53 +0200 Subject: [PATCH] Add a few unit tests for SHP API --- Makefile.am | 1 + tests/CMakeLists.txt | 2 +- tests/shp_test.cc | 92 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 tests/shp_test.cc diff --git a/Makefile.am b/Makefile.am index 65e00f6..a65e4a4 100644 --- a/Makefile.am +++ b/Makefile.am @@ -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 \ diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 85a9724..e7d5f48 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -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( diff --git a/tests/shp_test.cc b/tests/shp_test.cc new file mode 100644 index 0000000..505343d --- /dev/null +++ b/tests/shp_test.cc @@ -0,0 +1,92 @@ +#include +#include +#include +#include +#include + +#include +#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, OpenExistingSHX) +{ + const auto filename = kTestData / "anno.shx"; + const auto handle = SHPOpen(filename.string().c_str(), "rb"); + ASSERT_NE(nullptr, handle); + SHPClose(handle); +} + +TEST(SHPOpenTest, OpenExistingWithRestoreSHX) +{ + const auto filename = kTestData / "anno.shx"; + fs::remove(filename); + const auto sHooks = std::make_unique(); + SASetupDefaultHooks(sHooks.get()); + auto handle = + SHPOpenLLEx(filename.string().c_str(), "rb", sHooks.get(), false); + ASSERT_EQ(nullptr, handle); + handle = SHPOpenLLEx(filename.string().c_str(), "rb", sHooks.get(), true); + ASSERT_NE(nullptr, handle); + SHPClose(handle); + EXPECT_TRUE(fs::exists(filename)); +} + +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(); +}