From 3782b3a9a844d32a277adbbfad98bf64b4cb227c Mon Sep 17 00:00:00 2001 From: Josh Blum Date: Mon, 29 Jul 2013 22:52:12 -0700 Subject: gras: use common test utils for all --- python/gras/CMakeLists.txt | 1 + python/gras/GRAS_TestUtils.py | 109 ++++++++++++++++++++++++++++++++++++++++++ python/gras/__init__.py | 1 + tests/block_test.py | 42 ++++++++-------- tests/demo_blocks.py | 95 ------------------------------------ tests/query_test.py | 16 +++---- tests/thread_pool_test.py | 9 ++-- 7 files changed, 145 insertions(+), 128 deletions(-) create mode 100644 python/gras/GRAS_TestUtils.py delete mode 100644 tests/demo_blocks.py 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/python/gras/GRAS_TestUtils.py b/python/gras/GRAS_TestUtils.py new file mode 100644 index 0000000..2960a2d --- /dev/null +++ b/python/gras/GRAS_TestUtils.py @@ -0,0 +1,109 @@ +# Copyright (C) by Josh Blum. See LICENSE.txt for licensing information. + +import gras +import numpy +from PMC import * + +class NullSource(gras.Block): + def __init__(self, out_sig): + gras.Block.__init__(self, 'NullSource', out_sig=[out_sig]) + + def work(self, ins, outs): + outs[0][:] = numpy.zeros(len(outs[0])) + self.produce(0, len(outs[0])) + +class NullSink(gras.Block): + def __init__(self, in_sig): + gras.Block.__init__(self, 'NullSink', in_sig=[in_sig]) + + def work(self, ins, outs): + self.consume(0, len(ins[0])) + +class VectorSource(gras.Block): + def __init__(self, out_sig, vec): + gras.Block.__init__(self, name='VectorSource', out_sig=[out_sig]) + self._vec = vec + + def work(self, ins, outs): + num = min(len(outs[0]), len(self._vec)) + outs[0][:num] = self._vec[:num] + self.produce(0, num) + self._vec = self._vec[num:] + if not len(self._vec): + self.mark_done() + +class VectorSink(gras.Block): + def __init__(self, in_sig): + gras.Block.__init__(self, name='VectorSink', in_sig=[in_sig]) + self._vec = list() + + 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, + name = "Add2X", + in_sig = [sig, sig], + out_sig = [sig], + ) + + def work(self, ins, outs): + nitems = min(*map(len, (ins[0], ins[1], outs[0]))) + outs[0][:nitems] = ins[0][:nitems] + ins[1][:nitems] + self.consume(0, nitems) + self.consume(1, nitems) + self.produce(0, nitems) + +class TagSource(gras.Block): + def __init__(self, values): + gras.Block.__init__(self, + name = "TagSource", + out_sig = [numpy.uint8], + ) + self._values = values + + def work(self, ins, outs): + offset = self.get_produced(0) + tag = gras.Tag(offset, PMC_M(self._values[0])) + self.post_output_tag(0, tag) + self.produce(0, len(outs[0])) + self._values = self._values[1:] + if not self._values: + self.mark_done() + +class TagSink(gras.Block): + def __init__(self): + gras.Block.__init__(self, + name = "TagSink", + in_sig = [numpy.uint8], + ) + self._values = list() + + def get_values(self): + return tuple(self._values) + + def work(self, ins, outs): + max_read = self.get_consumed(0) + len(ins[0]) + for tag in self.get_input_tags(0): + if tag.offset < max_read: + self._values.append(tag.object()) + self.consume(0, len(ins[0])) 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/demo_blocks.py b/tests/demo_blocks.py deleted file mode 100644 index 8550402..0000000 --- a/tests/demo_blocks.py +++ /dev/null @@ -1,95 +0,0 @@ -# Copyright (C) by Josh Blum. See LICENSE.txt for licensing information. - -import gras -import numpy -from PMC import * - -class NullSource(gras.Block): - def __init__(self, out_sig): - gras.Block.__init__(self, 'NullSource', out_sig=[out_sig]) - - def work(self, ins, outs): - outs[0][:] = numpy.zeros(len(outs[0])) - self.produce(0, len(outs[0])) - -class NullSink(gras.Block): - def __init__(self, in_sig): - gras.Block.__init__(self, 'NullSink', in_sig=[in_sig]) - - def work(self, ins, outs): - self.consume(0, len(ins[0])) - -class VectorSource(gras.Block): - def __init__(self, out_sig, vec): - gras.Block.__init__(self, name='VectorSource', out_sig=[out_sig]) - self._vec = vec - - def work(self, ins, outs): - num = min(len(outs[0]), len(self._vec)) - outs[0][:num] = self._vec[:num] - self.produce(0, num) - self._vec = self._vec[num:] - if not self._vec: - self.mark_done() - -class VectorSink(gras.Block): - def __init__(self, in_sig): - gras.Block.__init__(self, name='VectorSink', in_sig=[in_sig]) - self._vec = list() - - def get_vector(self): - return tuple(self._vec) - - def work(self, ins, outs): - self._vec.extend(ins[0].copy()) - self.consume(0, len(ins[0])) - -class Add2X(gras.Block): - def __init__(self, sig): - gras.Block.__init__(self, - name = "Add2X", - in_sig = [sig, sig], - out_sig = [sig], - ) - - def work(self, ins, outs): - nitems = min(*map(len, (ins[0], ins[1], outs[0]))) - outs[0][:nitems] = ins[0][:nitems] + ins[1][:nitems] - self.consume(0, nitems) - self.consume(1, nitems) - self.produce(0, nitems) - -class TagSource(gras.Block): - def __init__(self, values): - gras.Block.__init__(self, - name = "TagSource", - out_sig = [numpy.uint8], - ) - self._values = values - - def work(self, ins, outs): - offset = self.get_produced(0) - tag = gras.Tag(offset, PMC_M(self._values[0])) - self.post_output_tag(0, tag) - self.produce(0, len(outs[0])) - self._values = self._values[1:] - if not self._values: - self.mark_done() - -class TagSink(gras.Block): - def __init__(self): - gras.Block.__init__(self, - name = "TagSink", - in_sig = [numpy.uint8], - ) - self._values = list() - - def get_values(self): - return tuple(self._values) - - def work(self, ins, outs): - max_read = self.get_consumed(0) + len(ins[0]) - for tag in self.get_input_tags(0): - if tag.offset < max_read: - self._values.append(tag.object()) - self.consume(0, len(ins[0])) 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() -- cgit