[libcxx] [test] Fix string type handling in a few fairly trivial class.path tests

Use string() for convenience for testing where possible, but keep using
native() for move tests where we want to check that no allocations are
made, constructing a reference fs::path::string_type instead.

Use the right value_type in a few places.

Make the synop test check for the right types and for the expected
preferred separator.

Differential Revision: https://reviews.llvm.org/D89537
This commit is contained in:
Martin Storsjö 2020-10-15 11:55:10 +03:00
parent b740899c50
commit 3784bdf217
11 changed files with 37 additions and 20 deletions

View File

@ -29,8 +29,8 @@ int main(int, char**) {
const path p(s); const path p(s);
path p2; path p2;
path& pref = (p2 = p); path& pref = (p2 = p);
assert(p.native() == s); assert(p.string() == s);
assert(p2.native() == s); assert(p2.string() == s);
assert(&pref == &p2); assert(&pref == &p2);
return 0; return 0;

View File

@ -29,13 +29,14 @@ int main(int, char**) {
const std::string s("we really really really really really really really " const std::string s("we really really really really really really really "
"really really long string so that we allocate"); "really really long string so that we allocate");
assert(globalMemCounter.checkOutstandingNewEq(1)); assert(globalMemCounter.checkOutstandingNewEq(1));
const fs::path::string_type ps(s.begin(), s.end());
path p(s); path p(s);
{ {
DisableAllocationGuard g; DisableAllocationGuard g;
path p2; path p2;
path& pref = (p2 = std::move(p)); path& pref = (p2 = std::move(p));
assert(p2.native() == s); assert(p2.native() == ps);
assert(p.native() != s); // Testing moved from state assert(p.native() != ps); // Testing moved from state
assert(&pref == &p2); assert(&pref == &p2);
} }

View File

@ -28,8 +28,8 @@ int main(int, char**) {
const std::string s("foo"); const std::string s("foo");
const path p(s); const path p(s);
path p2(p); path p2(p);
assert(p.native() == s); assert(p.string() == s);
assert(p2.native() == s); assert(p2.string() == s);
return 0; return 0;
} }

View File

@ -29,12 +29,13 @@ int main(int, char**) {
const std::string s("we really really really really really really really " const std::string s("we really really really really really really really "
"really really long string so that we allocate"); "really really long string so that we allocate");
assert(globalMemCounter.checkOutstandingNewEq(1)); assert(globalMemCounter.checkOutstandingNewEq(1));
const fs::path::string_type ps(s.begin(), s.end());
path p(s); path p(s);
{ {
DisableAllocationGuard g; DisableAllocationGuard g;
path p2(std::move(p)); path p2(std::move(p));
assert(p2.native() == s); assert(p2.native() == ps);
assert(p.native() != s); // Testing moved from state assert(p.native() != ps); // Testing moved from state
} }
return 0; return 0;

View File

@ -31,7 +31,7 @@
template <class CharT, class ...Args> template <class CharT, class ...Args>
void RunTestCaseImpl(MultiStringType const& MS, Args... args) { void RunTestCaseImpl(MultiStringType const& MS, Args... args) {
using namespace fs; using namespace fs;
const char* Expect = MS; const fs::path::value_type* Expect = MS;
const CharT* TestPath = MS; const CharT* TestPath = MS;
const CharT* TestPathEnd = StrEnd(TestPath); const CharT* TestPathEnd = StrEnd(TestPath);
const std::size_t Size = TestPathEnd - TestPath; const std::size_t Size = TestPathEnd - TestPath;

View File

@ -28,6 +28,7 @@ int main(int, char**)
using namespace fs; using namespace fs;
const char* const value = "hello world"; const char* const value = "hello world";
const std::string str_value = value; const std::string str_value = value;
const fs::path::string_type pathstr_value(str_value.begin(), str_value.end());
{ // Check signature { // Check signature
path p(value); path p(value);
ASSERT_SAME_TYPE(path::value_type const*, decltype(p.c_str())); ASSERT_SAME_TYPE(path::value_type const*, decltype(p.c_str()));
@ -35,7 +36,7 @@ int main(int, char**)
} }
{ {
path p(value); path p(value);
assert(p.c_str() == str_value); assert(p.c_str() == pathstr_value);
assert(p.native().c_str() == p.c_str()); assert(p.native().c_str() == p.c_str());
} }

View File

@ -26,6 +26,8 @@ int main(int, char**)
{ {
using namespace fs; using namespace fs;
const char* const value = "hello world"; const char* const value = "hello world";
std::string value_str(value);
fs::path::string_type pathstr_value(value_str.begin(), value_str.end());
{ // Check signature { // Check signature
path p(value); path p(value);
ASSERT_SAME_TYPE(path::string_type const&, decltype(p.native())); ASSERT_SAME_TYPE(path::string_type const&, decltype(p.native()));
@ -33,7 +35,7 @@ int main(int, char**)
} }
{ // native() is tested elsewhere { // native() is tested elsewhere
path p(value); path p(value);
assert(p.native() == value); assert(p.native() == pathstr_value);
} }
return 0; return 0;

View File

@ -28,6 +28,8 @@ int main(int, char**)
using namespace fs; using namespace fs;
using string_type = path::string_type; using string_type = path::string_type;
const char* const value = "hello world"; const char* const value = "hello world";
std::string value_str(value);
fs::path::string_type pathstr_value(value_str.begin(), value_str.end());
{ // Check signature { // Check signature
path p(value); path p(value);
static_assert(std::is_convertible<path, string_type>::value, ""); static_assert(std::is_convertible<path, string_type>::value, "");
@ -37,10 +39,10 @@ int main(int, char**)
} }
{ {
path p(value); path p(value);
assert(p.native() == value); assert(p.native() == pathstr_value);
string_type s = p; string_type s = p;
assert(s == value); assert(s == pathstr_value);
assert(p == value); assert(p == pathstr_value);
} }
return 0; return 0;

View File

@ -53,7 +53,7 @@ void doIOTest() {
{ // test input { // test input
path p_in; path p_in;
auto& ret = ss >> p_in; auto& ret = ss >> p_in;
assert(p_in.native() == (const char*)InStr); assert(p_in.native() == (const path::value_type*)InStr);
assert(&ret == &ss); assert(&ret == &ss);
} }
} }

View File

@ -29,6 +29,8 @@ int main(int, char**)
const char* value2 = "_THIS_IS_LONG_THIS_IS_LONG_THIS_IS_LONG_THIS_IS_LONG_THIS_IS_LONG_THIS_IS_LONG_THIS_IS_LONG"; const char* value2 = "_THIS_IS_LONG_THIS_IS_LONG_THIS_IS_LONG_THIS_IS_LONG_THIS_IS_LONG_THIS_IS_LONG_THIS_IS_LONG";
path p1(value1); path p1(value1);
path p2(value2); path p2(value2);
fs::path::string_type ps1 = p1.native();
fs::path::string_type ps2 = p2.native();
{ {
using namespace std; using namespace fs; using namespace std; using namespace fs;
ASSERT_NOEXCEPT(swap(p1, p2)); ASSERT_NOEXCEPT(swap(p1, p2));
@ -39,11 +41,11 @@ int main(int, char**)
using namespace std; using namespace std;
using namespace fs; using namespace fs;
swap(p1, p2); swap(p1, p2);
assert(p1.native() == value2); assert(p1.native() == ps2);
assert(p2.native() == value1); assert(p2.native() == ps1);
swap(p1, p2); swap(p1, p2);
assert(p1.native() == value1); assert(p1.native() == ps1);
assert(p2.native() == value2); assert(p2.native() == ps2);
} }
return 0; return 0;

View File

@ -25,13 +25,21 @@
int main(int, char**) { int main(int, char**) {
using namespace fs; using namespace fs;
#ifdef _WIN32
ASSERT_SAME_TYPE(path::value_type, wchar_t);
#else
ASSERT_SAME_TYPE(path::value_type, char); ASSERT_SAME_TYPE(path::value_type, char);
#endif
ASSERT_SAME_TYPE(path::string_type, std::basic_string<path::value_type>); ASSERT_SAME_TYPE(path::string_type, std::basic_string<path::value_type>);
{ {
ASSERT_SAME_TYPE(const path::value_type, decltype(path::preferred_separator)); ASSERT_SAME_TYPE(const path::value_type, decltype(path::preferred_separator));
#ifdef _WIN32
static_assert(path::preferred_separator == '\\', "");
#else
static_assert(path::preferred_separator == '/', ""); static_assert(path::preferred_separator == '/', "");
#endif
// Make preferred_separator ODR used by taking its address. // Make preferred_separator ODR used by taking its address.
const char* dummy = &path::preferred_separator; const path::value_type* dummy = &path::preferred_separator;
((void)dummy); ((void)dummy);
} }