Skip to content

Commit 6636fa8

Browse files
committed
Add a doctest example for rtllib/barrel.py
1 parent b4902ec commit 6636fa8

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

pyrtl/rtllib/barrel.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,28 @@ def barrel_shifter(
3232
) -> WireVector:
3333
"""Create a barrel shifter.
3434
35+
.. doctest only::
36+
37+
>>> import pyrtl
38+
>>> pyrtl.reset_working_block()
39+
40+
Example::
41+
42+
>>> bits_to_shift = pyrtl.Input(name="input", bitwidth=8)
43+
>>> shift_dist = pyrtl.Input(name="shift_dist", bitwidth=3)
44+
>>> output = pyrtl.Output(name="output")
45+
46+
>>> output <<= pyrtl.rtllib.barrel.barrel_shifter(
47+
... bits_to_shift,
48+
... bit_in=1,
49+
... direction=pyrtl.rtllib.barrel.Direction.RIGHT,
50+
... shift_dist=shift_dist)
51+
52+
>>> sim = pyrtl.Simulation()
53+
>>> sim.step(provided_inputs={"input": 0x55, "shift_dist": 4})
54+
>>> hex(sim.inspect("output"))
55+
'0xf5'
56+
3557
:param bits_to_shift: :class:`.WireVector` with the value to shift.
3658
:param bit_in: A 1-bit :class:`.WireVector` representing the value to shift in.
3759
:param direction: A one bit :class:`.WireVector` representing the shift direction

tests/rtllib/test_barrel.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
1+
import doctest
12
import random
23
import unittest
34

45
import pyrtl
56
from pyrtl.rtllib.barrel import Direction, barrel_shifter
67

78

9+
class TestDocTests(unittest.TestCase):
10+
"""Test documentation examples."""
11+
12+
def test_doctests(self):
13+
failures, tests = doctest.testmod(m=pyrtl.rtllib.barrel)
14+
self.assertGreater(tests, 0)
15+
self.assertEqual(failures, 0)
16+
17+
818
class TestBarrel(unittest.TestCase):
919
def setUp(self):
1020
pyrtl.reset_working_block()

0 commit comments

Comments
 (0)