From f86c2ebb3c6f878d65ef32217adc956057ac9649 Mon Sep 17 00:00:00 2001 From: Gil Date: Wed, 2 May 2018 13:32:00 -0700 Subject: Add Immutable SortedSet in C++ (#1206) * Add SortedSet * Add document_key_set.h * Add equality to SortedSet --- .../test/firebase/firestore/immutable/testing.h | 50 +++++++++++++++++++++- 1 file changed, 48 insertions(+), 2 deletions(-) (limited to 'Firestore/core/test/firebase/firestore/immutable/testing.h') diff --git a/Firestore/core/test/firebase/firestore/immutable/testing.h b/Firestore/core/test/firebase/firestore/immutable/testing.h index 9e839c6..8e496dd 100644 --- a/Firestore/core/test/firebase/firestore/immutable/testing.h +++ b/Firestore/core/test/firebase/firestore/immutable/testing.h @@ -18,16 +18,33 @@ #define FIRESTORE_CORE_TEST_FIREBASE_FIRESTORE_IMMUTABLE_TESTING_H_ #include +#include +#include #include #include #include "Firestore/core/src/firebase/firestore/util/secure_random.h" +#include "absl/strings/str_cat.h" #include "gtest/gtest.h" namespace firebase { namespace firestore { namespace immutable { +template +std::string Describe(const std::pair& pair) { + return absl::StrCat("(", pair.first, ", ", pair.second, ")"); +} + +// Describes the given item by its std::to_string implementation (if +// std::to_string is defined for V). The return type is not defined directly +// in terms of std::string in order to allow specialization failure to select +// a different overload. +template +auto Describe(const V& item) -> decltype(std::to_string(item)) { + return std::to_string(item); +} + template testing::AssertionResult NotFound(const Container& map, const K& key) { if (map.contains(key)) { @@ -40,11 +57,15 @@ testing::AssertionResult NotFound(const Container& map, const K& key) { return testing::AssertionSuccess(); } else { return testing::AssertionFailure() - << "Should not have found (" << found->first << ", " << found->second - << ")"; + << "Should not have found " << Describe(*found); } } +/** + * Asserts that the given key is found in the given container and that it maps + * to the given value. This only works with map-type containers where value_type + * is `std::pair`. + */ template testing::AssertionResult Found(const Container& map, const K& key, @@ -67,6 +88,31 @@ testing::AssertionResult Found(const Container& map, } } +/** + * Asserts that the given key is found in the given container without + * necessarily checking that the key maps to any value. This also makes + * this compatible with non-mapped containers where K is the value_type. + */ +template +testing::AssertionResult Found(const Container& container, const K& key) { + if (!container.contains(key)) { + return testing::AssertionFailure() + << "Did not find key " << key << " using contains()"; + } + + auto found = container.find(key); + if (found == container.end()) { + return testing::AssertionFailure() + << "Did not find key " << key << " using find()"; + } + if (*found == key) { + return testing::AssertionSuccess(); + } else { + return testing::AssertionFailure() + << "Found entry was " << Describe(*found); + } +} + /** Creates an empty vector (for readability). */ inline std::vector Empty() { return {}; -- cgit v1.2.3