Debian Codegraph @

unit_test/stuff/filesys_tools_tests.cpp

raw symbols SCIP
1 // g2o - General Graph Optimization
2 // Copyright (C) 2011 R. Kuemmerle, G. Grisetti, W. Burgard
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // * Redistributions of source code must retain the above copyright notice,
10 // this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above copyright
12 // notice, this list of conditions and the following disclaimer in the
13 // documentation and/or other materials provided with the distribution.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
16 // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17 // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
18 // PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 // HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
21 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
27 #include <cstdlib>
28
29 #include "g2o/config.h"
30 #include "g2o/stuff/filesys_tools.h"
31 #include "gmock/gmock.h"
32
33 namespace {
34 #ifdef WINDOWS
35 static const std::string pathSep = "\\";
36 #else
37 static const std::string pathSep = "/";
38 #endif
39 } // namespace
40
41 TEST(Stuff, GetFileExtension) {
42 ASSERT_EQ("txt", g2o::getFileExtension("test.txt"));
43 ASSERT_EQ("txt", g2o::getFileExtension("/home/g2o/test.txt"));
44 ASSERT_EQ("", g2o::getFileExtension("/home/g2o/test"));
45 ASSERT_EQ("", g2o::getFileExtension(""));
46 }
47
48 TEST(Stuff, GetPureFilename) {
49 ASSERT_EQ("test", g2o::getPureFilename("test.txt"));
50 ASSERT_EQ("test", g2o::getPureFilename("test"));
51 ASSERT_EQ("/home/g2o/test", g2o::getPureFilename("/home/g2o/test.txt"));
52 ASSERT_EQ("", g2o::getPureFilename(""));
53 }
54
55 TEST(Stuff, ChangeFileExtension) {
56 ASSERT_EQ("test.dat", g2o::changeFileExtension("test.txt", "dat", false));
57 ASSERT_EQ("test.dat", g2o::changeFileExtension("test.txt", ".dat", true));
58 ASSERT_EQ("test", g2o::changeFileExtension("test", "dat", false));
59 ASSERT_EQ("test", g2o::changeFileExtension("test", ".dat", true));
60 }
61
62 TEST(Stuff, GetBasename) {
63 ASSERT_EQ("test.txt", g2o::getBasename("test.txt"));
64 ASSERT_EQ("test", g2o::getBasename("test"));
65 #ifdef WINDOWS
66 ASSERT_EQ("test.txt", g2o::getBasename("C:\\users\\g2o\\test.txt"));
67 #else
68 ASSERT_EQ("test.txt", g2o::getBasename("/home/g2o/test.txt"));
69 #endif
70 ASSERT_EQ("", g2o::getBasename(""));
71 }
72
73 TEST(Stuff, GetDirname) {
74 ASSERT_EQ("", g2o::getDirname("test.txt"));
75 ASSERT_EQ("", g2o::getDirname("test"));
76 #ifdef WINDOWS
77 ASSERT_EQ("C:\\users\\g2o", g2o::getDirname("C:\\users\\g2o\\test.txt"));
78 #else
79 ASSERT_EQ("/home/g2o", g2o::getDirname("/home/g2o/test.txt"));
80 #endif
81 ASSERT_EQ("", g2o::getDirname(""));
82 }
83
84 TEST(Stuff, FileExists) {
85 ASSERT_FALSE(g2o::fileExists("test12345.txt"));
86 ASSERT_FALSE(g2o::fileExists("test12345"));
87
88 ASSERT_TRUE(g2o::fileExists(G2O_SRC_DIR));
89 ASSERT_TRUE(g2o::fileExists(
90 (std::string(G2O_SRC_DIR) + pathSep + "CMakeLists.txt").c_str()));
91 }
92
93 TEST(Stuff, GetFilesByPattern) {
94 using namespace testing;
95 std::string pattern =
96 std::string(G2O_SRC_DIR) + pathSep + "doc" + pathSep + "license*.txt";
97 std::vector<std::string> licenseFiles =
98 g2o::getFilesByPattern(pattern.c_str());
99 ASSERT_THAT(licenseFiles, SizeIs(3));
100 ASSERT_THAT(licenseFiles, Each(ContainsRegex("license.*\\.txt$")));
101 }