Background introduction
When writing an automated script, to simulate sending a network request, where the network request contains HID, MD5, machine is 64-bit or 32-bit, and so on, and these factors are still uncertain, product and development may be changed at any time, Here we want to automate the generation of case based on these factors in an automated script.
Here's how to write a good extension of the code to generate a full array of cases, do a simple share, the specific causes and consequences please pay attention to another article (to be sent).
Problem analysis
Now, let's say the following three case
hid |
MD5 |
64-bit/32-bit |
123 |
Abc |
32 |
456 |
Cde |
64 |
Now there are three factors, each factor 2 cases, the whole arrangement is 8 kinds, we most easily think of is for Loop, pseudo code as follows:
for(hid) for(md5) for(64位/32位)
This is simple to write, but if we now add a factor, such as whether to install Sogou browser, this code we need to change, we have to add a layer of loops. This makes the final code not only very cumbersome to maintain, but also unattractive.
What to do?
There are two ways I can think of it:
Recursive solution
The first time in the first array to go to a factor, recursive call to go to the second array to take a factor, until the last array is taken, this is a complete case. Then each array iterates through the elements to get all the paths.
The pseudo code is as follows (we maintain all the factors and the corresponding situation in a document, the function goes to the document in order to read all the factors, this is relatively simple I will not say more):
//DEP" is currently the number of factors, line represents a total number of factors void Getfullaz (int DEP, int line) {if (DEP >= Line) //The last factor of return ; for (int i = 1 ; I <= Len (DEP); i++) //traverse dep factor Getfullaz (dep + 1 , line);}
Here are some of the techniques of path recording, do not say a word, unified look at the Appendix Code
A solution similar to state compression
In fact, this is a full state enumeration, we use a single number to record a factor of the state, and finally the number together, from the bottom to the large enumeration, the full state is obtained.
Example: For the above example, because each factor has only two states, we can use three binary number to represent, then together is a three-bit binary number, the minimum value is 0, the maximum value is 7, we from 0 to 7, we can get the full state.
The pseudo code is as follows:
//Encode a number to each bit to form a case, such as enumeration 0 to 7 above enumeration to 5 o'clock, first decode 5 to 101, let go to see 101 corresponding situation, both: HID = 456 MD5 = ABC system = 64 bitvoidDecode (intStateintLine)//state current status bit, line factor number{printf("CASE:%-4D", casenum++); for(inti = line-1; I >=0; i--) {intTemp = state% factornum[i];//factornum[i] The first factor of the number of casesState/= Factornum[i];cout<<casearr[i][0]<<" = "<<casearr[i][temp+1]<<" "; }cout<<endl;}//enumeration status bit for(inti =0; I < State; i++) {Decode (I, line);}
Summarize
We believe that we have many factors in the test of the situation is still very much, and the change of factors will often occur, manual we can use some tools to generate orthogonal test or is the full array of use cases, but with automation we have to implement the corresponding algorithm. Here for everyone to do a simple share, but also hope to criticize correct.
Code Appendix
/ * * main.cpp * * Created on:2014-12-9 * Author:fangyu */#include<functional>#include<algorithm>#include<iostream>#include<sstream>#include<iomanip>#include<numeric>#include<cstring>#include<cassert>#include<cstdio>#include<string>#include<vector>#include<bitset>#include<queue>#include<stack>#include<cmath>#include<ctime>#include<list>#include<set>#include<map>using namespace STD;//#pragma comment (linker, "/stack:102400000,102400000")stringcasearr[ +][ -];intfactornum[ +], Casenum;//Recursive solutionVector<int> Casepath;voidOutPut () {intLen = Casepath.size ();printf("CASE:%-4D", casenum++); for(inti =0; i < Len; i++) {cout<<casearr[i][0]<<" = "<<CaseArr[i][CasePath[i]]<<" "; }cout<<endl;}voidGetfullaz (intDepintLine) {if(DEP >= Line) {OutPut ();return; } for(inti =1; I <= FACTORNUM[DEP]; i++) {//cout<<casearr[line][0]<< "=" <<CaseArr[Line][i]<< ""; //if (line = = 1) cout<<endl;Casepath.push_back (i); Getfullaz (DEP +1, line); Casepath.pop_back (); }}//state compression SolutionvoidDecode (intStateintLine) {printf("CASE:%-4D", casenum++);//cout<< "Case:" <<CaseNum++<< ""; for(inti = line-1; I >=0; i--) {intTemp = state% factornum[i]; State/= Factornum[i];cout<<casearr[i][0]<<" = "<<casearr[i][temp+1]<<" "; }cout<<endl;}intMain () {Freopen ("Testin.txt","R", stdin);stringFactorname;intline =0;intState =1; while(Cin>>factorname) {Cin>>FactorNum[Line]; State *= Factornum[line]; casearr[line][0] = Factorname; for(inti =1; I <= Factornum[line]; i++) {Cin>>CaseArr[Line][i]; } line++;//getchar ();}//RecursiveCasepath.clear (); Casenum =1; Getfullaz (0, line);//State compression//Casenum = 1;//for (int i = 0; i < state; i++)// {//Decode (I, line);// } return 0;}/*testin.txt*/Hid3 One A -Md53QW as Zxse2There is no
Summarize
The above is a small problem in the work process, do some summary, welcome suggestions or questions, in addition, for more information, please pay attention to the public number "Sogou test"
Some simple algorithms that are shipped in automation scripts