Added Flyweight utility class
Signed-off-by: Zane Shelley <zshelle@us.ibm.com>
Change-Id: I30ca984f914d782174292a1c673f62a60f506f7b
diff --git a/test/flyweight_test.cpp b/test/flyweight_test.cpp
new file mode 100644
index 0000000..1927360
--- /dev/null
+++ b/test/flyweight_test.cpp
@@ -0,0 +1,49 @@
+#include <util/hei_flyweight.hpp>
+
+#include "gtest/gtest.h"
+
+using namespace libhei;
+
+class Foo
+{
+ public:
+ Foo() = default;
+ explicit Foo( int i ) : iv_i(i) {}
+ int get() const { return iv_i; }
+ bool operator==( const Foo & i_r ) const { return iv_i == i_r.iv_i; }
+ bool operator<( const Foo & i_r ) const { return iv_i < i_r.iv_i; }
+ private:
+ int iv_i = 0;
+};
+
+Flyweight<Foo> factory {};
+
+Foo & addFoo( int i )
+{
+ Foo f { i };
+ return factory.get( f );
+}
+
+TEST( FlyweightTest, TestSet1 )
+{
+ // Add some unique entries in a random order and keep track of where those
+ // enties exist in memory.
+ Foo * a[5];
+ a[1] = &(addFoo(1));
+ a[2] = &(addFoo(2));
+ a[0] = &(addFoo(0));
+ a[4] = &(addFoo(4));
+ a[3] = &(addFoo(3));
+
+ // Now add more entries and verify the 'new' entries match the same
+ // addresses as the previously added entries.
+ for ( int i = 4; i >= 0; i-- )
+ {
+ ASSERT_EQ( a[i], &(addFoo(i)) );
+ }
+
+ // At this point, we have proven that duplicate entries will return
+ // references to the original unique entries. There is probably more we can
+ // do here, but this is enough to prove the Flyweight class follows the
+ // flyweight design pattern.
+}
diff --git a/test/meson.build b/test/meson.build
index a4e13ea..cdf8812 100644
--- a/test/meson.build
+++ b/test/meson.build
@@ -5,7 +5,10 @@
test_src = ['../src/util/hei_bit_string.cpp']
# build g-test framework unit tests
-gtests = ['bit_string_test']
+gtests = [
+ 'bit_string_test',
+ 'flyweight_test',
+]
gtest = dependency('gtest', main : true, required : false, method : 'system')