blob: 9ed9eb6d3cb9b170664cfc1acb2d7fc93872fc9e [file] [log] [blame]
Zane Shelley200c3452019-09-26 11:46:30 -05001#include <util/hei_flyweight.hpp>
2
3#include "gtest/gtest.h"
4
5using namespace libhei;
6
7class Foo
8{
9 public:
Zane Shelley83da2452019-10-25 15:45:34 -050010 explicit Foo(int i) : iv_i(i) {}
Zane Shelley7f7a42d2019-10-28 13:28:31 -050011
Zane Shelley7f7a42d2019-10-28 13:28:31 -050012 bool operator==(const Foo& i_r) const
13 {
14 return iv_i == i_r.iv_i;
15 }
16
17 bool operator<(const Foo& i_r) const
18 {
Zane Shelleyc4771992019-10-28 22:01:49 -050019 return iv_i < i_r.iv_i;
Zane Shelley7f7a42d2019-10-28 13:28:31 -050020 }
21
Zane Shelley200c3452019-09-26 11:46:30 -050022 private:
23 int iv_i = 0;
24};
25
Zane Shelley83da2452019-10-25 15:45:34 -050026TEST(FlyweightTest, TestSet1)
Zane Shelley200c3452019-09-26 11:46:30 -050027{
Zane Shelleyc11f23c2020-05-11 12:14:41 -050028 auto& foo_factory = Flyweight<Foo>::getSingleton();
Zane Shelley200c3452019-09-26 11:46:30 -050029
Zane Shelleyc11f23c2020-05-11 12:14:41 -050030 auto f1 = foo_factory.get(1);
31 auto f2 = foo_factory.get(2);
32 auto f3 = foo_factory.get(1); // same as f1
Zane Shelley200c3452019-09-26 11:46:30 -050033
Zane Shelleyc11f23c2020-05-11 12:14:41 -050034 ASSERT_NE(f1, f2); // Pointing to different objects
35 ASSERT_EQ(f1, f3); // Pointing to the same object
36
Ben Tyner6223ab52021-03-02 13:24:57 -060037 ASSERT_EQ(size_t(2),
38 foo_factory.size()); // Only two entries in the flyweight
Zane Shelley200c3452019-09-26 11:46:30 -050039}