6#include <unordered_map>
8#include "testing/testing.h"
24TEST(map, DefaultConstructor)
64TEST(map, ItemsConstructorDuplicates)
66 Map<int, int> map = {{1, 2}, {3, 4}, {1, 4}, {2, 5}, {2, 6}};
117 for (
int i = 0; i < 100; i++) {
146 std::optional<int> value = map.
pop_try(4);
179 for (
int i = 0; i < 100; i++) {
182 for (
int i = 25; i < 80; i++) {
185 for (
int i = 0; i < 100; i++) {
200 for (
float value : map.
values()) {
221 for (
int key : map.
keys()) {
244 for (
auto item : const_map.
items()) {
246 values.
add(item.value);
265 for (
int &value : map.
values()) {
279 for (
auto item : map.
items()) {
280 item.value += item.key;
287TEST(map, MutableItemToItemConversion)
296 values.
append(item.value);
312TEST(map, LookupOrAddCB_SeparateFunction)
321TEST(map, LookupOrAddCB_Lambdas)
324 auto lambda1 = []() {
return 11.0f; };
326 auto lambda2 = []() {
return 20.0f; };
336 auto create_func = [](
float *value) {
340 auto modify_func = [](
float *value) {
353 auto create_func = [](std::unique_ptr<int> *value) ->
int & {
354 new (value) std::unique_ptr<int>(
new int{10});
357 auto modify_func = [](std::unique_ptr<int> *value) ->
int & {
426 for (
int i = 0; i < 100; i++) {
443 map2 = std::move(map1);
484 auto value1 = std::make_unique<int>();
485 auto value2 = std::make_unique<int>();
486 auto value3 = std::make_unique<int>();
488 int *value1_ptr = value1.get();
491 map.
add_new(1, std::move(value1));
492 map.
add(2, std::move(value2));
495 map.
add_new(5, std::make_unique<int>());
496 map.
add(6, std::make_unique<int>());
539 map.
add(
"45",
"643");
563TEST(map, CopyConstructorExceptions)
569 map.lookup(2).throw_during_copy =
true;
570 EXPECT_ANY_THROW({ MapType map_copy(map); });
573TEST(map, MoveConstructorExceptions)
579 map.lookup(1).throw_during_move =
true;
580 EXPECT_ANY_THROW({ MapType map_moved(std::move(map)); });
590 EXPECT_ANY_THROW({ map.
add_new(key1, value1); });
595 EXPECT_ANY_THROW({ map.
add_new(key2, value2); });
604 map.
lookup(2).throw_during_move =
true;
605 EXPECT_ANY_THROW({ map.
reserve(100); });
614 map.
lookup(3).throw_during_move =
true;
615 EXPECT_ANY_THROW({ map.
pop(3); });
621TEST(map, AddOrModifyExceptions)
626 EXPECT_ANY_THROW({ map.
add_or_modify(3, create_fn, modify_fn); });
641 map.
add(TestEnum::A, 4);
642 map.
add(TestEnum::B, 6);
647 map.
lookup(TestEnum::D) = 10;
671 map.
add_as(3,
"hello", 2);
677TEST(map, RemoveDuringIteration)
691 for (Iter iter =
begin; iter != end; ++iter) {
693 if (item.
value == 2) {
710 const int64_t removed = map.
remove_if([](
auto item) {
return item.key > 100; });
738 map.
add({1, 2, 3}, 100);
739 map.
add({3, 2, 1}, 200);
746 std::array<int, 3>
array = {1, 2, 3};
772 std::string value =
"a";
773 bool value_checked =
false;
776 value_checked =
true;
797 return a->
value ==
b->value;
824template<
typename MapT>
829 for (
int i = 0; i < amount; i++) {
837 for (
int value : values) {
838 map.add(value, value);
844 for (
int value : values) {
845 count += map.contains(value);
850 for (
int value : values) {
851 count += map.remove(value);
856 std::cout <<
"Count: " << count <<
"\n";
863template<
typename Key,
typename Value>
class StdUnorderedMapWrapper {
865 using MapType = std::unordered_map<Key, Value, DefaultHash<Key>>;
874 bool is_empty()
const
884 template<
typename ForwardKey,
typename... ForwardValue>
885 void add_new(ForwardKey &&key, ForwardValue &&...value)
887 map_.insert({std::forward<ForwardKey>(key),
Value(std::forward<ForwardValue>(value)...)});
890 template<
typename ForwardKey,
typename... ForwardValue>
891 bool add(ForwardKey &&key, ForwardValue &&...value)
894 .insert({std::forward<ForwardKey>(key),
Value(std::forward<ForwardValue>(value)...)})
900 return map_.find(key) != map_.end();
903 bool remove(
const Key &key)
905 return bool(map_.erase(key));
910 return map_.find(key)->second;
915 return map_.find(key)->second;
923 void print_stats(StringRef =
"")
const {}
928 for (
int i = 0; i < 3; i++) {
929 benchmark_random_ints<Map<int, int>>(
"Map ", 1000000, 1);
930 benchmark_random_ints<StdUnorderedMapWrapper<int, int>>(
"std::unordered_map", 1000000, 1);
933 for (
int i = 0; i < 3; i++) {
935 benchmark_random_ints<Map<int, int>>(
"Map ", 1000000, factor);
936 benchmark_random_ints<StdUnorderedMapWrapper<int, int>>(
"std::unordered_map", 1000000, factor);
#define SCOPED_TIMER(name)
constexpr IndexRange()=default
SubIterator begin() const
const Value * lookup_ptr(const Key &key) const
Value pop(const Key &key)
std::optional< Value > lookup_try(const Key &key) const
std::optional< Value > pop_try(const Key &key)
Value & lookup_or_add_default(const Key &key)
const Key & lookup_key_as(const ForwardKey &key) const
bool remove_as(const ForwardKey &key)
bool add_overwrite(const Key &key, const Value &value)
ValueIterator values() const &
bool add(const Key &key, const Value &value)
const Value & lookup(const Key &key) const
const Key * lookup_key_ptr_as(const ForwardKey &key) const
Value & lookup_or_add_cb(const Key &key, const CreateValueF &create_value)
void foreach_item(const FuncT &func) const
const Key & lookup_key(const Key &key) const
bool add_as(ForwardKey &&key, ForwardValue &&...value)
const Key * lookup_key_ptr(const Key &key) const
void add_new(const Key &key, const Value &value)
const Value & lookup_as(const ForwardKey &key) const
bool remove(const Key &key)
KeyIterator keys() const &
bool contains(const Key &key) const
auto add_or_modify(const Key &key, const CreateValueF &create_value, const ModifyValueF &modify_value) -> decltype(create_value(nullptr))
int64_t remove_if(Predicate &&predicate)
Value pop_default(const Key &key, const Value &default_value)
ItemIterator items() const &
Value & lookup_or_add(const Key &key, const Value &value)
bool contains(const Key &key) const
bool contains(const T &value) const
void append(const T &value)
int64_t first_index_of(const T &value) const
MutableSpan< T > as_mutable_span()
#define EXPECT_FALSE(result)
#define EXPECT_NE(result, expect)
#define EXPECT_TRUE(result)
bool contains(const VArray< bool > &varray, const IndexMask &indices_to_check, bool value)
void add(Depsgraph &depsgraph, std::function< void()> fn)
int size(const AssetLibraryReference *library_reference)
void add_new(const MultiFunction &fn)
const MultiFunction & lookup(UString id)
TEST(any_virtual, DefaultConstructor)
unsigned __int64 uint64_t
BLI_INLINE float D(const float *data, const int res[3], int x, int y, int z)
Vector< nodes::ItemDeclarationPtr >::const_iterator ItemIterator
ATTR_WARN_UNUSED_RESULT const BMVert * v
struct RNG * BLI_rng_new(unsigned int seed)
void BLI_rng_free(struct RNG *rng) ATTR_NONNULL(1)
EXPECT_EQ(BLI_expr_pylike_eval(expr, nullptr, 0, &result), EXPR_PYLIKE_INVALID)
void int BLI_rng_get_int(struct RNG *rng) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
constexpr uint64_t get_default_hash(const T &v, const Args &...args)
bool operator()(const IntMapKey *a, const IntMapKey *b) const
uint64_t operator()(const IntMapKey *key) const