summaryrefslogtreecommitdiff
path: root/libadhocutil/unittests/testLazyPointer.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'libadhocutil/unittests/testLazyPointer.cpp')
-rw-r--r--libadhocutil/unittests/testLazyPointer.cpp77
1 files changed, 77 insertions, 0 deletions
diff --git a/libadhocutil/unittests/testLazyPointer.cpp b/libadhocutil/unittests/testLazyPointer.cpp
new file mode 100644
index 0000000..6c4ddcd
--- /dev/null
+++ b/libadhocutil/unittests/testLazyPointer.cpp
@@ -0,0 +1,77 @@
+#define BOOST_TEST_MODULE LazyPointer
+#include <boost/test/unit_test.hpp>
+
+#include <boost/bind.hpp>
+#include "intrusivePtrBase.h"
+#include "lazyPointer.h"
+
+class Test : public IntrusivePtrBase {
+ public:
+ Test(int v) :
+ val(v)
+ {
+ }
+ const int val;
+};
+
+typedef LazyPointer<Test> TestLazyPointer;
+
+static
+TestLazyPointer::pointer_type
+factory()
+{
+ return new Test(3);
+}
+
+static
+TestLazyPointer::pointer_type
+paramFactory(const std::string & str)
+{
+ return new Test(str.length());
+}
+
+BOOST_AUTO_TEST_CASE ( islazy )
+{
+ TestLazyPointer p(boost::bind(&factory));
+ BOOST_REQUIRE_EQUAL(false, p.hasValue());
+ Test * t = p.get();
+ BOOST_REQUIRE(t);
+ BOOST_REQUIRE_EQUAL(true, p.hasValue());
+ BOOST_REQUIRE_EQUAL(p, t);
+ BOOST_REQUIRE_EQUAL(3, t->val);
+ BOOST_REQUIRE_EQUAL(3, p->val);
+}
+
+BOOST_AUTO_TEST_CASE ( preinit )
+{
+ Test * t = new Test(4);
+ TestLazyPointer p(t);
+ BOOST_REQUIRE_EQUAL(true, p.hasValue());
+ BOOST_REQUIRE_EQUAL(p, t);
+ BOOST_REQUIRE_EQUAL(4, p->val);
+}
+
+BOOST_AUTO_TEST_CASE ( reset )
+{
+ Test * t = new Test(4);
+ TestLazyPointer p(t);
+ BOOST_REQUIRE_EQUAL(true, p.hasValue());
+ BOOST_REQUIRE_EQUAL(4, p->val);
+ p = nullptr;
+ BOOST_REQUIRE_EQUAL(true, p.hasValue());
+ BOOST_REQUIRE_EQUAL(true, !p);
+ p = boost::bind(&factory);
+ BOOST_REQUIRE_EQUAL(false, p.hasValue());
+ p.get();
+ BOOST_REQUIRE_EQUAL(true, p.hasValue());
+ BOOST_REQUIRE_EQUAL(3, p->val);
+}
+
+BOOST_AUTO_TEST_CASE ( nondefault )
+{
+ TestLazyPointer p(boost::bind(&paramFactory, "some string"));
+ BOOST_REQUIRE_EQUAL(false, p.hasValue());
+ BOOST_REQUIRE_EQUAL(11, (*p).val);
+ BOOST_REQUIRE_EQUAL(true, p.hasValue());
+}
+