Debian Codegraph @

libstdc++-v3/testsuite/27_io/filesystem/path/concat/120029.cc

raw
1 // { dg-do run { target c++17 } }
2
3 // Bug libstdc++/120029
4 // Dangling iterator usage in path::operator+=(const path& p) when this == p
5
6 #include <filesystem>
7 #include <testsuite_hooks.h>
8
9 namespace fs = std::filesystem;
10
11 void
12 test_root_dir()
13 {
14 fs::path p = "/";
15 p += p;
16 p += p;
17 VERIFY( p == "////" );
18 p += p.filename();
19 VERIFY( p == "////" );
20 p += *std::prev(p.end());
21 VERIFY( p == "////" );
22 }
23
24 void
25 test_root_name()
26 {
27 fs::path p = "C:/";
28 p += p;
29 p += p;
30 VERIFY( p == "C:/C:/C:/C:/" );
31 p += p.filename();
32 VERIFY( p == "C:/C:/C:/C:/" );
33 p += *std::prev(p.end());
34 VERIFY( p == "C:/C:/C:/C:/" );
35 }
36
37 void
38 test_filename()
39 {
40 fs::path p = "file";
41 p += p;
42 p += p;
43 VERIFY( p == "filefilefilefile" );
44 p += p.filename();
45 VERIFY( p == "filefilefilefilefilefilefilefile" );
46 p += *std::prev(p.end());
47 VERIFY( p == "filefilefilefilefilefilefilefilefilefilefilefilefilefilefilefile" );
48 }
49
50 void
51 test_multi()
52 {
53 fs::path p = "/home/username/Documents/mu";
54 p += p;
55 p += p;
56 VERIFY( p == "/home/username/Documents/mu/home/username/Documents/mu/home/username/Documents/mu/home/username/Documents/mu" );
57 p += p.filename();
58 VERIFY( p == "/home/username/Documents/mu/home/username/Documents/mu/home/username/Documents/mu/home/username/Documents/mumu" );
59 p += *std::prev(p.end());
60 VERIFY( p == "/home/username/Documents/mu/home/username/Documents/mu/home/username/Documents/mu/home/username/Documents/mumumumu" );
61 auto n = std::distance(p.begin(), p.end());
62 for (int i = 0; i < n; ++i)
63 p += *std::next(p.begin(), i);
64 }
65
66 int main()
67 {
68 test_root_dir();
69 test_root_name();
70 test_filename();
71 test_multi();
72 }