Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using namespace std;
- struct ObjectFactory2 {
- int count = 0;
- ObjectFactory2(int count) : count(count) {
- }
- ObjectFactory2() = default;
- ObjectPtr operator()(std::string id) {
- id += "_"s + to_string(count);
- ++count;
- return std::make_shared<Object>(std::move(id));
- }
- };
- void CacheTest() {
- using namespace std;
- {
- Cache<string, Object, ObjectFactory2> cache;
- auto alice1 = cache.GetValue("Alice"s);
- auto bob = cache.GetValue("Bob"s);
- assert(alice1 != bob);
- assert(alice1->GetId() == "Alice_0"s);
- assert(bob->GetId() == "Bob_1"s);
- auto alice2 = cache.GetValue("Alice"s);
- assert(alice1== alice2);
- } {
- ObjectFactory2 fc(10);
- Cache<string, Object, ObjectFactory2> cache(fc);
- auto alice1 = cache.GetValue("Alice"s);
- auto bob = cache.GetValue("Bob"s);
- assert(alice1 != bob);
- assert(alice1->GetId() == "Alice_10"s);
- assert(bob->GetId()== "Bob_11"s);
- auto alice2 = cache.GetValue("Alice"s);
- assert(alice1 == alice2);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement