Test Case Generation | Set 5 (Generating random Sorted Arrays and Palindromes)

28 Jun, 2021
Generating Random Sorted ArraysWe store the random array elements in an array and then sort it and print it.
// A C++ Program to generate test cases for// array filled with random numbers#include<bits/stdc++.h>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<NUM; j++)            arr[j] = rand() % MAX;          // Sort the generated random array        sort (arr, arr + NUM);          // Print the sorted random array        for (int j=0; j<NUM; j++)            printf("%d ", arr[j]);          printf("\n");    }      // Uncomment the below line to store    // the test data in a file    // fclose(stdout);    return(0);}
 
Generating Random Palindromes
The test case generation plan generates odd as well as even length palindromes.
The test case generation plan uses one of the most under-rated data structure- Deque
Since a palindrome is read the same from left as well as right so we simply put the same random characters on both the left side (done using push_front()) and the right side (done using push_back())
// A C++ Program to generate test cases for// random strings#include<bits/stdc++.h>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<char> container;    deque<char>::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);}
This article is contributed by Rachit Belwariar. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
References : –http://spojtoolkit.com/TestCaseGenerator/
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Competitive Programming
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.