def actual_test( dwid, qsize, qtype, sink_init, test_msgs ): msgs = test_msgs.draw( st.lists( pst.bits(dwid), min_size=1, max_size=100 ) ) th = s.TestHarness( mk_bits(dwid), msgs, msgs ) th.set_param( "top.sink.construct", initial_delay=sink_init ) th.set_param( "top.dut.construct", QueueType = qtype ) th.set_param( "top.dut.queue.construct", num_entries=qsize ) run_sim( th, max_cycles=5000 )
def test_signed(nbits): print("") @hypothesis.given(bits=pst.bits(nbits, True)) @hypothesis.settings(max_examples=16) def actual_test(bits): assert bits.nbits == nbits print(bits, bits.int()) actual_test()
def mesh_pkt_strat(draw, ncols, nrows, opaque_nbits=8, vc=1, payload_nbits=32): dst_x = draw(st.integers(0, ncols - 1)) dst_y = draw(st.integers(0, nrows - 1)) src_x = draw(st.integers(0, ncols - 1)) src_y = draw(st.integers(0, nrows - 1)) opaque = draw(pst.bits(opaque_nbits)) payload = draw(st.sampled_from([0, 0xdeadbeef, 0xfaceb00c, 0xc001cafe])) Pkt = mk_mesh_pkt(ncols, nrows, opaque_nbits, vc, payload_nbits) if vc == 1: return Pkt(src_x, src_y, dst_x, dst_y, opaque, payload) else: return Pkt(src_x, src_y, dst_x, dst_y, opaque, 0, payload)
def test_bits16_limited(): print("") @hypothesis.given( bits = pst.bits(16, min_value=2, max_value=11) ) @hypothesis.settings( max_examples=16 ) def actual_test( bits ): assert bits.nbits == 16 assert 2 <= bits.uint() <= 11 print( bits, bits.uint() ) actual_test()
class ChecksumCL_Tests( BaseTests ): def cksum_func( s, words ): return checksum_cl( words ) # ''' TUTORIAL TASK '''''''''''''''''''''''''''''''''''''''''''''''''''' # Use Hypothesis to test Checksum CL # ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''\/ #; Use Hypothesis to verify that ChecksumCL has the same behavior as #; ChecksumFL. Simply uncomment the following test_hypothesis method #; and rerun pytest. Make sure that you fix the indentation so that #; this new test_hypothesis method is correctly indented with respect #; to the class ChecksumCL_Tests #; #; @hypothesis.settings( deadline=None ) #; @hypothesis.given( #; words=st.lists( pm_st.bits(16), min_size=8, max_size=8 ) #; ) #; def test_hypothesis( s, words ): #; print( [ int(x) for x in words ] ) #; assert s.cksum_func( words ) == checksum( words ) #; #; This new test uses Hypothesis to generate random inputs, then uses #; the checksum_cl to run a little simulation and compares the output to #; the checksum function from ChecksumFL. #; #; To really see Hypothesis in action, go back to ChecksumCL and #; corrupt one word of the input by forcing it to always be zero. For #; example, change the update block in the CL implementation to be #; something like this: #; #; @s.update #; def up_checksum_cl(): #; if s.pipe.enq.rdy() and s.in_q.deq.rdy(): #; bits = s.in_q.deq() #; words = b128_to_words( bits ) #; words[5] = b16(0) # <--- INJECT A BUG! #; result = checksum( words ) #; s.pipe.enq( result ) !\vspace{0.07in}! #; if s.send.rdy() and s.pipe.deq.rdy(): #; s.send( s.pipe.deq() ) @hypothesis.settings( deadline=None ) @hypothesis.given( words = st.lists( pm_st.bits(16), min_size=8, max_size=8 ) ) def test_hypothesis( s, words ): print( [ int(x) for x in words ] ) assert s.cksum_func( words ) == checksum( words )
def test_bitslist_nested_user_strategy(): type_ = [ [Bits10, Bits11, Bits12], Bits13 ] limit_dict = { 1: pst.bits(13, min_value=0, max_value=10) } print("") @hypothesis.given( blist = pst.bitslists(type_, limit_dict) ) @hypothesis.settings( max_examples=16 ) def actual_test( blist ): assert blist[0][0].nbits == 10 assert blist[0][1].nbits == 11 assert blist[0][2].nbits == 12 assert blist[1].nbits == 13 assert 0 <= blist[1] <= 10 print(blist) actual_test()
class ChecksumCLSrcSink_Tests: # [setup_class] will be called by pytest before running all the tests in # the test class. Here we specify the type of the design under test # that is used in all test cases. We can easily reuse all the tests in # this class simply by creating a new test class that inherits from # this class and overwrite the setup_class to provide a different DUT # type. @classmethod def setup_class( cls ): cls.DutType = ChecksumCL #----------------------------------------------------------------------- # run_sim #----------------------------------------------------------------------- # A helper function in the test suite that creates a simulator and # runs test. We can overwrite this function when inheriting from the # test class to apply different passes to the DUT. def run_sim( s, th, max_cycles=1000 ): # Create a simulator th.apply( SimulationPass() ) ncycles = 0 th.sim_reset() print( "" ) # Tick the simulator print("{:3}: {}".format( ncycles, th.line_trace() )) while not th.done() and ncycles < max_cycles: th.tick() ncycles += 1 print("{:3}: {}".format( ncycles, th.line_trace() )) # Check timeout assert ncycles < max_cycles #----------------------------------------------------------------------- # test_srcsink_simple #----------------------------------------------------------------------- # is a simple test case with only 1 input. def test_srcsink_simple( s ): words = [ b16(x) for x in [ 1, 2, 3, 4, 5, 6, 7, 8 ] ] bits = words_to_b128( words ) result = b32( 0x00780024 ) src_msgs = [ bits ] sink_msgs = [ result ] th = TestHarness( s.DutType, src_msgs, sink_msgs ) s.run_sim( th ) #----------------------------------------------------------------------- # test_srcsink_pipeline #----------------------------------------------------------------------- # test the checksum unit with a sequence of inputs. def test_srcsink_pipeline( s ): words0 = [ b16(x) for x in [ 1, 2, 3, 4, 5, 6, 7, 8 ] ] words1 = [ b16(x) for x in [ 8, 7, 6, 5, 4, 3, 2, 1 ] ] bits0 = words_to_b128( words0 ) bits1 = words_to_b128( words1 ) result0 = b32( 0x00780024 ) result1 = b32( 0x00cc0024 ) src_msgs = [ bits0, bits1, bits0, bits1 ] sink_msgs = [ result0, result1, result0, result1 ] th = TestHarness( s.DutType, src_msgs, sink_msgs ) s.run_sim( th ) #----------------------------------------------------------------------- # test_srcsink_backpressure #----------------------------------------------------------------------- # test the checksum unit with a large sink delay. def test_srcsink_backpressure( s ): words0 = [ b16(x) for x in [ 1, 2, 3, 4, 5, 6, 7, 8 ] ] words1 = [ b16(x) for x in [ 8, 7, 6, 5, 4, 3, 2, 1 ] ] result0 = b32( 0x00780024 ) result1 = b32( 0x00cc0024 ) bits0 = words_to_b128( words0 ) bits1 = words_to_b128( words1 ) src_msgs = [ bits0, bits1, bits0, bits1 ] sink_msgs = [ result0, result1, result0, result1 ] th = TestHarness( s.DutType, src_msgs, sink_msgs ) th.set_param( "top.sink.construct", initial_delay=10 ) s.run_sim( th ) # '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''\// # Cutting out this hypothesis test since in the new flow we only want # the attendees to write a single hypothesis test above ... and we # don't want them to get confused by this additional hypothesis test. # This hypothesis test not only generates a sequence of input to the # the checksum unit but it also configure the test source and sink with # different initial and interval delays. @hypothesis.given( input_msgs = st.lists( st.lists( pm_st.bits(16), min_size=8, max_size=8 ) ), src_init = st.integers( 0, 10 ), src_intv = st.integers( 0, 3 ), sink_init = st.integers( 0, 10 ), sink_intv = st.integers( 0, 3 ), ) @hypothesis.settings( deadline=None, max_examples=16 ) def test_srcsink_hypothesis( s, input_msgs, src_init, src_intv, sink_init, sink_intv ): src_msgs = [ words_to_b128( words ) for words in input_msgs ] sink_msgs = [ checksum( words ) for words in input_msgs ] th = TestHarness( s.DutType, src_msgs, sink_msgs ) th.set_param( "top.src.construct", initial_delay = src_init, interval_delay = src_intv ) th.set_param( "top.sink.construct", initial_delay = sink_init, interval_delay = sink_intv ) s.run_sim( th )
#------------------------------------------------------------------------- def test_bits2words(): bits = b128(0x00010002000300040005000600070008) words = [b16(x) for x in [8, 7, 6, 5, 4, 3, 2, 1]] assert b128_to_words(bits) == words def test_words2bits(): bits = b128(0x00010002000300040005000600070008) words = [b16(x) for x in [8, 7, 6, 5, 4, 3, 2, 1]] assert words_to_b128(words) == bits #------------------------------------------------------------------------- # Hypothesis Tests #------------------------------------------------------------------------- # ''' TUTORIAL TASK '''''''''''''''''''''''''''''''''''''''''''''''''''''' # Add Hypothesis test # ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''\/ #; Add a Hypothesis tests to verify that converting words to bits and #; then bits to words always returns the original words. @hypothesis.given(words=st.lists(pst.bits(16), min_size=8, max_size=8)) def test_hypothesis(words): print(words) assert b128_to_words(words_to_b128(words)) == words