Re: [myhdl-list] beginning with unittests
Brought to you by:
jandecaluwe
From: Jan D. <ja...@ja...> - 2010-11-07 13:52:05
|
Martín Gaitán wrote: > On Thu, Nov 4, 2010 at 6:26 PM, Jan Decaluwe <ja...@ja... > <mailto:ja...@ja...>> wrote: > > Perhaps because 0b0010 != 3 ? > > If this is the reason, why use all these different representations > for the same thing? > > Python 2.6 has bit representation for integer values. Any representation > should work with intbv's and in conversion. > > Jan > > > right, and the best example of usefulness of unit test ;-) Yes, interesting isn't it :-) Your original test bench that "worked" also revealed the bug, but you overlooked it. It's the formal test that revealed it. Let me add a few comments here. Your example demonstrates one feature of unit tests: use a formal check instead of "inspection" to verify correctness. This works best if there is some high-level way to specify the expected result. In your case I would probably set up a dict with operators from module operator, or your own function if required: spec = {0b0001 : operator.add, 0b0110 : operator.sub, } and then randomly generate operands and operator control sequences. The alu operation would come down do: spec[op](*operands). More importantly, unit tests work best if you write the test before the implementation. A unit that has never failed is not very useful. The idea is to start with something that fails, and then starts working as the implemenation progresses. When your are done, you know that your design works according to the unit test spec, but also that it can fail when you change the implementation and introduce a bug! Personally, I find unittest way too heavy. I simply write regular python tests that use assert statements to verify results. You can run this with a regular python interpreter, but it is more useful to use a framework like py.test (there are others) for this: this looks up all your tests automatically, runs them, and uses some magic to report issues in a clearer way. Jan -- Jan Decaluwe - Resources bvba - http://www.jandecaluwe.com Python as a HDL: http://www.myhdl.org VHDL development, the modern way: http://www.sigasi.com Analog design automation: http://www.mephisto-da.com World-class digital design: http://www.easics.com |