📜  测试用例生成|第5组(生成随机排序的数组和回文式)

📅  最后修改于: 2021-06-26 22:28:07             🧑  作者: Mango

生成随机排序的数组
我们将随机数组元素存储在数组中,然后对其进行排序和打印。

// A C++ Program to generate test cases for
// array filled with random numbers
#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
#define MAX 100000
  
// Define the maximum number of array elements
#define MAXNUM 100
  
int main()
{
    // Uncomment the below line to store
    // the test data in a file
    // freopen("Test_Cases_Random_Sorted_Array.in",
    //          "w", stdout);
  
    // For random values every time
    srand(time(NULL));
  
    int NUM;    // Number of array elements
  
    for (int i=1; i<=RUN; i++)
    {
        int arr[MAXNUM];
  
        NUM = 1 + rand() % MAXNUM;
  
        // First print the number of array elements
        printf("%d\n", NUM);
  
        // Then print the array elements separated by
        // space
        for (int j=0; j

产生随机回文

  • 测试用例生成计划生成奇数和偶数回文。
  • 测试用例生成计划使用最被低估的数据结构之一-Deque
  • 由于回文从左右读取相同,因此我们只需在左侧(使用push_front()完成)和右侧(使用push_back( )完成)上都放置相同的随机字符
// 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 5
  
// 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 50
  
int main()
{
    // Uncomment the below line to store
    // the test data in a file
    // freopen("Test_Cases_Palindrome.in", "w",
    //                                  stdout);
  
    // For random values every time
    srand(time(NULL));
  
    // A container for storing the palindromes
    deque container;
    deque::iterator it;
  
    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);
  
        // If it is an odd-length palindrome
        if (LEN % 2)
            container.push_back('a' + rand() % MAX);
  
        // Then print the characters of the palindromic
        // string
        for (int j=1; j<=LEN/2; j++)
        {
            char ch = 'a' + rand() % MAX;
            container.push_back(ch);
            container.push_front(ch);
        }
  
        for (it=container.begin(); it!=container.end(); ++it)
            printf("%c",*it);
  
        container.clear();
        printf("\n");
    }
  
    // Uncomment the below line to store
    // the test data in a file
    // fclose(stdout);
    return(0);
}

参考 : –
http://spojtoolkit.com/TestCaseGenerator/

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