[0044] The present invention will be further described below with reference to the drawings and embodiments.
[0045] figure 1 The principle of the preferred embodiment of the optimized ant colony algorithm test case automatic generation method of the present invention is shown. See figure 1 The method of this embodiment mainly includes the following steps. In order to explain the realization of this method more conveniently, the following example is used as the source program for explanation:
[0046]
[0047] Step S11: Scan the source program, perform lexical analysis, and obtain a variable table.
[0048] For example, a lexical analyzer written in flex is used to scan the source program, and at the same time, the variable information is extracted during the scanning process, and the variable table is constructed.
[0049] Step S12: Scan the source program, use the pre-written syntax analyzer written by bison to perform grammatical analysis to obtain the queen, the little ant, the end ant and the influence information of each little ant on the variables, and combine the variable table obtained in step S11 to construct Ant variable table.
[0050] Taking the above source program as an example, through lexical analysis and grammatical analysis, the ant variable table can be obtained as follows:
[0051]
[0052] Step S13: Complete the source program according to the scanning information of steps S11 and S12, and perform the queen insertion according to the queen information in the ant variable table, and record the stake information.
[0053] Taking the above source program as an example, the results after instrumentation are as follows. The shaded sentences are inserted sentences, including the sentences of perfect program and instrumented sentences. The ones on the right start with M are ants, and those starting with h are queens. .
[0054]
[0055]
[0056]
[0057] Step S14: Initialize the value of each path pheromone according to the information of the queen, and based on the ant variable table, accumulate the pheromone of each path to obtain the result value of the path pheromone of each path, according to the path pheromone The result value of excluding the paths that cannot be covered, and taking a path from the remaining paths as the current test path.
[0058] Among them, the elite ant algorithm is used to accumulate the pheromone of each path.
[0059] Taking the above source program as an example, you can do the following program preprocessing, first get the first path: M0-M11-M21-M31-M41-M51, and then use the elite ant algorithm to accumulate path pheromone to M31 to get path information If tri = 6 contradicts the tri = 0 required by M41, it is determined that the path is unreachable.
[0060] Step S15: According to the relevant pheromone of the current test path, an initial test case is constructed using the SOR iteration method as the current test case. The SOR iteration method is an algorithm suitable for large sparse matrix equations in the existing iteration method.
[0061] In the process of constructing the initial test case, first, use a random function to construct a test case. Then, according to the order of the path, collect the pheromone on the path and the influence information of each ant on the variable, calculate the linear arithmetic representation of the predicate function of each predicate sentence, and construct the linear constraint of the input variable until the predicate sentence contains Stop when the predicate function cannot be expressed using linear arithmetic. Finally, the linear constraint system is solved and a new test case is obtained, which is the initial test case.
[0062] Taking the above program as an example, construct the initial test case as follows:
[0063] First, get the nth path: M0-M11-M21-M31-M43
[0064] Then, using the elite ant algorithm to accumulate the path pheromone, the path pheromone is i=j=k
[0065] Finally, use the random function to get the use case i=2,j=2,k=2
[0066] Step S16: Invoke the gcc command to execute the current test case, and write the traversal status of the current test case into the log file, so as to guide the change of pheromone.
[0067] Step S17: According to the traversal in step 6, judge whether the current test case can cover the current test case, if so, the current test case can be classified into the test case set, and the next path is selected, and then go to step 5; if not Then adjust the ant’s pheromone to make the generated test case change to better cover the test path, and generate a new test case based on the adjusted ant’s pheromone, as the current test case, and go to step S16, and finally The test case set of is the last output test case.
[0068] Adjusting ant's pheromone includes four aspects: adjusting the direction of change of ant's pheromone (direction, the value range is the number range of the variable, the number starts from 1 and ends with the total number of variables) to indicate Which variable in the test case changes; adjust the ant’s pheromone pointing growth amount (dgrow, its value type is integer, the starting value is 0), to indicate whether to increase the change pointing amount; adjust the ant’s The growth direction of the pheromone (grow, that is, whether the change in the test case is increasing or decreasing, and its value is -1, 0, 1), which is used to indicate the direction of change in the value of the variable in the test case; adjust the pheromone of the ant The growth rate (△x, the type is floating-point type, the value range is 0.0~9.9), which is used to indicate the growth rate of the variables in the test case.
[0069] Modify the pheromone of the ant to make the test case change in the direction of better covering the test path. The main steps are as follows:
[0070] The first step is to compare the test results this time with the previous one. If the path coverage is higher, that is, the test path is better covered, then skip to the third step, otherwise, skip to the second step;
[0071] The second step is to read the log content. According to the log content, modify the four variables of the ant pheromone as follows:
[0072] if(dgrow==3) {direction=direction+1;grow=1;dgrow=0;}
[0073] grow=grow–1;
[0074] dgrow=dgrow+1;
[0075] Δx=random(0.0,9.9);
[0076] The third step is to modify the test case as follows according to the ant pheromone:
[0077] Let the variable with number equal to direction be n
[0078] n=n+grow*△x
[0079] figure 2 show figure 1 Detailed process of the embodiment. See figure 2 ,according to figure 2 The shown process, taking the above-mentioned source program as an example, the implementation steps of this method are:
[0080] a) Obtain the k-th path: M0-M11-M22-M32-M43;
[0081] b) Using the elite ant algorithm to accumulate path pheromone, the path pheromone is i=j;
[0082] c) According to the path pheromone, use a random function to get the use case i=2, j=2, k=2;
[0083] d) Run use cases;
[0084] e) Reading the log file, we can see that the use case does not cover M22, and the ant pheromone reading M22 is i==k is false;
[0085] f) According to the ant pheromone and the initial information difference Δx=1, k=k+1=3;
[0086] g) Run case i=2, j=2, k=3;
[0087] h) Read the log file, we can see that the test path is covered by the use case.
[0088] image 3 The system structure of the preferred embodiment of the optimized ant colony algorithm test case automatic generation system of the present invention is shown. See image 3 The system of this embodiment includes: a lexical analysis device 11, a grammatical analysis device 12, a queen plugging device 13, a path preprocessing device 14, an initial test case construction device 15, a test case traversal detection device 16, an ant pheromone adjustment device 17.
[0089] The lexical analysis device 11 scans the source program, performs lexical analysis, and obtains a variable table.
[0090] The grammatical analysis device 12 scans the source program, performs grammatical analysis, obtains the queen, the little ant, the end ant, and the influence information of each little ant on the variables, and combines the variable table obtained by the lexical analysis device to construct an ant variable table.
[0091] The queen plugging device 13 completes the source program according to the scanning information of the lexical analysis device and the syntax analysis device, and performs the queen plugging according to the information of the queen in the ant variable table, and records the pile information.
[0092] The path preprocessing device 14 initializes the value of the pheromone of each path according to the information of the queen, and based on the ant variable table, accumulates the pheromone of each path to obtain the result value of the path pheromone of each path. The result value of the pheromone eliminates the path that cannot be covered, and takes a path from the remaining paths as the current test path. The path preprocessing device 14 uses the elite ant algorithm to accumulate the pheromone of each path.
[0093] The initial test case construction device 15 uses the SOR iteration method to construct the initial test case as the current test case according to the relevant pheromone of the current test path.
[0094] The test case traversal reconnaissance device 16 invokes commands to execute the current test case, and writes the traversal status of the current test case into the log file, so as to guide the change of pheromone.
[0095] The ant pheromone adjustment device 17 judges whether the current test case can cover the current test case according to the traversal situation of the test case traversal reconnaissance device. If so, the current test case can be classified into the test case set, and the next path is selected, and Transfer to the test case to traverse the reconnaissance device, if not, adjust the ant’s pheromone so that the generated test case changes to better cover the test path, and generate a new test case based on the adjusted ant’s pheromone as the current one Test cases are transferred to the test case traversal reconnaissance device, and the final test case set is the final output test case. The ant pheromone adjusting device 17 further includes: a variable direction amount adjustment module 171, a direction increase amount adjustment module 172, a growth direction amount adjustment module 173, and a growth range amount adjustment module 174.
[0096] The change pointing amount adjustment module 171 adjusts the change pointing amount of the pheromone of the ant to indicate which variable in the test case changes. The pointing growth amount adjustment module 172 adjusts the pointing growth amount of the pheromone of the ant to indicate whether to increase the change pointing amount. The growth direction amount adjustment module 173 adjusts the growth direction amount of the pheromone of the ant to indicate the change direction of the value of the variable in the test case. The growth rate adjustment module 174 adjusts the growth rate of the pheromone of the ant to indicate the growth rate of the variables in the test case.
[0097] Take the source program mentioned above as an example, if the test case is generated manually:
[0098]
[0099] From the table, there are 8 valid use cases:
[0100] 1) i=2; j=2; k=2
[0101] 2) i=2; j=2; k=3
[0102] 3) i=2; j=3; k=2
[0103] 4) i=3; j=2; k=2
[0104] 5) i=1; j=2; k=3
[0105] 6) i=3; j=5; k=1
[0106] 7) i=5; j=3; k=1
[0107] 8) i=3; j=1; k=5
[0108] It is easy to omit the use cases of Article 7 and Article 8 in the actual manual operation process, the operation process is more complicated, and the value of the variables in the use case is easily affected by personal preference for numbers.
[0109] If the method of the present invention is adopted, the following test cases can be obtained:
[0110] 1) i=2; j=2; k=2
[0111] 2) i=2; j=2; k=3
[0112] 3) i=3; j=2; k=3
[0113] 4) i=3; j=2; k=2
[0114] 5) i=1; j=2; k=3
[0115] 6) i=3; j=5; k=1
[0116] 7) i=7; j=5; k=1
[0117] 8) i=7; j=5; k=13
[0118] The above-mentioned embodiments are provided for those of ordinary skill in the art to implement and use the present invention. Those of ordinary skill in the art can make various modifications or changes to the above-mentioned embodiments without departing from the inventive idea of the present invention. The protection scope of the invention is not limited by the above-mentioned embodiments, but should be the maximum scope that meets the innovative features mentioned in the claims.