[NFC][sanitizer] Add SortAndDedup function

This commit is contained in:
Vitaly Buka 2020-12-28 17:45:54 -08:00
parent 4e74480e02
commit ababeca34b
2 changed files with 50 additions and 0 deletions

View File

@ -682,6 +682,27 @@ enum ModuleArch {
kModuleArchRISCV64
};
// Sorts and removes duplicates from the container.
template <class Container,
class Compare = CompareLess<typename Container::value_type>>
void SortAndDedup(Container &v, Compare comp = {}) {
Sort(v.data(), v.size(), comp);
uptr size = v.size();
if (size < 2)
return;
uptr last = 0;
for (uptr i = 1; i < size; ++i) {
if (comp(v[last], v[i])) {
++last;
if (last != i)
v[last] = v[i];
} else {
CHECK(!comp(v[i], v[last]));
}
}
v.resize(last + 1);
}
// Opens the file 'file_name" and reads up to 'max_len' bytes.
// The resulting buffer is mmaped and stored in '*buff'.
// Returns true if file was successfully opened and read.

View File

@ -269,6 +269,35 @@ TEST(SanitizerCommon, InternalLowerBoundVsStdLowerBound) {
}
}
class SortAndDedupTest : public ::testing::TestWithParam<std::vector<int>> {};
TEST_P(SortAndDedupTest, SortAndDedup) {
std::vector<int> v_std = GetParam();
std::sort(v_std.begin(), v_std.end());
v_std.erase(std::unique(v_std.begin(), v_std.end()), v_std.end());
std::vector<int> v = GetParam();
SortAndDedup(v);
EXPECT_EQ(v_std, v);
}
const std::vector<int> kSortAndDedupTests[] = {
{},
{1},
{1, 1},
{1, 1, 1},
{1, 2, 3},
{3, 2, 1},
{1, 2, 2, 3},
{3, 3, 2, 1, 2},
{3, 3, 2, 1, 2},
{1, 2, 1, 1, 2, 1, 1, 1, 2, 2},
{1, 3, 3, 2, 3, 1, 3, 1, 4, 4, 2, 1, 4, 1, 1, 2, 2},
};
INSTANTIATE_TEST_CASE_P(SortAndDedupTest, SortAndDedupTest,
::testing::ValuesIn(kSortAndDedupTests));
#if SANITIZER_LINUX && !SANITIZER_ANDROID
TEST(SanitizerCommon, FindPathToBinary) {
char *true_path = FindPathToBinary("true");