diff options
author | Denis Brockus <dbrockus@google.com> | 2023-03-20 10:25:52 -0600 |
---|---|---|
committer | Chromeos LUCI <chromeos-scoped@luci-project-accounts.iam.gserviceaccount.com> | 2023-03-21 17:29:12 +0000 |
commit | 852c64501bd7a6162d02f48f0faac7fef5080718 (patch) | |
tree | 05f4f90e2b6ac583f561bc6c9f9e4cb6b4cdd80a | |
parent | c4c39551c442101a46ee73f66912d29495a398a1 (diff) | |
download | libchrome-gestures-852c64501bd7a6162d02f48f0faac7fef5080718.tar.gz |
Add ListAt to the gesture utilities
BUG=b:271591258
TEST=USE="coverage" FEATURES="test noclean" emerge-brya chromeos-base/gestures
Change-Id: Ie95a888ddccb16255db75f3e99436896ef367016
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/gestures/+/4352449
Code-Coverage: Harry Cutts <hcutts@chromium.org>
Auto-Submit: Denis Brockus <dbrockus@chromium.org>
Commit-Queue: Harry Cutts <hcutts@chromium.org>
Reviewed-by: Harry Cutts <hcutts@chromium.org>
Tested-by: Denis Brockus <dbrockus@chromium.org>
-rw-r--r-- | include/util.h | 22 | ||||
-rw-r--r-- | src/util_unittest.cc | 40 |
2 files changed, 62 insertions, 0 deletions
diff --git a/include/util.h b/include/util.h index 2e87a7b..9feb2d5 100644 --- a/include/util.h +++ b/include/util.h @@ -5,7 +5,9 @@ #ifndef GESTURES_UTIL_H_ #define GESTURES_UTIL_H_ +#include <list> #include <map> +#include <optional> #include <set> #include <math.h> @@ -105,6 +107,26 @@ inline bool SetContainsValue(const Set& the_set, return the_set.find(elt) != the_set.end(); } +template<typename R, typename L> +R ListAt(L& the_list, int offset) { + // Traverse to the appropriate offset + if (offset < 0) { + // negative offset is from end to begin + for (auto iter = the_list.rbegin(); iter != the_list.rend(); ++iter) { + if (++offset == 0) + return R(*iter); + } + } else { + // positive offset is from begin to end + for (auto iter = the_list.begin(); iter != the_list.end(); ++iter) { + if (offset-- == 0) + return R(*iter); + } + } + // Invalid offset + return std::nullopt; +} + } // namespace gestures #endif // GESTURES_UTIL_H_ diff --git a/src/util_unittest.cc b/src/util_unittest.cc index 5d0a435..9784d70 100644 --- a/src/util_unittest.cc +++ b/src/util_unittest.cc @@ -21,4 +21,44 @@ TEST(UtilTest, DistSqTest) { EXPECT_FLOAT_EQ(DistSqXY(fs[0], 4, 6), 25); } +TEST(UtilTest, ListAtTest) { + const int kMaxElements = 3; + struct element { + int x; + }; + + typedef std::list<element> ElemListType; + typedef std::optional<std::reference_wrapper<element>> OptionalRefElem; + + struct ElemList : public ElemListType { + OptionalRefElem at(int offset) { + return ListAt<OptionalRefElem, ElemListType>(*this, offset); + } + } list; + + for (auto i = 0; i < kMaxElements; ++i) { + auto& elem = list.emplace_back(); + elem.x = i; + } + EXPECT_EQ(list.at(-1)->get().x, list.at(list.size() - 1)->get().x); + + for (auto i = 0; i < kMaxElements; ++i) { + for (auto j = 0; j < kMaxElements; ++j) { + if (i == j) { + EXPECT_EQ(list.at(i)->get().x, list.at(j)->get().x); + EXPECT_EQ(&(list.at(i)->get()), &(list.at(j)->get())); + } else { + EXPECT_NE(list.at(i)->get().x, list.at(j)->get().x); + EXPECT_NE(&(list.at(i)->get()), &(list.at(j)->get())); + } + } + } + + int list_count = list.size(); + EXPECT_EQ(list.at(list_count), std::nullopt); + EXPECT_NE(list.at(list_count - 1), std::nullopt); + EXPECT_EQ(list.at(-list_count - 1), std::nullopt); + EXPECT_NE(list.at(-list_count), std::nullopt); +} + } // namespace gestures |