summaryrefslogtreecommitdiff
path: root/test/utils.cpp
diff options
context:
space:
mode:
authorKieran Bingham <kieran.bingham@ideasonboard.com>2019-10-22 16:01:02 +0100
committerKieran Bingham <kieran.bingham@ideasonboard.com>2020-02-24 09:36:04 +0000
commita15e8d92ea2ebc20abd54a7ecaa406f8e0de0c66 (patch)
treeb6976d0de881cab39ea673061eaed90bc7e64c36 /test/utils.cpp
parenta8be6e94e79f602d543a15afd44ef60e378b138f (diff)
libcamera: utils: Add a C++ dirname implementation
Provide a std::string based implementation which conforms to the behaviour of the dirname() fucntion defined by POSIX. Tests are added to cover expected corner cases of the implementation. Signed-off-by: Kieran Bingham <kieran.bingham@ideasonboard.com> Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Diffstat (limited to 'test/utils.cpp')
-rw-r--r--test/utils.cpp54
1 files changed, 54 insertions, 0 deletions
diff --git a/test/utils.cpp b/test/utils.cpp
index db1fbdde..58816f15 100644
--- a/test/utils.cpp
+++ b/test/utils.cpp
@@ -19,6 +19,56 @@ using namespace libcamera;
class UtilsTest : public Test
{
protected:
+ int testDirname()
+ {
+ static const std::vector<std::string> paths = {
+ "",
+ "///",
+ "/bin",
+ "/usr/bin",
+ "//etc////",
+ "//tmp//d//",
+ "current_file",
+ "./current_file",
+ "./current_dir/",
+ "current_dir/",
+ };
+
+ static const std::vector<std::string> expected = {
+ ".",
+ "/",
+ "/",
+ "/usr",
+ "/",
+ "//tmp",
+ ".",
+ ".",
+ ".",
+ ".",
+ };
+
+ std::vector<std::string> results;
+
+ for (const auto &path : paths)
+ results.push_back(utils::dirname(path));
+
+ if (results != expected) {
+ cerr << "utils::dirname() tests failed" << endl;
+
+ cerr << "expected: " << endl;
+ for (const auto &path : expected)
+ cerr << "\t" << path << endl;
+
+ cerr << "results: " << endl;
+ for (const auto &path : results)
+ cerr << "\t" << path << endl;
+
+ return TestFail;
+ }
+
+ return TestPass;
+ }
+
int run()
{
/* utils::hex() test. */
@@ -71,6 +121,10 @@ protected:
return TestFail;
}
+ /* utils::dirname() tests. */
+ if (TestPass != testDirname())
+ return TestFail;
+
return TestPass;
}
};