diff options
author | Josh Blum <josh@joshknows.com> | 2011-06-28 21:26:28 -0700 |
---|---|---|
committer | Josh Blum <josh@joshknows.com> | 2011-06-28 21:26:28 -0700 |
commit | d2f79c07281604c1b48ec81f1cdb2754e97bbe65 (patch) | |
tree | e41a2000af165384bbe03b1ff6d6ea118bbbcf1f /host/tests/property_test.cpp | |
parent | ebd2ecc6ff2b82cb06701bbcda17d4caaa4ba8c1 (diff) | |
download | uhd-d2f79c07281604c1b48ec81f1cdb2754e97bbe65.tar.gz uhd-d2f79c07281604c1b48ec81f1cdb2754e97bbe65.tar.bz2 uhd-d2f79c07281604c1b48ec81f1cdb2754e97bbe65.zip |
uhd: added properties unit tests, use shared ptr<void> in tree
Diffstat (limited to 'host/tests/property_test.cpp')
-rw-r--r-- | host/tests/property_test.cpp | 86 |
1 files changed, 68 insertions, 18 deletions
diff --git a/host/tests/property_test.cpp b/host/tests/property_test.cpp index 6c80cea70..cd9691dca 100644 --- a/host/tests/property_test.cpp +++ b/host/tests/property_test.cpp @@ -18,6 +18,7 @@ #include <boost/test/unit_test.hpp> #include <uhd/property_tree.hpp> #include <boost/bind.hpp> +#include <exception> #include <iostream> struct coercer_type{ @@ -34,44 +35,92 @@ struct setter_type{ int _x; }; +struct getter_type{ + int doit(void){ + return _x; + } + + int _x; +}; + BOOST_AUTO_TEST_CASE(test_prop_simple){ - uhd::property<int>::sptr prop = uhd::property<int>::make(); - prop->set(42); - BOOST_CHECK_EQUAL(prop->get(), 42); - prop->set(34); - BOOST_CHECK_EQUAL(prop->get(), 34); + uhd::property_tree::sptr tree = uhd::property_tree::make(); + uhd::property<int> &prop = tree->create<int>("/"); + prop.set(42); + BOOST_CHECK_EQUAL(prop.get(), 42); + prop.set(34); + BOOST_CHECK_EQUAL(prop.get(), 34); } BOOST_AUTO_TEST_CASE(test_prop_with_subscriber){ - uhd::property<int>::sptr prop = uhd::property<int>::make(); + uhd::property_tree::sptr tree = uhd::property_tree::make(); + uhd::property<int> &prop = tree->create<int>("/"); setter_type setter; - prop->subscribe(boost::bind(&setter_type::doit, &setter, _1)); + prop.subscribe(boost::bind(&setter_type::doit, &setter, _1)); - prop->set(42); - BOOST_CHECK_EQUAL(prop->get(), 42); + prop.set(42); + BOOST_CHECK_EQUAL(prop.get(), 42); BOOST_CHECK_EQUAL(setter._x, 42); - prop->set(34); - BOOST_CHECK_EQUAL(prop->get(), 34); + prop.set(34); + BOOST_CHECK_EQUAL(prop.get(), 34); BOOST_CHECK_EQUAL(setter._x, 34); } +BOOST_AUTO_TEST_CASE(test_prop_with_publisher){ + uhd::property_tree::sptr tree = uhd::property_tree::make(); + uhd::property<int> &prop = tree->create<int>("/"); + + getter_type getter; + prop.publish(boost::bind(&getter_type::doit, &getter)); + + getter._x = 42; + prop.set(0); //should not change + BOOST_CHECK_EQUAL(prop.get(), 42); + + getter._x = 34; + prop.set(0); //should not change + BOOST_CHECK_EQUAL(prop.get(), 34); +} + +BOOST_AUTO_TEST_CASE(test_prop_with_publisher_and_subscriber){ + uhd::property_tree::sptr tree = uhd::property_tree::make(); + uhd::property<int> &prop = tree->create<int>("/"); + + getter_type getter; + prop.publish(boost::bind(&getter_type::doit, &getter)); + + setter_type setter; + prop.subscribe(boost::bind(&setter_type::doit, &setter, _1)); + + getter._x = 42; + prop.set(0); + BOOST_CHECK_EQUAL(prop.get(), 42); + BOOST_CHECK_EQUAL(setter._x, 0); + + getter._x = 34; + prop.set(1); + BOOST_CHECK_EQUAL(prop.get(), 34); + BOOST_CHECK_EQUAL(setter._x, 1); +} + BOOST_AUTO_TEST_CASE(test_prop_with_coercion){ - uhd::property<int>::sptr prop = uhd::property<int>::make(); + uhd::property_tree::sptr tree = uhd::property_tree::make(); + uhd::property<int> &prop = tree->create<int>("/"); setter_type setter; - prop->subscribe(boost::bind(&setter_type::doit, &setter, _1)); + prop.subscribe(boost::bind(&setter_type::doit, &setter, _1)); coercer_type coercer; - prop->subscribe_master(boost::bind(&coercer_type::doit, &coercer, _1)); + prop.subscribe_master(boost::bind(&coercer_type::doit, &coercer, _1)); - prop->set(42); - BOOST_CHECK_EQUAL(prop->get(), 40); + prop.set(42); + BOOST_CHECK_EQUAL(prop.get(), 40); BOOST_CHECK_EQUAL(setter._x, 40); - prop->set(34); - BOOST_CHECK_EQUAL(prop->get(), 32); + prop.set(34); + BOOST_CHECK_EQUAL(prop.get(), 32); BOOST_CHECK_EQUAL(setter._x, 32); } @@ -82,6 +131,7 @@ BOOST_AUTO_TEST_CASE(test_prop_tree){ tree->create<int>("/test/prop1"); BOOST_CHECK(tree->exists("/test")); + BOOST_CHECK_THROW(tree->access<int>("/test"), std::exception); BOOST_CHECK(tree->exists("/test/prop0")); BOOST_CHECK(tree->exists("/test/prop1")); |