Code testing method and device, equipment and medium
Through particle swarm algorithm and fitness optimization technology, combined with the time factor of input parameters, test cases that meet specific time conditions are generated, which solves the problem of difficulty in covering time-related codes with existing technology and improves the coverage and efficiency of code testing.
Patent Information
- Application Number
- CN202411001310.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-07-24
- Publication Date
- 2024-10-18
AI Technical Summary
Existing code testing methods are difficult to effectively cover time-related code fragments, resulting in low code coverage. Traditional automatic test case generation methods cannot be applied to embedded code and chip design code.
By introducing the particle swarm algorithm and combining the injection time of the input parameters, the particle swarm is updated to generate test cases that meet specific time conditions, and the fitness and adaptive particle algorithms are used to optimize the test process until the preset test stop condition is reached.
It improves the code coverage of code testing, is suitable for time-related code, reduces the workload of verifiers, reduces the invalid testing process, and improves testing efficiency.
Smart Images

Figure CN118796701A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of software testing technology, and in particular to a code testing method, device, equipment and medium. Background Art
[0002] Code testing is a crucial step in the code development process. It includes functional testing and code coverage testing, which determine whether the code functionality, stability, and logic meet expectations. Code coverage testing strives for 100% coverage, but this is difficult to achieve for large projects with complex logic. Manually designing a large set of test cases is obviously costly and prone to omissions. Randomly generated test cases often contain a large number of invalid test cases, resulting in low code testing efficiency. To improve code testing efficiency and code coverage, and reduce labor costs, a series of intelligent search algorithms have been introduced into the field of code verification. Intelligent search algorithms are typically used to generate test case sets and use overall code coverage information as a metric to evaluate test performance.
[0003] The automatic test case generation method based on intelligent search algorithm usually assumes that the code to be tested is static. The so-called static code means that the code does not involve time-related program fragments. If the processor instruction fetch and execution time are not considered, the output is obtained immediately after the input is given, and the output is unique. This is applicable to most pure software codes, but it is difficult to generate effective test cases for embedded codes or chip design codes that need to process time-related codes, because the input parameters can only trigger the corresponding code behavior and produce the expected output if they change within the specified time interval. In other words, the traditional test case automatic generation method only generates the input parameters at the initial moment, and does not generate additional input parameter sequences in subsequent program runs. It can be seen that this cannot cover time-related code fragments, and the code coverage rate still needs to be improved.
[0004] It can be seen that how to improve the code coverage of code testing is a problem that those skilled in the art need to solve. Summary of the Invention
[0005] The purpose of the embodiments of the present invention is to provide a code testing method, apparatus, device, and medium that can improve the code coverage of code testing. The specific scheme is as follows:
[0006] In a first aspect, the present invention discloses a code testing method, comprising:
[0007] Determine the current particle group as the current test case cluster; wherein each particle in the current particle group includes the input parameters of the test case in the current test case cluster and the injection time of the input parameters;
[0008] Injecting the corresponding input parameter into the code to be tested based on the injection time to obtain a code test result corresponding to the current test operation;
[0009] If the code test result corresponding to the current test operation does not meet the preset test stop condition, then determine the difference between the code coverage of each particle in the current particle swarm and the total code coverage in the previous test operation, and determine each difference as the fitness of each particle in the current particle swarm in the current test operation;
[0010] The current particle swarm is updated using the fitness of each particle in the current particle swarm and the adaptive particle algorithm, and the updated particle swarm is determined as the new current particle swarm, and then the process jumps back to the step of determining the current particle swarm as the current test case cluster until a code test result that meets the preset test stop condition is obtained.
[0011] Optionally, the updating of the current particle swarm by using the fitness of each particle in the current particle swarm and an adaptive particle algorithm includes:
[0012] Based on the particle individual update formula and using the fitness of each particle in the current particle swarm, determine the current individual historical optimal fitness of each particle in the current particle swarm under the current optimization window and the current individual historical optimal position corresponding to the current individual historical optimal fitness;
[0013] Update the previous individual's historical optimal fitness and the previous individual's historical optimal position to the current individual's historical optimal fitness and the current individual's historical optimal position respectively;
[0014] Determine the largest current individual historical optimal fitness among the current individual historical optimal fitnesses in the current optimization window as the particle swarm historical optimal fitness of the current particle swarm, and determine the current individual historical optimal position corresponding to the particle swarm historical optimal fitness as the particle swarm historical optimal position of the current particle swarm;
[0015] The position and speed of the current particle swarm are updated based on the current individual historical optimal position, the particle swarm historical optimal position, the individual learning factor, and the social learning factor to obtain an updated particle swarm.
[0016] Optionally, the particle individual update formula is:
[0017] ;
[0018] in, is the current individual historical optimal fitness of the mth particle in the current particle swarm under the current optimization window, For the Test operation, is the mth particle in the current particle swarm The fitness of the test operation, is the width of the optimization window, is the fitness of the mth particle in the current particle swarm in the Rth test operation, is the position of the mth particle in the current particle swarm at the Rth test operation, is the current individual historical optimal position of the mth particle in the current particle swarm.
[0019] Optionally, before updating the position and speed of the current particle swarm based on the current individual historical optimal position, the particle swarm historical optimal position, the individual learning factor, and the social learning factor, the method further includes:
[0020] The individual learning factor and the social learning factor are determined using a preset learning factor determination formula; wherein the preset learning factor determination formula is:
[0021] ;
[0022] in, is the individual learning factor of the mth particle in the current particle swarm, is the social learning factor of the mth particle in the current particle swarm, 、 is a constant, is the current individual historical optimal fitness of the mth particle in the current particle swarm under the current optimization window, The mth particle is The fitness of the test operation.
[0023] Optionally, if the current test operation is an initial test operation, before determining the current particle swarm as the current test case cluster, the process further includes:
[0024] Initialize the relevant parameters of the adaptive particle algorithm; wherein the relevant parameters include particle swarm size, particle dimension, maximum number of iterations, inertia weight, learning factor, position range, and iteration step range;
[0025] Each initial particle is constructed based on the particle dimension to obtain an initial particle group including each initial particle.
[0026] Optionally, the code testing method further includes:
[0027] Initialize an input parameter constraint table based on the feasible domain of each input parameter in the test case and the constraint relationship between each input parameter;
[0028] Accordingly, the method of updating the current particle swarm by using the fitness of each particle in the current particle swarm and the adaptive particle algorithm, and determining the updated particle swarm as the new current particle swarm, includes:
[0029] The current particle swarm is updated using the fitness of each particle in the current particle swarm and an adaptive particle algorithm to obtain an initial updated particle swarm;
[0030] Screening out first particles that satisfy the constraint relationship corresponding to the input parameter constraint table and second particles that do not satisfy the constraint relationship corresponding to the input parameter constraint table from the initially updated particle swarm respectively;
[0031] A formula is generated based on the test case, and a fourth particle is generated using the second particle and a third particle generated according to a feasible domain of each of the input parameters in the input parameter constraint table to obtain a target updated particle swarm including the first particle and the fourth particle, and the target updated particle swarm is updated as the current particle swarm.
[0032] Optionally, the test case generation formula is:
[0033] ;
[0034] in, is a third particle generated according to the feasible domain of each input parameter in the input parameter constraint table, is the second particle, is the number of input parameters.
[0035] In a second aspect, the present invention discloses a code testing device, comprising:
[0036] A use case determination module, configured to determine a current particle swarm as a current test case cluster; wherein each particle in the current particle swarm includes input parameters of a test case in the current test case cluster and an injection time of the input parameters;
[0037] A code testing module, configured to inject the corresponding input parameters into the code to be tested based on the injection time, so as to obtain a code test result corresponding to the current test operation;
[0038] a fitness determination module, configured to determine, if a code test result corresponding to the current test operation does not satisfy a preset test stop condition, a difference between the code coverage of each particle in the current particle swarm and the total code coverage in the previous test operation, and determine each difference as the fitness of the corresponding particle in the current particle swarm in the current test operation;
[0039] The particle swarm update module is used to update the current particle swarm using the fitness of each particle in the current particle swarm and the adaptive particle algorithm, and determine the updated particle swarm as the new current particle swarm, and then jump back to the step of determining the current particle swarm as the current test case cluster until a code test result that meets the preset test stop condition is obtained.
[0040] In a third aspect, the present application discloses an electronic device, comprising:
[0041] Memory, used to store computer programs;
[0042] A processor is used to execute the computer program to implement the steps of the aforementioned disclosed code testing method.
[0043] In a fourth aspect, the present application discloses a computer-readable storage medium for storing a computer program; wherein, when the computer program is executed by a processor, the steps of the aforementioned disclosed code testing method are implemented.
[0044] It can be seen that the present invention discloses determining the current particle group as the current test case cluster; wherein, each particle in the current particle group respectively includes the input parameters of the test case in the current test case cluster and the injection time of the input parameters; based on the injection time, the corresponding input parameters are injected into the code to be tested to obtain the code test result corresponding to the current test operation; if the code test result corresponding to the current test operation does not meet the preset test stop condition, then determine the difference between the code coverage of each particle in the current particle group and the total code coverage in the previous test operation, and determine each difference as the fitness of each particle in the corresponding current particle group in the current test operation; use the fitness of each particle in the current particle group and the adaptive particle algorithm to update the current particle group, and determine the updated particle group as the new current particle group, and then jump again to the step of determining the current particle group as the current test case cluster until the code test result that meets the preset test stop condition is obtained.
[0045] Beneficial effect: Each particle of the present invention includes the input parameters of the test case in the current test case cluster and the injection time of the input parameters. That is to say, the present invention introduces an accompanying time factor into the input parameters in the test case, which can meet the actual needs of code testing containing timing control processes or time-related processes such as embedded code and chip design code. The corresponding input parameters are injected into the code to be tested based on the injection time, that is, by injecting test stimuli at a specific time to cover the time-related code, thereby solving the problem that the traditional test case generation method is difficult to apply to hardware code testing because it is oriented towards pure software testing, thereby improving the application surface of the test case automatic generation method and improving the code coverage rate of the code test; further, the fitness of each particle in the current particle group and the adaptive particle algorithm are used to update the current particle group to realize the automatic generation of test cases for the code to be tested, on the one hand, reducing the workload of verification personnel, and on the other hand, improving verification efficiency by more efficiently searching for valid test cases and reducing invalid testing processes. BRIEF DESCRIPTION OF THE DRAWINGS
[0046] In order to more clearly illustrate the embodiments of the present invention, the following is a brief introduction to the drawings required for use in the embodiments. Obviously, the drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without any creative work.
[0047] Figure 1 A code testing flow chart provided by an embodiment of the present invention;
[0048] Figure 2 A schematic diagram of a specific test case injection provided by an embodiment of the present invention;
[0049] Figure 3 A specific code testing flow chart provided by an embodiment of the present invention;
[0050] Figure 4 A schematic diagram of a specific adaptive particle algorithm process provided by an embodiment of the present invention;
[0051] Figure 5 A specific code test schematic diagram provided by an embodiment of the present invention;
[0052] Figure 6 A schematic diagram of the structure of a code testing device provided by an embodiment of the present invention;
[0053] Figure 7 A structural diagram of an electronic device provided in an embodiment of the present invention. DETAILED DESCRIPTION
[0054] The following will clearly and completely describe the technical solutions in the embodiments of the present invention in conjunction with the accompanying drawings. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of them. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without making any creative efforts shall fall within the scope of protection of the present invention.
[0055] The terms "including" and "having," as used in the present description and accompanying drawings, and any variations thereof, are intended to cover non-exclusive inclusions. For example, a process, method, system, product, or apparatus comprising a series of steps or elements is not limited to the listed steps or elements and may include steps or elements that are not listed.
[0056] Automatic test case generation methods based on intelligent search algorithms typically assume that the code under test is static. Static code refers to code that does not involve time-dependent program fragments. If processor fetch and execution time are not considered, given an input, the output is instantaneous and unique, as shown in Program 1 below:
[0057] function dut1(a, b)
[0058] if (a > 5 && b<7)
[0059] return 0;
[0060] else
[0061] return 1;
[0062] This is applicable to most pure software codes, but it is difficult to generate effective test cases for embedded codes or chip design codes that need to deal with time-related issues. This is because the input parameters can only trigger the corresponding code behavior and produce the expected output if they change within the specified time interval. Obviously, the traditional test case automatic generation method only generates the input parameters at the initial moment, and does not generate additional input parameter sequences in subsequent program runs. This is unable to cover time-related code fragments, such as shown in Program 2 below:
[0063] function dut2(a, b)
[0064] c<-0;
[0065] d <-b;
[0066] if a>5 then
[0067] while c<5 do
[0068] wait 1 sec;
[0069] c < -c+1;
[0070] if d==0 and b==1 then
[0071] return 2;
[0072] else
[0073] return 0;
[0074] else
[0075] return 1;
[0076] The time-related code "wait 1sec" indicates a one-second wait. The function of Program 2 is as follows: 1) If parameter a is greater than 5 at the start of the program, then wait for 5 seconds. If parameter b changes from 0 to 1 within these 5 seconds, the program outputs 2; otherwise, it outputs 0. 2) If parameter a is not greater than 5 at the start of the program, then the program outputs 1. For Program 2, if a and b are assigned values only at the initial moment, the output will never be 2. Only if a is assigned a value greater than 5 and b is assigned 0 at the initial moment, and b is then increased to 1 within the next 5 seconds, will Program 2 output 2.
[0077] It can be seen that the traditional test case automatic generation method only generates input parameters at the initial moment, and does not generate additional input parameter sequences in subsequent program runs, resulting in the inability to cover time-related code fragments, and the coverage rate still needs to be improved.
[0078] In order to enable those skilled in the art to better understand the present invention, the present invention will be further described in detail below with reference to the accompanying drawings and specific implementation methods.
[0079] Next, a code testing solution provided by an embodiment of the present invention is introduced in detail. Figure 1 A code testing method provided in an embodiment of the present invention includes:
[0080] Step S11: determining the current particle group as the current test case cluster; wherein each particle in the current particle group includes the input parameters of the test case in the current test case cluster and the injection time of the input parameters.
[0081] In this embodiment, if the current test operation is an initial test operation, before determining the current particle group as the current test case cluster, it also includes: initializing the relevant parameters of the adaptive particle algorithm; wherein the relevant parameters include particle group scale, particle dimension, maximum number of iterations, inertia weight, learning factor, position range, and iteration step range; constructing each initial particle based on the particle dimension to obtain an initial particle group containing each initial particle. If the first test operation is to be performed, it is necessary to first initialize the particle group scale, particle dimension, maximum number of iterations, inertia weight, learning factor, position range, iteration step range and other relevant parameters, and construct each initial particle based on the particle dimension to obtain an initial particle group containing each initial particle. It should be noted that the initial particles include the input parameters of the test case and the injection time of the input parameters, that is, each particle is a binary number, that is, if the current test operation n is 1, that is, the input parameter x of the test case under the kth particle dimension in the initial test operation is specifically , where x represents the input parameters of the test case, k represents the particle dimension, Indicates input parameters The injection time can be Considered as The accompanying input parameter of , but for the adaptive particle swarm algorithm, the two are independent; further, if the particle dimension of the output parameter x is k in total, then in the initial test operation, the mth particle is of the form .
[0082] If the current test operation n is not the initial test operation, that is, n is not 1, then if the particle dimensions of the output parameter x are k in total, in the current test operation n, the mth particle is of the form That is to say, in any test operation, the particle includes the input parameters of the test case and the injection time of the input parameters. That is, each particle is composed of binary numbers of multiple dimensions, and each binary number contains the input parameters of the test case under the current dimension and the injection time of the input parameters.
[0083] Step S12: injecting the corresponding input parameters into the code to be tested based on the injection time to obtain a code test result corresponding to the current test operation.
[0084] Inject the corresponding input parameters into the code to be tested based on the injection time. That is, each input parameter is injected into the code to be tested at the injection time. During the injection, other input parameters remain unchanged until all input parameters are injected and the current test operation ends. Figure 2 A specific test case injection diagram is shown, assuming that the particle dimension is 2 particles, the pattern is as follows , the time unit is ms, the value of the n-1th time is [2, 0.1, 9, 0.2], the value of the nth time is [5, 0.4, 20, 0.7], in the nth iteration, at 0.4ms, , , at 0.7ms, , ,It is understandable that the code to be tested is always running during the ,entire test process, with different parameter values being injected at ,different moments.
[0085] For the input parameters in the test case, an accompanying time factor is introduced to form a binary number structure of (input parameter, injection time). The added time dimension is processed according to the normal particle dimension. During the test, the program to be tested continues to run in a loop, and each input parameter in the test case is injected at the time of the accompanying time parameter. At this time, other input parameters remain unchanged. This can better handle the testing requirements of embedded code, chip design code, etc. containing timing control processes or time-related flow codes. By injecting test stimuli at specific times to cover time-related codes, the problem that traditional test case generation methods are difficult to apply to hardware code testing because they are oriented towards pure software testing is solved, thereby improving the application scope of test case automatic generation methods.
[0086] Step S13: If the code test result corresponding to the current test operation does not meet the preset test stop condition, the difference between the code coverage of each particle in the current particle swarm and the total code coverage in the previous test operation is determined, and each difference is respectively determined as the fitness of each particle in the current particle swarm in the current test operation.
[0087] In a specific test stop judgment embodiment, the code test result may include the code coverage corresponding to the current test operation, the performance of the code to be tested, and the number of iterations corresponding to the current test operation. The preset test stop condition may be set according to the number of test operations, that is, if the number of iterations corresponding to the current test operation is the maximum number of iterations, then the code test result corresponding to the current test operation is determined to meet the preset test stop condition; if the number of iterations corresponding to the current test operation is less than the maximum number of iterations, then the code test result corresponding to the current test operation is determined to not meet the preset test stop condition; the preset test stop condition may also be set according to the code coverage, that is, if the code coverage corresponding to the current test operation is less than the preset coverage threshold, then the code test result corresponding to the current test operation is determined to not meet the preset test stop condition; if the code coverage corresponding to the current test operation is not less than the preset coverage threshold, then the code test result corresponding to the current test operation is determined to meet the preset test stop condition.
[0088] In another specific test stop judgment embodiment, after obtaining the code coverage corresponding to each test operation, a code coverage growth curve can be drawn. It can be understood that the slope trend of the growth curve is from large to small, and there may even be a situation where the slope is 0, that is, the code coverage will not change after reaching a certain value. Therefore, the preset test stop condition can also be determined according to the change of the slope; further, the code coverage growth curve can allow users to more intuitively understand the changes in code coverage under different test operations, and the adaptive particle algorithm can also update the particle group according to the changes in code coverage.
[0089] If the code test result corresponding to the current test operation does not meet the preset test stop condition, the difference between the code coverage of each particle in the current particle swarm and the total code coverage in the previous test operation is determined, and each difference is determined as the fitness of each particle in the current particle swarm in the current test operation. The specific fitness determination formula is as follows:
[0090] ;
[0091] Where, represents the fitness of the mth particle in the nth test operation, It represents the code coverage of the mth particle in the nth test operation. It should be noted that the coverage of the mth particle in the nth iteration is combined with the total coverage of all particles in the n-1th iteration to calculate , Represents the total code coverage in the n-1th test operation. The coverage of all particles in the n-1th test operation is combined with the overall coverage in the n-2th test operation and recorded as the overall coverage of the n-1th test operation. The coverage calculated in this way is the total coverage of the n-1th iteration. In addition, .
[0092] Step S14: Update the current particle swarm using the fitness of each particle in the current particle swarm and the adaptive particle algorithm, and determine the updated particle swarm as the new current particle swarm, then jump back to the step of determining the current particle swarm as the current test case cluster until a code test result that meets the preset test stop condition is obtained.
[0093] In this embodiment, the fitness of each particle in the current particle swarm and the adaptive particle algorithm are used to update the current particle swarm, including: based on the particle individual update formula and using the fitness of each particle in the current particle swarm, determining the current individual historical optimal fitness of each particle in the current particle swarm under the current optimization window and the current individual historical optimal position corresponding to the current individual historical optimal fitness; updating the previous individual historical optimal fitness and the previous individual historical optimal position to the current individual historical optimal fitness and the current individual historical optimal position respectively; determining the largest current individual historical optimal fitness among the current individual historical optimal fitness under the current optimization window as the particle swarm historical optimal fitness of the current particle swarm, and determining the current individual historical optimal position corresponding to the particle swarm historical optimal fitness as the particle swarm historical optimal position of the current particle swarm; updating the position and speed of the current particle swarm based on the current individual historical optimal position, the particle swarm historical optimal position, the individual learning factor and the social learning factor to obtain the updated particle swarm. The specific particle swarm update process is as follows:
[0094] 1) Based on the particle individual update formula and using the fitness of each particle in the current particle swarm, determine the current individual historical optimal fitness of each particle in the current particle swarm under the current optimization window and the current individual historical optimal position corresponding to the current individual historical optimal fitness; it is understandable that, as the test iteration proceeds, the uncovered code is continuously reduced, and the coverage increment is also generally on a downward trend, and the coverage increment in the initial few test operations will be significantly higher than that in the subsequent iterations. In this embodiment, the coverage increment is used as the fitness. If the individual historical optimal position of the particle is defined as the position with the largest fitness among all previous historical positions, the algorithm can easily fall into a local optimal state, that is, the unreasonable situation of always taking the position with the largest fitness among the initial few historical positions as the individual historical optimal position will occur. Then, it is also unreasonable for the particle swarm historical optimal position to be the position with the largest fitness among all particle historical optimal positions, and it is difficult to escape, resulting in low efficiency of subsequent tests. For this reason, in this embodiment, only the historical values within a certain number of previous test operations (defined as the optimization window) are considered to avoid the pre-emptive advantage of the initial few iterations of the test, which causes the algorithm to be unable to jump out of the local optimal state determined by it. The particle individual update formula is:
[0095] ;
[0096] in, is the current individual historical optimal fitness of the mth particle in the current particle swarm under the current optimization window, q is the qth test operation, is the fitness of the mth particle in the current particle swarm in the qth test operation, L is the width of the optimization window, is the fitness of the mth particle in the current particle swarm in the Rth test operation, is the position of the mth particle in the current particle swarm at the Rth test operation, is the current individual historical optimal position of the mth particle in the current particle swarm;
[0097] 2) Update the previous individual's historical optimal fitness and the previous individual's historical optimal position to the current individual's historical optimal fitness and the current individual's historical optimal position respectively;
[0098] 3) The largest current individual historical optimal fitness among the current individual historical optimal fitness in the current optimization window is determined as the particle swarm historical optimal fitness of the current particle swarm, and the current individual historical optimal position corresponding to the particle swarm historical optimal fitness is determined as the particle swarm historical optimal position of the current particle swarm; the particle swarm update formula is as follows:
[0099] ;
[0100] Where, Indicates the historical optimal fitness of the current particle swarm in the current optimization window. Indicates the current individual historical optimal fitness of the mth particle in the current particle swarm under the current optimization window, Indicates the particle swarm size of the current particle swarm, Indicates the fitness of the Sth particle in the current optimization window, Indicates the position of the Sth particle in the current optimization window, Indicates the historical optimal position of the particle swarm in the current particle swarm. If there are multiple historical optimal positions of the particle swarm, the position corresponding to the maximum number of test operations is taken; 4) Based on the current individual historical optimal position , the particle swarm's historical optimal position , individual learning factor and social learning factors Update the position and velocity of the current particle swarm to obtain the updated particle swarm; the position and velocity update formulas are as follows:
[0101] ;
[0102] Where, It represents the speed of the mth particle of the current particle group in the nth test operation. It can be understood that the speed is a vector, and different dimensions correspond to different speeds. represents the individual learning factor, represents the current individual's best historical position, represents the social learning factor, 、 Represents a random number between [0, 1], represents the historical optimal position of the particle swarm, Indicates the inertia weight of the nth test operation. This embodiment designs the inertia weight based on coverage. Considering that coverage approaches 1 as the number of iterations increases, the code fragments that need to be searched in the later stage are often piecemeal, and the local search capability needs to be gradually enhanced. The inertia weight The formula for determining is as follows;
[0103] ;
[0104] Where, Indicates the preset maximum inertia weight, Indicates the preset minimum inertia weight, Indicates the total code coverage in the n-1th test operation.
[0105] In this embodiment, before updating the position and velocity of the current particle swarm based on the current individual historical optimal position, the particle swarm historical optimal position, the individual learning factor, and the social learning factor, the method further includes: determining the individual learning factor and the social learning factor using a preset learning factor determination formula; wherein the determination formula of the preset learning factor determination formula is:
[0106] ;
[0107] in, is the individual learning factor of the mth particle in the current particle swarm, is the social learning factor of the mth particle in the current particle swarm, 、 is a constant, is the current individual historical optimal fitness of the mth particle in the current particle swarm under the current optimization window, The mth particle is Considering that the decrease in particle fitness means that the uncovered code at its location is gradually decreasing, the formula for determining the learning factor can be preset to reduce individual factors and increase social factors when generating the next particle swarm.
[0108] It can be seen that the present invention discloses determining the current particle group as the current test case cluster; wherein, each particle in the current particle group respectively includes the input parameters of the test case in the current test case cluster and the injection time of the input parameters; based on the injection time, the corresponding input parameters are injected into the code to be tested to obtain the code test result corresponding to the current test operation; if the code test result corresponding to the current test operation does not meet the preset test stop condition, then determine the difference between the code coverage of each particle in the current particle group and the total code coverage in the previous test operation, and determine each difference as the fitness of each particle in the corresponding current particle group in the current test operation; use the fitness of each particle in the current particle group and the adaptive particle algorithm to update the current particle group, and determine the updated particle group as the new current particle group, and then jump again to the step of determining the current particle group as the current test case cluster until the code test result that meets the preset test stop condition is obtained.
[0109] Beneficial effect: Each particle of the present invention includes the input parameters of the test case in the current test case cluster and the injection time of the input parameters. That is to say, the present invention introduces an accompanying time factor into the input parameters in the test case, which can meet the actual needs of code testing containing timing control processes or time-related processes such as embedded code and chip design code. The corresponding input parameters are injected into the code to be tested based on the injection time, that is, by injecting test stimuli at a specific time to cover the time-related code, thereby solving the problem that the traditional test case generation method is difficult to apply to hardware code testing because it is oriented towards pure software testing, thereby improving the application surface of the test case automatic generation method and improving the code coverage rate of the code test; further, the fitness of each particle in the current particle group and the adaptive particle algorithm are used to update the current particle group to realize the automatic generation of test cases for the code to be tested, on the one hand, reducing the workload of verification personnel, and on the other hand, improving verification efficiency by more efficiently searching for valid test cases and reducing invalid testing processes.
[0110] See also Figure 3 The embodiment of the present invention discloses a specific code testing method. Compared with the previous embodiment, this embodiment further illustrates and optimizes the technical solution. Specifically, it includes:
[0111] Step S21: Initializing an input parameter constraint table based on the feasible domain of each input parameter in the test case and the constraint relationship between each input parameter.
[0112] Traditional particle swarm algorithms assume that each parameter within a particle is independent, and each parameter determines the position and velocity ranges when the algorithm is initialized. However, in reality, each parameter is usually not independent, but rather has certain constraints, and even cyclic constraints exist. For example, the code to be tested has three input parameters, a, b, and c, each with an unconstrained value range of 1 to 100, but there is a cyclic constraint relationship as follows:
[0113] 1) If a is in the range of 1 to 49, the value of b is in the range of 10 to 20; if a is in the range of 50 to 100, the value of b is not restricted;
[0114] 2) If b is in the range of 30 to 59, the value of c is in the range of 1 to 40; if b is in other ranges, the value of c is not restricted;
[0115] 3) If c is in the range of 30 to 59, the value of a is in the range of 1 to 10; if c is in other ranges, the value of a is not restricted.
[0116] If the particle swarm algorithm does not consider the parameter constraints when automatically generating the next position particle, particles such as a=20, b=5, and c=40 will appear after iteration. Even though they all meet the unconstrained value range of the three parameters, they do not meet the constraints. This will be an invalid test case. It can be seen that the parameter constraints need to be considered in the particle swarm algorithm to increase the proportion of valid test cases and thus improve the verification efficiency.
[0117] Initialize the input parameter constraint table based on the feasible domain of each input parameter in the test case and the constraint relationship between each input parameter. This input parameter constraint table can describe the constraint relationship between each input parameter. The test cases generated subsequently need to satisfy each constraint relationship in the input parameter constraint table, thereby avoiding the generation of invalid test cases. The input parameter constraint table is presented in the form of a function in the verification platform and is designed by the verification personnel according to the design manual of the code to be tested. It can be understood that the more complete the constraint relationship and feasible domain of the input parameters contained in the input parameter constraint table, the higher the test efficiency. The verification personnel can design a specific input parameter constraint table according to the specific scenario.
[0118] Step S22: determining the current particle group as the current test case cluster; wherein each particle in the current particle group includes the input parameters of the test case in the current test case cluster and the injection time of the input parameters.
[0119] Step S23: injecting the corresponding input parameters into the code to be tested based on the injection time to obtain a code test result corresponding to the current test operation.
[0120] Step S24: If the code test result corresponding to the current test operation does not meet the preset test stop condition, the difference between the code coverage of each particle in the current particle swarm and the total code coverage in the previous test operation is determined, and each difference is determined as the fitness of each particle in the current particle swarm in the current test operation.
[0121] Step S25: The current particle swarm is updated using the fitness of each particle in the current particle swarm and the adaptive particle algorithm to obtain an initial updated particle swarm; and a first particle that satisfies the constraint relationship corresponding to the input parameter constraint table and a second particle that does not satisfy the constraint relationship corresponding to the input parameter constraint table are respectively screened out from the initial updated particle swarm.
[0122] For example Figure 4 As shown in the figure, a specific process diagram of the adaptive particle algorithm is shown. In the process of obtaining the particle swarm after the initial update, the particle historical position, the local historical optimal position, the particle current position, the global historical optimal position, and the global optimal position are determined to update the particle swarm.
[0123] The first particle and the second particle are respectively selected from the particle swarm after the initial update, where the first particle is the particle that satisfies the corresponding constraint relationship in the input parameter constraint table, and the second particle is the particle that does not satisfy the corresponding constraint relationship in the input parameter constraint table; the input parameter constraint table can determine whether each particle generated by the adaptive particle swarm algorithm satisfies all the constraint relationships, as shown in Program 3:
[0124] function constraint(a,b,c)
[0125] if a > 100 or a < -1 then
[0126] return 0;
[0127] if b < 1000 and b > 500 then
[0128] return 0;
[0129] if a > 5 and b < 100 then
[0130] return 0;
[0131] if a <= 5 and b >= 300 then
[0132] return 0;
[0133] return 1;
[0134] The input parameters are a, b, and c. If the constraints are met, 1 is returned; otherwise, 0 is returned. The constraints are: a's value range is [-1, 100]; b's value range is (-∞, 500] ∪ [1000, +∞); if a is greater than 5, b must be greater than or equal to 10; if a is less than or equal to 5, b must be less than 30; there are no constraints on c. When the return value of program 3 is 1, the corresponding particles generated by the adaptive particle swarm algorithm meet the constraints and can be used for the next code test. If the return value is 0, new particles will need to be generated based on the test case generation formula and used for the next code test.
[0135] Step S26: Generate a formula based on the test case, and generate a fourth particle using the second particle and the third particle generated according to the feasible domain of each input parameter in the input parameter constraint table to obtain a target updated particle swarm including the first particle and the fourth particle, and update the target updated particle swarm to the current particle swarm.
[0136] In this embodiment, the test case generation formula is:
[0137] ;
[0138] in, is a third particle generated according to the feasible domain of each input parameter in the input parameter constraint table, is the second particle, and K is the number of the input parameters.
[0139] A third particle is generated according to the feasible domain of each input parameter in the input parameter constraint table, and a fourth particle is generated using the second particle and the third particle that do not satisfy the corresponding constraint relationship in the input parameter constraint table, so as to obtain a target updated particle group including the first particle and the fourth particle.
[0140] Step S27: Jump back to the step of determining the current particle swarm as the current test case cluster until a code test result that meets the preset test stop condition is obtained.
[0141] It can be seen that the present invention uses an intelligent search algorithm to realize the automatic generation of test cases for the code to be tested. On the one hand, it reduces the workload of the verification personnel. On the other hand, it improves the verification efficiency by searching for valid test cases more efficiently and reducing invalid test processes. Moreover, since it includes an intelligent search process, it can more easily realize the generation of corner test cases and the test completeness is better. Furthermore, based on prior knowledge such as design manuals, constraint relationships are introduced into the test case generation process to reduce the probability of generating invalid test cases, thereby further improving the test efficiency of the algorithm.
[0142] Below is Figure 5Taking a specific code test schematic diagram shown as an example, the present invention is described accordingly.
[0143] 1) Initialize the input parameter constraint table based on the feasible domain of each input parameter in the test case and the constraint relationship between each input parameter;
[0144] 2) Initializing relevant parameters of the adaptive particle algorithm; constructing each initial particle based on the particle dimension to obtain an initial particle swarm including each initial particle;
[0145] 3) Determine the initial particle swarm as the current particle swarm;
[0146] 4) determining the current particle group as the current test case cluster; wherein each particle in the current particle group includes the input parameters of the test case in the current test case cluster and the injection time of the input parameters;
[0147] 5) injecting the corresponding input parameters into the design under test (DUT) based on the injection time to test the code under test and obtain a code test result corresponding to the current test operation;
[0148] 6) If the code test result corresponding to the current test operation does not meet the preset test stop condition, the coverage increment is used as the fitness evaluation criterion, that is, the difference between the code coverage of each particle in the current particle swarm and the total code coverage in the previous test operation is determined, and each difference is respectively determined as the fitness of the corresponding particle in the current particle swarm in the current test operation;
[0149] 7) Using the fitness of each particle in the current particle swarm and the adaptive particle algorithm to determine the current individual historical optimal fitness and the current individual historical optimal position of each particle in the current particle swarm under the current optimization window, and then determining the particle swarm historical optimal fitness and the particle swarm historical optimal position of the current particle swarm to obtain the initial updated particle swarm;
[0150] 8) Regenerate the particles in the initial updated particle swarm that do not meet the input parameter constraint table to obtain the target updated particle swarm, use the target updated particle swarm as the current particle swarm, and then jump back to step 4), that is, after obtaining the next test case cluster, perform the next code test until the code test result that meets the preset test stop condition is obtained.
[0151] Figure 6 A schematic diagram of the structure of a code testing device provided in an embodiment of the present invention, the device comprising:
[0152] A use case determination module 11 is configured to determine a current particle group as a current test case cluster; wherein each particle in the current particle group includes input parameters of a test case in the current test case cluster and an injection time of the input parameters;
[0153] A code testing module 12 is configured to inject the corresponding input parameters into the code to be tested based on the injection time to obtain a code test result corresponding to the current test operation;
[0154] a fitness determination module 13, configured to determine the difference between the code coverage of each particle in the current particle swarm and the total code coverage in the previous test operation if the code test result corresponding to the current test operation does not meet a preset test stop condition, and determine each difference as the fitness of the corresponding particle in the current particle swarm in the current test operation;
[0155] The particle swarm updating module 14 is used to update the current particle swarm using the fitness of each particle in the current particle swarm and the adaptive particle algorithm, and determine the updated particle swarm as the new current particle swarm, and then jump back to the step of determining the current particle swarm as the current test case cluster until a code test result that meets the preset test stop condition is obtained.
[0156] As can be seen, the present invention discloses a code testing device, including a use case determination module, a code testing module, a fitness determination module, and a particle swarm update module. Each particle of the present invention includes the input parameters of the test case in the current test case cluster and the injection time of the input parameters. That is, the present invention introduces an accompanying time factor into the input parameters in the test case, which can meet the actual needs of code testing containing timing control processes or time-related processes such as embedded code and chip design code. Based on the injection time, the corresponding input parameters are injected into the code to be tested, that is, by injecting test stimuli at a specific time to cover time-related code, thereby solving the problem that traditional test case generation methods are difficult to apply to hardware code testing due to their focus on pure software testing. This improves the application of the test case automatic generation method and improves the code coverage rate of code testing. Furthermore, the fitness of each particle in the current particle swarm and the adaptive particle algorithm are used to update the current particle swarm to achieve automatic generation of test cases for the code to be tested. On the one hand, this reduces the workload of verification personnel, and on the other hand, it improves verification efficiency by more efficiently searching for valid test cases and reducing invalid test processes.
[0157] Since the embodiments of the apparatus part correspond to the embodiments of the method part, the embodiments of the apparatus part can be found in the description of the embodiments of the method part, which will not be described here. Moreover, the same beneficial effects as the above-mentioned code testing method are achieved.
[0158] Figure 7 A structural diagram of an electronic device provided by an embodiment of the present invention, such as Figure 7 As shown, the electronic device includes: a memory 20 for storing computer programs;
[0159] The processor 21 is configured to implement the steps of the code testing method of the above embodiment when executing a computer program.
[0160] The electronic device provided in this embodiment may include but is not limited to a smart phone, a tablet computer, a laptop computer, or a desktop computer.
[0161] The processor 21 may include one or more processing cores, such as a quad-core processor or an octa-core processor. The processor 21 may be implemented using at least one of the following hardware forms: a digital signal processing (DSP), a field-programmable gate array (FPGA), or a programmable logic array (PLA). The processor 21 may also include a main processor and a coprocessor. The main processor is a processor for processing data in the awake state, also known as a central processing unit (CPU); the coprocessor is a low-power processor for processing data in the standby state. In some embodiments, the processor 21 may be integrated with a graphics processing unit (GPU), which is responsible for rendering and drawing the content required to be displayed on the display screen. In some embodiments, the processor 21 may also include an artificial intelligence (AI) processor, which is responsible for processing computing operations related to machine learning.
[0162] The memory 20 may include one or more computer-readable storage media, which may be non-transitory. The memory 20 may also include a high-speed random access memory, and a non-volatile memory, such as one or more disk storage devices, flash memory storage devices. In this embodiment, the memory 20 is at least used to store the following computer program 201, wherein, after the computer program is loaded and executed by the processor 21, it can implement the relevant steps of the code testing method disclosed in any of the aforementioned embodiments. In addition, the resources stored in the memory 20 may also include an operating system 202 and data 203, etc., and the storage method may be temporary storage or permanent storage. Among them, the operating system 202 may include Windows, Unix, Linux, etc. The data 203 may include but is not limited to data transmitted from an external device and received by the electronic device, and may also include data collected by its own input and output interface 23, etc.
[0163] In some embodiments, the electronic device may further include a display screen 22 , an input / output interface 23 , a communication interface 24 , a power supply 25 , and a communication bus 26 .
[0164] Those skilled in the art will understand that Figure 7 The structure shown in the figure does not constitute a limitation of the electronic device, and may include more or fewer components than shown in the figure.
[0165] It is understandable that if the code testing method in the above embodiment is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present invention, or the part that contributes to the current technology, or all or part of the technical solution can be embodied in the form of a software product. The computer software product is stored in a storage medium and executes all or part of the steps of the methods of each embodiment of the present invention. The aforementioned storage medium includes: U disk, mobile hard disk, read-only memory (ROM), random access memory (RAM), electrically erasable programmable ROM, register, hard disk, removable disk, CD-ROM, magnetic disk or optical disk, etc. Various media that can store program code.
[0166] Based on this, an embodiment of the present invention further provides a computer-readable storage medium, on which a computer program is stored. When the computer program is executed by a processor, the steps of the above-mentioned code testing method are implemented.
[0167] The above describes in detail a code testing method, apparatus, device, and medium provided by an embodiment of the present invention. The various embodiments in this specification are described in a progressive manner, with each embodiment focusing on the differences from other embodiments. Similar or identical parts between the various embodiments can be referenced for reference. For the apparatus disclosed in the embodiments, since it corresponds to the method disclosed in the embodiments, the description is relatively simple. For relevant details, refer to the method description.
[0168] Professionals may further appreciate that the units and algorithm steps of each example described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, computer software, or a combination of the two. In order to clearly illustrate the interchangeability of hardware and software, the above description has generally described the components and steps of each example according to their functions. Whether these functions are performed in hardware or software depends on the specific application and design constraints of the technical solution. Professionals and technicians may use different methods to implement the described functions for each specific application, but such implementation should not be considered beyond the scope of the present invention.
[0169] The above is a detailed introduction to a code testing method, device, equipment and medium provided by the present invention. Specific examples are used herein to illustrate the principles and implementation methods of the present invention. The description of the above embodiments is only used to help understand the method of the present invention and its core ideas. It should be pointed out that for ordinary technicians in this technical field, without departing from the principles of the present invention, the present invention can also be improved and modified in several ways, and these improvements and modifications also fall within the scope of protection of the claims of the present invention.
Claims
1. A code testing method, characterized in that: include: Determine the current particle group as the current test case cluster; wherein each particle in the current particle group includes the input parameters of the test case in the current test case cluster and the injection time of the input parameters; Injecting the corresponding input parameter into the code to be tested based on the injection time to obtain a code test result corresponding to the current test operation; If the code test result corresponding to the current test operation does not meet the preset test stop condition, the difference between the code coverage of each particle in the current particle swarm and the total code coverage in the previous test operation is determined, and each difference is respectively determined as the fitness of each particle in the current particle swarm in the current test operation; The current particle swarm is updated using the fitness of each particle in the current particle swarm and the adaptive particle algorithm, and the updated particle swarm is determined as a new current particle swarm, and then the process jumps again to the step of determining the current particle swarm as the current test case cluster until a code test result that meets the preset test stop condition is obtained.
2. The code testing method according to claim 1, characterized in that: The updating of the current particle swarm by using the fitness of each particle in the current particle swarm and the adaptive particle algorithm comprises: Based on the particle individual update formula and using the fitness of each particle in the current particle swarm, determine the current individual historical optimal fitness of each particle in the current particle swarm under the current optimization window and the current individual historical optimal position corresponding to the current individual historical optimal fitness; Update the best fitness value and the best position of the previous individual history to the best fitness value and the best position of the current individual history respectively; Determine the largest current individual historical optimal fitness among the current individual historical optimal fitness in the current optimization window as the particle swarm historical optimal fitness of the current particle swarm, and determine the current individual historical optimal position corresponding to the particle swarm historical optimal fitness as the particle swarm historical optimal position of the current particle swarm; The position and speed of the current particle swarm are updated based on the current individual historical optimal position, the particle swarm historical optimal position, the individual learning factor and the social learning factor to obtain an updated particle swarm.
3. The code testing method according to claim 2, characterized in that: The particle individual update formula is: ; in, is the current individual historical optimal fitness of the mth particle in the current particle swarm under the current optimization window, For the Test operation, is the mth particle in the current particle group The fitness of the test operation, is the width of the optimization window, is the fitness of the mth particle in the current particle swarm in the Rth test operation, is the position of the mth particle in the current particle swarm at the Rth test operation, is the current individual historical optimal position of the mth particle in the current particle swarm.
4. The code testing method according to claim 2, characterized in that: Before updating the position and speed of the current particle swarm based on the current individual historical optimal position, the particle swarm historical optimal position, the individual learning factor and the social learning factor, the method further includes: The individual learning factor and the social learning factor are determined using a preset learning factor determination formula; wherein the preset learning factor determination formula is: ; in, is the individual learning factor of the mth particle in the current particle swarm, is the social learning factor of the mth particle in the current particle swarm, , is a constant, is the current individual historical optimal fitness of the mth particle in the current particle swarm under the current optimization window, is the mth particle in the The fitness of the test operation.
5. The code testing method according to claim 1, characterized in that: If the current test operation is an initial test operation, before determining the current particle group as the current test case cluster, the method further includes: Initialize the relevant parameters of the adaptive particle algorithm; wherein the relevant parameters include particle swarm size, particle dimension, maximum number of iterations, inertia weight, learning factor, position range, and iteration step range; Each initial particle is constructed based on the particle dimension to obtain an initial particle group including each initial particle.
6. The code testing method according to any one of claims 1 to 5, characterized in that: Also includes: Initialize an input parameter constraint table based on the feasible domain of each input parameter in the test case and the constraint relationship between each input parameter; Correspondingly, the method of updating the current particle swarm by using the fitness of each particle in the current particle swarm and the adaptive particle algorithm, and determining the updated particle swarm as the new current particle swarm, includes: The current particle swarm is updated using the fitness of each particle in the current particle swarm and an adaptive particle algorithm to obtain an initial updated particle swarm; Screening out first particles satisfying the constraint relationship corresponding to the input parameter constraint table and second particles not satisfying the constraint relationship corresponding to the input parameter constraint table from the initially updated particle group respectively; A formula is generated based on the test case, and a fourth particle is generated using the second particle and a third particle generated according to the feasible domain of each of the input parameters in the input parameter constraint table, so as to obtain a target updated particle swarm including the first particle and the fourth particle, and the target updated particle swarm is updated as the current particle swarm.
7. The code testing method according to claim 6, characterized in that: The test case generation formula is: ; in, is a third particle generated according to the feasible domain of each input parameter in the input parameter constraint table, is the second particle, is the number of input parameters.
8. A code testing device, characterized in that: include: A case determination module, used to determine the current particle group as the current test case cluster; wherein each particle in the current particle group includes the input parameters of the test case in the current test case cluster and the injection time of the input parameters; A code testing module, used for injecting the corresponding input parameters into the code to be tested based on the injection time, so as to obtain a code test result corresponding to the current test operation; A fitness determination module, configured to determine the difference between the code coverage of each particle in the current particle swarm and the total code coverage in the previous test operation if the code test result corresponding to the current test operation does not meet the preset test stop condition, and determine each difference as the fitness of each particle in the current particle swarm in the current test operation; The particle swarm updating module is used to update the current particle swarm by using the fitness of each particle in the current particle swarm and the adaptive particle algorithm, and determine the updated particle swarm as the new current particle swarm, and then jump back to the step of determining the current particle swarm as the current test case cluster until a code test result that meets the preset test stop condition is obtained.
9. An electronic device, characterized in that: include: Memory for storing computer programs; A processor, configured to execute the computer program to implement the steps of the code testing method according to any one of claims 1 to 7.
10. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the steps of the code testing method according to any one of claims 1 to 7 are implemented.
Citation Information
Cited By
Test case combination generation method, device, system, equipment and medium
CN120492333A
