Skip to content

Commit

Permalink
Drop unittest from TestCaseWithSimulator (kuznia-rdzeni/coreblock…
Browse files Browse the repository at this point in the history
  • Loading branch information
lekcyjna123 authored Apr 10, 2024
1 parent 030ab18 commit ac075e9
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 121 deletions.
4 changes: 2 additions & 2 deletions test/test_connectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ def process():

for i in range(total):
out = yield m.outputs[i].data
self.assertEqual(out, expected_output_prefix[i])
assert out == expected_output_prefix[i]

self.assertEqual((yield m.output_cnt), total)
assert (yield m.output_cnt) == total
yield

with self.run_simulation(m) as sim:
Expand Down
85 changes: 38 additions & 47 deletions test/test_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def elaborate(self, platform):


class TestHwCounter(TestCaseWithSimulator):
def setUp(self) -> None:
def setup_method(self) -> None:
random.seed(42)

def test_counter_in_method(self):
Expand All @@ -86,7 +86,7 @@ def test_process():

# Note that it takes one cycle to update the register value, so here
# we are comparing the "previous" values.
self.assertEqual(called_cnt, (yield m._dut.counter.count.value))
assert called_cnt == (yield m._dut.counter.count.value)

if call_now:
called_cnt += 1
Expand All @@ -111,7 +111,7 @@ def test_process():

# Note that it takes one cycle to update the register value, so here
# we are comparing the "previous" values.
self.assertEqual(called_cnt, (yield m._dut.counter.count.value))
assert called_cnt == (yield m._dut.counter.count.value)

if call_now and condition == 1:
called_cnt += 1
Expand All @@ -133,7 +133,7 @@ def test_process():

# Note that it takes one cycle to update the register value, so here
# we are comparing the "previous" values.
self.assertEqual(called_cnt, (yield m.counter.count.value))
assert called_cnt == (yield m.counter.count.value)

if condition == 1:
called_cnt += 1
Expand Down Expand Up @@ -173,7 +173,7 @@ def elaborate(self, platform):


class TestTaggedCounter(TestCaseWithSimulator):
def setUp(self) -> None:
def setup_method(self) -> None:
random.seed(42)

def do_test_enum(self, tags: range | Type[Enum] | list[int], tag_values: list[int]):
Expand All @@ -187,7 +187,7 @@ def do_test_enum(self, tags: range | Type[Enum] | list[int], tag_values: list[in
def test_process():
for _ in range(200):
for i in tag_values:
self.assertEqual(counts[i], (yield m.counter.counters[i].value))
assert counts[i] == (yield m.counter.counters[i].value)

tag = random.choice(list(tag_values))

Expand Down Expand Up @@ -290,38 +290,38 @@ def test_process():
histogram = m._dut.histogram
# Skip the assertion if the min is still uninitialized
if min != max_sample_value + 1:
self.assertEqual(min, (yield histogram.min.value))
assert min == (yield histogram.min.value)

self.assertEqual(max, (yield histogram.max.value))
self.assertEqual(sum, (yield histogram.sum.value))
self.assertEqual(count, (yield histogram.count.value))
assert max == (yield histogram.max.value)
assert sum == (yield histogram.sum.value)
assert count == (yield histogram.count.value)

total_count = 0
for i in range(self.bucket_count):
bucket_value = yield histogram.buckets[i].value
total_count += bucket_value
self.assertEqual(buckets[i], bucket_value)
assert buckets[i] == bucket_value

# Sanity check if all buckets sum up to the total count value
self.assertEqual(total_count, (yield histogram.count.value))
assert total_count == (yield histogram.count.value)

with self.run_simulation(m) as sim:
sim.add_sync_process(test_process)


class TestLatencyMeasurerBase(TestCaseWithSimulator):
def check_latencies(self, m: SimpleTestCircuit, latencies: list[int]):
self.assertEqual(min(latencies), (yield m._dut.histogram.min.value))
self.assertEqual(max(latencies), (yield m._dut.histogram.max.value))
self.assertEqual(sum(latencies), (yield m._dut.histogram.sum.value))
self.assertEqual(len(latencies), (yield m._dut.histogram.count.value))
assert min(latencies) == (yield m._dut.histogram.min.value)
assert max(latencies) == (yield m._dut.histogram.max.value)
assert sum(latencies) == (yield m._dut.histogram.sum.value)
assert len(latencies) == (yield m._dut.histogram.count.value)

for i in range(m._dut.histogram.bucket_count):
bucket_start = 0 if i == 0 else 2 ** (i - 1)
bucket_end = 1e10 if i == m._dut.histogram.bucket_count - 1 else 2**i

count = sum(1 for x in latencies if bucket_start <= x < bucket_end)
self.assertEqual(count, (yield m._dut.histogram.buckets[i].value))
assert count == (yield m._dut.histogram.buckets[i].value)


@parameterized_class(
Expand Down Expand Up @@ -495,37 +495,28 @@ def test_metrics_metadata(self):
with self.run_simulation(m):
pass

self.assertEqual(
metrics_manager.get_metrics()["foo.counter1"].to_json(), # type: ignore
json.dumps(
{
"fully_qualified_name": "foo.counter1",
"description": "this is the description",
"regs": {"count": {"name": "count", "description": "the value of the counter", "width": 32}},
}
),
assert metrics_manager.get_metrics()["foo.counter1"].to_json() == json.dumps( # type: ignore
{
"fully_qualified_name": "foo.counter1",
"description": "this is the description",
"regs": {"count": {"name": "count", "description": "the value of the counter", "width": 32}},
}
)

self.assertEqual(
metrics_manager.get_metrics()["bar.baz.counter2"].to_json(), # type: ignore
json.dumps(
{
"fully_qualified_name": "bar.baz.counter2",
"description": "",
"regs": {"count": {"name": "count", "description": "the value of the counter", "width": 32}},
}
),
assert metrics_manager.get_metrics()["bar.baz.counter2"].to_json() == json.dumps( # type: ignore
{
"fully_qualified_name": "bar.baz.counter2",
"description": "",
"regs": {"count": {"name": "count", "description": "the value of the counter", "width": 32}},
}
)

self.assertEqual(
metrics_manager.get_metrics()["bar.baz.counter3"].to_json(), # type: ignore
json.dumps(
{
"fully_qualified_name": "bar.baz.counter3",
"description": "yet another description",
"regs": {"count": {"name": "count", "description": "the value of the counter", "width": 32}},
}
),
assert metrics_manager.get_metrics()["bar.baz.counter3"].to_json() == json.dumps( # type: ignore
{
"fully_qualified_name": "bar.baz.counter3",
"description": "yet another description",
"regs": {"count": {"name": "count", "description": "the value of the counter", "width": 32}},
}
)

def test_returned_reg_values(self):
Expand All @@ -548,9 +539,9 @@ def test_process():
if rand[i] == 1:
counters[i] += 1

self.assertEqual(counters[0], (yield metrics_manager.get_register_value("foo.counter1", "count")))
self.assertEqual(counters[1], (yield metrics_manager.get_register_value("bar.baz.counter2", "count")))
self.assertEqual(counters[2], (yield metrics_manager.get_register_value("bar.baz.counter3", "count")))
assert counters[0] == (yield metrics_manager.get_register_value("foo.counter1", "count"))
assert counters[1] == (yield metrics_manager.get_register_value("bar.baz.counter2", "count"))
assert counters[2] == (yield metrics_manager.get_register_value("bar.baz.counter3", "count"))

with self.run_simulation(m) as sim:
sim.add_sync_process(test_process)
2 changes: 1 addition & 1 deletion test/testing/test_infrastructure.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def elaborate(self, platform):


class TestNow(TestCaseWithSimulator):
def setUp(self):
def setup_method(self):
self.test_cycles = 10
self.m = SimpleTestCircuit(EmptyCircuit())

Expand Down
49 changes: 23 additions & 26 deletions test/testing/test_log.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import pytest
from amaranth import *

from transactron import *
Expand Down Expand Up @@ -64,61 +65,57 @@ def elaborate(self, platform):


class TestLog(TestCaseWithSimulator):
def test_log(self):
def test_log(self, caplog):
m = LogTest()

def proc():
for i in range(50):
yield
yield m.input.eq(i)

with self.assertLogs(LOGGER_NAME) as logs:
with self.run_simulation(m) as sim:
sim.add_sync_process(proc)
with self.run_simulation(m) as sim:
sim.add_sync_process(proc)

self.assertIn(
"WARNING:test_logger:test/transactron/testing/test_log.py:21] Log triggered under Amaranth If value+3=0x2d",
logs.output,
print(caplog.text)
assert (
"WARNING test_logger:logging.py:87 test/transactron/testing/test_log.py:22] "
+ "Log triggered under Amaranth If value+3=0x2d"
in caplog.text
)
for i in range(0, 50, 2):
expected_msg = (
"WARNING:test_logger:test/transactron/testing/test_log.py:23] "
"WARNING test_logger:logging.py:87 test/transactron/testing/test_log.py:24] "
+ f"Input is even! input={i}, counter={i + 2}"
)
self.assertIn(
expected_msg,
logs.output,
)
assert expected_msg in caplog.text

def test_error_log(self):
def test_error_log(self, caplog):
m = ErrorLogTest()

def proc():
yield
yield m.input.eq(1)

with self.assertLogs(LOGGER_NAME) as logs:
with self.assertRaises(AssertionError):
with self.run_simulation(m) as sim:
sim.add_sync_process(proc)
with pytest.raises(AssertionError):
with self.run_simulation(m) as sim:
sim.add_sync_process(proc)

extected_out = (
"ERROR:test_logger:test/transactron/testing/test_log.py:40] "
"ERROR test_logger:logging.py:87 test/transactron/testing/test_log.py:41] "
+ "Input is different than output! input=0x1 output=0x0"
)
self.assertIn(extected_out, logs.output)
assert extected_out in caplog.text

def test_assertion(self):
def test_assertion(self, caplog):
m = AssertionTest()

def proc():
yield
yield m.input.eq(1)

with self.assertLogs(LOGGER_NAME) as logs:
with self.assertRaises(AssertionError):
with self.run_simulation(m) as sim:
sim.add_sync_process(proc)
with pytest.raises(AssertionError):
with self.run_simulation(m) as sim:
sim.add_sync_process(proc)

extected_out = "ERROR:test_logger:test/transactron/testing/test_log.py:61] Output differs"
self.assertIn(extected_out, logs.output)
extected_out = "ERROR test_logger:logging.py:87 test/transactron/testing/test_log.py:62] Output differs"
assert extected_out in caplog.text
Loading

0 comments on commit ac075e9

Please sign in to comment.