📜  测试用例生成|第2组(随机字符,字符串和随机字符串数组)

📅  最后修改于: 2021-06-26 15:24:02             🧑  作者: Mango

集合1(随机数,数组和矩阵)

  • 生成随机字符
    // A C++ Program to generate test cases for
    // random characters
    #include
    using namespace std;
      
    // Define the number of runs for the test data
    // generated
    #define RUN 5
      
    // Define the range of the test data generated
    // Here it is 'a' to 'z'
    #define MAX 25
      
    int main()
    {
        // Uncomment the below line to store
        // the test data in a file
        // freopen ("Test_Cases_Random_Character.in", "w", stdout);
      
        // For random values every time
        srand(time(NULL));
      
        for (int i=1; i<=RUN; i++)
            printf("%c\n", 'a' + rand() % MAX);
      
        // Uncomment the below line to store
        // the test data in a file
        // fclose(stdout);
        return(0);
    }
    
  • 生成随机字符串
    // A C++ Program to generate test cases for
    // random strings
    #include
    using namespace std;
      
    // Define the number of runs for the test data
    // generated
    #define RUN 100000
      
    // Define the range of the test data generated
    // Here it is 'a' to 'z'
    #define MAX 25
      
    // Define the maximum length of string
    #define MAXLEN 100
      
    int main()
    {
        // Uncomment the below line to store
        // the test data in a file
        // freopen ("Test_Cases_Random_String.in", "w", stdout);
      
        //For random values every time
        srand(time(NULL));
      
        int LEN;    // Length of string
      
        for (int i=1; i<=RUN; i++)
        {
            LEN = 1 + rand() % MAXLEN;
      
            // First print the length of string
            printf("%d\n", LEN);
      
            // Then print the characters of the string
            for (int j=1; j<=LEN; j++)
                printf("%c", 'a' + rand() % MAX);
      
            printf("\n");
        }
      
        // Uncomment the below line to store
        // the test data in a file
        // fclose(stdout);
        return(0);
    }
    
  • 生成随机字符串数组
    // A C++ Program to generate test cases for
    // random strings
    #include
    using namespace std;
      
    // Define the number of runs for the test data
    // generated
    #define RUN 1000
      
    // Define the range of the test data generated
    // Here it is 'a' to 'z'
    #define MAX 25
      
    // Define the range of number of strings in the array
    #define MAXNUM 20
      
    // Define the maximum length of string
    #define MAXLEN 20
      
    int main()
    {
        // Uncomment the below line to store
        // the test data in a file
        // freopen ("Test_Cases_Array_of_Strings.in", "w", stdout);
      
        //For random values every time
        srand(time(NULL));
      
        int NUM; // Number of strings in array
      
        int LEN;    // Length of string
      
        for (int i=1; i<=RUN; i++)
        {
            NUM = 1 + rand() % MAXNUM;
            printf("%d\n", NUM);
      
            for (int k=1; k<=NUM; k++)
            {
                LEN = 1 + rand() % MAXLEN;
      
                // Then print the characters of the string
                for (int j=1; j<=LEN; j++)
                    printf("%c", 'a' + rand() % MAX);
      
                printf(" ");
            }
            printf("\n");
        }
        // Uncomment the below line to store
        // the test data in a file
        // fclose(stdout);
        return(0);
    }
    

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。