Some simple algorithms used in automated scripts

Source: Internet
Author: User

Some simple algorithms used in automated scripts
Background

When writing an automated script, you need to simulate sending network requests. The network requests include hid, md5, 64-bit or 32-bit machines, and these factors are still unknown, the product and development may change at any time. Here we want to automatically generate a Case based on the full arrangement of these factors through automated scripts.

Here we will discuss how to write a code with better scalability to generate a fully-arranged Case, and share it with you briefly. Please pay attention to another article (to be released) for the specific causes and consequences ).

Problem Analysis

Now let's assume the three cases below

Hid Md5 64-bit/32-bit
123 Abc 32
456 Cde 64

Now there are three more factors, with each factor in two cases. There are eight in the full arrangement, and we are most likely to think of a for loop. The pseudo code is as follows:

For (hid) for (md5) for (64-bit/32-bit)

This is easy to write, but if we add another factor, such as whether to install the sogou browser, we need to modify the code and add a loop. In this way, the Code is not only difficult to maintain, but also not beautiful.

What should I do?

I think of two solutions:

Recursive Solution

The first time a factor is removed from the first array, recursively calling the second array to obtain the factor until the last array is obtained, this is a complete Case. Then, retrieve the elements of each array to obtain all the paths.

The pseudo code is as follows (we maintain all the factors and their corresponding conditions in one document, and the function itself reads all the factors in order, which is simple as I will not talk about ):

// Dep indicates the current number of factors, and line indicates the total number of factors void GetFullAz (int dep, int Line) {if (dep> = Line) // return; for (int I = 1; I <= len (dep); I ++) // traverses the dep factor GetFullAz (dep + 1, line );}

Here we will discuss some path record techniques. Let's look at the Appendix code in a unified manner.

Solution similar to state Compression

In fact, this is a full-state enumeration. We use a single digit to record the state of a factor, and finally combine these numbers to obtain the full State from bottom to top.

Example: For the above example, since each factor has only two States, we can use three binary numbers to represent them. After all, the result is a three-digit binary number with a minimum value of 0, if the maximum value is 7, we can obtain the full State from 0 to 7.

The pseudo code is as follows:

// Decodes a number to each digit to form a case. For example, if the number ranges from 0 to 7 to 5, the number is decoded to 101 first, and then the result is 101, both: hid = 456 md5 = abc system = 64-bit void Decode (int state, int Line) // current state bit, number of line factors {printf ("Case: %-4d ", CaseNum ++); for (int I = Line-1; I >=0; I --) {int Temp = state % FactorNum [I]; // how many conditions does FactorNum [I] factor I have? state/= FactorNum [I]; cout <CaseArr [I] [0] <"=" <CaseArr [I] [Temp + 1] <";}cout <endl ;} // enumeration status bit for (int I = 0; I <State; I ++) {Decode (I, Line );}
Summary

I believe that there are still a lot of multi-factor situations we encounter during the test, and factor changes often happen, manually, we can use some tools to generate orthogonal experiments or use cases with full arrangement, but we have to implement the corresponding algorithms by ourselves with automation. I would like to share this with you briefly and hope to criticize and correct it.

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") string CaseArr [1000] [100]; int FactorNum [1000], CaseNum; // recursive solution vector <int> CasePath; void OutPut () {int Len = CasePath. size (); printf ("Case: %-4d", CaseNum ++); for (int I = 0; I <Len; I ++) {cout <CaseArr [I] [0] <"=" <CaseArr [I] [CasePath [I] <"" ;}cout <endl ;} void GetFullAz (int dep, int Line) {if (dep> = Line) {OutPut (); return ;}for (int I = 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 () ;}}// status compression solution void Decode (int state, int Line) {printf ("Case: %-4d", CaseNum ++ ); // cout <"Case:" <CaseNum ++ <""; for (int I = Line-1; I> = 0; I --) {int Temp = state % FactorNum [I]; state/= FactorNum [I]; cout <CaseArr [I] [0] <"=" <CaseArr [I] [Temp + 1] <";}cout <endl ;} int main () {freopen ("testin.txt", "r", stdin); string FactorName; int Line = 0; int State = 1; while (cin> FactorName) {cin> FactorNum [Line]; State * = FactorNum [Line]; CaseArr [Line] [0] = FactorName; for (int I = 1; I <= FactorNum [Line]; I ++) {cin> CaseArr [Line] [I];} Line ++; // getchar ();} // recursive CasePath. clear (); CaseNum = 1; GetFullAz (0, Line); // status compression // CaseNum = 1; // for (int I = 0; I <State; I ++) // {// Decode (I, Line); //} return 0;}/* testin.txt */Hid 3 11 22 33Md5 3 qw as zxSE 2 has none

Summary

The above is a summary of a small problem in the work process. You are welcome to raise suggestions or questions. In addition, if you want more information, please pay attention to the Public Account "sogou test"

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.