From 0b1623d07db963ecf3789aba7163321812cba88e Mon Sep 17 00:00:00 2001 From: Jay Berkenbilt Date: Thu, 18 Feb 2021 07:55:39 -0500 Subject: Add QUtil::path_basename --- libtests/qtest/qutil/qutil.out | 7 +++++++ libtests/qutil.cc | 25 +++++++++++++++++++++++++ 2 files changed, 32 insertions(+) (limited to 'libtests') diff --git a/libtests/qtest/qutil/qutil.out b/libtests/qtest/qutil/qutil.out index 7dded2e3..90f1fd16 100644 --- a/libtests/qtest/qutil/qutil.out +++ b/libtests/qtest/qutil/qutil.out @@ -97,6 +97,13 @@ file1: -qutil.out-, file2: -other-file-; same: 0: PASS file1: -qutil.out-, file2: --; same: 0: PASS file1: -qutil.out-, file2: -(null)-; same: 0: PASS file1: --, file2: -qutil.out-; same: 0: PASS +---- path +//// -> / +a/b/cdef -> cdef +a/b/cdef/ -> cdef +/ -> / + -> +quack -> quack ---- read from file This file is used for qutil testing. It has mixed newlines. diff --git a/libtests/qutil.cc b/libtests/qutil.cc index abe05f35..7b1a8788 100644 --- a/libtests/qutil.cc +++ b/libtests/qutil.cc @@ -439,6 +439,29 @@ void same_file_test() assert_same_file("", "qutil.out", false); } +void path_test() +{ + auto check = [](bool print, std::string const& a, std::string const& b) { + auto result = QUtil::path_basename(a); + if (print) + { + std::cout << a << " -> " << result << std::endl; + } + assert(result == b); + }; + +#ifdef _WIN32 + check(false, "asdf\\qwer", "qwer"); + check(false, "asdf\\qwer/\\", "qwer"); +#endif + check(true, "////", "/"); + check(true, "a/b/cdef", "cdef"); + check(true, "a/b/cdef/", "cdef"); + check(true, "/", "/"); + check(true, "", ""); + check(true, "quack", "quack"); +} + void read_from_file_test() { std::list lines = QUtil::read_lines_from_file("other-file"); @@ -636,6 +659,8 @@ int main(int argc, char* argv[]) get_whoami_test(); std::cout << "---- file" << std::endl; same_file_test(); + std::cout << "---- path" << std::endl; + path_test(); std::cout << "---- read from file" << std::endl; read_from_file_test(); std::cout << "---- hex encode/decode" << std::endl; -- cgit v1.2.3-54-g00ecf