|
3 | 3 | #include "pch.h" |
4 | 4 | #include "TestCommon.h" |
5 | 5 | #include <winget/Filesystem.h> |
| 6 | +#include <winget/PathTree.h> |
6 | 7 | #include <AppInstallerStrings.h> |
7 | 8 |
|
8 | 9 | using namespace AppInstaller::Utility; |
@@ -113,3 +114,112 @@ TEST_CASE("GetExecutablePathForProcess", "[filesystem]") |
113 | 114 | REQUIRE(thisExecutable.has_extension()); |
114 | 115 | REQUIRE(thisExecutable.filename() == L"AppInstallerCLITests.exe"); |
115 | 116 | } |
| 117 | + |
| 118 | +TEST_CASE("PathTree_InsertAndFind", "[filesystem][pathtree]") |
| 119 | +{ |
| 120 | + PathTree<bool> pathTree; |
| 121 | + |
| 122 | + std::filesystem::path path1 = L"C:\\test"; |
| 123 | + std::filesystem::path path1sub = L"C:\\test\\sub"; |
| 124 | + std::filesystem::path path2 = L"C:\\diff"; |
| 125 | + std::filesystem::path path3 = L"D:\\test"; |
| 126 | + |
| 127 | + REQUIRE(nullptr == pathTree.Find(path1)); |
| 128 | + pathTree.FindOrInsert(path1) = true; |
| 129 | + |
| 130 | + REQUIRE(nullptr != pathTree.Find(path1)); |
| 131 | + REQUIRE(*pathTree.Find(path1)); |
| 132 | + |
| 133 | + REQUIRE(nullptr == pathTree.Find(path1sub)); |
| 134 | + REQUIRE(nullptr == pathTree.Find(path2)); |
| 135 | + REQUIRE(nullptr == pathTree.Find(path3)); |
| 136 | +} |
| 137 | + |
| 138 | +TEST_CASE("PathTree_InsertAndFind_Negative", "[filesystem][pathtree]") |
| 139 | +{ |
| 140 | + PathTree<bool> pathTree; |
| 141 | + pathTree.FindOrInsert(L"C:\\a\\aa\\aaa"); |
| 142 | + |
| 143 | + REQUIRE(nullptr == pathTree.Find({})); |
| 144 | + REQUIRE_THROWS_HR(pathTree.FindOrInsert({}), E_INVALIDARG); |
| 145 | +} |
| 146 | + |
| 147 | +size_t CountVisited(const PathTree<bool>& pathTree, const std::filesystem::path& path, std::function<bool(const bool&)> predicate) |
| 148 | +{ |
| 149 | + size_t result = 0; |
| 150 | + pathTree.VisitIf(path, [&](const bool&) { ++result; }, predicate); |
| 151 | + return result; |
| 152 | +} |
| 153 | + |
| 154 | +TEST_CASE("PathTree_VisitIf_Count", "[filesystem][pathtree]") |
| 155 | +{ |
| 156 | + PathTree<bool> pathTree; |
| 157 | + |
| 158 | + pathTree.FindOrInsert(L"C:\\a\\aa\\aaa") = true; |
| 159 | + pathTree.FindOrInsert(L"C:\\a\\aa\\bbb") = true; |
| 160 | + pathTree.FindOrInsert(L"C:\\a\\aa\\ccc") = false; |
| 161 | + pathTree.FindOrInsert(L"C:\\a\\aa") = true; |
| 162 | + |
| 163 | + pathTree.FindOrInsert(L"C:\\a\\bb\\aaa") = false; |
| 164 | + pathTree.FindOrInsert(L"C:\\a\\bb\\bbb") = true; |
| 165 | + pathTree.FindOrInsert(L"C:\\a\\bb\\ccc") = false; |
| 166 | + pathTree.FindOrInsert(L"C:\\a\\bb") = true; |
| 167 | + |
| 168 | + pathTree.FindOrInsert(L"C:\\a\\cc\\aaa") = true; |
| 169 | + pathTree.FindOrInsert(L"C:\\a\\cc\\bbb") = false; |
| 170 | + pathTree.FindOrInsert(L"C:\\a\\cc\\ccc") = false; |
| 171 | + pathTree.FindOrInsert(L"C:\\a\\cc") = false; |
| 172 | + |
| 173 | + pathTree.FindOrInsert(L"C:\\a") = true; |
| 174 | + pathTree.FindOrInsert(L"C:\\b") = false; |
| 175 | + pathTree.FindOrInsert(L"D:\\a") = false; |
| 176 | + |
| 177 | + auto always = [](const bool&) { return true; }; |
| 178 | + auto never = [](const bool&) { return false; }; |
| 179 | + auto if_input = [](const bool& b) { return b; }; |
| 180 | + |
| 181 | + REQUIRE(0 == CountVisited(pathTree, {}, always)); |
| 182 | + |
| 183 | + REQUIRE(15 == CountVisited(pathTree, L"C:\\", always)); |
| 184 | + REQUIRE(2 == CountVisited(pathTree, L"D:\\", always)); |
| 185 | + REQUIRE(0 == CountVisited(pathTree, L"E:\\", always)); |
| 186 | + |
| 187 | + REQUIRE(1 == CountVisited(pathTree, L"C:\\", never)); |
| 188 | + REQUIRE(1 == CountVisited(pathTree, L"D:\\", never)); |
| 189 | + REQUIRE(0 == CountVisited(pathTree, L"E:\\", never)); |
| 190 | + |
| 191 | + REQUIRE(7 == CountVisited(pathTree, L"C:\\", if_input)); |
| 192 | + REQUIRE(6 == CountVisited(pathTree, L"C:\\a", if_input)); |
| 193 | + REQUIRE(2 == CountVisited(pathTree, L"C:\\a\\cc", if_input)); |
| 194 | + REQUIRE(1 == CountVisited(pathTree, L"D:\\", if_input)); |
| 195 | + REQUIRE(0 == CountVisited(pathTree, L"E:\\", if_input)); |
| 196 | +} |
| 197 | + |
| 198 | +TEST_CASE("PathTree_VisitIf_Correct", "[filesystem][pathtree]") |
| 199 | +{ |
| 200 | + PathTree<std::pair<bool, bool>> pathTree; |
| 201 | + |
| 202 | + pathTree.FindOrInsert(L"C:\\a\\aa\\aaa") = { true, true }; |
| 203 | + pathTree.FindOrInsert(L"C:\\a\\aa\\bbb") = { true, true }; |
| 204 | + pathTree.FindOrInsert(L"C:\\a\\aa\\ccc") = { false, false }; |
| 205 | + pathTree.FindOrInsert(L"C:\\a\\aa") = { true, true }; |
| 206 | + |
| 207 | + pathTree.FindOrInsert(L"C:\\a\\bb\\aaa") = { false, false }; |
| 208 | + pathTree.FindOrInsert(L"C:\\a\\bb\\bbb") = { true, true }; |
| 209 | + pathTree.FindOrInsert(L"C:\\a\\bb\\ccc") = { false, false }; |
| 210 | + pathTree.FindOrInsert(L"C:\\a\\bb") = { true, true }; |
| 211 | + |
| 212 | + pathTree.FindOrInsert(L"C:\\a\\cc\\aaa") = { true, true }; |
| 213 | + pathTree.FindOrInsert(L"C:\\a\\cc\\bbb") = { false, false }; |
| 214 | + pathTree.FindOrInsert(L"C:\\a\\cc\\ccc") = { false, false }; |
| 215 | + pathTree.FindOrInsert(L"C:\\a\\cc") = { false, false }; |
| 216 | + |
| 217 | + pathTree.FindOrInsert(L"C:\\a") = { true, true }; |
| 218 | + pathTree.FindOrInsert(L"C:\\b") = { false, false }; |
| 219 | + pathTree.FindOrInsert(L"C:") = { true, false }; |
| 220 | + |
| 221 | + auto check_input = [](const std::pair<bool, bool>& p) { REQUIRE(p.first); }; |
| 222 | + auto if_input = [](const std::pair<bool, bool>& p) { return p.second; }; |
| 223 | + |
| 224 | + pathTree.VisitIf(L"C:", check_input, if_input); |
| 225 | +} |
0 commit comments