forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfile_path_set_unittest.cc
168 lines (142 loc) · 5.91 KB
/
file_path_set_unittest.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/chrome_cleaner/os/file_path_set.h"
#include "base/files/scoped_temp_dir.h"
#include "base/strings/string_util.h"
#include "chrome/chrome_cleaner/test/test_file_util.h"
#include "chrome/chrome_cleaner/test/test_util.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace chrome_cleaner {
namespace {
const wchar_t kFileNameFull[] = L"C:\\Filename";
const wchar_t kLongFileName[] = L"Long File Name.bla";
const wchar_t kLongFileNameFull[] = L"C:\\Long File Name.bla";
} // namespace
TEST(FilePathSetTests, Empty) {
FilePathSet file_paths;
// Start empty.
EXPECT_TRUE(file_paths.empty());
// Clear should be callable on empty sets.
file_paths.clear();
}
TEST(FilePathSetTests, InsertedOnce) {
FilePathSet file_paths;
// Same path should be inserted only once.
base::FilePath file_path1(kFileNameFull);
EXPECT_TRUE(file_paths.Insert(file_path1));
EXPECT_FALSE(file_paths.Insert(file_path1));
EXPECT_EQ(1UL, file_paths.size());
// But still should be found.
EXPECT_TRUE(file_paths.Contains(file_path1));
}
TEST(FilePathSetTests, EqualOperator) {
FilePathSet file_paths1;
base::FilePath file_path(kFileNameFull);
EXPECT_TRUE(file_paths1.Insert(file_path));
EXPECT_EQ(file_paths1, file_paths1);
FilePathSet file_paths2;
EXPECT_TRUE(file_paths2.Insert(file_path));
EXPECT_EQ(file_paths1, file_paths2);
base::FilePath long_file_path(kLongFileNameFull);
EXPECT_TRUE(file_paths1.Insert(long_file_path));
EXPECT_TRUE(file_paths2.Insert(long_file_path));
EXPECT_EQ(file_paths1, file_paths2);
base::FilePath other_path(L"C:\\other_path.txt");
EXPECT_TRUE(file_paths2.Insert(other_path));
// To avoid implementing operator!=();
EXPECT_FALSE(file_paths1 == file_paths2);
}
TEST(FilePathSetTests, LongName) {
FilePathSet file_paths;
// Long paths should also be found, even by their short version.
base::ScopedTempDir scoped_temp_dir;
ASSERT_TRUE(scoped_temp_dir.CreateUniqueTempDir());
base::FilePath short_name_path;
base::FilePath long_name_path(
scoped_temp_dir.GetPath().Append(kLongFileName));
CreateFileAndGetShortName(long_name_path, &short_name_path);
EXPECT_TRUE(file_paths.Insert(long_name_path));
EXPECT_FALSE(file_paths.Insert(long_name_path));
EXPECT_FALSE(file_paths.Insert(short_name_path));
base::FilePath long_name_path_upper(
base::ToUpperASCII(long_name_path.value()));
EXPECT_FALSE(file_paths.Insert(long_name_path_upper));
// And they should be found-able in all its different forms.
EXPECT_TRUE(file_paths.Contains(long_name_path));
EXPECT_TRUE(file_paths.Contains(short_name_path));
EXPECT_TRUE(file_paths.Contains(long_name_path_upper));
}
TEST(FilePathSetTests, ShortName) {
FilePathSet file_paths;
// Short paths should also be found, even by their long version.
base::ScopedTempDir scoped_temp_dir;
ASSERT_TRUE(scoped_temp_dir.CreateUniqueTempDir());
base::FilePath short_name_path;
base::FilePath long_name_path(
scoped_temp_dir.GetPath().Append(kLongFileName));
CreateFileAndGetShortName(long_name_path, &short_name_path);
EXPECT_TRUE(file_paths.Insert(short_name_path));
EXPECT_FALSE(file_paths.Insert(short_name_path));
EXPECT_FALSE(file_paths.Insert(long_name_path));
base::FilePath long_name_path_upper(
base::ToUpperASCII(long_name_path.value()));
EXPECT_FALSE(file_paths.Insert(long_name_path_upper));
// And they should be found-able in all its different forms.
EXPECT_TRUE(file_paths.Contains(long_name_path));
EXPECT_TRUE(file_paths.Contains(short_name_path));
EXPECT_TRUE(file_paths.Contains(long_name_path_upper));
}
TEST(FilePathSetTests, ProperOrder) {
// Ensure that all the items in a folder are listed before the folder.
const base::FilePath folder(L"C:\\folder");
base::FilePath file = folder.Append(L"file.exe");
base::FilePath sub_folder = folder.Append(L"sub_folder");
base::FilePath sub_file = sub_folder.Append(L"sub_file.txt");
FilePathSet file_paths;
EXPECT_TRUE(file_paths.Insert(folder));
EXPECT_TRUE(file_paths.Insert(file));
EXPECT_TRUE(file_paths.Insert(sub_folder));
EXPECT_TRUE(file_paths.Insert(sub_file));
const auto sorted_files = file_paths.ReverseSorted();
ASSERT_EQ(4UL, sorted_files.size());
EXPECT_EQ(sub_file, sorted_files[0]);
EXPECT_EQ(sub_folder, sorted_files[1]);
EXPECT_EQ(file, sorted_files[2]);
EXPECT_EQ(folder, sorted_files[3]);
}
TEST(FilePathMap, Find) {
// Long paths should also be found, even by their short version.
base::ScopedTempDir scoped_temp_dir;
ASSERT_TRUE(scoped_temp_dir.CreateUniqueTempDir());
base::FilePath short_name_path;
base::FilePath long_name_path(
scoped_temp_dir.GetPath().Append(kLongFileName));
CreateFileAndGetShortName(long_name_path, &short_name_path);
FilePathMap<int> map;
EXPECT_TRUE(map.Insert(short_name_path, 42));
const int* found_value = nullptr;
ASSERT_NE(nullptr, found_value = map.Find(short_name_path));
EXPECT_EQ(42, *found_value);
ASSERT_NE(nullptr, found_value = map.Find(long_name_path));
EXPECT_EQ(42, *found_value);
EXPECT_EQ(nullptr, map.Find(base::FilePath(kFileNameFull)));
}
TEST(FilePathMap, DoNotInsertDuplicates) {
base::ScopedTempDir scoped_temp_dir;
ASSERT_TRUE(scoped_temp_dir.CreateUniqueTempDir());
base::FilePath short_name_path;
base::FilePath long_name_path(
scoped_temp_dir.GetPath().Append(kLongFileName));
CreateFileAndGetShortName(long_name_path, &short_name_path);
FilePathMap<int> map;
EXPECT_TRUE(map.Insert(long_name_path, 42));
EXPECT_FALSE(map.Insert(long_name_path, 43));
EXPECT_FALSE(map.Insert(short_name_path, 44));
EXPECT_EQ(1u, map.map().size());
const int* found_value = nullptr;
EXPECT_NE(nullptr, found_value = map.Find(short_name_path));
EXPECT_EQ(42, *found_value);
}
} // namespace chrome_cleaner