예제 #1
0
    def test_ShortCircuit(self):
        # Short-Circuit

        blk = block.ShortCircuit()

        blk.write(2)
        (yk, ) = blk.read()
        self.assertEqual(yk, 2)

        blk.write(2, 4)
        yk = blk.read()
        self.assertEqual(yk, (2, 4))

        blk.write(np.array([2, 4]))
        (yk, ) = blk.read()
        self.assertTrue(np.all(yk == np.array([2, 4])))

        blk.write(np.array([2, 4]), -1)
        yk = blk.read()
        self.assertTrue(np.all(yk[0] == np.array([2, 4])) and yk[1] == -1)
예제 #2
0
def test_ShortCircuit():

    # Short-Circuit

    blk = block.ShortCircuit()

    blk.write(2)
    (yk, ) = blk.read()
    assert yk == 2

    blk.write(2, 4)
    yk = blk.read()
    assert yk == (2, 4)

    blk.write(np.array([2, 4]))
    (yk, ) = blk.read()
    assert np.all(yk == [2, 4])

    blk.write(np.array([2, 4]), -1)
    yk = blk.read()
    assert np.all(yk[0] == [2, 4]) and yk[1] == -1
예제 #3
0
    def test_container(self):

        import pyctrl
        import numpy
        import pyctrl.block as block
        import pyctrl.block.system as system
        import pyctrl.block.logic as logic
        # import pyctrl.block.random as blkrnd

        from pyctrl import BlockType
        from pyctrl.block.container import Container
        container = Container()

        # initial signals
        _signals = container.list_signals()
        _sinks = container.list_sinks()
        _sources = container.list_sources()
        _filters = container.list_filters()

        self.assertTrue(not container.is_enabled())

        container.add_signal('_test_')
        self.assertTrue('_test_' in container.list_signals())

        #with self.assertRaises(block.container.ContainerWarning):
        container.add_signal('_test_')

        self.assertEqual(container.get_signal('_test_'), 0)

        container.set_signal('_test_', 1.2)
        self.assertEqual(container.get_signal('_test_'), 1.2)

        container.remove_signal('_test_')
        self.assertTrue('_test_' not in container.list_signals())

        with self.assertRaises(pyctrl.block.container.ContainerException):
            container.set_signal('_test_', 1.2)

        container.add_signals('_test1_', '_test2_')
        self.assertTrue('_test1_' in container.list_signals())
        self.assertTrue('_test2_' in container.list_signals())

        container.remove_signal('_test1_')
        container.remove_signal('_test2_')
        self.assertTrue('_test1_' not in container.list_signals())
        self.assertTrue('_test2_' not in container.list_signals())

        # test info
        print(container.info())
        self.assertTrue(isinstance(container.info(), str))
        self.assertTrue(isinstance(container.info('summary'), str))
        self.assertTrue(isinstance(container.info('sources', 'sinks'), str))

        # test sink

        container.add_signal('clock')

        container.add_sink('_logger_', block.Logger(), ['_test_'])

        self.assertTrue('_logger_' in container.list_sinks())
        self.assertTrue('_test_' in container.list_signals())

        self.assertEqual(
            container.get_sink('_logger_'), {
                'current': 0,
                'auto_reset': False,
                'page': 0,
                'enabled': True,
                'labels': ['_test_'],
                'index': None
            })

        self.assertEqual(
            container.get_sink('_logger_', 'current', 'auto_reset'), {
                'current': 0,
                'auto_reset': False
            })

        self.assertEqual(container.get_sink('_logger_', 'current'), 0)

        container.set_sink('_logger_', current=1)

        self.assertEqual(container.get_sink('_logger_', 'current'), 1)

        # try to remove signal _test_
        container.remove_signal('_test_')
        self.assertTrue('_test_' in container.list_signals())

        container.add_sink('_logger_', block.Logger(), ['clock'])
        self.assertTrue('_logger_' in container.list_sinks())

        # TODO: test for changed signals

        container.set_sink('_logger_', reset=True)

        log = container.get_sink('_logger_', 'log')
        self.assertTrue(isinstance(log['clock'], numpy.ndarray))

        self.assertShape(log['clock'].shape, (0, 1))

        with self.assertRaises(block.BlockException):
            container.set_sink('_logger_', _reset=True)

        container.set_enabled(True)
        container.run()
        container.run()
        container.set_enabled(False)

        log = container.get_sink('_logger_', 'log')

        self.assertTrue(isinstance(log['clock'], numpy.ndarray))
        self.assertShape(log['clock'].shape, (2, 1))

        container.set_sink('_logger_', reset=True)
        log = container.get_sink('_logger_', 'log')
        self.assertTrue(isinstance(log['clock'], numpy.ndarray))
        self.assertShape(log['clock'].shape, (0, 1))

        container.remove_sink('_logger_')
        self.assertTrue('_logger_' not in container.list_sinks())

        container.add_signal('_test_')

        container.add_sink('_logger_', block.Logger(), ['clock', '_test_'])
        self.assertTrue('_logger_' in container.list_sinks())

        container.set_enabled(True)
        container.run()
        container.run()
        container.set_enabled(False)

        log = container.get_sink('_logger_', 'log')
        self.assertTrue(isinstance(log['clock'], numpy.ndarray))
        self.assertTrue(isinstance(log['_test_'], numpy.ndarray))
        self.assertShape(log['clock'].shape, (2, 1))
        self.assertShape(log['_test_'].shape, (2, 1))

        container.set_sink('_logger_', reset=True)
        log = container.get_sink('_logger_', 'log')
        self.assertTrue(isinstance(log['clock'], numpy.ndarray))
        self.assertTrue(isinstance(log['_test_'], numpy.ndarray))
        self.assertShape(log['clock'].shape, (0, 1))
        self.assertShape(log['_test_'].shape, (0, 1))

        container.remove_sink('_logger_')
        self.assertTrue('_logger_' not in container.list_sinks())

        # test source

        #container.add_source('_rand_', blkrnd.Uniform(), ['clock'])
        container.add_source('_rand_', block.Constant(), ['clock'])
        self.assertTrue('_rand_' in container.list_sources())

        #container.add_source('_rand_', blkrnd.Uniform(), ['_test_'])
        container.add_source('_rand_', block.Constant(value=2), ['_test_'])
        self.assertTrue('_rand_' in container.list_sources())

        #self.assertTrue( container.get_source('_rand_') == {'demux': False, 'mux': False, 'low': 0, 'high': 1, 'enabled': True, 'seed': None, 'm': 1} )
        self.assertTrue(
            container.get_source('_rand_') == {
                'demux': False,
                'mux': False,
                'value': 2,
                'enabled': True
            })

        self.assertTrue(
            container.get_source('_rand_', 'value', 'demux') == {
                'value': 2,
                'demux': False
            })

        self.assertTrue(container.get_source('_rand_', 'value') == 2)

        container.set_source('_rand_', value=1)

        self.assertTrue(container.get_source('_rand_', 'value') == 1)

        # TODO: test for changed signals

        container.set_source('_rand_', reset=True)

        a = container.read_source('_rand_')
        self.assertTrue(isinstance(a, tuple))
        self.assertTrue(0 <= a[0] <= 1)

        with self.assertRaises(block.BlockException):
            container.set_source('_rand_', _reset=True)

        container.remove_source('_rand_')
        self.assertTrue('_rand_' not in container.list_sources())

        # test filter

        container.add_signal('_output_')

        #container.add_source('_rand_', blkrnd.Uniform(), ['_test_'])
        container.add_source('_rand_', block.Constant(), ['_test_'])
        self.assertTrue('_rand_' in container.list_sources())

        container.add_filter('_gain_', block.ShortCircuit(), ['_test_'],
                             ['_output_'])
        self.assertTrue('_gain_' in container.list_filters())

        # TODO: test for changed block

        container.add_filter('_gain_', system.Gain(gain=2), ['_test_'],
                             ['_output_'])
        self.assertTrue('_gain_' in container.list_filters())

        self.assertTrue(
            container.get_filter('_gain_') == {
                'demux': False,
                'enabled': True,
                'gain': 2,
                'mux': False
            })

        self.assertTrue(
            container.get_filter('_gain_', 'demux', 'gain') == {
                'demux': False,
                'gain': 2
            })

        self.assertTrue(container.get_filter('_gain_', 'gain') == 2)

        container.add_sink('_logger_', block.Logger(), ['_test_', '_output_'])
        self.assertTrue('_logger_' in container.list_sinks())

        container.set_enabled(True)
        container.run()
        container.run()
        container.set_enabled(False)

        log = container.get_sink('_logger_', 'log')
        self.assertTrue(isinstance(log['_test_'], numpy.ndarray))
        self.assertTrue(isinstance(log['_output_'], numpy.ndarray))
        self.assertShape(log['_test_'].shape, (2, 1))
        self.assertShape(log['_output_'].shape, (2, 1))

        self.assertTrue(numpy.array_equal(log['_output_'], log['_test_'] * 2))

        # test reset
        signals = container.list_signals()
        sinks = container.list_sinks()
        sources = container.list_sources()
        filters = container.list_filters()
        print(signals, sources, filters, sinks)

        print(container.info('all'))

        container.reset()

        container = Container()

        signals = container.list_signals()
        sinks = container.list_sinks()
        sources = container.list_sources()
        filters = container.list_filters()
        print(signals, sources, filters, sinks)

        self.assertTrue(signals == _signals)
        self.assertTrue(sources == _sources)
        self.assertTrue(filters == _filters)
        self.assertTrue(sinks == _sinks)

        print(container.info('all'))

        container.add_signal('timer')
        container.add_timer('timer', block.Constant(value=1), None, ['timer'],
                            1, False)

        print(container.info('all'))

        self.assertTrue(container.get_signal('timer') == 0)

        self.assertTrue(
            container.get_timer('timer') == {
                'enabled': True,
                'demux': False,
                'mux': False,
                'value': 1
            })

        self.assertTrue(
            container.get_timer('timer', 'enabled', 'demux') == {
                'enabled': True,
                'demux': False
            })

        self.assertTrue(container.get_timer('timer', 'enabled') == True)

        container.set_enabled(True)
        container.run()
        time.sleep(2)
        container.run()
        container.set_enabled(False)

        self.assertTrue(container.get_signal('timer') == 1)

        container.set_signal('timer', 0)
        self.assertTrue(container.get_signal('timer') == 0)

        container.set_enabled(True)
        container.run()
        time.sleep(.5)
        container.run()
        container.set_enabled(False)

        self.assertTrue(container.get_signal('timer') == 0)

        container.set_signal('timer', 0)
        self.assertTrue(container.get_signal('timer') == 0)

        container.add_timer('stop', block.Constant(value=0), None,
                            ['is_running'], 2, False)

        #print('##########')
        container.set_enabled(True)
        container.run()
        #print('##########')

        time.sleep(3)

        container.run()

        container.set_enabled(False)

        self.assertTrue(container.get_signal('timer') == 1)

        # test set
        container = Container()

        container.add_signals('s1', 's2')

        container.add_source('const', block.Constant(value=1), ['s1'])

        container.add_sink(
            'set1',
            logic.SetSource(label='const',
                            on_rise={'value': 0.6},
                            on_fall={'value': 0.4}), ['s2'])

        container.set_enabled(True)
        container.run()
        container.set_enabled(False)

        self.assertTrue(container.get_signal('s2') == 0)
        self.assertTrue(container.get_source('const', 'value') == 1)

        container.set_enabled(True)
        container.run()
        container.set_signal('s2', 1)
        container.run()
        container.set_enabled(False)

        self.assertTrue(container.get_signal('s2') == 1)
        self.assertTrue(container.get_source('const', 'value') == 0.6)

        container.set_enabled(True)
        container.run()
        container.set_signal('s2', 0)
        container.run()
        container.set_enabled(False)

        self.assertTrue(container.get_signal('s2') == 0)
        self.assertTrue(container.get_source('const', 'value') == 0.4)
예제 #4
0
def test_BufferBlock():

    # no mux, no demux
    obj = block.ShortCircuit()

    obj.write()
    assert obj.read() == ()

    obj.write(1)
    assert obj.read() == (1, )

    obj.write(1, 2)
    assert obj.read() == (1, 2)

    obj.write(1, numpy.array([2, 3]))
    assert len(obj.read()) == 2
    assert obj.read()[0] == 1
    assert numpy.array_equal(obj.read()[1], numpy.array([2, 3]))

    obj.write(1, numpy.array([2, 3]), numpy.array([4, 5]))
    assert len(obj.read()) == 3
    assert obj.read()[0] == 1
    assert numpy.array_equal(obj.read()[1], numpy.array([2, 3]))
    assert numpy.array_equal(obj.read()[2], numpy.array([4, 5]))

    # mux, no demux
    obj = block.ShortCircuit(mux=True)

    obj.write()
    assert obj.read() == ()

    obj.write(1)
    assert obj.read() == (1, )

    obj.write(1, 2)
    assert len(obj.read()) == 1
    assert numpy.array_equal(obj.read()[0], numpy.array([1, 2]))

    obj.write(1, numpy.array([2, 3]))
    assert len(obj.read()) == 1
    assert numpy.array_equal(obj.read()[0], numpy.array([1, 2, 3]))

    obj.write(1, numpy.array([2, 3]), numpy.array([4, 5]))
    assert len(obj.read()) == 1
    assert numpy.array_equal(obj.read()[0], numpy.array([1, 2, 3, 4, 5]))

    # mux, demux
    obj = block.ShortCircuit(mux=True, demux=True)

    obj.write()
    assert obj.read() == ()

    obj.write(1)
    assert obj.read() == (1, )

    obj.write(1, 2)
    assert len(obj.read()) == 2
    assert obj.read() == (1, 2)

    obj.write(1, numpy.array([2, 3]))
    assert len(obj.read()) == 3
    assert obj.read() == (1, 2, 3)

    obj.write(1, numpy.array([2, 3]), numpy.array([4, 5]))
    assert len(obj.read()) == 5
    assert obj.read() == (1, 2, 3, 4, 5)

    with pytest.raises(block.BlockException):
        obj = block.ShortCircuit(asd=1)
예제 #5
0
def test_html():

    obj = block.ShortCircuit()

    print(obj.html())
예제 #6
0
def test_container():

    import pyctrl
    import numpy
    import pyctrl.block as block
    import pyctrl.block.system as system
    import pyctrl.block.logic as logic
    import pyctrl.block.random as blkrnd

    from pyctrl import BlockType
    from pyctrl.block.container import Container
    container = Container()

    # initial signals
    _signals = container.list_signals()
    _sinks = container.list_sinks()
    _sources = container.list_sources()
    _filters = container.list_filters()

    assert not container.is_enabled()

    container.add_signal('_test_')
    assert '_test_' in container.list_signals()

    #with pytest.raises(pyctrl.block.container.ContainerException):
    container.add_signal('_test_')

    assert container.get_signal('_test_') == 0

    container.set_signal('_test_', 1.2)
    assert container.get_signal('_test_') == 1.2

    container.remove_signal('_test_')
    assert '_test_' not in container.list_signals()

    with pytest.raises(pyctrl.block.container.ContainerException):
        container.set_signal('_test_', 1.2)

    container.add_signals('_test1_', '_test2_')
    assert '_test1_' in container.list_signals()
    assert '_test2_' in container.list_signals()

    container.remove_signal('_test1_')
    container.remove_signal('_test2_')
    assert '_test1_' not in container.list_signals()
    assert '_test2_' not in container.list_signals()

    # test info
    assert isinstance(container.info(), str)
    assert isinstance(container.info('summary'), str)
    assert isinstance(container.info('source', 'sink'), str)

    # test sink

    container.add_signal('clock')

    container.add_sink('_logger_', block.Logger(), ['_test_'])
    assert '_logger_' in container.list_sinks()
    assert '_test_' in container.list_signals()

    assert container.get_sink('_logger_') == {
        'current': 0,
        'auto_reset': False,
        'page': 0,
        'enabled': True,
        'labels': ['_test_'],
        'index': None
    }

    assert container.get_sink('_logger_', 'current', 'auto_reset') == {
        'current': 0,
        'auto_reset': False
    }

    assert container.get_sink('_logger_', 'current') == 0

    container.set_sink('_logger_', current=1)

    assert container.get_sink('_logger_', 'current') == 1

    # try to remove signal _test_
    container.remove_signal('_test_')
    assert '_test_' in container.list_signals()

    container.add_sink('_logger_', block.Logger(), ['clock'])
    assert '_logger_' in container.list_sinks()

    # TODO: test for changed signals

    container.set_sink('_logger_', reset=True)

    log = container.get_sink('_logger_', 'log')
    assert isinstance(log['clock'], numpy.ndarray)
    assert log['clock'].shape == (0, 1)

    with pytest.raises(block.BlockException):
        container.set_sink('_logger_', _reset=True)

    container.set_enabled(True)
    container.run()
    container.run()
    container.set_enabled(False)

    log = container.get_sink('_logger_', 'log')

    #print(log)
    assert isinstance(log['clock'], numpy.ndarray)
    assert log['clock'].shape == (2, 1)

    container.set_sink('_logger_', reset=True)
    log = container.get_sink('_logger_', 'log')
    assert isinstance(log['clock'], numpy.ndarray)
    assert log['clock'].shape == (0, 1)

    container.remove_sink('_logger_')
    assert '_logger_' not in container.list_sinks()

    container.add_signal('_test_')

    container.add_sink('_logger_', block.Logger(), ['clock', '_test_'])
    assert '_logger_' in container.list_sinks()

    container.set_enabled(True)
    container.run()
    container.run()
    container.set_enabled(False)

    log = container.get_sink('_logger_', 'log')
    assert isinstance(log['clock'], numpy.ndarray)
    assert isinstance(log['_test_'], numpy.ndarray)
    assert log['clock'].shape == (2, 1)
    assert log['_test_'].shape == (2, 1)

    container.set_sink('_logger_', reset=True)
    log = container.get_sink('_logger_', 'log')
    assert isinstance(log['clock'], numpy.ndarray)
    assert isinstance(log['_test_'], numpy.ndarray)
    assert log['clock'].shape == (0, 1)
    assert log['_test_'].shape == (0, 1)

    container.remove_sink('_logger_')
    assert '_logger_' not in container.list_sinks()

    # test source

    container.add_source('_rand_', blkrnd.Uniform(), ['clock'])
    assert '_rand_' in container.list_sources()

    container.add_source('_rand_', blkrnd.Uniform(), ['_test_'])
    assert '_rand_' in container.list_sources()

    assert container.get_source('_rand_') == {
        'demux': False,
        'mux': False,
        'low': 0,
        'high': 1,
        'enabled': True,
        'seed': None,
        'm': 1
    }

    assert container.get_source('_rand_', 'low', 'high') == {
        'low': 0,
        'high': 1
    }

    assert container.get_source('_rand_', 'low') == 0

    container.set_source('_rand_', low=1)

    assert container.get_source('_rand_', 'low') == 1

    # TODO: test for changed signals

    container.set_source('_rand_', reset=True)

    a = container.read_source('_rand_')
    assert isinstance(a[0], float)
    assert 0 <= a[0] <= 1

    with pytest.raises(block.BlockException):
        container.set_source('_rand_', _reset=True)

    container.remove_source('_rand_')
    assert '_rand_' not in container.list_sources()

    # test filter

    container.add_signal('_output_')

    container.add_source('_rand_', blkrnd.Uniform(), ['_test_'])
    assert '_rand_' in container.list_sources()

    container.add_filter('_gain_', block.ShortCircuit(), ['_test_'],
                         ['_output_'])
    assert '_gain_' in container.list_filters()

    # TODO: test for changed block

    container.add_filter('_gain_', system.Gain(gain=2), ['_test_'],
                         ['_output_'])
    assert '_gain_' in container.list_filters()

    assert container.get_filter('_gain_') == {
        'demux': False,
        'enabled': True,
        'gain': 2,
        'mux': False
    }

    assert container.get_filter('_gain_', 'demux', 'gain') == {
        'demux': False,
        'gain': 2
    }

    assert container.get_filter('_gain_', 'gain') == 2

    container.add_sink('_logger_', block.Logger(), ['_test_', '_output_'])
    assert '_logger_' in container.list_sinks()

    container.set_enabled(True)
    container.run()
    container.run()
    container.set_enabled(False)

    log = container.get_sink('_logger_', 'log')
    assert isinstance(log['_test_'], numpy.ndarray)
    assert isinstance(log['_output_'], numpy.ndarray)
    assert log['_test_'].shape == (2, 1)
    assert log['_output_'].shape == (2, 1)

    assert numpy.all(numpy.fabs(log['_output_'] - 2 * log['_test_']) < 1e-6)

    # test reset
    signals = container.list_signals()
    sinks = container.list_sinks()
    sources = container.list_sources()
    filters = container.list_filters()
    print(signals, sources, filters, sinks)

    print(container.info('all'))

    container.reset()

    container = Container()

    signals = container.list_signals()
    sinks = container.list_sinks()
    sources = container.list_sources()
    filters = container.list_filters()
    print(signals, sources, filters, sinks)

    assert signals == _signals
    assert sources == _sources
    assert filters == _filters
    assert sinks == _sinks

    print(container.info('all'))

    container.add_signal('timer')
    container.add_timer('timer', block.Constant(value=1), None, ['timer'], 1,
                        False)

    print(container.info('all'))

    assert container.get_signal('timer') == 0

    assert container.get_timer('timer') == {
        'enabled': True,
        'demux': False,
        'mux': False,
        'value': 1
    }

    assert container.get_timer('timer', 'enabled', 'demux') == {
        'enabled': True,
        'demux': False
    }

    assert container.get_timer('timer', 'enabled') == True

    container.set_enabled(True)
    container.run()
    time.sleep(2)
    container.run()
    container.set_enabled(False)

    assert container.get_signal('timer') == 1

    container.set_signal('timer', 0)
    assert container.get_signal('timer') == 0

    container.set_enabled(True)
    container.run()
    time.sleep(.5)
    container.run()
    container.set_enabled(False)

    assert container.get_signal('timer') == 0

    container.set_signal('timer', 0)
    assert container.get_signal('timer') == 0

    container.add_timer('stop', block.Constant(value=0), None, ['is_running'],
                        2, False)

    #print('##########')
    container.set_enabled(True)
    container.run()
    #print('##########')

    time.sleep(3)

    container.run()

    container.set_enabled(False)

    assert container.get_signal('timer') == 1

    # test set
    container = Container()

    print('* * * TEST SET * * *')

    print(container.info('all'))

    container.add_signals('s1', 's2')

    container.add_source('const', block.Constant(value=1), ['s1'])

    container.add_sink(
        'set1',
        logic.SetSource(label='const',
                        on_rise={'value': 0.6},
                        on_fall={'value': 0.4}), ['s2'])

    container.set_enabled(True)
    container.run()
    container.set_enabled(False)

    print(container.get_source('const'))

    assert container.get_signal('s2') == 0
    assert container.get_source('const', 'value') == 1

    container.set_enabled(True)
    container.run()
    container.set_signal('s2', 1)
    container.run()
    container.set_enabled(False)

    assert container.get_signal('s2') == 1
    assert container.get_source('const', 'value') == 0.6

    container.set_enabled(True)
    container.run()
    container.set_signal('s2', 0)
    container.run()
    container.set_enabled(False)

    assert container.get_signal('s2') == 0
    assert container.get_source('const', 'value') == 0.4
예제 #7
0
def _test_basic(controller):

    import pyctrl
    import numpy
    import pyctrl.block as block
    import pyctrl.block.system as system
    import pyctrl.block.random as blkrnd

    print('\n> * * * TEST BASIC {} * * *'.format(controller.__class__))

    # reset controller
    controller.reset()

    # initial signals
    _signals = controller.list_signals()
    _sinks = controller.list_sinks()
    _sources = controller.list_sources()
    _filters = controller.list_filters()

    # period
    #assert controller.get_period() == 0.01 # default
    #controller.set_period(0.1)
    #assert controller.get_period() == 0.1

    # test signals
    #assert 'clock' in controller.list_signals() # clock is default

    controller.add_signal('_test_')
    assert '_test_' in controller.list_signals()

    #with pytest.raises(pyctrl.ControllerException):
    controller.add_signal('_test_')

    assert controller.get_signal('_test_') == 0

    controller.set_signal('_test_', 1.2)
    assert controller.get_signal('_test_') == 1.2

    controller.remove_signal('_test_')
    assert '_test_' not in controller.list_signals()

    with pytest.raises(Exception):
        controller.set_signal('_test_', 1.2)

    controller.add_signals('_test1_', '_test2_')
    assert '_test1_' in controller.list_signals()
    assert '_test2_' in controller.list_signals()

    controller.remove_signal('_test1_')
    controller.remove_signal('_test2_')
    assert '_test1_' not in controller.list_signals()
    assert '_test2_' not in controller.list_signals()

    # test info
    assert isinstance(controller.info(), str)
    assert isinstance(controller.info('summary'), str)
    assert isinstance(controller.info('sources', 'sinks'), str)

    # test sink

    controller.add_signal('clock')

    logger = block.Logger()
    controller.add_sink('_logger_', logger, ['_test_'])
    assert '_logger_' in controller.list_sinks()
    assert '_test_' in controller.list_signals()

    assert controller.get_sink('_logger_') == {
        'current': 0,
        'auto_reset': False,
        'page': 0,
        'enabled': True,
        'labels': ['_test_'],
        'index': None
    }

    assert controller.get_sink('_logger_', 'current', 'auto_reset') == {
        'current': 0,
        'auto_reset': False
    }

    assert controller.get_sink('_logger_', 'current') == 0

    controller.set_sink('_logger_', current=1)

    assert controller.get_sink('_logger_', 'current') == 1

    # try to remove signal _test_
    controller.remove_signal('_test_')
    assert '_test_' in controller.list_signals()

    controller.add_sink('_logger_', block.Logger(), ['clock'])
    assert '_logger_' in controller.list_sinks()

    # TODO: test for changed signals

    controller.set_sink('_logger_', reset=True)

    log = controller.get_sink('_logger_', 'log')

    assert not hasattr(logger, 'log')

    assert isinstance(log['clock'], numpy.ndarray)
    assert log['clock'].shape == (0, 1)

    with pytest.raises(block.BlockException):
        controller.set_sink('_logger_', _reset=True)

    with controller:
        time.sleep(.2)

    log = controller.get_sink('_logger_', 'log')

    assert isinstance(log['clock'], numpy.ndarray)
    assert log['clock'].shape[1] == 1
    assert log['clock'].shape[0] > 1

    controller.set_sink('_logger_', reset=True)

    log = controller.get_sink('_logger_', 'log')

    assert isinstance(log['clock'], numpy.ndarray)
    assert log['clock'].shape == (0, 1)

    controller.remove_sink('_logger_')
    assert '_logger_' not in controller.list_sinks()

    controller.add_signal('_test_')

    controller.add_sink('_logger_', block.Logger(), ['clock', '_test_'])
    assert '_logger_' in controller.list_sinks()

    with controller:
        time.sleep(.2)

    log = controller.get_sink('_logger_', 'log')

    assert isinstance(log['clock'], numpy.ndarray)
    assert isinstance(log['_test_'], numpy.ndarray)
    assert log['clock'].shape[1] == 1
    assert log['clock'].shape[0] > 1
    assert log['_test_'].shape[1] == 1
    assert log['_test_'].shape[0] > 1

    controller.set_sink('_logger_', reset=True)

    log = controller.get_sink('_logger_', 'log')

    assert isinstance(log['clock'], numpy.ndarray)
    assert isinstance(log['_test_'], numpy.ndarray)
    assert log['clock'].shape == (0, 1)
    assert log['_test_'].shape == (0, 1)

    controller.remove_sink('_logger_')
    assert '_logger_' not in controller.list_sinks()

    # test source

    controller.add_source('_rand_', blkrnd.Uniform(), ['clock'])
    assert '_rand_' in controller.list_sources()

    controller.add_source('_rand_', blkrnd.Uniform(), ['_test_'])
    assert '_rand_' in controller.list_sources()

    assert controller.get_source('_rand_') == {
        'demux': False,
        'mux': False,
        'low': 0,
        'high': 1,
        'enabled': True,
        'seed': None,
        'm': 1
    }

    assert controller.get_source('_rand_', 'low', 'high') == {
        'low': 0,
        'high': 1
    }

    assert controller.get_source('_rand_', 'low') == 0

    controller.set_source('_rand_', low=1)

    assert controller.get_source('_rand_', 'low') == 1

    # TODO: test for changed signals

    controller.set_source('_rand_', reset=True)

    a = controller.read_source('_rand_')
    assert isinstance(a[0], float)
    assert 0 <= a[0] <= 1

    with pytest.raises(block.BlockException):
        controller.set_source('_rand_', _reset=True)

    controller.remove_source('_rand_')
    assert '_rand_' not in controller.list_sources()

    # test filter

    controller.add_signal('_output_')

    controller.add_source('_rand_', blkrnd.Uniform(), ['_test_'])
    assert '_rand_' in controller.list_sources()

    controller.add_filter('_gain_', block.ShortCircuit(), ['_test_'],
                          ['_output_'])
    assert '_gain_' in controller.list_filters()

    # TODO: test for changed block

    controller.add_filter('_gain_', system.Gain(gain=2), ['_test_'],
                          ['_output_'])
    assert '_gain_' in controller.list_filters()

    assert controller.get_filter('_gain_') == {
        'demux': False,
        'enabled': True,
        'gain': 2,
        'mux': False
    }

    assert controller.get_filter('_gain_', 'demux', 'gain') == {
        'demux': False,
        'gain': 2
    }

    assert controller.get_filter('_gain_', 'gain') == 2

    with controller:
        time.sleep(.2)

    controller.add_sink('_logger_', block.Logger(), ['_test_', '_output_'])
    assert '_logger_' in controller.list_sinks()

    with controller:
        time.sleep(.2)

    log = controller.get_sink('_logger_', 'log')

    assert isinstance(log['_test_'], numpy.ndarray)
    assert isinstance(log['_output_'], numpy.ndarray)
    assert log['_test_'].shape[1] == 1
    assert log['_test_'].shape[0] > 1
    assert log['_test_'].shape[1] == 1
    assert log['_test_'].shape[0] > 1

    assert numpy.all(numpy.fabs(log['_output_'] - 2 * log['_test_']) < 1e-6)

    # test reset
    signals = controller.list_signals()
    sinks = controller.list_sinks()
    sources = controller.list_sources()
    filters = controller.list_filters()

    controller.reset()

    signals = controller.list_signals()
    sinks = controller.list_sinks()
    sources = controller.list_sources()
    filters = controller.list_filters()

    assert signals == _signals
    assert sources == _sources
    assert filters == _filters
    assert sinks == _sinks

    # test is_running
    assert controller.get_signal('is_running') == False

    controller.start()
    assert controller.get_signal('is_running') == True

    controller.stop()
    assert controller.get_signal('is_running') == False
예제 #8
0
    def run_test_basic(self, controller):

        import numpy
        import pyctrl.block as block
        import pyctrl.block.system as system

        print('\n> * * * TEST BASIC {} * * *'.format(controller.__class__))

        # reset controller
        controller.reset()

        # initial signals
        _signals = controller.list_signals()
        _sinks = controller.list_sinks()
        _sources = controller.list_sources()
        _filters = controller.list_filters()

        # period
        # self.assertTrue( controller.get_period() == 0.01 # default
        # controller.set_period(0.1)
        # self.assertTrue( controller.get_period() == 0.1

        # test signals
        # self.assertTrue( 'clock' in controller.list_signals() # clock is default

        controller.add_signal('_test_')
        self.assertTrue('_test_' in controller.list_signals())

        # with self.assertRaises(pyctrl.ControllerException):
        controller.add_signal('_test_')

        self.assertTrue(controller.get_signal('_test_') == 0)

        controller.set_signal('_test_', 1.2)
        self.assertTrue(controller.get_signal('_test_') == 1.2)

        controller.remove_signal('_test_')
        self.assertTrue('_test_' not in controller.list_signals())

        with self.assertRaises(Exception):
            controller.set_signal('_test_', 1.2)

        controller.add_signals('_test1_', '_test2_')
        self.assertTrue('_test1_' in controller.list_signals())
        self.assertTrue('_test2_' in controller.list_signals())

        controller.remove_signal('_test1_')
        controller.remove_signal('_test2_')
        self.assertTrue('_test1_' not in controller.list_signals())
        self.assertTrue('_test2_' not in controller.list_signals())

        # test info
        self.assertTrue(isinstance(controller.info(), str))
        self.assertTrue(isinstance(controller.info('summary'), str))
        self.assertTrue(isinstance(controller.info('sources', 'sinks'), str))

        # test sink

        controller.add_signal('clock')

        logger = block.Logger()
        controller.add_sink('_logger_', logger, ['_test_'])
        self.assertTrue('_logger_' in controller.list_sinks())
        self.assertTrue('_test_' in controller.list_signals())

        self.assertTrue(
            controller.get_sink('_logger_') == {
                'current': 0,
                'auto_reset': False,
                'page': 0,
                'enabled': True,
                'labels': ['_test_'],
                'index': None
            })

        self.assertTrue(
            controller.get_sink('_logger_', 'current', 'auto_reset') == {
                'current': 0,
                'auto_reset': False
            })

        self.assertTrue(controller.get_sink('_logger_', 'current') == 0)

        controller.set_sink('_logger_', current=1)

        self.assertTrue(controller.get_sink('_logger_', 'current') == 1)

        # try to remove signal _test_
        controller.remove_signal('_test_')
        self.assertTrue('_test_' in controller.list_signals())

        controller.add_sink('_logger_', block.Logger(), ['clock'])
        self.assertTrue('_logger_' in controller.list_sinks())

        # TODO: test for changed signals

        controller.set_sink('_logger_', reset=True)

        log = controller.get_sink('_logger_', 'log')

        self.assertTrue(not hasattr(logger, 'log'))

        self.assertTrue(isinstance(log['clock'], numpy.ndarray))
        self.assertShape(log['clock'].shape, (0, 1))

        with self.assertRaises(block.BlockException):
            controller.set_sink('_logger_', _reset=True)

        with controller:
            time.sleep(.2)

        log = controller.get_sink('_logger_', 'log')

        self.assertTrue(isinstance(log['clock'], numpy.ndarray))
        self.assertShape(log['clock'].shape, 1, 1)
        self.assertShapeGreater(log['clock'].shape, 1, 0)

        controller.set_sink('_logger_', reset=True)

        log = controller.get_sink('_logger_', 'log')

        self.assertTrue(isinstance(log['clock'], numpy.ndarray))
        self.assertShape(log['clock'].shape, (0, 1))

        controller.remove_sink('_logger_')
        self.assertTrue('_logger_' not in controller.list_sinks())

        controller.add_signal('_test_')

        controller.add_sink('_logger_', block.Logger(), ['clock', '_test_'])
        self.assertTrue('_logger_' in controller.list_sinks())

        with controller:
            time.sleep(.2)

        log = controller.get_sink('_logger_', 'log')

        self.assertTrue(isinstance(log['clock'], numpy.ndarray))
        self.assertTrue(isinstance(log['_test_'], numpy.ndarray))
        self.assertShape(log['clock'].shape, 1, 1)
        self.assertShapeGreater(log['clock'].shape, 1, 0)
        self.assertShape(log['_test_'].shape, 1, 1)
        self.assertShapeGreater(log['_test_'].shape, 1, 0)

        controller.set_sink('_logger_', reset=True)

        log = controller.get_sink('_logger_', 'log')

        self.assertTrue(isinstance(log['clock'], numpy.ndarray))
        self.assertTrue(isinstance(log['_test_'], numpy.ndarray))
        self.assertShape(log['clock'].shape, (0, 1))
        self.assertShape(log['_test_'].shape, (0, 1))

        controller.remove_sink('_logger_')
        self.assertTrue('_logger_' not in controller.list_sinks())

        # test source

        # controller.add_source('_rand_', blkrnd.Uniform(), ['clock'])
        controller.add_source('_rand_', block.Constant(), ['clock'])
        self.assertTrue('_rand_' in controller.list_sources())

        # controller.add_source('_rand_', blkrnd.Uniform(), ['_test_'])
        controller.add_source('_rand_', block.Constant(value=2), ['_test_'])
        self.assertTrue('_rand_' in controller.list_sources())

        self.assertTrue(
            controller.get_source('_rand_') == {
                'demux': False,
                'mux': False,
                'value': 2,
                'enabled': True
            })

        self.assertTrue(
            controller.get_source('_rand_', 'value', 'demux') == {
                'value': 2,
                'demux': False
            })

        self.assertTrue(controller.get_source('_rand_', 'value') == 2)

        controller.set_source('_rand_', value=1)

        self.assertTrue(controller.get_source('_rand_', 'value') == 1)

        # TODO: test for changed signals

        controller.set_source('_rand_', reset=True)

        a, = controller.read_source('_rand_')
        self.assertTrue(a == 1)

        with self.assertRaises(block.BlockException):
            controller.set_source('_rand_', _reset=True)

        controller.remove_source('_rand_')
        self.assertTrue('_rand_' not in controller.list_sources())

        # test filter

        controller.add_signal('_output_')

        controller.add_source('_rand_', block.Constant(), ['_test_'])
        self.assertTrue('_rand_' in controller.list_sources())

        controller.add_filter('_gain_', block.ShortCircuit(), ['_test_'],
                              ['_output_'])
        self.assertTrue('_gain_' in controller.list_filters())

        # TODO: test for changed block

        controller.add_filter('_gain_', system.Gain(gain=2), ['_test_'],
                              ['_output_'])
        self.assertTrue('_gain_' in controller.list_filters())

        self.assertTrue(
            controller.get_filter('_gain_') == {
                'demux': False,
                'enabled': True,
                'gain': 2,
                'mux': False
            })

        self.assertTrue(
            controller.get_filter('_gain_', 'demux', 'gain') == {
                'demux': False,
                'gain': 2
            })

        self.assertTrue(controller.get_filter('_gain_', 'gain') == 2)

        with controller:
            time.sleep(.2)

        controller.add_sink('_printer_', block.Printer(),
                            ['_test_', '_output_'])
        self.assertTrue('_printer_' in controller.list_sinks())

        controller.add_sink('_logger_', block.Logger(), ['_test_', '_output_'])
        self.assertTrue('_logger_' in controller.list_sinks())

        with controller:
            time.sleep(.2)

        log = controller.get_sink('_logger_', 'log')

        self.assertTrue(isinstance(log['_test_'], numpy.ndarray))
        self.assertTrue(isinstance(log['_output_'], numpy.ndarray))
        self.assertShape(log['_test_'].shape, 1, 1)
        self.assertShapeGreater(log['_test_'].shape, 1, 0)
        self.assertShape(log['_test_'].shape, 1, 1)
        self.assertShapeGreater(log['_test_'].shape, 1, 0)

        self.assertTrue(numpy.array_equal(log['_output_'], log['_test_'] * 2))

        # test reset
        signals = controller.list_signals()
        sinks = controller.list_sinks()
        sources = controller.list_sources()
        filters = controller.list_filters()

        controller.reset()

        signals = controller.list_signals()
        sinks = controller.list_sinks()
        sources = controller.list_sources()
        filters = controller.list_filters()

        self.assertTrue(signals == _signals)
        self.assertTrue(sources == _sources)
        self.assertTrue(filters == _filters)
        self.assertTrue(sinks == _sinks)

        # test is_running
        self.assertTrue(controller.get_signal('is_running') == False)

        controller.start()
        time.sleep(1)
        self.assertTrue(controller.get_signal('is_running') == True)

        controller.stop()
        time.sleep(1)
        self.assertTrue(controller.get_signal('is_running') == False)