Blender V4.3
BLI_memory_cache_test.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2024 Blender Authors
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#include "BLI_hash.hh"
6#include "BLI_memory_cache.hh"
8
9#include "testing/testing.h"
10
11#include "BLI_strict_flags.h" /* Keep last. */
12
14
15class GenericIntKey : public GenericKey {
16 private:
17 int value_;
18
19 public:
20 GenericIntKey(int value) : value_(value) {}
21
22 uint64_t hash() const override
23 {
24 return get_default_hash(value_);
25 }
26
27 bool equal_to(const GenericKey &other) const override
28 {
29 if (const auto *other_typed = dynamic_cast<const GenericIntKey *>(&other)) {
30 return other_typed->value_ == value_;
31 }
32 return false;
33 }
34
35 std::unique_ptr<GenericKey> to_storable() const override
36 {
37 return std::make_unique<GenericIntKey>(*this);
38 }
39};
40
42 public:
43 int value;
44
45 CachedInt(int initial_value) : value(initial_value) {}
46
47 void count_memory(MemoryCounter &memory) const override
48 {
49 memory.add(sizeof(int));
50 }
51};
52
53TEST(memory_cache, Simple)
54{
56 {
57 bool newly_computed = false;
59 newly_computed = true;
60 return std::make_unique<CachedInt>(4);
61 })->value);
62 EXPECT_TRUE(newly_computed);
63 }
64 {
65 bool newly_computed = false;
67 newly_computed = true;
68 return std::make_unique<CachedInt>(4);
69 })->value);
70 EXPECT_FALSE(newly_computed);
71 }
73 {
74 bool newly_computed = false;
76 newly_computed = true;
77 return std::make_unique<CachedInt>(4);
78 })->value);
79 EXPECT_TRUE(newly_computed);
80 }
81}
82
83} // namespace blender::memory_cache::tests
EXPECT_EQ(BLI_expr_pylike_eval(expr, nullptr, 0, &result), EXPR_PYLIKE_INVALID)
void count_memory(MemoryCounter &memory) const override
bool equal_to(const GenericKey &other) const override
std::unique_ptr< GenericKey > to_storable() const override
std::shared_ptr< const T > get(const GenericKey &key, FunctionRef< std::unique_ptr< T >()> compute_fn)
uint64_t get_default_hash(const T &v)
Definition BLI_hash.hh:219
unsigned __int64 uint64_t
Definition stdint.h:90