Advertisement
cepxuozab

CacheTest

Sep 28th, 2024
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.13 KB | None | 0 0
  1. using namespace std;
  2. struct ObjectFactory2 {
  3.    
  4.     int count = 0;
  5.  
  6.     ObjectFactory2(int count) : count(count) {
  7.     }
  8.  
  9.     ObjectFactory2() = default;
  10.  
  11.     ObjectPtr operator()(std::string id) {
  12.         id += "_"s + to_string(count);
  13.         ++count;
  14.         return std::make_shared<Object>(std::move(id));
  15.     }
  16. };
  17. void CacheTest() {
  18.     using namespace std;
  19.     {
  20.         Cache<string, Object, ObjectFactory2> cache;
  21.  
  22.         auto alice1 = cache.GetValue("Alice"s);
  23.         auto bob = cache.GetValue("Bob"s);
  24.         assert(alice1 != bob);
  25.         assert(alice1->GetId() == "Alice_0"s);
  26.         assert(bob->GetId() == "Bob_1"s);
  27.  
  28.         auto alice2 = cache.GetValue("Alice"s);
  29.         assert(alice1== alice2);
  30.     } {
  31.         ObjectFactory2 fc(10);
  32.         Cache<string, Object, ObjectFactory2> cache(fc);
  33.  
  34.         auto alice1 = cache.GetValue("Alice"s);
  35.         auto bob = cache.GetValue("Bob"s);
  36.         assert(alice1 != bob);
  37.         assert(alice1->GetId() == "Alice_10"s);
  38.         assert(bob->GetId()== "Bob_11"s);
  39.  
  40.         auto alice2 = cache.GetValue("Alice"s);
  41.         assert(alice1 == alice2);
  42.     }
  43. }
  44.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement