1//===-- FileSpecListTest.cpp ----------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "gtest/gtest.h"
10
11#include "lldb/Utility/FileSpecList.h"
12
13using namespace lldb_private;
14
15static FileSpec PosixSpec(llvm::StringRef path) {
16 return FileSpec(path, FileSpec::Style::posix);
17}
18
19static FileSpec WindowsSpec(llvm::StringRef path) {
20 return FileSpec(path, FileSpec::Style::windows);
21}
22
23TEST(SupportFileListTest, RelativePathMatchesPosix) {
24
25 const FileSpec fullpath = PosixSpec(path: "/build/src/main.cpp");
26 const FileSpec relative = PosixSpec(path: "./src/main.cpp");
27 const FileSpec basename = PosixSpec(path: "./main.cpp");
28 const FileSpec full_wrong = PosixSpec(path: "/other/wrong/main.cpp");
29 const FileSpec rel_wrong = PosixSpec(path: "./wrong/main.cpp");
30 // Make sure these don't match "src/main.cpp" as we want to match full
31 // directories only
32 const FileSpec rel2_wrong = PosixSpec(path: "asrc/main.cpp");
33 const FileSpec rel3_wrong = PosixSpec(path: "rc/main.cpp");
34
35 SupportFileList files;
36 files.Append(file: fullpath);
37 files.Append(file: relative);
38 files.Append(file: basename);
39 files.Append(file: full_wrong);
40 files.Append(file: rel_wrong);
41 files.Append(file: rel2_wrong);
42 files.Append(file: rel3_wrong);
43
44 // Make sure the full path only matches the first entry
45 EXPECT_EQ((size_t)0, files.FindCompatibleIndex(0, fullpath));
46 EXPECT_EQ((size_t)1, files.FindCompatibleIndex(1, fullpath));
47 EXPECT_EQ((size_t)2, files.FindCompatibleIndex(2, fullpath));
48 EXPECT_EQ((size_t)UINT32_MAX, files.FindCompatibleIndex(3, fullpath));
49 // Make sure the relative path matches the all of the entries that contain
50 // the relative path
51 EXPECT_EQ((size_t)0, files.FindCompatibleIndex(0, relative));
52 EXPECT_EQ((size_t)1, files.FindCompatibleIndex(1, relative));
53 EXPECT_EQ((size_t)2, files.FindCompatibleIndex(2, relative));
54 EXPECT_EQ((size_t)UINT32_MAX, files.FindCompatibleIndex(3, relative));
55
56 // Make sure looking file a file using the basename matches all entries
57 EXPECT_EQ((size_t)0, files.FindCompatibleIndex(0, basename));
58 EXPECT_EQ((size_t)1, files.FindCompatibleIndex(1, basename));
59 EXPECT_EQ((size_t)2, files.FindCompatibleIndex(2, basename));
60 EXPECT_EQ((size_t)3, files.FindCompatibleIndex(3, basename));
61 EXPECT_EQ((size_t)4, files.FindCompatibleIndex(4, basename));
62 EXPECT_EQ((size_t)5, files.FindCompatibleIndex(5, basename));
63 EXPECT_EQ((size_t)6, files.FindCompatibleIndex(6, basename));
64
65 // Make sure that paths that have a common suffix don't return values that
66 // don't match on directory delimiters.
67 EXPECT_EQ((size_t)2, files.FindCompatibleIndex(0, rel2_wrong));
68 EXPECT_EQ((size_t)5, files.FindCompatibleIndex(3, rel2_wrong));
69 EXPECT_EQ((size_t)UINT32_MAX, files.FindCompatibleIndex(6, rel2_wrong));
70
71 EXPECT_EQ((size_t)2, files.FindCompatibleIndex(0, rel3_wrong));
72 EXPECT_EQ((size_t)6, files.FindCompatibleIndex(3, rel3_wrong));
73}
74
75TEST(SupportFileListTest, RelativePathMatchesWindows) {
76
77 const FileSpec fullpath = WindowsSpec(path: R"(C:\build\src\main.cpp)");
78 const FileSpec relative = WindowsSpec(path: R"(.\src\main.cpp)");
79 const FileSpec basename = WindowsSpec(path: R"(.\main.cpp)");
80 const FileSpec full_wrong = WindowsSpec(path: R"(\other\wrong\main.cpp)");
81 const FileSpec rel_wrong = WindowsSpec(path: R"(.\wrong\main.cpp)");
82 // Make sure these don't match "src\main.cpp" as we want to match full
83 // directories only
84 const FileSpec rel2_wrong = WindowsSpec(path: R"(asrc\main.cpp)");
85 const FileSpec rel3_wrong = WindowsSpec(path: R"("rc\main.cpp)");
86
87 SupportFileList files;
88 files.Append(file: fullpath);
89 files.Append(file: relative);
90 files.Append(file: basename);
91 files.Append(file: full_wrong);
92 files.Append(file: rel_wrong);
93 files.Append(file: rel2_wrong);
94 files.Append(file: rel3_wrong);
95
96 // Make sure the full path only matches the first entry
97 EXPECT_EQ((size_t)0, files.FindCompatibleIndex(0, fullpath));
98 EXPECT_EQ((size_t)1, files.FindCompatibleIndex(1, fullpath));
99 EXPECT_EQ((size_t)2, files.FindCompatibleIndex(2, fullpath));
100 EXPECT_EQ((size_t)UINT32_MAX, files.FindCompatibleIndex(3, fullpath));
101 // Make sure the relative path matches the all of the entries that contain
102 // the relative path
103 EXPECT_EQ((size_t)0, files.FindCompatibleIndex(0, relative));
104 EXPECT_EQ((size_t)1, files.FindCompatibleIndex(1, relative));
105 EXPECT_EQ((size_t)2, files.FindCompatibleIndex(2, relative));
106 EXPECT_EQ((size_t)UINT32_MAX, files.FindCompatibleIndex(3, relative));
107
108 // Make sure looking file a file using the basename matches all entries
109 EXPECT_EQ((size_t)0, files.FindCompatibleIndex(0, basename));
110 EXPECT_EQ((size_t)1, files.FindCompatibleIndex(1, basename));
111 EXPECT_EQ((size_t)2, files.FindCompatibleIndex(2, basename));
112 EXPECT_EQ((size_t)3, files.FindCompatibleIndex(3, basename));
113 EXPECT_EQ((size_t)4, files.FindCompatibleIndex(4, basename));
114 EXPECT_EQ((size_t)5, files.FindCompatibleIndex(5, basename));
115 EXPECT_EQ((size_t)6, files.FindCompatibleIndex(6, basename));
116
117 // Make sure that paths that have a common suffix don't return values that
118 // don't match on directory delimiters.
119 EXPECT_EQ((size_t)2, files.FindCompatibleIndex(0, rel2_wrong));
120 EXPECT_EQ((size_t)5, files.FindCompatibleIndex(3, rel2_wrong));
121 EXPECT_EQ((size_t)UINT32_MAX, files.FindCompatibleIndex(6, rel2_wrong));
122
123 EXPECT_EQ((size_t)2, files.FindCompatibleIndex(0, rel3_wrong));
124 EXPECT_EQ((size_t)6, files.FindCompatibleIndex(3, rel3_wrong));
125}
126

source code of lldb/unittests/Core/FileSpecListTest.cpp