Open In App

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

Improve
Improve
Like Article
Like
Save
Share
Report

Generating Random Sorted Arrays We store the random array elements in an array and then sort it and print it. 

CPP
// 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);
}
Java
import java.util.Arrays;
import java.util.Random;

public class TestCasesGenerator {
    // Define the number of runs for the test data generated
    static final int RUN = 5;

    // Define the range of the test data generated
    static final int MAX = 100000;

    // Define the maximum number of array elements
    static final int MAXNUM = 100;

    public static void main(String[] args) {
        // Uncomment the below line to store
        // the test data in a file
        // System.setOut(new PrintStream(new
        // FileOutputStream("Test_Cases_Random_Sorted_Array.txt")));

        // For random values every time
        Random rand = new Random();

        int NUM; // Number of array elements

        for (int i = 1; i <= RUN; i++) {
            int[] arr = new int[MAXNUM];

            NUM = 1 + rand.nextInt(MAXNUM);

            // First print the number of array elements
            System.out.println(NUM);

            // Then print the array elements separated by space
            for (int j = 0; j < NUM; j++)
                arr[j] = rand.nextInt(MAX);

            // Sort the generated random array
            Arrays.sort(arr, 0, NUM);

            // Print the sorted random array
            for (int j = 0; j < NUM; j++)
                System.out.print(arr[j] + " ");

            System.out.println();
        }

        // Uncomment the below line to store
        // the test data in a file
        // System.out.close();
    }
}
C#
// A C# Program to generate test cases for
// array filled with random numbers
using System;

namespace TestCasesGenerator {
class Program {
    // Define the number of runs for the test data
    // generated
    const int RUN = 5;
    // Define the range of the test data generated
    const int MAX = 100000;

    // Define the maximum number of array elements
    const int MAXNUM = 100;

    static void Main(string[] args)
    {
        // Uncomment the below line to store
        // the test data in a file
        // Console.SetOut(new
        // System.IO.StreamWriter("Test_Cases_Random_Sorted_Array.txt"));

        // For random values every time
        Random rand = new Random();

        int NUM; // Number of array elements

        for (int i = 1; i <= RUN; i++) {
            int[] arr = new int[MAXNUM];

            NUM = 1 + rand.Next() % MAXNUM;

            // First print the number of array elements
            Console.WriteLine(NUM);

            // Then print the array elements separated by
            // space
            for (int j = 0; j < NUM; j++)
                arr[j] = rand.Next() % MAX;

            // Sort the generated random array
            Array.Sort(arr, 0, NUM);

            // Print the sorted random array
            for (int j = 0; j < NUM; j++)
                Console.Write(arr[j] + " ");

            Console.WriteLine();
        }

        // Uncomment the below line to store
        // the test data in a file
        // Console.Out.Close();
    }
}
}
Javascript
// Define the number of runs for the test data generated
const RUN = 5;

// Define the range of the test data generated
const MAX = 100000;

// Define the maximum number of array elements
const MAXNUM = 100;

// Function to generate random integer within a range
function getRandomInt(min, max) {
  return Math.floor(Math.random() * (max - min + 1)) + min;
}

for (let i = 1; i <= RUN; i++) {
  const NUM = 1 + getRandomInt(0, MAXNUM);
  const arr = [];

  // First print the number of array elements
  console.log(NUM);

  // Then print the array elements separated by space
  for (let j = 0; j < NUM; j++) {
    arr.push(getRandomInt(0, MAX));
  }

  // Sort the generated random array
  arr.sort((a, b) => a - b);

  // Print the sorted random array
  console.log(arr.join(' '));
}
Python3
import random

# Define the number of runs for the test data generated
RUN = 5

# Define the range of the test data generated
MAX = 100000

# Define the maximum number of array elements
MAXNUM = 100

# For random values every time
rand = random.Random()

for i in range(1, RUN+1):
    arr = []

    NUM = 1 + rand.randint(0, MAXNUM-1)

    # First print the number of array elements
    print(NUM)

    # Then print the array elements separated by space
    for j in range(NUM):
        arr.append(rand.randint(0, MAX-1))

    # Sort the generated random array
    arr.sort()

    # Print the sorted random array
    print(" ".join(str(x) for x in arr))

Time complexity :  O(N log N)

Space complexity : O(N)

  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())
CPP
// 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);
}
Java
import java.util.Random;

public class PalindromeGenerator {

    // Define the number of runs for the test data generated
    private static final int RUN = 5;

    // Define the range of the test data generated
    // Here it is 'a' to 'z'
    private static final int MAX = 25;

    // Define the maximum length of string
    private static final int MAXLEN = 50;

    // A function to generate palindromic strings
    private static void generatePalindrome() {
        // Length of string
        int LEN = 1 + new Random().nextInt(MAXLEN);

        // Print the length of the string
        System.out.println(LEN);

        // If it is an odd-length palindrome
        if (LEN % 2 == 1) {
            System.out.print((char) ('a' + new Random().nextInt(MAX)));
        }

        // Then print the characters of the palindromic string
        for (int j = 0; j < LEN / 2; j++) {
            char ch = (char) ('a' + new Random().nextInt(MAX));
            System.out.print(ch);
            System.out.print(ch);
        }

        System.out.println();
    }

    public static void main(String[] args) {
        // Set the seed for reproducibility (optional in Java)
        // No direct equivalent to Python's random.seed() in Java,
        // but you can use Random instance with a seed

        // Generate palindromic strings
        for (int i = 0; i < RUN; i++) {
            generatePalindrome();
        }
    }
}
C#
// A C# program to generate test cases for
// random strings
using System;
using System.Collections.Generic;

namespace Test_Cases_Random_Strings {
class Program {
    // Define the number of runs for the test data
    // generated
    const int RUN = 5;
    // Define the range of the test data generated
    // Here it is 'a' to 'z'
    const int MAX = 25;

    // Define the maximum length of string
    const int MAXLEN = 50;

    static void Main(string[] args)
    {
        // Uncomment the below line to store
        // the test data in a file
        // Console.SetOut(new
        // System.IO.StreamWriter("Test_Cases_Palindrome.in"));

        // For random values every time
        Random rand = new Random();

        // A container for storing the palindromes
        LinkedList<char> container = new LinkedList<char>();

        int LEN; // Length of string

        for (int i = 1; i <= RUN; i++) {
            LEN = 1 + rand.Next(MAXLEN);

            // First print the length of string
            Console.WriteLine(LEN);

            // If it is an odd-length palindrome
            if (LEN % 2 == 1)
                container.AddLast(
                    (char)('a' + rand.Next(MAX)));

            // Then print the characters of the palindromic
            // string
            for (int j = 1; j <= LEN / 2; j++) {
                char ch = (char)('a' + rand.Next(MAX));
                container.AddLast(ch);
                container.AddFirst(ch);
            }

            foreach(char ch in container) Console.Write(ch);

            container.Clear();
            Console.WriteLine();
        }

        // Uncomment the below line to store
        // the test data in a file
        // Console.Out.Close();
    }
}
}
Python3
import random

# Define the number of runs for the test data generated
RUN = 5

# Define the range of the test data generated
# Here it is 'a' to 'z'
MAX = 25

# Define the maximum length of string
MAXLEN = 50

# A function to generate palindromic strings
def generate_palindrome():
    # Length of string
    LEN = 1 + random.randint(0, MAXLEN)

    # Print the length of the string
    print(LEN)

    # If it is an odd-length palindrome
    if LEN % 2:
        print(chr(ord('a') + random.randint(0, MAX)), end='')

    # Then print the characters of the palindromic string
    for _ in range(LEN // 2):
        ch = chr(ord('a') + random.randint(0, MAX))
        print(ch, end='')
        print(ch, end='')

    print()

# Set the seed for reproducibility
random.seed()

# Generate palindromic strings
for i in range(1, RUN + 1):
    generate_palindrome()

Time complexity :  O(N)

Space complexity : O(N)

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.



Last Updated : 18 Mar, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads