summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2023-04-14 14:49:52 +0100
committerDan Goodliffe <dan@randomdan.homeip.net>2023-04-14 14:49:52 +0100
commit70bddc3f8ef3eeb020b4d6114f3abf4ab160003f (patch)
treef8e83df70e00c9f7f3588638c756ac66a8ab0199 /test
parentExternalise a neater definition of TGAHead (diff)
parentLoad texture images in Worker (diff)
downloadilt-70bddc3f8ef3eeb020b4d6114f3abf4ab160003f.tar.bz2
ilt-70bddc3f8ef3eeb020b4d6114f3abf4ab160003f.tar.xz
ilt-70bddc3f8ef3eeb020b4d6114f3abf4ab160003f.zip
Merge branch 'worker' into assimp
Diffstat (limited to 'test')
-rw-r--r--test/Jamfile.jam1
-rw-r--r--test/test-worker.cpp104
2 files changed, 105 insertions, 0 deletions
diff --git a/test/Jamfile.jam b/test/Jamfile.jam
index 390880d..482b388 100644
--- a/test/Jamfile.jam
+++ b/test/Jamfile.jam
@@ -57,6 +57,7 @@ run test-glContextBhvr.cpp ;
run test-assetFactory.cpp : -- : [ sequence.insertion-sort [ glob-tree $(res) : *.* ] fixtures/rgb.txt ] : <library>test ;
run perf-assetFactory.cpp : : : <library>benchmark <library>test <dependency>test-assetFactory ;
run perf-persistence.cpp : : : <library>benchmark <library>test <dependency>test-persistence ;
+run test-worker.cpp ;
compile test-static-enumDetails.cpp ;
compile test-static-stream_support.cpp ;
explicit perf-assetFactory ;
diff --git a/test/test-worker.cpp b/test/test-worker.cpp
new file mode 100644
index 0000000..76b7138
--- /dev/null
+++ b/test/test-worker.cpp
@@ -0,0 +1,104 @@
+#define BOOST_TEST_MODULE test_worker
+
+#include "testHelpers.h"
+#include <boost/test/unit_test.hpp>
+#include <set>
+#include <stream_support.hpp>
+#include <worker.h>
+
+uint32_t
+workCounter()
+{
+ static std::atomic_uint32_t n;
+ usleep(1000);
+ return n++;
+}
+
+void
+workVoid()
+{
+ usleep(1000);
+}
+
+void
+workFail()
+{
+ usleep(1000);
+ throw std::runtime_error {"test"};
+}
+
+BOOST_AUTO_TEST_CASE(basic_slow_counter)
+{
+ std::vector<Worker::WorkPtrT<uint32_t>> ps;
+ for (int i {}; i < 30; ++i) {
+ ps.push_back(Worker::addWork(workCounter));
+ }
+ std::set<uint32_t> out;
+ std::transform(ps.begin(), ps.end(), std::inserter(out, out.end()), [](auto && p) {
+ return p->get();
+ });
+ BOOST_REQUIRE_EQUAL(out.size(), ps.size());
+ BOOST_CHECK_EQUAL(*out.begin(), 0);
+ BOOST_CHECK_EQUAL(*out.rbegin(), ps.size() - 1);
+}
+
+BOOST_AUTO_TEST_CASE(basic_error_handler)
+{
+ auto workitem = Worker::addWork(workFail);
+ BOOST_CHECK_THROW(workitem->get(), std::runtime_error);
+}
+
+BOOST_AUTO_TEST_CASE(basic_void_work)
+{
+ auto workitem = Worker::addWork(workVoid);
+ BOOST_CHECK_NO_THROW(workitem->get());
+}
+
+BOOST_AUTO_TEST_CASE(lambda_void)
+{
+ BOOST_CHECK_NO_THROW(Worker::addWork([]() {})->get());
+ BOOST_CHECK_NO_THROW(Worker::addWork([](int) {}, 0)->get());
+ BOOST_CHECK_NO_THROW(Worker::addWork([](int, int) {}, 0, 0)->get());
+}
+
+BOOST_AUTO_TEST_CASE(lambda_value)
+{
+ BOOST_CHECK_EQUAL(1, Worker::addWork([]() {
+ return 1;
+ })->get());
+ BOOST_CHECK_EQUAL(2,
+ Worker::addWork(
+ [](int i) {
+ return i;
+ },
+ 2)
+ ->get());
+ BOOST_CHECK_EQUAL(3,
+ Worker::addWork(
+ [](int i, int j) {
+ return i + j;
+ },
+ 1, 2)
+ ->get());
+}
+
+BOOST_AUTO_TEST_CASE(recursive, *boost::unit_test::timeout(5))
+{
+ auto recurse = []() {
+ std::vector<Worker::WorkPtrT<uint32_t>> ps;
+ for (int i {}; i < 30; ++i) {
+ ps.push_back(Worker::addWork(workCounter));
+ }
+ return std::accumulate(ps.begin(), ps.end(), 0U, [](auto && out, auto && p) {
+ return out += p->get();
+ });
+ };
+ std::vector<Worker::WorkPtrT<uint32_t>> ps;
+ for (int i {}; i < 30; ++i) {
+ ps.push_back(Worker::addWork(recurse));
+ }
+ std::set<uint32_t> out;
+ std::transform(ps.begin(), ps.end(), std::inserter(out, out.end()), [](auto && p) {
+ return p->get();
+ });
+}