From 50982500ee5a156ea9b20afad83de18b36fa3461 Mon Sep 17 00:00:00 2001 From: Eamonn Kent Date: Mon, 6 Nov 2017 14:43:42 -0800 Subject: [PATCH] Add utility method and tests Summary: This commit provides a utility template method to check if a map contains any of a list of keys. The keys are provided using start/end pointers (iterator). Included are a number of test cases to exercise the new code and validate the implementation. There is a need for these changes (in stacked commits). Reviewed By: wez Differential Revision: D6203377 fbshipit-source-id: 9afe0f0cf8a4d4a82e94009c53a500a67a159976 --- MapUtil.h | 11 +++++++ tests/MapUtilTest.cpp | 67 ++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 77 insertions(+), 1 deletion(-) diff --git a/MapUtil.h b/MapUtil.h index fd564967b313..9c965f1ef0c1 100644 --- a/MapUtil.h +++ b/MapUtil.h @@ -30,6 +30,17 @@ bool mapContainsAny(const Map& map, const Key& firstKey, Args... args) { return mapContainsAny(map, firstKey) || mapContainsAny(map, args...); } +// Returns true if the map contains any of a list of passed keys +template +bool mapContainsAnyOf(const Map& map, Iterator first, Iterator last) { + for (auto it = first; it != last; ++it) { + if (map.find(*it) != map.end()) { + return true; + } + } + return false; +} + // Returns Map[Key] or if it isn't present, returns a default value. // if the default isn't specified, returns a default-constructed value. template diff --git a/tests/MapUtilTest.cpp b/tests/MapUtilTest.cpp index e3f1e81e1dc2..e94310e368f0 100644 --- a/tests/MapUtilTest.cpp +++ b/tests/MapUtilTest.cpp @@ -9,6 +9,7 @@ #include "thirdparty/tap.h" using watchman::mapContainsAny; +using watchman::mapContainsAnyOf; void test_map_contains_any() { std::unordered_map uMap = { @@ -48,9 +49,73 @@ void test_map_contains_any() { ok(!mapContainsAny(eMap, "xcase1"), "mapContainsAny absent on empty mape"); } +void test_map_contains_any_of() { + std::unordered_map uMap = { + {"one", 1}, {"two", 2}, {"three", 3}}; + + { + // Using iterator to do the lookup + std::unordered_set kSet = {"one"}; + ok(mapContainsAnyOf(uMap, kSet.begin(), kSet.end()), + "mapContainsAnyOf single string present"); + + kSet.emplace("two"); + ok(mapContainsAnyOf(uMap, kSet.begin(), kSet.end()), + "mapContainsAnyOf two strings present"); + + kSet.emplace("three"); + ok(mapContainsAnyOf(uMap, kSet.begin(), kSet.end()), + "mapContainsAnyOf all strings present"); + } + { + std::unordered_set kSet = {"one", "xcase1", "xcase2", "xcase3"}; + ok(mapContainsAnyOf(uMap, kSet.begin(), kSet.end()), + "mapContainsAnyOf one of several strings present"); + + kSet.emplace("two"); + ok(mapContainsAnyOf(uMap, kSet.begin(), kSet.end()), + "mapContainsAnyOf two of several strings present"); + } + { + std::unordered_set kSet; + ok(!mapContainsAnyOf(uMap, kSet.begin(), kSet.end()), + "mapContainsAnyOf empty set"); + + kSet.emplace("xcase1"); + ok(!mapContainsAnyOf(uMap, kSet.begin(), kSet.end()), + "mapContainsAnyOf single string absent"); + + kSet.emplace("xcase2"); + ok(!mapContainsAnyOf(uMap, kSet.begin(), kSet.end()), + "mapContainsAnyOf two strings absent"); + + kSet.emplace("xcase3"); + ok(!mapContainsAnyOf(uMap, kSet.begin(), kSet.end()), + "mapContainsAnyOf three strings absent"); + } + + // Empty map tests + { + std::unordered_map eMap; + + std::unordered_set kSet; + ok(!mapContainsAnyOf(eMap, kSet.begin(), kSet.end()), + "mapContainsAnyOf absent on empty map and set"); + + kSet.emplace("one"); + ok(!mapContainsAnyOf(eMap, kSet.begin(), kSet.end()), + "mapContainsAnyOf absent on empty map and non-empty set"); + + kSet.emplace("two"); + ok(!mapContainsAnyOf(eMap, kSet.begin(), kSet.end()), + "mapContainsAnyOf absent on empty map and 2 item set"); + } +} + int main(int, char**) { - plan_tests(10); + plan_tests(22); test_map_contains_any(); + test_map_contains_any_of(); return exit_status(); }