# Automatic Test Data Generation Using the Activity Diagram and Search-Based Technique
## 3 AutoTDGen Approach
### Automatic Data Flow Analysis and Test Data Generation
#### 3.3.2 Test Data Generation
Our designed fitness function rewards the test cases that are very close to the guard condition boundary. Guiding the search towards generating test data that are very close to the boundary has further the advantage of allowing to easily generate follow-up test data by simply adding or subtracting a small number from the original data so that tester can select multiple test cases for testing the invalid boundary domain as well. For example, in a guard condition [sales \(\geq\) 1000], a test data \(t\) = 1799 will have the fitness value \(f\) = 1.27 regardless of both satisfying the guard condition and def-use pairs, whereas, \(t\) = 1001 will have a fitness  
repeated until \(f(v_{i},dup)=0\). As shown below in Equation (4), when the fitness reaches zero, the test data covering all def-use pairs associated with the variable sales have been optimized. The example in Equation (5) shows a situation in which neither the guard condition is covered nor the \(DUP_{CL}\) has reached zero. Therefore, \(f(v_{i},dup)>1\), indicating that the individuals are not optimal test data. \[f(\textit{sales},(8,\textit{sales}))=\frac{(6-3)}{6+5}+0=0.27 \tag{2}\] \[\text{GC}_{\text{D}}=((24\times 45)+(14\times 30)+(12\times 25))-1800=0\]  
\[f(\textit{sales},(8,\textit{sales}))=\frac{(6-3-2)}{3+5}+0=0.12 \tag{3}\] \[\text{GC}_{\text{D}}=((4\times 45)+(4\times 30)+(28\times 25))-1000=0\]  
\[f(\textit{sales},(8,\textit{sales}))=\frac{(6-3-2-1)}{1+5}+0=0.00 \tag{4}\] \[\text{GC}_{\text{D}}=990-((8\times 45)+(16\times 30)+(6\times 25))=0.00\]  
\[f(\textit{sales},(8,\textit{sales}))=\frac{(6-0)}{6+5}+85=85.54 \tag{5}\] \[\text{GC}_{\text{D}}=((23\times 45)+(10\times 30)+(22\times 25))-1800 =85\]  
The overall fitness function to minimize a set of test data \(V\) on a set of def-use pairs \(L\) is thus as follows:  
\[fitness(V,L)=\sum_{dup\in L}f(v_{i},dup) \tag{6}\]  
**Search Operators**: From a high-level, GAs are mostly similar for creating a solution to all kinds of engineering problems. When it is used to solve a specific problem, the search operators need to be adjusted accordingly. Hence, we have defined the search operators as follows. **Selection**: The selection is used to pick individual test data from the current test data population set as parents of the next generation. We have used the tournament selection strategy to pick a random number of test data from the current test data population set, then pick the one with the best fitness value from that group and repeat until the pool is completed. Simply put, we select \(n\) number of test data (individuals) from the test data population, and based on the ranking of their fitness values, we pick the fittest individual from the \(n\) individuals as the next generation which then goes through crossover and mutation and the steps are repeated until we have the final candidates passing to the next generation. Crossover: The crossover operator is used to create two new test data offspring from the two parents. We have used a single-point crossover operator in which we first select the two target parents and then randomly select the crossover point \(p\) (\(p=0\) to \(n-1\)). Mutation: The test data mutation is performed after the crossover operation using the uniform mutation strategy, with the mutation probability \(p=(1/\text{pop\_size})\). In this case, every individual test data has an equal probability to undergo mutation. We pick a random integer \"count\" from (2, Len(genes) and calculate its initial fitness value. While the count is greater than zero, we select two random samples \(a\) and \(b\) from the current population, swap the two samples, calculate the fitness value, if the calculated fitness value is greater than the initial fitness, then the mutation is performed.