|
| 1 | +from pyrtl.core import set_working_block |
1 | 2 | import unittest
|
2 | 3 | import pyrtl
|
3 | 4 | from pyrtl import transform
|
@@ -50,7 +51,8 @@ def insert_random_inversions(rate=0.5):
|
50 | 51 |
|
51 | 52 | def randomly_replace(wire):
|
52 | 53 | if random.random() < rate:
|
53 |
| - new_src, new_dst = transform.clone_wire(wire), transform.clone_wire(wire) |
| 54 | + new_src = transform.clone_wire(wire, pyrtl.wire.next_tempvar_name()) |
| 55 | + new_dst = transform.clone_wire(wire, pyrtl.wire.next_tempvar_name()) |
54 | 56 | new_dst <<= ~new_src
|
55 | 57 | return new_src, new_dst
|
56 | 58 | return wire, wire
|
@@ -258,6 +260,81 @@ def test_replace_only_dst_wire(self):
|
258 | 260 | self.assertEqual(w4_dst_net.dests, w1_dst_net.dests)
|
259 | 261 |
|
260 | 262 |
|
| 263 | +class TestCloning(unittest.TestCase): |
| 264 | + def setUp(self): |
| 265 | + pyrtl.reset_working_block() |
| 266 | + |
| 267 | + def test_same_type(self): |
| 268 | + for ix, cls in enumerate([pyrtl.WireVector, pyrtl.Register, pyrtl.Input, pyrtl.Output]): |
| 269 | + w1 = cls(4, 'w%d' % ix) |
| 270 | + w2 = pyrtl.clone_wire(w1, 'y%d' % ix) |
| 271 | + self.assertIsInstance(w2, cls) |
| 272 | + self.assertEqual(w1.bitwidth, w2.bitwidth) |
| 273 | + |
| 274 | + def test_clone_wire_no_name_same_block(self): |
| 275 | + a = pyrtl.WireVector(1, 'a') |
| 276 | + with self.assertRaises(pyrtl.PyrtlError) as error: |
| 277 | + pyrtl.clone_wire(a) |
| 278 | + self.assertEqual( |
| 279 | + str(error.exception), |
| 280 | + "Must provide a name for the newly cloned wire " |
| 281 | + "when cloning within the same block." |
| 282 | + ) |
| 283 | + |
| 284 | + def test_clone_wire_same_name_same_block(self): |
| 285 | + a = pyrtl.WireVector(1, 'a') |
| 286 | + with self.assertRaises(pyrtl.PyrtlError) as error: |
| 287 | + pyrtl.clone_wire(a, 'a') |
| 288 | + self.assertEqual( |
| 289 | + str(error.exception), |
| 290 | + "Cannot give a newly cloned wire the same name as an existing wire." |
| 291 | + ) |
| 292 | + |
| 293 | + def test_clone_wire_different_name_same_block(self): |
| 294 | + a = pyrtl.WireVector(1, 'a') |
| 295 | + self.assertEqual(a.name, 'a') |
| 296 | + self.assertEqual(pyrtl.working_block().wirevector_set, {a}) |
| 297 | + self.assertIs(pyrtl.working_block().wirevector_by_name['a'], a) |
| 298 | + |
| 299 | + w = pyrtl.clone_wire(a, name='w') |
| 300 | + self.assertEqual(w.name, 'w') |
| 301 | + self.assertEqual(a.name, 'a') |
| 302 | + self.assertIs(pyrtl.working_block().wirevector_by_name['w'], w) |
| 303 | + self.assertIs(pyrtl.working_block().wirevector_by_name['a'], a) |
| 304 | + self.assertEqual(pyrtl.working_block().wirevector_set, {a, w}) |
| 305 | + |
| 306 | + pyrtl.working_block().remove_wirevector(a) |
| 307 | + self.assertEqual(pyrtl.working_block().wirevector_set, {w}) |
| 308 | + |
| 309 | + def test_clone_wire_no_or_same_name_different_block(self): |
| 310 | + for clone_name in (None, 'a'): |
| 311 | + a = pyrtl.WireVector(1, 'a') |
| 312 | + b = pyrtl.Block() |
| 313 | + with pyrtl.set_working_block(b): |
| 314 | + w = pyrtl.clone_wire(a, name=clone_name) |
| 315 | + |
| 316 | + self.assertEqual(a.name, 'a') |
| 317 | + self.assertIs(pyrtl.working_block().wirevector_by_name['a'], a) |
| 318 | + self.assertEqual(pyrtl.working_block().wirevector_set, {a}) |
| 319 | + |
| 320 | + self.assertEqual(w.name, 'a') |
| 321 | + self.assertIs(b.wirevector_by_name['a'], w) |
| 322 | + self.assertEqual(b.wirevector_set, {w}) |
| 323 | + pyrtl.reset_working_block() |
| 324 | + |
| 325 | + def test_clone_wire_different_name_different_block(self): |
| 326 | + a = pyrtl.WireVector(1, 'a') |
| 327 | + b = pyrtl.Block() |
| 328 | + with set_working_block(b): |
| 329 | + w = pyrtl.clone_wire(a, 'w') |
| 330 | + self.assertEqual(a.name, 'a') |
| 331 | + self.assertEqual(w.name, 'w') |
| 332 | + self.assertIs(pyrtl.working_block().wirevector_by_name['a'], a) |
| 333 | + self.assertEqual(pyrtl.working_block().wirevector_set, {a}) |
| 334 | + self.assertIs(b.wirevector_by_name['w'], w) |
| 335 | + self.assertEqual(b.wirevector_set, {w}) |
| 336 | + |
| 337 | + |
261 | 338 | # this code needs mocking from python 3's unittests to work
|
262 | 339 | """
|
263 | 340 | @mock.patch('transform_examples.pyrtl.probe')
|
|
0 commit comments