diff options
author | Josh Blum | 2013-07-08 00:14:57 -0700 |
---|---|---|
committer | Josh Blum | 2013-07-08 00:14:57 -0700 |
commit | 4e28cd4159fad7c01a8945957b98fa94d7f66c62 (patch) | |
tree | f6ae3606449b44e6518a8f858886cf62cbeaf539 /tests | |
parent | fbec0c405a1fec4e8ee82d065fdc657cdd333f52 (diff) | |
parent | 07070a8000400ced8dc8ff612c82c418df73f7ef (diff) | |
download | sandhi-4e28cd4159fad7c01a8945957b98fa94d7f66c62.tar.gz sandhi-4e28cd4159fad7c01a8945957b98fa94d7f66c62.tar.bz2 sandhi-4e28cd4159fad7c01a8945957b98fa94d7f66c62.zip |
Merge branch 'function_registry'
Diffstat (limited to 'tests')
-rw-r--r-- | tests/CMakeLists.txt | 16 | ||||
-rw-r--r-- | tests/block_props_test.cpp | 27 | ||||
-rw-r--r-- | tests/block_props_test.py | 34 | ||||
-rw-r--r-- | tests/callable_test.cpp | 116 | ||||
-rw-r--r-- | tests/example_module.cpp | 34 | ||||
-rw-r--r-- | tests/factory_test.cpp | 77 | ||||
-rw-r--r-- | tests/module_loader_test.py | 13 | ||||
-rw-r--r-- | tests/query_test.py | 44 |
8 files changed, 301 insertions, 60 deletions
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index bc3815e..5024954 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -14,8 +14,10 @@ if (NOT Boost_FOUND) endif() set(test_sources + callable_test.cpp chrono_time_test.cpp block_props_test.cpp + factory_test.cpp serialize_tags_test.cpp ) @@ -48,3 +50,17 @@ GR_ADD_TEST(sbuffer_test ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/sbuffe GR_ADD_TEST(query_test ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/query_test.py) GR_ADD_TEST(block_props_test ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/block_props_test.py) GR_ADD_TEST(time_tags_test ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/time_tags_test.py) + +######################################################################## +# Build an example loadable module +######################################################################## +add_library(example_module MODULE example_module.cpp) +target_link_libraries(example_module ${GRAS_LIBRARIES}) + +set(CURRENT_LIBRARY_BUILD_DIR ${CMAKE_CURRENT_BINARY_DIR}) +if(WIN32) +set(CURRENT_LIBRARY_BUILD_DIR ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_BUILD_TYPE}) +endif() +file(TO_NATIVE_PATH "${CURRENT_LIBRARY_BUILD_DIR}" CURRENT_LIBRARY_BUILD_DIR) +list(APPEND GR_TEST_ENVIRONS "GRAS_MODULE_PATH=${CURRENT_LIBRARY_BUILD_DIR}") +GR_ADD_TEST(module_loader_test ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/module_loader_test.py) diff --git a/tests/block_props_test.cpp b/tests/block_props_test.cpp index f53a1c3..b14677d 100644 --- a/tests/block_props_test.cpp +++ b/tests/block_props_test.cpp @@ -11,8 +11,8 @@ struct MyBlock : gras::Block gras::Block("MyBlock") { foo = 0; - this->register_getter("foo", &MyBlock::get_foo); - this->register_setter("foo", &MyBlock::set_foo); + this->register_call("get_foo", &MyBlock::get_foo); + this->register_call("set_foo", &MyBlock::set_foo); } //dummy work @@ -36,35 +36,20 @@ BOOST_AUTO_TEST_CASE(test_property_set_get_with_return) MyBlock my_block; BOOST_CHECK_EQUAL(my_block.foo, size_t(0)); - my_block.set("foo", size_t(42)); + my_block.x("set_foo", size_t(42)); BOOST_CHECK_EQUAL(my_block.foo, size_t(42)); - const size_t my_foo = my_block.get<size_t>("foo"); + const size_t my_foo = my_block.x<size_t>("get_foo"); BOOST_CHECK_EQUAL(my_foo, size_t(42)); } -BOOST_AUTO_TEST_CASE(test_property_set_get_with_reference) -{ - MyBlock my_block; - BOOST_CHECK_EQUAL(my_block.foo, size_t(0)); - - my_block.set("foo", size_t(42)); - BOOST_CHECK_EQUAL(my_block.foo, size_t(42)); - - size_t my_foo; my_block.get("foo", my_foo); - BOOST_CHECK_EQUAL(my_foo, size_t(42)); - - double my_foo_d; my_block.get("foo", my_foo_d); - BOOST_CHECK_EQUAL(my_foo_d, double(42)); -} - BOOST_AUTO_TEST_CASE(test_property_errors) { MyBlock my_block; //property does not exist - BOOST_CHECK_THROW(my_block.get<size_t>("bar"), std::exception); + BOOST_CHECK_THROW(my_block.x<size_t>("get_bar"), std::exception); //wrong type for property - BOOST_CHECK_THROW(my_block.set("foo", "a string"), std::exception); + BOOST_CHECK_THROW(my_block.x("set_foo", "a string"), std::exception); } diff --git a/tests/block_props_test.py b/tests/block_props_test.py index 0b068f9..7f5dad2 100644 --- a/tests/block_props_test.py +++ b/tests/block_props_test.py @@ -8,8 +8,8 @@ class MyBlock(gras.Block): def __init__(self): gras.Block.__init__(self, "MyBlock") self.foo = 0 - self.register_getter("foo", self.get_foo) - self.register_setter("foo", self.set_foo) + self.register_call("get_foo", self.get_foo) + self.register_call("set_foo", self.set_foo) def work(self, *args): pass @@ -27,10 +27,10 @@ class BlockPropsTest(unittest.TestCase): my_block = MyBlock() self.assertEqual(my_block.foo, 0) - my_block.set("foo", 42) + my_block.set_foo(42) self.assertEqual(my_block.foo, 42) - my_foo = my_block.get("foo") + my_foo = my_block.get_foo() self.assertEqual(my_foo, 42) def test_property_errors(self): @@ -38,13 +38,13 @@ class BlockPropsTest(unittest.TestCase): #property does not exist threw = False - try: my_block.get("bar") + try: my_block.get_bar() except: threw = True self.assertTrue(threw) #wrong type for property threw = False - try: my_block.set("foo", None) + try: my_block.set_foo(None) except: threw = True self.assertTrue(threw) @@ -56,30 +56,30 @@ class BlockPropsTest(unittest.TestCase): tb.adopt_element("my_hier", hb) hb.adopt_element("my_block", my_block) - my_block.set("foo", 42) - self.assertEqual(my_block.get("foo"), 42) + my_block.set_foo(42) + self.assertEqual(my_block.get_foo(), 42) - my_block0 = tb.locate_block('/my_hier/my_block') - self.assertEqual(my_block0.get("foo"), 42) + my_block0 = tb.locate_element('/my_hier/my_block') + self.assertEqual(my_block0.get_foo(), 42) - my_block1 = hb.locate_block('my_block') - self.assertEqual(my_block1.get("foo"), 42) + my_block1 = hb.locate_element('my_block') + self.assertEqual(my_block1.get_foo(), 42) - my_block2 = hb.locate_block('./../my_hier/my_block') - self.assertEqual(my_block2.get("foo"), 42) + my_block2 = hb.locate_element('./../my_hier/my_block') + self.assertEqual(my_block2.get_foo(), 42) threw = False - try: hb.locate_block('../../my_hier/my_block') + try: hb.locate_element('../../my_hier/my_block') except: threw = True self.assertTrue(threw) threw = False - try: hb.locate_block('../../my_hier/my_block0') + try: hb.locate_element('../../my_hier/my_block0') except: threw = True self.assertTrue(threw) threw = False - try: hb.locate_block('../../my_hier/my_block/test') + try: hb.locate_element('../../my_hier/my_block/test') except: threw = True self.assertTrue(threw) diff --git a/tests/callable_test.cpp b/tests/callable_test.cpp new file mode 100644 index 0000000..3cbbc5e --- /dev/null +++ b/tests/callable_test.cpp @@ -0,0 +1,116 @@ +// Copyright (C) by Josh Blum. See LICENSE.txt for licensing information. + +#include <boost/test/unit_test.hpp> +#include <iostream> + +#include <gras/callable.hpp> + + +struct MyClass : gras::Callable +{ + MyClass(void) + { + count = 0; + this->register_call("test_args0_with_return", &MyClass::test_args0_with_return); + this->register_call("test_args1_with_return", &MyClass::test_args1_with_return); + this->register_call("test_args2_with_return", &MyClass::test_args2_with_return); + this->register_call("test_args3_with_return", &MyClass::test_args3_with_return); + this->register_call("test_args0_with_no_return", &MyClass::test_args0_with_no_return); + this->register_call("test_args1_with_no_return", &MyClass::test_args1_with_no_return); + this->register_call("test_args2_with_no_return", &MyClass::test_args2_with_no_return); + this->register_call("test_args3_with_no_return", &MyClass::test_args3_with_no_return); + } + + int test_args0_with_return(void) + { + return count; + } + + int test_args1_with_return(const int &a0) + { + return a0; + } + + int test_args2_with_return(const int &a0, const std::string &a1) + { + return a0 + a1.size(); + } + + int test_args3_with_return(const int &a0, const std::string &a1, const bool &a3) + { + return a3? a0 + a1.size() : 0; + } + + void test_args0_with_no_return(void) + { + count++; + } + + void test_args1_with_no_return(const int &a0) + { + count = a0; + } + + void test_args2_with_no_return(const int &a0, const std::string &a1) + { + count = a0 + a1.size(); + } + + void test_args3_with_no_return(const int &a0, const std::string &a1, const bool &a2) + { + if (a2) count = a0 + a1.size(); + else count = 0; + } + + int count; +}; + +BOOST_AUTO_TEST_CASE(test_registered_methods) +{ + MyClass my_class; + { + size_t ret = my_class.x<size_t>("test_args0_with_return"); + BOOST_CHECK_EQUAL(ret, size_t(0)); + } + { + size_t ret = my_class.x<size_t>("test_args1_with_return", 42); + BOOST_CHECK_EQUAL(ret, size_t(42)); + } + { + size_t ret = my_class.x<size_t>("test_args2_with_return", 1, "OK"); + BOOST_CHECK_EQUAL(ret, size_t(3)); + } + { + size_t ret = my_class.x<size_t>("test_args3_with_return", 1, "OK", true); + BOOST_CHECK_EQUAL(ret, size_t(3)); + } + { + size_t ret = my_class.x<size_t>("test_args3_with_return", 1, "OK", false); + BOOST_CHECK_EQUAL(ret, size_t(0)); + } + { + my_class.x("test_args0_with_no_return"); //this adds 1 to count + size_t ret = my_class.x<size_t>("test_args0_with_return"); + BOOST_CHECK_EQUAL(ret, size_t(1)); + } + { + my_class.x("test_args1_with_no_return", 42); + size_t ret = my_class.x<size_t>("test_args0_with_return"); + BOOST_CHECK_EQUAL(ret, size_t(42)); + } + { + my_class.x("test_args2_with_no_return", 1, "OK"); + size_t ret = my_class.x<size_t>("test_args0_with_return"); + BOOST_CHECK_EQUAL(ret, size_t(3)); + } + { + my_class.x("test_args3_with_no_return", 1, "OK", true); + size_t ret = my_class.x<size_t>("test_args0_with_return"); + BOOST_CHECK_EQUAL(ret, size_t(3)); + } + { + my_class.x("test_args3_with_no_return", 1, "OK", false); + size_t ret = my_class.x<size_t>("test_args0_with_return"); + BOOST_CHECK_EQUAL(ret, size_t(0)); + } +} diff --git a/tests/example_module.cpp b/tests/example_module.cpp new file mode 100644 index 0000000..2191650 --- /dev/null +++ b/tests/example_module.cpp @@ -0,0 +1,34 @@ +// Copyright (C) by Josh Blum. See LICENSE.txt for licensing information. + +#include <gras/block.hpp> +#include <gras/factory.hpp> +#include <iostream> + +struct MyBlock : gras::Block +{ + MyBlock(void): + gras::Block("MyBlock") + { + this->register_call("get_num", &MyBlock::get_num); + } + + ~MyBlock(void) + { + std::cout << "~MyBlock" << std::endl; + } + + int get_num(void) + { + return 42; + } + + //dummy work + void work(const InputItems &, const OutputItems &){} +}; + +gras::Block *make_my_block(void) +{ + return new MyBlock(); +} + +GRAS_REGISTER_FACTORY("/tests/my_block", make_my_block) diff --git a/tests/factory_test.cpp b/tests/factory_test.cpp new file mode 100644 index 0000000..999e86e --- /dev/null +++ b/tests/factory_test.cpp @@ -0,0 +1,77 @@ +// Copyright (C) by Josh Blum. See LICENSE.txt for licensing information. + +#include <boost/test/unit_test.hpp> +#include <iostream> + +#include <gras/block.hpp> + +struct MyBlock : gras::Block +{ + MyBlock(const long &a0 = 0, const std::string &a1 = "", const bool &a2 = false): + gras::Block("MyBlock"), a0(a0), a1(a1), a2(a2) + { + } + + const long a0; + const std::string a1; + const bool a2; + + //dummy work + void work(const InputItems &, const OutputItems &){} +}; + +// a few test factory functions + +gras::Block *make_my_block_args0(void) +{ + return new MyBlock(); +} + +gras::Block *make_my_block_args1(const long &a0) +{ + return new MyBlock(a0); +} + +gras::Block *make_my_block_args2(const long &a0, const std::string &a1) +{ + return new MyBlock(a0, a1); +} + +gras::Block *make_my_block_args3(const long &a0, const std::string &a1, const bool &a2) +{ + return new MyBlock(a0, a1, a2); +} + +#include <gras/factory.hpp> + +GRAS_REGISTER_FACTORY("/tests/my_block_args0", make_my_block_args0) +GRAS_REGISTER_FACTORY("/tests/my_block_args1", make_my_block_args1) +GRAS_REGISTER_FACTORY("/tests/my_block_args2", make_my_block_args2) +GRAS_REGISTER_FACTORY("/tests/my_block_args3", make_my_block_args3) + +BOOST_AUTO_TEST_CASE(test_register_and_make) +{ + gras::Element *my_block0 = gras::Factory::make("/tests/my_block_args0"); + BOOST_CHECK(dynamic_cast<MyBlock *>(my_block0) != NULL); + BOOST_CHECK_EQUAL(dynamic_cast<MyBlock *>(my_block0)->a0, 0); + BOOST_CHECK_EQUAL(dynamic_cast<MyBlock *>(my_block0)->a1, ""); + BOOST_CHECK_EQUAL(dynamic_cast<MyBlock *>(my_block0)->a2, false); + + gras::Element *my_block1 = gras::Factory::make("/tests/my_block_args1", 42); + BOOST_CHECK(dynamic_cast<MyBlock *>(my_block1) != NULL); + BOOST_CHECK_EQUAL(dynamic_cast<MyBlock *>(my_block1)->a0, 42); + BOOST_CHECK_EQUAL(dynamic_cast<MyBlock *>(my_block1)->a1, ""); + BOOST_CHECK_EQUAL(dynamic_cast<MyBlock *>(my_block1)->a2, false); + + gras::Element *my_block2 = gras::Factory::make("/tests/my_block_args2", 1, "foo"); + BOOST_CHECK(dynamic_cast<MyBlock *>(my_block2) != NULL); + BOOST_CHECK_EQUAL(dynamic_cast<MyBlock *>(my_block2)->a0, 1); + BOOST_CHECK_EQUAL(dynamic_cast<MyBlock *>(my_block2)->a1, "foo"); + BOOST_CHECK_EQUAL(dynamic_cast<MyBlock *>(my_block2)->a2, false); + + gras::Element *my_block3 = gras::Factory::make("/tests/my_block_args3", -2, "bar", true); + BOOST_CHECK(dynamic_cast<MyBlock *>(my_block3) != NULL); + BOOST_CHECK_EQUAL(dynamic_cast<MyBlock *>(my_block3)->a0, -2); + BOOST_CHECK_EQUAL(dynamic_cast<MyBlock *>(my_block3)->a1, "bar"); + BOOST_CHECK_EQUAL(dynamic_cast<MyBlock *>(my_block3)->a2, true); +} diff --git a/tests/module_loader_test.py b/tests/module_loader_test.py new file mode 100644 index 0000000..8b7a0c9 --- /dev/null +++ b/tests/module_loader_test.py @@ -0,0 +1,13 @@ +# Copyright (C) by Josh Blum. See LICENSE.txt for licensing information. + +import unittest +import gras + +class ModuleLoaderTest(unittest.TestCase): + + def test_load_module(self): + my_block = gras.Factory.make("/tests/my_block") + self.assertEqual(my_block.get_num(), 42) + +if __name__ == '__main__': + unittest.main() diff --git a/tests/query_test.py b/tests/query_test.py index 98561ff..8e5bfaf 100644 --- a/tests/query_test.py +++ b/tests/query_test.py @@ -9,11 +9,11 @@ class MyBlock(gras.Block): def __init__(self): gras.Block.__init__(self, "MyBlock", out_sig=[numpy.uint32], in_sig=[numpy.uint32]) self.numeric_value = 0 - self.register_getter("numeric_value", self.get_numeric_value) - self.register_setter("numeric_value", self.set_numeric_value) + self.register_call("get_numeric_value", self.get_numeric_value) + self.register_call("set_numeric_value", self.set_numeric_value) self.vector_value = [0] - self.register_getter("vector_value", self.get_vector_value) - self.register_setter("vector_value", self.set_vector_value) + self.register_call("get_vector_value", self.get_vector_value) + self.register_call("set_vector_value", self.set_vector_value) def work(self, ins, outs): n = min(len(ins[0]), len(outs[0])) @@ -85,37 +85,37 @@ class QueryTest(unittest.TestCase): #set the integer property self.tb.query(dict( - path="/props.json", + path="/calls.json", block='test_numeric_query', - key='numeric_value', - value=42, + name='set_numeric_value', + args=[42], )) self.assertEqual(block.numeric_value, 42) #get the integer property - block.set('numeric_value', 21) + block.set_numeric_value(21) result = self.tb.query(dict( - path="/props.json", + path="/calls.json", block='test_numeric_query', - key='numeric_value', + name='get_numeric_value', )) self.assertEqual(result['value'], 21) #set the complex property self.tb.query(dict( - path="/props.json", + path="/calls.json", block='test_numeric_query', - key='numeric_value', - value='(0, 42)', + name='set_numeric_value', + args=['(0, 42)'], )) self.assertEqual(block.numeric_value, 42j) #get the complex property - block.set('numeric_value', 21j) + block.set_numeric_value(21j) result = self.tb.query(dict( - path="/props.json", + path="/calls.json", block='test_numeric_query', - key='numeric_value', + name='get_numeric_value', )) self.assertEqual(result['value'], '(0,21)') @@ -129,19 +129,19 @@ class QueryTest(unittest.TestCase): #set the vector property self.tb.query(dict( - path="/props.json", + path="/calls.json", block='test_vector_query', - key='vector_value', - value=[1, 2, 3, 4, 5], + name='set_vector_value', + args=[[1, 2, 3, 4, 5]], )) self.assertEqual(list(block.vector_value), [1, 2, 3, 4, 5]) #get the vector property - block.set('vector_value', [6, 7, 8, 9]) + block.set_vector_value([6, 7, 8, 9]) result = self.tb.query(dict( - path="/props.json", + path="/calls.json", block='test_vector_query', - key='vector_value', + name='get_vector_value', )) self.assertEqual(list(result['value']), [6, 7, 8, 9]) |