1 //===-- FileSpecListTest.cpp ----------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "gtest/gtest.h"
11 #include "lldb/Utility/FileSpecList.h"
13 using namespace lldb_private
;
15 static FileSpec
PosixSpec(llvm::StringRef path
) {
16 return FileSpec(path
, FileSpec::Style::posix
);
19 static FileSpec
WindowsSpec(llvm::StringRef path
) {
20 return FileSpec(path
, FileSpec::Style::windows
);
23 TEST(FileSpecListTest
, RelativePathMatchesPosix
) {
25 const FileSpec fullpath
= PosixSpec("/build/src/main.cpp");
26 const FileSpec relative
= PosixSpec("./src/main.cpp");
27 const FileSpec basename
= PosixSpec("./main.cpp");
28 const FileSpec full_wrong
= PosixSpec("/other/wrong/main.cpp");
29 const FileSpec rel_wrong
= PosixSpec("./wrong/main.cpp");
30 // Make sure these don't match "src/main.cpp" as we want to match full
32 const FileSpec rel2_wrong
= PosixSpec("asrc/main.cpp");
33 const FileSpec rel3_wrong
= PosixSpec("rc/main.cpp");
36 files
.Append(fullpath
);
37 files
.Append(relative
);
38 files
.Append(basename
);
39 files
.Append(full_wrong
);
40 files
.Append(rel_wrong
);
41 files
.Append(rel2_wrong
);
42 files
.Append(rel3_wrong
);
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
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
));
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
));
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
));
71 EXPECT_EQ((size_t)2, files
.FindCompatibleIndex(0, rel3_wrong
));
72 EXPECT_EQ((size_t)6, files
.FindCompatibleIndex(3, rel3_wrong
));
75 TEST(FileSpecListTest
, RelativePathMatchesWindows
) {
77 const FileSpec fullpath
= WindowsSpec(R
"(C:\build\src\main.cpp)");
78 const FileSpec relative
= WindowsSpec(R
"(.\src\main.cpp)");
79 const FileSpec basename
= WindowsSpec(R
"(.\main.cpp)");
80 const FileSpec full_wrong
= WindowsSpec(R
"(\other\wrong\main.cpp)");
81 const FileSpec rel_wrong
= WindowsSpec(R
"(.\wrong\main.cpp)");
82 // Make sure these don't match "src\main.cpp" as we want to match full
84 const FileSpec rel2_wrong
= WindowsSpec(R
"(asrc\main.cpp)");
85 const FileSpec rel3_wrong
= WindowsSpec(R
"("rc\main
.cpp
)");
88 files.Append(fullpath);
89 files.Append(relative);
90 files.Append(basename);
91 files.Append(full_wrong);
92 files.Append(rel_wrong);
93 files.Append(rel2_wrong);
94 files.Append(rel3_wrong);
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
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));
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));
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));
123 EXPECT_EQ((size_t)2, files.FindCompatibleIndex(0, rel3_wrong));
124 EXPECT_EQ((size_t)6, files.FindCompatibleIndex(3, rel3_wrong));