Skip to content

Commit

Permalink
Add more test cases
Browse files Browse the repository at this point in the history
* add tests for filesys_tools
  • Loading branch information
RainerKuemmerle committed Jan 7, 2021
1 parent f082074 commit c51eebe
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 4 deletions.
7 changes: 3 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -387,10 +387,6 @@ set(G2O_SHARED_LIBS ${BUILD_SHARED_LIBS})
set(G2O_LGPL_SHARED_LIBS ${BUILD_LGPL_SHARED_LIBS})
set(G2O_CXX_COMPILER "${CMAKE_CXX_COMPILER_ID} ${CMAKE_CXX_COMPILER}")

configure_file(config.h.in "${PROJECT_BINARY_DIR}/g2o/config.h")
install(FILES ${PROJECT_BINARY_DIR}/g2o/config.h DESTINATION ${INCLUDES_DESTINATION}/g2o)


# Generate cmake configuration scripts
set(G2O_GENERATED_DIR "${CMAKE_CURRENT_BINARY_DIR}/generated")
set(G2O_VERSION_CONFIG "${G2O_GENERATED_DIR}/${PROJECT_NAME}ConfigVersion.cmake")
Expand All @@ -406,6 +402,9 @@ WRITE_BASIC_PACKAGE_VERSION_FILE(
"${G2O_VERSION_CONFIG}" VERSION ${G2O_VERSION} COMPATIBILITY SameMajorVersion
)

configure_file(config.h.in "${PROJECT_BINARY_DIR}/g2o/config.h")
install(FILES ${PROJECT_BINARY_DIR}/g2o/config.h DESTINATION ${INCLUDES_DESTINATION}/g2o)

configure_file("${g2o_SOURCE_DIR}/cmake_modules/Config.cmake.in" "${G2O_PROJECT_CONFIG}" @ONLY)
configure_file("${g2o_SOURCE_DIR}/script/codecov.sh.in" "${CMAKE_CURRENT_BINARY_DIR}/codecov.sh" @ONLY)

Expand Down
1 change: 1 addition & 0 deletions config.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#endif

#cmakedefine G2O_CXX_COMPILER "@G2O_CXX_COMPILER@"
#cmakedefine G2O_SRC_DIR "@G2O_SRC_DIR@"

#ifdef __cplusplus
#include <g2o/core/eigen_types.h>
Expand Down
3 changes: 3 additions & 0 deletions unit_test/general/robust_kernel_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ class RobustKernelTests : public Test {
Kernel kernel_;
std::vector<double> error_values_;

/**
* Estimate the first order derivative numerically
*/
number_t estimateDerivative(number_t x) {
constexpr number_t delta = g2o::cst(1e-9);

Expand Down
1 change: 1 addition & 0 deletions unit_test/stuff/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
add_executable(unittest_stuff
command_args_tests.cpp
filesys_tools_tests.cpp
string_tools_tests.cpp
tuple_tools_tests.cpp
)
Expand Down
98 changes: 98 additions & 0 deletions unit_test/stuff/filesys_tools_tests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// g2o - General Graph Optimization
// Copyright (C) 2011 R. Kuemmerle, G. Grisetti, W. Burgard
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
// IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
// PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
// TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#include <cstdlib>

#include "g2o/config.h"
#include "g2o/stuff/filesys_tools.h"
#include "gmock/gmock.h"

namespace {
#ifdef WINDOWS
static const std::string pathSep = "\\";
#else
static const std::string pathSep = "/";
#endif
} // namespace

TEST(Stuff, GetFileExtension) {
ASSERT_EQ("txt", g2o::getFileExtension("test.txt"));
ASSERT_EQ("txt", g2o::getFileExtension("/home/g2o/test.txt"));
ASSERT_EQ("", g2o::getFileExtension("/home/g2o/test"));
ASSERT_EQ("", g2o::getFileExtension(""));
}

TEST(Stuff, GetPureFilename) {
ASSERT_EQ("test", g2o::getPureFilename("test.txt"));
ASSERT_EQ("test", g2o::getPureFilename("test"));
ASSERT_EQ("/home/g2o/test", g2o::getPureFilename("/home/g2o/test.txt"));
ASSERT_EQ("", g2o::getPureFilename(""));
}

TEST(Stuff, ChangeFileExtension) {
ASSERT_EQ("test.dat", g2o::changeFileExtension("test.txt", "dat", false));
ASSERT_EQ("test.dat", g2o::changeFileExtension("test.txt", ".dat", true));
ASSERT_EQ("test", g2o::changeFileExtension("test", "dat", false));
ASSERT_EQ("test", g2o::changeFileExtension("test", ".dat", true));
}

TEST(Stuff, GetBasename) {
ASSERT_EQ("test.txt", g2o::getBasename("test.txt"));
ASSERT_EQ("test", g2o::getBasename("test"));
#ifdef WINDOWS
ASSERT_EQ("test.txt", g2o::getBasename("C:\\users\\g2o\\test.txt"));
#else
ASSERT_EQ("test.txt", g2o::getBasename("/home/g2o/test.txt"));
#endif
ASSERT_EQ("", g2o::getBasename(""));
}

TEST(Stuff, GetDirname) {
ASSERT_EQ("", g2o::getDirname("test.txt"));
ASSERT_EQ("", g2o::getDirname("test"));
#ifdef WINDOWS
ASSERT_EQ("C:\\users\\g2o", g2o::getDirname("C:\\users\\g2o\\test.txt"));
#else
ASSERT_EQ("/home/g2o", g2o::getDirname("/home/g2o/test.txt"));
#endif
ASSERT_EQ("", g2o::getDirname(""));
}

TEST(Stuff, FileExists) {
ASSERT_FALSE(g2o::fileExists("test12345.txt"));
ASSERT_FALSE(g2o::fileExists("test12345"));

ASSERT_TRUE(g2o::fileExists(G2O_SRC_DIR));
ASSERT_TRUE(g2o::fileExists((std::string(G2O_SRC_DIR) + pathSep + "CMakeLists.txt").c_str()));
}

TEST(Stuff, GetFilesByPattern) {
using namespace testing;
std::string pattern = std::string(G2O_SRC_DIR) + pathSep + "doc" + pathSep + "license*.txt";
std::vector<std::string> licenseFiles = g2o::getFilesByPattern(pattern.c_str());
ASSERT_THAT(licenseFiles, SizeIs(3));
ASSERT_THAT(licenseFiles, Each(ContainsRegex("license.*\\.txt$")));
}
16 changes: 16 additions & 0 deletions unit_test/stuff/string_tools_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#include <cstdlib>

#include "g2o/stuff/string_tools.h"
#include "gtest/gtest.h"

Expand Down Expand Up @@ -94,3 +96,17 @@ TEST(Stuff, StrEndsWith) {
ASSERT_TRUE(g2o::strEndsWith("Hello World!", "Hello World!"));
ASSERT_FALSE(g2o::strEndsWith("Hello World!", "!!Hello World!"));
}

#if defined (UNIX) && !defined(ANDROID)
TEST(Stuff, StrExpand) {
char* envVar = getenv("HOME");
if (envVar == nullptr) {
std::cerr << "HOME not defined" << std::endl;
SUCCEED();
return;
}
std::string expanded = g2o::strExpandFilename("$HOME/filename.txt");
std::string expected = g2o::formatString("%s/filename.txt", envVar);
EXPECT_EQ(expanded, expected);
}
#endif

0 comments on commit c51eebe

Please sign in to comment.