Zane Shelley | 200c345 | 2019-09-26 11:46:30 -0500 | [diff] [blame] | 1 | #include <util/hei_flyweight.hpp> |
| 2 | |
| 3 | #include "gtest/gtest.h" |
| 4 | |
| 5 | using namespace libhei; |
| 6 | |
| 7 | class Foo |
| 8 | { |
| 9 | public: |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 10 | explicit Foo(int i) : iv_i(i) {} |
Zane Shelley | 7f7a42d | 2019-10-28 13:28:31 -0500 | [diff] [blame] | 11 | |
Zane Shelley | 7f7a42d | 2019-10-28 13:28:31 -0500 | [diff] [blame] | 12 | 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 Shelley | c477199 | 2019-10-28 22:01:49 -0500 | [diff] [blame] | 19 | return iv_i < i_r.iv_i; |
Zane Shelley | 7f7a42d | 2019-10-28 13:28:31 -0500 | [diff] [blame] | 20 | } |
| 21 | |
Zane Shelley | 200c345 | 2019-09-26 11:46:30 -0500 | [diff] [blame] | 22 | private: |
| 23 | int iv_i = 0; |
| 24 | }; |
| 25 | |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 26 | TEST(FlyweightTest, TestSet1) |
Zane Shelley | 200c345 | 2019-09-26 11:46:30 -0500 | [diff] [blame] | 27 | { |
Zane Shelley | c11f23c | 2020-05-11 12:14:41 -0500 | [diff] [blame] | 28 | auto& foo_factory = Flyweight<Foo>::getSingleton(); |
Zane Shelley | 200c345 | 2019-09-26 11:46:30 -0500 | [diff] [blame] | 29 | |
Zane Shelley | c11f23c | 2020-05-11 12:14:41 -0500 | [diff] [blame] | 30 | auto f1 = foo_factory.get(1); |
| 31 | auto f2 = foo_factory.get(2); |
| 32 | auto f3 = foo_factory.get(1); // same as f1 |
Zane Shelley | 200c345 | 2019-09-26 11:46:30 -0500 | [diff] [blame] | 33 | |
Zane Shelley | c11f23c | 2020-05-11 12:14:41 -0500 | [diff] [blame] | 34 | ASSERT_NE(f1, f2); // Pointing to different objects |
| 35 | ASSERT_EQ(f1, f3); // Pointing to the same object |
| 36 | |
| 37 | ASSERT_EQ(2, foo_factory.size()); // Only two entries in the flyweight |
Zane Shelley | 200c345 | 2019-09-26 11:46:30 -0500 | [diff] [blame] | 38 | } |