Skip to content

Commit

Permalink
Add utility method and tests
Browse files Browse the repository at this point in the history
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
  • Loading branch information
eamonnkent authored and facebook-github-bot committed Nov 6, 2017
1 parent c651e59 commit 5098250
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 1 deletion.
11 changes: 11 additions & 0 deletions MapUtil.h
Expand Up @@ -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 <typename Map, typename Iterator>
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 <class Map, typename Key = typename Map::key_type>
Expand Down
67 changes: 66 additions & 1 deletion tests/MapUtilTest.cpp
Expand Up @@ -9,6 +9,7 @@
#include "thirdparty/tap.h"

using watchman::mapContainsAny;
using watchman::mapContainsAnyOf;

void test_map_contains_any() {
std::unordered_map<w_string, int> uMap = {
Expand Down Expand Up @@ -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<w_string, int> uMap = {
{"one", 1}, {"two", 2}, {"three", 3}};

{
// Using iterator to do the lookup
std::unordered_set<w_string> 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<w_string> 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<w_string> 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<w_string, w_string> eMap;

std::unordered_set<w_string> 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();
}

0 comments on commit 5098250

Please sign in to comment.