6#include <unordered_map>
8#include "testing/testing.h"
21TEST(map, DefaultConstructor)
25 EXPECT_TRUE(map.is_empty());
32 EXPECT_TRUE(map.is_empty());
35 EXPECT_FALSE(map.is_empty());
38 EXPECT_FALSE(map.is_empty());
44 EXPECT_FALSE(map.contains(4));
46 EXPECT_FALSE(map.contains(4));
48 EXPECT_TRUE(map.contains(4));
60TEST(map, LookupNotExisting)
72 for (
int i = 0; i < 100; i++) {
83 EXPECT_TRUE(map.contains(2));
84 EXPECT_TRUE(map.contains(1));
87 EXPECT_TRUE(map.contains(2));
88 EXPECT_FALSE(map.contains(1));
91 EXPECT_FALSE(map.contains(2));
92 EXPECT_FALSE(map.contains(1));
101 std::optional<int> value = map.pop_try(4);
103 EXPECT_FALSE(value.has_value());
104 value = map.pop_try(2);
106 EXPECT_TRUE(value.has_value());
134 for (
int i = 0; i < 100; i++) {
137 for (
int i = 25; i < 80; i++) {
140 for (
int i = 0; i < 100; i++) {
141 EXPECT_EQ(map.contains(i), i < 25 || i >= 80);
155 for (
float value : map.values()) {
161 EXPECT_TRUE(values.contains(5.0f));
162 EXPECT_TRUE(values.contains(-2.0f));
163 EXPECT_TRUE(values.contains(2.0f));
176 for (
int key : map.keys()) {
199 for (
auto item : const_map.
items()) {
201 values.add(item.value);
209 EXPECT_TRUE(values.contains(3.0f));
210 EXPECT_TRUE(values.contains(9.0f));
211 EXPECT_TRUE(values.contains(0.0f));
220 for (
int &value : map.values()) {
234 for (
auto item : map.items()) {
235 item.value += item.key;
242TEST(map, MutableItemToItemConversion)
251 values.append(item.value);
258 EXPECT_TRUE(values.contains(6));
259 EXPECT_TRUE(values.contains(1));
267TEST(map, LookupOrAddCB_SeparateFunction)
276TEST(map, LookupOrAddCB_Lambdas)
279 auto lambda1 = []() {
return 11.0f; };
280 EXPECT_EQ(map.lookup_or_add_cb(0, lambda1), 11.0f);
281 auto lambda2 = []() {
return 20.0f; };
282 EXPECT_EQ(map.lookup_or_add_cb(1, lambda2), 20.0f);
284 EXPECT_EQ(map.lookup_or_add_cb(0, lambda2), 11.0f);
285 EXPECT_EQ(map.lookup_or_add_cb(1, lambda1), 20.0f);
295 auto modify_func = [](
float *value) {
299 EXPECT_TRUE(map.add_or_modify(1,
create_func, modify_func));
301 EXPECT_FALSE(map.add_or_modify(1,
create_func, modify_func));
308 auto create_func = [](std::unique_ptr<int> *value) ->
int & {
309 new (value) std::unique_ptr<int>(
new int{10});
312 auto modify_func = [](std::unique_ptr<int> *value) ->
int & {
317 int &a = map.add_or_modify(1,
create_func, modify_func);
326 EXPECT_FALSE(map.contains(3));
327 EXPECT_TRUE(map.add_overwrite(3, 6.0f));
329 EXPECT_FALSE(map.add_overwrite(3, 7.0f));
331 EXPECT_FALSE(map.add(3, 8.0f));
340 map.lookup_or_add_default(5) = 2;
342 map.lookup_or_add_default(3) += 4;
351 map.lookup_or_add(6, 4) += 10;
371 for (
int i = 0; i < 100; i++) {
388 map2 = std::move(map1);
417 EXPECT_TRUE(map.contains(1));
418 EXPECT_TRUE(map.contains(2));
423 EXPECT_FALSE(map.contains(1));
424 EXPECT_FALSE(map.contains(2));
429 auto value1 = std::make_unique<int>();
430 auto value2 = std::make_unique<int>();
431 auto value3 = std::make_unique<int>();
433 int *value1_ptr = value1.get();
436 map.
add_new(1, std::move(value1));
437 map.add(2, std::move(value2));
438 map.add_overwrite(3, std::move(value3));
439 map.lookup_or_add_cb(4, []() {
return std::make_unique<int>(); });
440 map.add_new(5, std::make_unique<int>());
441 map.add(6, std::make_unique<int>());
442 map.add_overwrite(7, std::make_unique<int>());
443 map.lookup_or_add(8, std::make_unique<int>());
444 map.pop_default(9, std::make_unique<int>());
446 EXPECT_EQ(map.lookup(1).get(), value1_ptr);
455 EXPECT_FALSE(map.remove(3));
457 EXPECT_TRUE(map.remove(2));
466 EXPECT_TRUE(map.add(&a, 5));
467 EXPECT_FALSE(map.add(&a, 4));
471 EXPECT_TRUE(map.remove(&
b));
472 EXPECT_TRUE(map.add(&
b, 8));
473 EXPECT_FALSE(map.remove(&d));
474 EXPECT_TRUE(map.remove(&a));
475 EXPECT_TRUE(map.remove(&
b));
476 EXPECT_TRUE(map.remove(&c));
477 EXPECT_TRUE(map.is_empty());
484 map.add(
"45",
"643");
485 EXPECT_TRUE(map.contains(
"45"));
486 EXPECT_FALSE(map.contains(
"54"));
497 map.foreach_item([&](
int key,
int value) {
499 values.append(value);
508TEST(map, CopyConstructorExceptions)
514 map.lookup(2).throw_during_copy =
true;
515 EXPECT_ANY_THROW({ MapType map_copy(map); });
518TEST(map, MoveConstructorExceptions)
524 map.lookup(1).throw_during_move =
true;
525 EXPECT_ANY_THROW({ MapType map_moved(std::move(map)); });
535 EXPECT_ANY_THROW({ map.add_new(key1, value1); });
540 EXPECT_ANY_THROW({ map.add_new(key2, value2); });
549 map.lookup(2).throw_during_move =
true;
550 EXPECT_ANY_THROW({ map.reserve(100); });
559 map.lookup(3).throw_during_move =
true;
560 EXPECT_ANY_THROW({ map.pop(3); });
566TEST(map, AddOrModifyExceptions)
571 EXPECT_ANY_THROW({ map.add_or_modify(3, create_fn, modify_fn); });
586 map.
add(TestEnum::A, 4);
587 map.add(TestEnum::B, 6);
591 EXPECT_FALSE(map.contains(TestEnum::C));
592 map.lookup(TestEnum::D) = 10;
604 EXPECT_TRUE(std::any_of(map.keys().begin(), map.keys().end(), [](
int v) { return v == 1; }));
605 EXPECT_TRUE(std::any_of(map.values().begin(), map.values().end(), [](
int v) { return v == 1; }));
606 EXPECT_TRUE(std::any_of(
607 map.items().begin(), map.items().end(), [](
auto item) { return item.value == 1; }));
608 EXPECT_EQ(std::count(map.values().begin(), map.values().end(), 2), 2);
609 EXPECT_EQ(std::count(map.values().begin(), map.values().end(), 4), 1);
610 EXPECT_EQ(std::count(map.keys().begin(), map.keys().end(), 7), 1);
616 map.
add_as(3,
"hello", 2);
617 map.add_as(2,
"test", 1);
622TEST(map, RemoveDuringIteration)
634 Iter begin = map.items().
begin();
635 Iter end = map.items().
end();
636 for (Iter iter = begin; iter != end; ++iter) {
638 if (item.
value == 2) {
655 const int64_t removed = map.remove_if([](
auto item) {
return item.key > 100; });
662 EXPECT_FALSE(map.contains(i * i));
675 EXPECT_EQ(map.lookup_key_ptr_as(
"d"),
nullptr);
676 EXPECT_EQ(map.lookup_key_ptr_as(
"b")->size(), 1);
677 EXPECT_EQ(map.lookup_key_ptr(
"a"), map.lookup_key_ptr_as(
"a"));
683 map.
add({1, 2, 3}, 100);
684 map.add({3, 2, 1}, 200);
689 EXPECT_FALSE(map.contains({1, 2}));
691 std::array<int, 3>
array = {1, 2, 3};
694 map.remove_as(
Vector<int>({1, 2, 3}).as_mutable_span());
718template<
typename MapT>
723 for (
int i = 0; i < amount; i++) {
731 for (
int value : values) {
732 map.add(value, value);
738 for (
int value : values) {
739 count += map.contains(value);
744 for (
int value : values) {
745 count += map.remove(value);
750 std::cout <<
"Count: " <<
count <<
"\n";
757template<
typename Key,
typename Value>
class StdUnorderedMapWrapper {
759 using MapType = std::unordered_map<Key, Value, blender::DefaultHash<Key>>;
768 bool is_empty()
const
778 template<
typename ForwardKey,
typename... ForwardValue>
779 void add_new(ForwardKey &&key, ForwardValue &&...value)
781 map_.insert({std::forward<ForwardKey>(key),
Value(std::forward<ForwardValue>(value)...)});
784 template<
typename ForwardKey,
typename... ForwardValue>
785 bool add(ForwardKey &&key, ForwardValue &&...value)
788 .insert({std::forward<ForwardKey>(key),
Value(std::forward<ForwardValue>(value)...)})
792 bool contains(
const Key &key)
const
794 return map_.find(key) != map_.end();
797 bool remove(
const Key &key)
799 return bool(map_.erase(key));
804 return map_.find(key)->second;
807 const Value &
lookup(
const Key &key)
const
809 return map_.find(key)->second;
817 void print_stats(StringRef =
"")
const {}
822 for (
int i = 0; i < 3; i++) {
823 benchmark_random_ints<blender::Map<int, int>>(
"blender::Map ", 1000000, 1);
824 benchmark_random_ints<blender::StdUnorderedMapWrapper<int, int>>(
825 "std::unordered_map", 1000000, 1);
828 for (
int i = 0; i < 3; i++) {
830 benchmark_random_ints<blender::Map<int, int>>(
"blender::Map ", 1000000, factor);
831 benchmark_random_ints<blender::StdUnorderedMapWrapper<int, int>>(
832 "std::unordered_map", 1000000, factor);
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)
struct RNG * BLI_rng_new(unsigned int seed)
void BLI_rng_free(struct RNG *rng) ATTR_NONNULL(1)
#define SCOPED_TIMER(name)
Group Output data from inside of a node group A color picker Mix two input colors RGB to Convert a color s luminance to a grayscale value Generate a normal vector and a dot product Brightness Control the brightness and contrast of the input color Vector Map input vector components with curves Camera Retrieve information about the camera and how it relates to the current shading point s position Clamp a value between a minimum and a maximum Vector Perform vector math operation Invert Invert a producing a negative Combine Generate a color from its and blue Hue Saturation Value
ATTR_WARN_UNUSED_RESULT const BMVert * v
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
SubIterator begin() const
const Value * lookup_ptr(const Key &key) const
Value & lookup_or_add_default(const Key &key)
bool add(const Key &key, const Value &value)
const Value & lookup(const Key &key) const
bool add_as(ForwardKey &&key, ForwardValue &&...value)
void add_new(const Key &key, const Value &value)
ItemIterator items() const
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
local_group_size(16, 16) .push_constant(Type b
static void clear(Message &msg)
static void add(blender::Map< std::string, std::string > &messages, Message &msg)
GAttributeReader lookup(const void *owner, const StringRef attribute_id)
TEST(any, DefaultConstructor)
static PyObject * create_func(PyObject *, PyObject *args)