def test_display_cache():
    """should display table for direct-mapped/set associative cache"""
    out = io.StringIO()
    with contextlib.redirect_stdout(out):
        sim.display_cache({
            '000': [
                {'tag': '0101', 'data': [88, 89]}
            ],
            '001': [
                {'tag': '0000', 'data': [2, 3]},
                {'tag': '0010', 'data': [42, 43]},
            ]
        }, table_width=TABLE_WIDTH)
    table_output = out.getvalue()
    num_cols = 2
    col_width = TABLE_WIDTH // num_cols
    nose.assert_regexp_matches(
        table_output, '{}\n{}'.format(
            'Cache'.center(TABLE_WIDTH),
            ('-' * TABLE_WIDTH)))
    nose.assert_equal(
        table_output.count('-'), TABLE_WIDTH * 2)
    nose.assert_regexp_matches(
        table_output, r'{}{}'.format(
            '000'.center(col_width),
            '001'.center(col_width)))
    nose.assert_regexp_matches(
        table_output, r'{}{}'.format(
            '88,89'.center(col_width),
            '2,3 42,43'.center(col_width)))
def test_display_cache_fully_assoc():
    """should correctly display table for fully associative cache"""
    out = io.StringIO()
    with contextlib.redirect_stdout(out):
        sim.display_cache({
            '0': [
                {'tag': '0000001', 'data': [2, 3]},
                {'tag': '1111110', 'data': [252, 253]}
            ]
        }, table_width=TABLE_WIDTH)
    table_output = out.getvalue()
    nose.assert_regexp_matches(
        table_output, '{}\n{}'.format(
            'Cache'.center(TABLE_WIDTH),
            ('-' * TABLE_WIDTH)))
    nose.assert_equal(
        table_output.count('-'), TABLE_WIDTH)
    nose.assert_regexp_matches(
        table_output, '2,3 252,253'.center(TABLE_WIDTH))