Best Python code snippet using uiautomator
vhdl_tb.py
Source:vhdl_tb.py  
1import os2from poroto.common import load_template3from poroto.config import gen_path4class VhdlTestBench:5    def __init__(self, designer, functions, mmap, streams_map, debug):6        self.designer = designer7        self.functions = functions8        self.debug = debug9        self.registers_map = mmap10        self.streams_map = streams_map11        self.dma_channel = 012    def channelInSignals(self, stream_name, channel, test_point, data):13        print >> self.out, "\tsignal data_%s_%d : word_vector_t(0 to %d-1) := (" % (stream_name, test_point, len(data))14        for (i, value) in enumerate(data):15            print >> self.out, "\t  std_logic_vector(to_unsigned(%d, C_PCI_DATA_WIDTH))%s" % (value, ',' if i < len(data) - 1 else '')16        print >> self.out, "\t);"17    def channelOutSignals(self, stream_name, channel, test_point, data):18        print >> self.out, "\tsignal data_%s_%d : word_vector_t(0 to %d-1);" % (stream_name, test_point, len(data))19        print >> self.out, "\tsignal data_%s_%d_offset : natural;" % (stream_name, test_point)20        print >> self.out, "\tsignal data_%s_%d_len : natural;" % (stream_name, test_point)21        print >> self.out, "\tsignal data_%s_%d_last : std_logic;" % (stream_name, test_point)22        print >> self.out, "\tsignal data_%s_%d_expected : word_vector_t(0 to %d-1) := (" % (stream_name, test_point, len(data))23        for (i, value) in enumerate(data):24            print >> self.out, "\t  std_logic_vector(to_unsigned(%d, C_PCI_DATA_WIDTH))%s" % (value, ',' if i < len(data) - 1 else '')25        print >> self.out, "\t);"26    def channelWrite(self, stream_name, channel, test_point):27        print >> self.out, '\t------------------------------------------'28        print >> self.out, '\t-- Write %s' % (stream_name)29        print >> self.out, '\t------------------------------------------'30        print >> self.out, '\treport "Writing channel %s (%d)";' % (stream_name, channel)31        print >> self.out, "\triffa_channel_write(0, data_%s_%d, '1', clk, riffa_rx_m2s(%d), riffa_rx_s2m(%d));" % (stream_name, test_point, channel, channel)32    def channelRead(self, stream_name, channel, test_point):33        print >> self.out, '\t------------------------------------------'34        print >> self.out, '\t-- Read %s' % (stream_name)35        print >> self.out, '\t------------------------------------------'36        print >> self.out, '\treport "Reading channel %s (%d)";' % (stream_name, channel)37        print >> self.out, "\triffa_channel_read(data_%s_%d_offset, data_%s_%d_len, data_%s_%d, data_%s_%d_last, clk, riffa_tx_m2s(%d), riffa_tx_s2m(%d));" % (stream_name, test_point, stream_name, test_point, stream_name, test_point, stream_name, test_point, channel, channel)38    def channelVerify(self, stream_name, channel, test_point):39        print >> self.out, '\t------------------------------------------'40        print >> self.out, '\t-- Read %s' % (stream_name)41        print >> self.out, '\t------------------------------------------'42        print >> self.out, '\treport "Verify channel %s (%d)";' % (stream_name, channel)43        print >> self.out, "\tverify_memory(data_%s_%d, data_%s_%d_len, data_%s_%d_expected, test_ok);" % (stream_name, test_point, stream_name, test_point, stream_name, test_point)44    def registersInSignals(self, registers, test_point, test_vectors):45        print >> self.out, '\t------------------------------------------'46        print >> self.out, '\t-- Write Registers'47        print >> self.out, '\t------------------------------------------'48        count = 149        for (i, register) in enumerate(registers):50            if register.short_name is not "rst" and register.short_name is not "stall":51                count = count + 152        print >> self.out, "\tsignal registers_in_%d : word_vector_t(0 to %d-1) := (" % (test_point, count)53        offset = 054        print >> self.out, "\t  std_logic_vector(to_unsigned(0, C_PCI_DATA_WIDTH))%s" % (',' if offset < count - 1 else '')55        offset += 156        for (i, register) in enumerate(registers):57            if register.short_name is not "rst" and register.short_name is not "stall":58                value = test_vectors[register.short_name][test_point]59                print >> self.out, "\t  std_logic_vector(to_unsigned(%d, C_PCI_DATA_WIDTH))%s" % (value, ',' if offset < count - 1 else '')60                offset += 161        print >> self.out, "\t);"62    def registersOutSignals(self, registers, test_point, test_vectors):63        print >> self.out, '\t------------------------------------------'64        print >> self.out, '\t-- Read Registers'65        print >> self.out, '\t------------------------------------------'66        count = 167        for (i, register) in enumerate(registers):68            if register.short_name is not "rst" and register.short_name is not "stall":69                count = count + 170        print >> self.out, "\tsignal registers_out_%d : word_vector_t(0 to %d-1);" % (test_point, count)71        print >> self.out, "\tsignal registers_out_%d_offset : natural;" % test_point72        print >> self.out, "\tsignal registers_out_%d_len : natural;" % test_point73        print >> self.out, "\tsignal registers_out_%d_last : std_logic;" % test_point74    def registersInWrite(self, test_point):75        print >> self.out, '\t------------------------------------------'76        print >> self.out, '\t-- Write Registers'77        print >> self.out, '\t------------------------------------------'78        print >> self.out, '\treport "Writing registers channel";'79        print >> self.out, "\triffa_channel_write(0, registers_in_%d, '1', clk, riffa_rx_m2s(0), riffa_rx_s2m(0));" % test_point80    def registersOutRead(self, test_point):81        print >> self.out, '\t------------------------------------------'82        print >> self.out, '\t-- Read Registers'83        print >> self.out, '\t------------------------------------------'84        print >> self.out, '\treport "Reading registers channel";'85        print >> self.out, "\triffa_channel_read(registers_out_%d_offset, registers_out_%d_len, registers_out_%d, registers_out_%d_last, clk, riffa_tx_m2s(0), riffa_tx_s2m(0));" % (test_point, test_point, test_point, test_point)86    def registersResetWrite(self):87        print >> self.out, '\treport "Trigger reset";'88        print >> self.out, "\triffa_channel_write(0, registers_reset, '1', clk, riffa_rx_m2s(0), riffa_rx_s2m(0));"89        print >> self.out, "\twait for clk_period * 10;"90    91    def verifyFunctionEntry(self, instance, test_points, test_vectors):92        for test_point in range(test_points):93            for stream in instance.streams:94                if stream.in_stream:95                    self.channelWrite(stream.in_name, stream.memory.index + 1, test_point)96            self.registersInWrite(test_point)97            self.registersOutRead(test_point)98            for stream in instance.streams:99                if stream.out_stream:100                    self.channelRead(stream.out_name, stream.memory.index + 1, test_point)101            for stream in instance.streams:102                if stream.out_stream:103                    self.channelVerify(stream.out_name, stream.memory.index + 1, test_point)104            self.registersResetWrite()105    def printSignals(self, test_vectors):106        for instance in self.functions:107            for test_point in range(test_vectors[instance.name].test_points):108                if instance.name in test_vectors:109                    for stream in instance.streams:110                        if stream.in_stream:111                            self.channelInSignals(stream.in_name, stream.memory.index, test_point, test_vectors[instance.name].test_vectors[stream.in_name][test_point])112                            #Inject virtual register value113                            if not stream.in_name+"_size" in test_vectors[instance.name].test_vectors:114                                test_vectors[instance.name].test_vectors[stream.in_name+"_size"] = []115                            test_vectors[instance.name].test_vectors[stream.in_name+"_size"].append(len(test_vectors[instance.name].test_vectors[stream.in_name][test_point]))116                        if stream.out_stream:117                            self.channelOutSignals(stream.out_name, stream.memory.index, test_point, test_vectors[instance.name].test_vectors[stream.in_name][test_point])118                            #Inject virtual register value119                            if not stream.out_name+"_size" in test_vectors[instance.name].test_vectors:120                                test_vectors[instance.name].test_vectors[stream.out_name+"_size"] = []121                            test_vectors[instance.name].test_vectors[stream.in_name+"_size"].append(len(test_vectors[instance.name].test_vectors[stream.in_name][test_point]))122                    self.registersInSignals(self.registers_map.block_in_registers[instance.name], test_point, test_vectors[instance.name].test_vectors)123                    self.registersOutSignals(self.registers_map.block_out_registers[instance.name], test_point, test_vectors[instance.name].test_vectors)124    def printTb(self, test_vectors):125        for instance in self.functions:126            if instance.name in test_vectors:127                print >> self.out128                print >> self.out, '\t--*****************************************************************************'129                print >> self.out, '\t-- APPLICATION: SIMULATION  PATTERN  -- Function %s' % instance.name130                print >> self.out, '\t--*****************************************************************************'131                print >> self.out132                self.verifyFunctionEntry(instance, test_vectors[instance.name].test_points, test_vectors[instance.name].test_vectors)133    134    def write(self, test_vectors):135        #TODO: hardcoded name136        self.tb_template = load_template('tb.vhdl')137        self.designer.add_file(gen_path, "tb.vhdl", synth=False)138        self.out = open(os.path.join(gen_path, 'vhdl', 'tb.vhdl'), 'w' )139        for line in self.tb_template:140            if '%%%SIGNALS%%%' in line:141                self.printSignals(test_vectors)142            elif '%%%STIMULATE%%%' in line:143                self.printTb(test_vectors)144            else:145                print >> self.out, line,...c_tb.py
Source:c_tb.py  
1import os2from .common import load_template3from .config import gen_path, c_path4from pycparser import c_ast5class CTestBench:6    def __init__(self, functions, mmap, streams_map, debug):7        self.functions = functions8        self.debug = debug9        self.registers_map = mmap10        self.streams_map = streams_map11        self.dma_channel = 012    def dmaWrite(self, stream_name, addr_block, test_point):13        #data_name = "%s_%d" % (stream_name, test_point)14        #print >> self.out, '\tfpga_write_vector(%d, sizeof(%s), %s);' % (addr_block, data_name, data_name)15        pass16    def dmaReadAndVerify(self, stream_name, addr_block, test_point, data_len):17        data_name = "%s_%d" % (stream_name, test_point)18        data_expected_name = "%s_expected_%d" % (stream_name, test_point)19        #print >> self.out, '\tfpga_read_vector(%d, sizeof(%s), %s);' % (addr_block, data_name, data_name)20        print >> self.out, '\tprintf("%s: ");' % data_name21        print >> self.out, '\tfor (i = 0; i < %d; ++i) {' % data_len22        print >> self.out, '\t\tprintf("%%d ", %s[i]);' % data_name23        print >> self.out, '\t}'24        print >> self.out, '\tprintf("\\n");'25        print >> self.out, '\tif ( memcmp(%s, %s, %d*4) != 0 ) {' % ( data_name, data_expected_name, data_len)26        print >> self.out, '\t\tprintf( "Retrieved data %s is not correct\\n" );' % data_name27        print >> self.out, '\t\ttest_successful = 0;'28        print >> self.out, '\t}'29    def declareStream(self, stream, stream_name, test_point, data):30        print >> self.out, '\t%s %s_%d[%d] = {' % (stream.data_type, stream_name, test_point, len(data))31        print >> self.out, '\t\t', ', '.join(str(item) for item in data)32        print >> self.out, '\t};'33    def declareEmptyStream(self, stream, stream_name, test_point, data):34        #print >> self.out, '\t%s %s_%d[%d];' % (stream.data_type, stream_name, test_point, len(data))            35        print >> self.out, '\t%s %s_%d[%d] = {' % (stream.data_type, stream_name, test_point, len(data))36        print >> self.out, '\t\t', ', '.join(str(-1) for item in data)37        print >> self.out, '\t};'38    def declareVars(self, registers):39        for register in registers:40            print >> self.out, '\tuint32_t %s;' % register.short_name41    def writeData(self, registers, test_vectors, test_point):    42        for register in registers:43            if register.short_name in test_vectors:44                print >> self.out, '\t%s = %d;' % (register.short_name, test_vectors[register.short_name][test_point])45            else:46                print >> self.out, '\t//%s not set' % register.short_name47            48    def call(self, function, test_point):49        for stream in function.streams:50            for line in stream.get_set_data(stream.in_name+'_%d'%test_point):51                print >> self.out, '\t'+line52        params = []53        for arg in function.args.params:54            if isinstance(arg.type, c_ast.PtrDecl):55                params.append( '%s_%d' % (arg.name, test_point ) )56            else:57                params.append( arg.name )58        print >>self.out, '\t', function.name, '(', ', '.join(params), ');'        59        for stream in function.streams:60            for line in stream.get_get_data(stream.out_name+'_%d'%test_point):61                print >> self.out, '\t'+line62    def verifyData(self, registers, test_vectors, test_point):    63        for register in registers:64            if register.short_name in test_vectors:65                print >> self.out, '\tif (%s != %d ) {' % (register.short_name, test_vectors[register.short_name][test_point])66                print >> self.out, '\t\tprintf( "%s: Got %%d iso %d\\n", %s);' % (register.short_name, test_vectors[register.short_name][test_point], register.short_name)67                print >> self.out, '\t\ttest_successful = 0;'68                print >> self.out, '\t}'69            else:70                print >> self.out, '\t//%s not verified' % register.short_name71    72    def verifyFunctionEntry(self, instance, test_points, test_vectors):73        for test_point in range(test_points):74            for stream in instance.streams:75                if stream.in_stream:76                    self.declareStream(stream, stream.in_name, test_point, test_vectors[stream.in_name][test_point])77                if stream.out_stream:78                    self.declareStream(stream, stream.out_name + '_expected', test_point, test_vectors[stream.out_name][test_point])79                    self.declareEmptyStream(stream, stream.out_name, test_point, test_vectors[stream.out_name][test_point])80        self.declareVars(self.registers_map.block_in_registers[instance.name])81        self.declareVars(self.registers_map.block_out_registers[instance.name])82        for test_point in range(test_points):83            print >> self.out, "\n\t// Test point %d\n" % test_point84            for stream_name in instance.streams:85                if stream.in_stream:86                    self.dmaWrite(stream_name, stream.memory.index, test_point)87            self.writeData(self.registers_map.block_in_registers[instance.name], test_vectors, test_point)88            self.call(instance, test_point)89            self.verifyData(self.registers_map.block_out_registers[instance.name], test_vectors, test_point)90            for stream in instance.streams:91                if stream.out_stream:92                    self.dmaReadAndVerify(stream.out_name, stream.memory.index, test_point, len(test_vectors[stream.out_name][test_point]))93    def printTb(self, test_vectors):94        for instance in self.functions:95            if instance.name in test_vectors:96                print >> self.out97                print >> self.out, '\t//*****************************************************************************'98                print >> self.out, '\t// APPLICATION: SIMULATION  PATTERN  -- Function %s' % instance.name99                print >> self.out, '\t//*****************************************************************************'100                print >> self.out101                self.verifyFunctionEntry(instance, test_vectors[instance.name].test_points, test_vectors[instance.name].test_vectors)102    103    def printTbInit(self, test_vectors):104        for instance in self.functions:105            for stream in instance.streams:106                for line in stream.get_c_def():107                    print >> self.out, line108    def write(self, test_vectors):109        self.tb_template = load_template('tb.c', top_dir=c_path)110        self.out = open(os.path.join(gen_path, 'c', 'tb.cpp'), 'w' )111        for line in self.tb_template:112            if '%%%TB%%%' in line:113                self.printTb(test_vectors)114            elif '%%%INIT%%%' in line:115                self.printTbInit(test_vectors)116            else:117                print >> self.out, line,...MazeGenerator.py
Source:MazeGenerator.py  
1import random2class MazeGenerator:3    def __init__(self, width, height):4        self.width = width5        self.height = height6        self.path = []7        self.start_point = []8        self.dead_ends = []9        self.goal = 010        self.n = 111    def boundary_check(self, point):12        if point[0] > self.width - 40 or point[0] < 0 or point[1] > self.height - 40 or point[1] < 0:13            return True14        else:15            return False16    def __generate_starting_point(self):17        right_bound = int((self.width - 80)/40)18        x = random.randint(1, right_bound)19        while x % 2 == 0:20            x = random.randint(1, right_bound)21        top_bound = int((self.height - 80) / 40)22        y = random.randint(1, top_bound)23        while y % 2 == 0:24            y = random.randint(1, top_bound)25        self.start_point = [x*40, y*40]26    def __generate_path(self, current_point, pathway):27        while True:28            if pathway.count([current_point[0] + 80, current_point[1]]) == 1 or self.boundary_check([current_point[0] + 80, current_point[1]]):29                if pathway.count([current_point[0] - 80, current_point[1]]) == 1 or self.boundary_check([current_point[0] - 80, current_point[1]]):30                    if pathway.count([current_point[0], current_point[1] + 80]) == 1 or self.boundary_check([current_point[0], current_point[1] + 80]):31                        if pathway.count([current_point[0], current_point[1] - 80]) == 1 or self.boundary_check([current_point[0], current_point[1] - 80]):32                            self.dead_ends.append(current_point)33                            if self.n == 1:34                                self.goal = current_point35                                self.n = 036                            return pathway37            x = random.randint(1, 4)38            test_point = [current_point[0], current_point[1]]39            if x == 1:40                test_point[0] += 8041                if pathway.count(test_point) == 1 or self.boundary_check(test_point):42                    continue43                else:44                    current_point = test_point45                    pathway.append(test_point)46                    pathway.append([test_point[0] - 40, test_point[1]])47            elif x == 2:48                test_point[0] -= 8049                if pathway.count(test_point) == 1 or self.boundary_check(test_point):50                    continue51                else:52                    current_point = test_point53                    pathway.append(test_point)54                    pathway.append([test_point[0] + 40, test_point[1]])55            elif x == 3:56                test_point[1] += 8057                if pathway.count(test_point) == 1 or self.boundary_check(test_point):58                    continue59                else:60                    current_point = test_point61                    pathway.append(test_point)62                    pathway.append([test_point[0], test_point[1] - 40])63            elif x == 4:64                test_point[1] -= 8065                if pathway.count(test_point) == 1 or self.boundary_check(test_point):66                    continue67                else:68                    current_point = test_point69                    pathway.append(test_point)70                    pathway.append([test_point[0], test_point[1] + 40])71            pathway = self.__generate_path(current_point, pathway)72    def redefine_goal(self):73        p = random.randint(1, len(self.dead_ends) - 1)74        return self.dead_ends[p]75    def generate_maze(self):76        """Returns the coordinates of points for the normal tiles, the starting point, and the goal"""77        self.__generate_starting_point()78        pathway = [self.start_point]79        self.path = self.__generate_path(self.start_point, pathway)...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
