async def test_time_ps_deprecated(_): with assert_deprecated(): Timer(time_ps=7, units='ns') with assert_raises(TypeError): Timer(time=0, time_ps=7, units='ns') with assert_raises(TypeError): Timer(units='ps')
def test_wait_for_services(): c = CapabilitiesClient() with assert_raises(ValueError): c.wait_for_services(0.1, ['invalid_service']) assert c.wait_for_services(0.1) is False c.establish_bond(0.1) c._used_capabilities.add('some_pkg/SomeCap') with assert_raises(ServiceNotAvailableException): c.free_capability('some_pkg/SomeCap', 0.1)
def test_list_of_lists_with_index_firstrow(): "Output: a table with a running index and header='firstrow'" dd = zip(*[["a"] + list(range(3)), ["b"] + list(range(101, 104))]) expected = "\n".join([ " a b", "-- --- ---", " 0 0 101", " 1 1 102", " 2 2 103" ]) result = tabulate(dd, headers="firstrow", showindex=True) assert_equal(result, expected) # TODO: make it a separate test case # the index must be as long as the number of rows assert_raises(ValueError, lambda: tabulate(dd, headers="firstrow", showindex=[1, 2]))
def test_list_of_lists_with_supplied_index(): "Output: a table with a supplied index" dd = zip(*[list(range(3)), list(range(101, 104))]) expected = "\n".join([ " a b", "-- --- ---", " 1 0 101", " 2 1 102", " 3 2 103" ]) result = tabulate(dd, headers=["a", "b"], showindex=[1, 2, 3]) assert_equal(result, expected) # TODO: make it a separate test case # the index must be as long as the number of rows assert_raises(ValueError, lambda: tabulate(dd, headers=["a", "b"], showindex=[1, 2]))
def test_list_of_lists_with_index_firstrow(): "Output: a table with a running index and header='firstrow'" dd = zip(*[["a"]+list(range(3)), ["b"]+list(range(101,104))]) expected = "\n".join([ ' a b', '-- --- ---', ' 0 0 101', ' 1 1 102', ' 2 2 103']) result = tabulate(dd, headers="firstrow", showindex=True) assert_equal(result, expected) # TODO: make it a separate test case # the index must be as long as the number of rows assert_raises(ValueError, lambda: tabulate(dd, headers="firstrow", showindex=[1,2]))
def test_list_of_lists_with_supplied_index(): "Output: a table with a supplied index" dd = zip(*[list(range(3)), list(range(101,104))]) expected = "\n".join([ ' a b', '-- --- ---', ' 1 0 101', ' 2 1 102', ' 3 2 103']) result = tabulate(dd, headers=["a","b"], showindex=[1,2,3]) assert_equal(result, expected) # TODO: make it a separate test case # the index must be as long as the number of rows assert_raises(ValueError, lambda: tabulate(dd, headers=["a","b"], showindex=[1,2]))
async def test_internalevent(dut): """Test _InternalEvent trigger.""" e = _InternalEvent('test parent') assert repr(e) == "'test parent'" async def set_internalevent(): await Timer(1, units='ns') e.set('data') # Test waiting more than once cocotb.start_soon(set_internalevent()) time_ns = get_sim_time(units='ns') await e assert e.is_set() assert e.data == 'data' assert get_sim_time(units='ns') == time_ns + 1 # _InternalEvent can only be awaited once with assert_raises(RuntimeError): await e e = _InternalEvent(None) assert repr(e) == 'None' ran = False async def await_internalevent(): nonlocal ran await e ran = True # Test multiple coroutines waiting await cocotb.start(await_internalevent()) assert not e.is_set() assert not ran # _InternalEvent can only be awaited by one coroutine with assert_raises(RuntimeError): await e e.set() await Timer(1) assert e.is_set() assert ran # Test waiting after set e = _InternalEvent(None) assert not e.is_set() cocotb.start_soon(set_internalevent()) await Timer(2, units='ns') assert e.is_set() time_ns = get_sim_time(units='ns') await e assert get_sim_time(units='ns') == time_ns
async def test_delayed_assignment_still_errors(dut): """ Writing a bad value should fail even if the write is scheduled to happen later """ # note: all these fail because BinaryValue.assign rejects them with assert_raises(ValueError): dut.stream_in_int.setimmediatevalue("1010 not a real binary string") with assert_raises(TypeError): dut.stream_in_int.setimmediatevalue([]) with assert_raises(ValueError): dut.stream_in_int <= "1010 not a real binary string" with assert_raises(TypeError): dut.stream_in_int <= []
async def test_timer_with_units(dut): time_fs = get_sim_time(units='fs') # Await for one simulator time step await Timer(1) # NOTE: explicitly no units argument here! time_step = get_sim_time(units='fs') - time_fs pattern = "Unable to accurately represent .* with the simulator precision of .*" with assert_raises(ValueError, pattern): await Timer(2.5*time_step, units='fs') dut._log.info("As expected, unable to create a timer of 2.5 simulator time steps") time_fs = get_sim_time(units='fs') await Timer(3, 'ns') assert get_sim_time(units='fs') == time_fs+3_000_000.0, "Expected a delay of 3 ns" time_fs = get_sim_time(units='fs') await Timer(1.5, 'ns') assert get_sim_time(units='fs') == time_fs+1_500_000.0, "Expected a delay of 1.5 ns" time_fs = get_sim_time(units='fs') await Timer(10.0, 'ps') assert get_sim_time(units='fs') == time_fs+10_000.0, "Expected a delay of 10 ps" time_fs = get_sim_time(units='fs') await Timer(1.0, 'us') assert get_sim_time(units='fs') == time_fs+1_000_000_000.0, "Expected a delay of 1 us"
async def test_assigning_setitem_syntax_deprecated(dut): with assert_deprecated(): dut.stream_in_data[0] = 1 with assert_deprecated(): with assert_raises(IndexError): # attempt to use __setitem__ syntax on signal that doesn't exist dut.stream_in_data[800000] = 1
async def test_logging_default_config(dut): # The cocotb.log module is shadowed by an instance of # cocotb.log.SimBaseLog() from cocotb.log import default_config as log_default_config cocotb_log = logging.getLogger('cocotb') # Save pre-test configuration log_level_prev = cocotb_log.level os_environ_prev = os.environ.copy() try: # Set a valid log level os.environ['COCOTB_LOG_LEVEL'] = 'DEBUG' log_default_config() assert cocotb_log.level == logging.DEBUG, cocotb_log.level # Try to set log level to an invalid log level os.environ['COCOTB_LOG_LEVEL'] = 'INVALID_LOG_LEVEL' with assert_raises(ValueError): log_default_config() # Try to set log level to a valid log level with wrong capitalization os.environ['COCOTB_LOG_LEVEL'] = 'error' log_default_config() assert cocotb_log.level == logging.ERROR, cocotb_log.level finally: # Restore pre-test configuration os.environ = os_environ_prev cocotb_log.level = log_level_prev
async def test_task_coroutine_function_exception(dut): async def coro(dut): pass pattern = "Coroutine function {} should be called " \ "prior to being scheduled.".format(coro) with assert_raises(TypeError, pattern): cocotb.decorators.RunningTask(coro)
async def test_fork_coroutine_function_exception(dut): async def coro(): pass pattern = "Coroutine function {} should be called " \ "prior to being scheduled.".format(coro) with assert_raises(TypeError, pattern): cocotb.start_soon(coro)
async def test_neg_timer(dut): """Test negative timer values are forbidden""" with assert_raises(TriggerException): Timer(-42) # no need to even `await`, constructing it is an error # handle 0 special case with warnings.catch_warnings(record=True) as w: warnings.simplefilter("always") Timer(0) assert "Timer setup with value 0, which might exhibit undefined behavior in some simulators" in str(w[-1].message) assert issubclass(w[-1].category, RuntimeWarning)
async def int_overflow_test(signal, n_bits, test_mode, limits=_Limits.VECTOR_NBIT): """Test integer overflow.""" if test_mode == "ovfl": value = gen_int_ovfl_value(n_bits, limits) elif test_mode == "unfl": value = gen_int_unfl_value(n_bits, limits) else: value = None with assert_raises(OverflowError): signal.value = value
async def test_bad_attr(dut): with assert_raises(AttributeError): fake_signal = dut.fake_signal try: _ = dut.stream_in_data.whoops except AttributeError as e: assert 'whoops' in str(e) else: assert False, "Expected AttributeError"
async def test_access_underscore_name(dut): """Test accessing HDL name starting with an underscore""" # direct access does not work because we consider such names cocotb-internal with assert_raises(AttributeError): dut._underscore_name # indirect access works dut._id("_underscore_name", extended=False) <= 0 await Timer(1, 'ns') assert dut._id("_underscore_name", extended=False).value == 0 dut._id("_underscore_name", extended=False) <= 1 await Timer(1, 'ns') assert dut._id("_underscore_name", extended=False).value == 1 dut._id("_underscore_name", extended=False) <= 0 await Timer(1, 'ns') assert dut._id("_underscore_name", extended=False).value == 0
async def test_raise_error_deprecated(dut): with assert_deprecated(): with assert_raises(cocotb.result.TestError): cocotb.result.raise_error(cocotb.triggers.Timer(1), "A test exception")
async def test_assigning_setattr_syntax_deprecated(dut): with assert_deprecated(): dut.stream_in_data = 1 with assert_raises(AttributeError): # attempt to use __setattr__ syntax on signal that doesn't exist dut.does_not_exist = 0
def test_list_of_dicts_with_list_of_headers(): "Input: ValueError on a list of headers with a list of dicts (issue #23)" table = [{"letters": "ABCDE", "digits": 12345}] headers = ["DIGITS", "LETTERS"] with assert_raises(ValueError): tabulate(table, headers=headers)
def test_coroutine_error(dut): """Error in a coroutine that we yield""" yield clock_gen(dut.clk) with assert_raises(NameError): yield erroring_coro()