1// Unit test for boost::lexical_cast.
2//
3// See http://www.boost.org for most recent version, including documentation.
4//
5// Copyright Antony Polukhin, 2013-2024.
6//
7// Distributed under the Boost
8// Software License, Version 1.0. (See accompanying file
9// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt).
10//
11// Test lexical_cast usage with long filesystem::path. Bug 7704.
12
13#include <boost/lexical_cast.hpp>
14
15#include <boost/core/lightweight_test.hpp>
16#include <boost/filesystem/path.hpp>
17
18void test_filesystem()
19{
20 boost::filesystem::path p;
21 std::string s1 = "aaaaaaaaaaaaaaaaaaaaaaa";
22 p = boost::lexical_cast<boost::filesystem::path>(arg: s1);
23 BOOST_TEST(!p.empty());
24 BOOST_TEST_EQ(p, s1);
25 p.clear();
26
27 const char ab[] = "aaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb";
28 p = boost::lexical_cast<boost::filesystem::path>(arg: ab);
29 BOOST_TEST(!p.empty());
30 BOOST_TEST_EQ(p, ab);
31
32 // Tests for
33 // https://github.com/boostorg/lexical_cast/issues/25
34
35 const char quoted_path[] = "\"/home/my user\"";
36 p = boost::lexical_cast<boost::filesystem::path>(arg: quoted_path);
37 BOOST_TEST(!p.empty());
38 const char unquoted_path[] = "/home/my user";
39 BOOST_TEST_EQ(p, boost::filesystem::path(unquoted_path));
40
41 // Converting back to std::string gives the initial string
42 BOOST_TEST_EQ(boost::lexical_cast<std::string>(p), quoted_path);
43
44 try {
45 // Without quotes the path will have only `/home/my` in it.
46 // `user` remains in the stream, so an exception must be thrown.
47 p = boost::lexical_cast<boost::filesystem::path>(arg: unquoted_path);
48 BOOST_TEST(false);
49 } catch (const boost::bad_lexical_cast& ) {
50 // Exception is expected
51 }
52}
53
54int main()
55{
56 test_filesystem();
57
58 return boost::report_errors();
59}
60
61

source code of boost/libs/lexical_cast/test/filesystem_test.cpp