From 62c18729f1cf8c5f1b7dc6f0d37edbffd2bb78b5 Mon Sep 17 00:00:00 2001 From: levlam Date: Thu, 7 Jan 2021 04:03:38 +0300 Subject: [PATCH] Fix td::unique. --- tdutils/td/utils/algorithm.h | 2 +- tdutils/td/utils/translit.cpp | 1 + tdutils/test/misc.cpp | 7 +++++++ 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/tdutils/td/utils/algorithm.h b/tdutils/td/utils/algorithm.h index 9baedc620..06f5fbb62 100644 --- a/tdutils/td/utils/algorithm.h +++ b/tdutils/td/utils/algorithm.h @@ -98,7 +98,7 @@ void unique(V &v) { size_t j = 1; for (size_t i = 1; i < v.size(); i++) { - if (v[i] != v[i - 1]) { + if (v[i] != v[j - 1]) { if (i != j) { v[j] = std::move(v[i]); } diff --git a/tdutils/td/utils/translit.cpp b/tdutils/td/utils/translit.cpp index 60ee4e78a..239fa7f5c 100644 --- a/tdutils/td/utils/translit.cpp +++ b/tdutils/td/utils/translit.cpp @@ -7,6 +7,7 @@ #include "td/utils/translit.h" #include "td/utils/algorithm.h" +#include "td/utils/format.h" #include "td/utils/misc.h" #include "td/utils/utf8.h" diff --git a/tdutils/test/misc.cpp b/tdutils/test/misc.cpp index 08c437922..55d172ac1 100644 --- a/tdutils/test/misc.cpp +++ b/tdutils/test/misc.cpp @@ -349,8 +349,14 @@ TEST(Misc, remove) { } static void test_unique(td::vector v, td::vector expected) { + auto v_str = td::transform(v, &td::to_string); + auto expected_str = td::transform(expected, &td::to_string); + td::unique(v); ASSERT_EQ(expected, v); + + td::unique(v_str); + ASSERT_EQ(expected_str, v_str); } TEST(Misc, unique) { @@ -362,6 +368,7 @@ TEST(Misc, unique) { test_unique({0, 1}, {0, 1}); test_unique({1, 0}, {0, 1}); test_unique({1, 1}, {1}); + test_unique({1, 1, 0, 0}, {0, 1}); test_unique({3, 3, 3, 3, 3, 2, 2, 2, 1, 1, 0}, {0, 1, 2, 3}); test_unique({3, 3, 3, 3, 3}, {3}); test_unique({3, 3, -1, 3, 3}, {-1, 3});