diff options
author | Josh Blum | 2013-07-29 22:52:12 -0700 |
---|---|---|
committer | Josh Blum | 2013-07-29 22:52:12 -0700 |
commit | 3782b3a9a844d32a277adbbfad98bf64b4cb227c (patch) | |
tree | 7928950316b8a933eb0e16d46b3d54ed8b6d2d38 | |
parent | cf8ca0ba62e78e70b7621b0c81bf7c031eabe3ac (diff) | |
download | sandhi-3782b3a9a844d32a277adbbfad98bf64b4cb227c.tar.gz sandhi-3782b3a9a844d32a277adbbfad98bf64b4cb227c.tar.bz2 sandhi-3782b3a9a844d32a277adbbfad98bf64b4cb227c.zip |
gras: use common test utils for all
-rw-r--r-- | python/gras/CMakeLists.txt | 1 | ||||
-rw-r--r-- | python/gras/GRAS_TestUtils.py (renamed from tests/demo_blocks.py) | 18 | ||||
-rw-r--r-- | python/gras/__init__.py | 1 | ||||
-rw-r--r-- | tests/block_test.py | 42 | ||||
-rw-r--r-- | tests/query_test.py | 16 | ||||
-rw-r--r-- | tests/thread_pool_test.py | 9 |
6 files changed, 52 insertions, 35 deletions
diff --git a/python/gras/CMakeLists.txt b/python/gras/CMakeLists.txt index dda4934..766431d 100644 --- a/python/gras/CMakeLists.txt +++ b/python/gras/CMakeLists.txt @@ -88,6 +88,7 @@ GR_PYTHON_INSTALL( FILES __init__.py GRAS_Utils.py + GRAS_TestUtils.py ${CMAKE_CURRENT_BINARY_DIR}/GRAS_Loader.py DESTINATION ${GR_PYTHON_DIR}/gras COMPONENT ${GRAS_COMP_PYTHON} diff --git a/tests/demo_blocks.py b/python/gras/GRAS_TestUtils.py index 8550402..2960a2d 100644 --- a/tests/demo_blocks.py +++ b/python/gras/GRAS_TestUtils.py @@ -29,7 +29,7 @@ class VectorSource(gras.Block): outs[0][:num] = self._vec[:num] self.produce(0, num) self._vec = self._vec[num:] - if not self._vec: + if not len(self._vec): self.mark_done() class VectorSink(gras.Block): @@ -37,13 +37,27 @@ class VectorSink(gras.Block): gras.Block.__init__(self, name='VectorSink', in_sig=[in_sig]) self._vec = list() - def get_vector(self): + def data(self): return tuple(self._vec) def work(self, ins, outs): self._vec.extend(ins[0].copy()) self.consume(0, len(ins[0])) +class Head(gras.Block): + def __init__(self, sig, num_items): + gras.Block.__init__(self, name='Head', in_sig=[sig], out_sig=[sig]) + self._num_items = num_items + + def work(self, ins, outs): + n = min(len(ins[0]), len(outs[0]), self._num_items) + outs[0][:n] = ins[0][:n] + self.consume(0, n) + self.produce(0, n) + self._num_items -= n + if not self._num_items: + self.mark_done() + class Add2X(gras.Block): def __init__(self, sig): gras.Block.__init__(self, diff --git a/python/gras/__init__.py b/python/gras/__init__.py index 793eba8..da3567f 100644 --- a/python/gras/__init__.py +++ b/python/gras/__init__.py @@ -19,3 +19,4 @@ from GRAS_PyHierBlocks import PyHierBlock as HierBlock from GRAS_PyHierBlocks import PyTopBlock as TopBlock from GRAS_ThreadPool import ThreadPoolConfig, ThreadPool import GRAS_Loader +import GRAS_TestUtils as TestUtils diff --git a/tests/block_test.py b/tests/block_test.py index 2f8d91b..614e702 100644 --- a/tests/block_test.py +++ b/tests/block_test.py @@ -3,7 +3,7 @@ import unittest import gras import numpy -from demo_blocks import * +from gras import TestUtils class BlockTest(unittest.TestCase): @@ -14,55 +14,55 @@ class BlockTest(unittest.TestCase): self.tb = None def test_vector_blocks(self): - vec_source = VectorSource(numpy.uint32, [0, 9, 8, 7, 6]) - vec_sink = VectorSink(numpy.uint32) + vec_source = TestUtils.VectorSource(numpy.uint32, [0, 9, 8, 7, 6]) + vec_sink = TestUtils.VectorSink(numpy.uint32) self.tb.connect(vec_source, vec_sink) self.tb.run() - self.assertEqual(vec_sink.get_vector(), (0, 9, 8, 7, 6)) + self.assertEqual(vec_sink.data(), (0, 9, 8, 7, 6)) def test_add_f32(self): - src0 = VectorSource(numpy.float32, [1, 3, 5, 7, 9]) - src1 = VectorSource(numpy.float32, [0, 2, 4, 6, 8]) - adder = Add2X(numpy.float32) - sink = VectorSink(numpy.float32) + src0 = TestUtils.VectorSource(numpy.float32, [1, 3, 5, 7, 9]) + src1 = TestUtils.VectorSource(numpy.float32, [0, 2, 4, 6, 8]) + adder = TestUtils.Add2X(numpy.float32) + sink = TestUtils.VectorSink(numpy.float32) self.tb.connect((src0, 0), (adder, 0)) self.tb.connect((src1, 0), (adder, 1)) self.tb.connect(adder, sink) self.tb.run() - self.assertEqual(sink.get_vector(), (1, 5, 9, 13, 17)) + self.assertEqual(sink.data(), (1, 5, 9, 13, 17)) def test_add_fc32(self): - src0 = VectorSource(numpy.complex64, [1, 3j, 5, 7j, 9]) - src1 = VectorSource(numpy.complex64, [0, 2j, 4, 6j, 8]) - adder = Add2X(numpy.complex64) - sink = VectorSink(numpy.complex64) + src0 = TestUtils.VectorSource(numpy.complex64, [1, 3j, 5, 7j, 9]) + src1 = TestUtils.VectorSource(numpy.complex64, [0, 2j, 4, 6j, 8]) + adder = TestUtils.Add2X(numpy.complex64) + sink = TestUtils.VectorSink(numpy.complex64) self.tb.connect((src0, 0), (adder, 0)) self.tb.connect((src1, 0), (adder, 1)) self.tb.connect(adder, sink) self.tb.run() - self.assertEqual(sink.get_vector(), (1, 5j, 9, 13j, 17)) + self.assertEqual(sink.data(), (1, 5j, 9, 13j, 17)) def test_add_f32_feedback(self): """ Feedback adder output to input1 of the adder. Preload input1 of the adder to set the delay. """ - src0 = VectorSource(numpy.float32, [1, 3, 5, 7, 9]) - adder = Add2X(numpy.float32) + src0 = TestUtils.VectorSource(numpy.float32, [1, 3, 5, 7, 9]) + adder = TestUtils.Add2X(numpy.float32) adder.input_config(1).preload_items = 1 #make this a feedback delay of 1 - sink = VectorSink(numpy.float32) + sink = TestUtils.VectorSink(numpy.float32) self.tb.connect((src0, 0), (adder, 0)) self.tb.connect((adder, 0), (adder, 1)) self.tb.connect(adder, sink) self.tb.run() - self.assertEqual(sink.get_vector(), (1, 4, 9, 16, 25)) + self.assertEqual(sink.data(), (1, 4, 9, 16, 25)) def test_tag_source_sink(self): values = (0, 'hello', 4.2, True, None, [2, 3, 4], (9, 8, 7), 1j, {2:'d'}) - src = TagSource(values) - sink = TagSink() + src = TestUtils.TagSource(values) + sink = TestUtils.TagSink() self.tb.connect(src, sink) self.tb.run() self.assertEqual(sink.get_values(), values) @@ -79,7 +79,7 @@ class BlockTest(unittest.TestCase): except: pass self.consume(0, len(ins[0])) - source = VectorSource(numpy.uint32, [0, 9, 8, 7, 6]) + source = TestUtils.VectorSource(numpy.uint32, [0, 9, 8, 7, 6]) sink = BadTouch(numpy.uint32) self.tb.connect(source, sink) diff --git a/tests/query_test.py b/tests/query_test.py index 8e5bfaf..f4f45f9 100644 --- a/tests/query_test.py +++ b/tests/query_test.py @@ -3,7 +3,7 @@ import unittest import gras import numpy -from demo_blocks import * +from gras import TestUtils class MyBlock(gras.Block): def __init__(self): @@ -44,13 +44,13 @@ class QueryTest(unittest.TestCase): self.tb = None def test_simple(self): - vec_source = VectorSource(numpy.uint32, [0, 9, 8, 7, 6]) - vec_sink = VectorSink(numpy.uint32) + vec_source = TestUtils.VectorSource(numpy.uint32, [0, 9, 8, 7, 6]) + vec_sink = TestUtils.VectorSink(numpy.uint32) self.tb.connect(vec_source, vec_sink) self.tb.run() - self.assertEqual(vec_sink.get_vector(), (0, 9, 8, 7, 6)) + self.assertEqual(vec_sink.data(), (0, 9, 8, 7, 6)) #query the block list blocks_result = self.tb.query(dict(path="/blocks.json")) @@ -71,8 +71,8 @@ class QueryTest(unittest.TestCase): self.assertTrue(block_id in stats_result['blocks']) def test_numeric_query(self): - vec_source = VectorSource(numpy.uint32, [0, 9, 8, 7, 6]) - vec_sink = VectorSink(numpy.uint32) + vec_source = TestUtils.VectorSource(numpy.uint32, [0, 9, 8, 7, 6]) + vec_sink = TestUtils.VectorSink(numpy.uint32) block = MyBlock() block.set_uid("test_numeric_query") self.tb.connect(vec_source, block, vec_sink) @@ -120,8 +120,8 @@ class QueryTest(unittest.TestCase): self.assertEqual(result['value'], '(0,21)') def test_vector_query(self): - vec_source = VectorSource(numpy.uint32, [0, 9, 8, 7, 6]) - vec_sink = VectorSink(numpy.uint32) + vec_source = TestUtils.VectorSource(numpy.uint32, [0, 9, 8, 7, 6]) + vec_sink = TestUtils.VectorSink(numpy.uint32) block = MyBlock() block.set_uid("test_vector_query") self.tb.connect(vec_source, block, vec_sink) diff --git a/tests/thread_pool_test.py b/tests/thread_pool_test.py index 2765b07..58a00fe 100644 --- a/tests/thread_pool_test.py +++ b/tests/thread_pool_test.py @@ -2,7 +2,8 @@ import unittest import gras -from demo_blocks import * +from gras import TestUtils +import numpy class ThreadPoolTest(unittest.TestCase): @@ -26,8 +27,8 @@ class ThreadPoolTest(unittest.TestCase): def test_migrate_to_thread_pool(self): tb = gras.TopBlock() - vec_source = VectorSource(numpy.uint32, [0, 9, 8, 7, 6]) - vec_sink = VectorSink(numpy.uint32) + vec_source = TestUtils.VectorSource(numpy.uint32, [0, 9, 8, 7, 6]) + vec_sink = TestUtils.VectorSink(numpy.uint32) c = gras.ThreadPoolConfig() tp = gras.ThreadPool(c) @@ -36,7 +37,7 @@ class ThreadPoolTest(unittest.TestCase): tb.connect(vec_source, vec_sink) tb.run() - self.assertEqual(vec_sink.get_vector(), (0, 9, 8, 7, 6)) + self.assertEqual(vec_sink.data(), (0, 9, 8, 7, 6)) if __name__ == '__main__': unittest.main() |