mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-23 01:21:42 +00:00
@@ -11,9 +11,13 @@
|
||||
#endif
|
||||
|
||||
#include "wpi/DenseMap.h"
|
||||
#include "wpi/DenseMapInfo.h"
|
||||
#include "gmock/gmock.h"
|
||||
#include "gtest/gtest.h"
|
||||
#include <map>
|
||||
#include <set>
|
||||
#include <utility>
|
||||
#include <variant>
|
||||
|
||||
using namespace wpi;
|
||||
|
||||
@@ -446,6 +450,7 @@ TEST(DenseMapCustomTest, InitFromIterator) {
|
||||
std::vector<std::pair<int, CountCopyAndMove>> Values;
|
||||
// The size is a random value greater than 64 (hardcoded DenseMap min init)
|
||||
const int Count = 65;
|
||||
Values.reserve(Count);
|
||||
for (int i = 0; i < Count; i++)
|
||||
Values.emplace_back(i, CountCopyAndMove());
|
||||
|
||||
@@ -586,6 +591,15 @@ TEST(DenseMapCustomTest, LargeSmallDenseMapCompaction) {
|
||||
EXPECT_TRUE(map.find(0) == map.end());
|
||||
}
|
||||
|
||||
TEST(DenseMapCustomTest, SmallDenseMapWithNumBucketsNonPowerOf2) {
|
||||
// Is not power of 2.
|
||||
const unsigned NumInitBuckets = 33;
|
||||
// Power of 2 less then NumInitBuckets.
|
||||
constexpr unsigned InlineBuckets = 4;
|
||||
// Constructor should not trigger assert.
|
||||
SmallDenseMap<int, int, InlineBuckets> map(NumInitBuckets);
|
||||
}
|
||||
|
||||
TEST(DenseMapCustomTest, TryEmplaceTest) {
|
||||
DenseMap<int, std::unique_ptr<int>> Map;
|
||||
std::unique_ptr<int> P(new int(2));
|
||||
@@ -648,7 +662,7 @@ struct B : public A {
|
||||
|
||||
namespace wpi {
|
||||
template <typename T>
|
||||
struct DenseMapInfo<T, std::enable_if_t<std::is_base_of<A, T>::value>> {
|
||||
struct DenseMapInfo<T, std::enable_if_t<std::is_base_of_v<A, T>>> {
|
||||
static inline T getEmptyKey() { return {static_cast<int>(~0)}; }
|
||||
static inline T getTombstoneKey() { return {static_cast<int>(~0U - 1)}; }
|
||||
static unsigned getHashValue(const T &Val) { return Val.value; }
|
||||
@@ -677,4 +691,18 @@ TEST(DenseMapCustomTest, SFINAEMapInfo) {
|
||||
EXPECT_EQ(Map.find(Keys[1]), Map.end());
|
||||
EXPECT_EQ(Map.find(Keys[2]), Map.end());
|
||||
}
|
||||
|
||||
TEST(DenseMapCustomTest, VariantSupport) {
|
||||
using variant = std::variant<int, int>;
|
||||
DenseMap<variant, int> Map;
|
||||
variant Keys[] = {
|
||||
variant(std::in_place_index<0>, 1),
|
||||
variant(std::in_place_index<1>, 1),
|
||||
};
|
||||
Map.try_emplace(Keys[0], 0);
|
||||
Map.try_emplace(Keys[1], 1);
|
||||
EXPECT_THAT(Map, testing::SizeIs(2));
|
||||
EXPECT_NE(DenseMapInfo<variant>::getHashValue(Keys[0]),
|
||||
DenseMapInfo<variant>::getHashValue(Keys[1]));
|
||||
}
|
||||
} // namespace
|
||||
|
||||
Reference in New Issue
Block a user