Test Case Generation | Set 1 (Random Numbers, Arrays and Matrices)

The test cases are extremely important part of any “Software/Project Testing Process”. Hence this Set will be very important for all the aspiring software developers. The following are the programs to generate test cases.

  • Generating Random Numbers

    C++

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // A C++ Program to generate test cases for
    // random number
    #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 10000000
      
    int main()
    {
        // Uncomment the below line to store
        // the test data in a file
        // freopen("Test_Cases.in", "w", stdout);
      
        // For random values every time
        srand(time(NULL));
      
        for (int i=1; i<=RUN; i++)
            printf("%d\n", rand() % MAX);
      
        // Uncomment the below line to store
        // the test data in a file
        //fclose(stdout);
        return(0);
    }

    chevron_right

    
    

    Java

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // A Java Program to generate test cases 
    // for random number
    import java.io.*;
    import java.util.Random;
      
    class GeneratingRandomNumbers 
    {
      
        // the number of runs 
        // for the test data generated
        static int requiredNumbers = 5;
      
        // miminum range of random numbers
        static int lowerBound = 0;
      
        // maximum range of random numbers
        static int upperBound = 1000;
      
        // Driver Code
        public static void main (String[] args) throws IOException 
        {
            Random random = new Random();
              
            for(int i = 0; i < requiredNumbers; i++)
            {
                int a = random.nextInt(upperBound - lowerBound) + 
                                                    lowerBound;
                System.out.println(a);
            }
        }
    }
      
    // This code is contributed by Madfrost

    chevron_right

    
    

    Python3

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    # A Python3 Program to generate test cases
    # for random number
    import random
      
    # the number of runs
    # for the test data generated
    requiredNumbers = 5;
      
    # miminum range of random numbers
    lowerBound = 0;
      
    # maximum range of random numbers
    upperBound = 1000;
      
    # Driver Code
    if __name__ == '__main__':
      
        for i in range(requiredNumbers):
            a = random.randrange(0, upperBound - 
                        lowerBound) + lowerBound;
            print(a);
      
    # This code is contributed by 29AjayKumar

    chevron_right

    
    

    C#

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // A C# Program to generate test cases 
    // for random number
    using System;
      
    class GeneratingRandomNumbers 
    {
      
        // the number of runs 
        // for the test data generated
        static int requiredNumbers = 5;
      
        // miminum range of random numbers
        static int lowerBound = 0;
      
        // maximum range of random numbers
        static int upperBound = 1000;
      
        // Driver Code
        public static void Main(String[] args)
        {
            Random random = new Random();
              
            for(int i = 0; i < requiredNumbers; i++)
            {
                int a = random.Next(upperBound - lowerBound) + 
                                                    lowerBound;
                Console.WriteLine(a);
            }
        }
    }
      
    // This code is contributed by Rajput-Ji

    chevron_right

    
    

  • Generating Random Arrays

    C++

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // 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 10000000
      
    // 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_Array.in", "w", stdout);
      
        //For random values every time
        srand(time(NULL));
      
        for (int i=1; i<=RUN; i++)
        {
            // Number of array elements
            int 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=1; j<=NUM; j++)
                printf("%d ", rand() % MAX);
      
            printf("\n");
        }
      
        // Uncomment the below line to store
        // the test data in a file
        //fclose(stdout);
        return(0);
    }

    chevron_right

    
    

    Java

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // A Java Program to generate test cases 
    // for array filled with random numbers
    import java.io.*;
    import java.util.Random;
      
    class GeneratingRandomArrays
    {
      
        // the number of runs 
        // for the test data generated
        static int RUN = 5;
      
        // miminum range of random numbers
        static int lowerBound = 0;
      
        // maximum range of random numbers
        static int upperBound = 1000;
      
        // miminum size of reqd array
        static int minSize = 10;
      
        // maximum size of reqd array
        static int maxSize = 20;
      
        // Driver Code
        public static void main (String[] args) throws IOException 
        {
            Random random = new Random();
      
            for(int i = 0; i < RUN; i++)
            {
                int size = random.nextInt(maxSize - minSize) + 
                                                    minSize;
                int[] array = new int[size];
      
                System.out.println(size);
      
                for(int j = 0; j < size; j++)
                {
                    int a = random.nextInt(upperBound - lowerBound) + 
                                                        lowerBound;
                    System.out.print(a + " ");
                }
                System.out.println();
            }
        }
    }
      
    // This code is contributed by Madfrost

    chevron_right

    
    

    Python3

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    # A Python3 Program to generate test cases
    # for array filled with random numbers
    import random
      
    # the number of runs
    # for the test data generated
    RUN = 5;
      
    # miminum range of random numbers
    lowerBound = 0;
      
    # maximum range of random numbers
    upperBound = 1000;
      
    # miminum size of reqd array
    minSize = 10;
      
    # maximum size of reqd array
    maxSize = 20;
      
    # Driver Code
    if __name__ == '__main__':
      
        for i in range(RUN):
            size = random.randrange(0, maxSize - minSize) + minSize;
            array = [0]*size;
      
            print(size);
      
            for j in range(size):
                a = random.randrange(0, upperBound - lowerBound) + lowerBound;
                print(a, end=" ");
      
            print();
      
      
    # This code is contributed by 29AjayKumar

    chevron_right

    
    

    C#

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // A C# Program to generate test cases 
    // for array filled with random numbers
    using System;
      
    class GeneratingRandomArrays
    {
      
        // the number of runs 
        // for the test data generated
        static int RUN = 5;
      
        // miminum range of random numbers
        static int lowerBound = 0;
      
        // maximum range of random numbers
        static int upperBound = 1000;
      
        // miminum size of reqd array
        static int minSize = 10;
      
        // maximum size of reqd array
        static int maxSize = 20;
      
        // Driver Code
        public static void Main(String[] args)
        {
            Random random = new Random();
      
            for(int i = 0; i < RUN; i++)
            {
                int size = random.Next(maxSize - minSize) + 
                            minSize;
                int[] array = new int[size];
      
                Console.WriteLine(size);
      
                for(int j = 0; j < size; j++)
                {
                    int a = random.Next(upperBound - lowerBound) + 
                            lowerBound;
                    Console.Write(a + " ");
                }
                Console.WriteLine();
            }
        }
    }
      
    // This code is contributed by 29AjayKumar

    chevron_right

    
    

  • Generating Random Matrix

    C++

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // A C++ Program to generate test cases for
    // matrix filled with random numbers
    #include<bits/stdc++.h>
    using namespace std;
      
    // Define the number of runs for the test data
    // generated
    #define RUN 3
      
    // Define the range of the test data generated
    #define MAX 100000
      
    // Define the maximum rows in matrix
    #define MAXROW 10
      
    // Define the maximum columns in matrix
    #define MAXCOL 10
      
    int main()
    {
        // Uncomment the below line to store
        // the test data in a file
        // freopen ("Test_Cases_Random_Matrix.in", "w", stdout);
      
        // For random values every time
        srand(time(NULL));
      
        for (int i=1; i<=RUN; i++)
        {
            // Number of rows and columns
            int row = 1 + rand() % MAXROW;
            int col = 1 + rand() % MAXCOL;
      
            // First print the number of rows and columns
            printf("%d %d\n", row, col);
      
            // Then print the matrix
            for (int j=1; j<=row; j++)
            {
                for (int k=1; k<=col; k++)
                    printf("%d ", rand() % MAX);
                printf("\n");
            }
            printf("\n");
        }
      
        // Uncomment the below line to store
        // the test data in a file
        // fclose(stdout);
        return(0);
    }

    chevron_right

    
    

    Java

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // A Java Program to generate test cases for
    // matrix filled with random numbers
    import java.io.*;
    import java.util.Random;
      
    class GeneratingRandomMatrix 
    {
        // the number of runs 
        // for the test data generated
        static int RUN = 5;
      
        // miminum range of random numbers
        static int lowerBound = 0;
      
        // maximum range of random numbers
        static int upperBound = 1000;
      
        // maximum size of colomn
        static int maxColomn = 10;
      
        // miminum size of colomn
        static int minColomn = 1;
      
        // minimum size of row
        static int minRow = 1;
      
        // maximum size of row
        static int maxRow = 10;
      
        // Driver Code
        public static void main (String[] args) throws IOException 
        {
            Random random = new Random();
      
            for(int i = 0; i < RUN; i++)
            {
                int row = random.nextInt(maxRow - minRow) + 
                                                  minRow;
                int colomn = random.nextInt(maxColomn - minColomn) + 
                                                        minColomn;
      
                int[][] matrix = new int[row][colomn];
      
                System.out.println(row + " " + colomn);
      
                for(int j = 0; j < row; j++)
                {
                    for(int k = 0; k < colomn; k++) 
                    {
                        int a = random.nextInt(upperBound - lowerBound) + 
                                                            lowerBound;
                        System.out.print(a + " ");
                    }
                    System.out.println();
                }
                System.out.println();
            }
        }
    }
      
    // This code is contributed by Madfrost

    chevron_right

    
    

    C#

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // A C# Program to generate test cases for
    // matrix filled with random numbers
    using System;
      
    public class GeneratingRandomMatrix 
    {
        // the number of runs 
        // for the test data generated
        static int RUN = 5;
      
        // miminum range of random numbers
        static int lowerBound = 0;
      
        // maximum range of random numbers
        static int upperBound = 1000;
      
        // maximum size of colomn
        static int maxColomn = 10;
      
        // miminum size of colomn
        static int minColomn = 1;
      
        // minimum size of row
        static int minRow = 1;
      
        // maximum size of row
        static int maxRow = 10;
      
        // Driver Code
        public static void Main(String[] args)
        {
            Random random = new Random();
      
            for(int i = 0; i < RUN; i++)
            {
                int row = random.Next(maxRow - minRow) + 
                                                minRow;
                int colomn = random.Next(maxColomn - minColomn) + 
                                                        minColomn;
      
                int[,] matrix = new int[row, colomn];
      
                Console.WriteLine(row + " " + colomn);
      
                for(int j = 0; j < row; j++)
                {
                    for(int k = 0; k < colomn; k++) 
                    {
                        int a = random.Next(upperBound - lowerBound) + 
                                                            lowerBound;
                        Console.Write(a + " ");
                    }
                    Console.WriteLine();
                }
                Console.WriteLine();
            }
        }
    }
      
    // This code is contributed by 29AjayKumar

    chevron_right

    
    

  • Library Functions Used
    1. rand() Function
      -> Generate random numbers from the range 0 to RAND_MAX (32767)
      -> Defined in <stdlib.h>/<cstdlib> header
      -> If we want to assign a random number in the range – m to n [n >= m] to a variable var, then use-

      var = m + ( rand() % ( n – m + 1 ) );
      -> This function is a run-time function. So the values – n, m must be declared before compiling just like we have to declare the size of array before compiling.
      -> Call this function every time you want to generate a random number

    2. time() Function
      -> Return the number of seconds from [00:00:00 UTC, January 1, 1970]
      -> Defined in <time.h> header
    3. srand(seed)
      -> Generates random number according to the seed passed to it.
      -> If this function is not used and we use rand() function then every time we run the program the same random numbers gets generated.
      -> To overcome the above limitation, we pass time(NULL) as a seed. Hence srand(time(NULL)) is used to generate random values every time the program is made to run.
      -> Always use this at the beginning of the program, i.e- just after int main() {
      -> No need to call this function every time you generate a random number
      -> Defined in <stdlib.h>/<cstdlib> header
    4. freopen(“output.txt”, “w”, stdout)
      -> Writes (that’s why we passed “w” as the second argument) all the data to output.txt file (The file must be in the same file as the program is in).
      -> Used to redirect stdout to a file.
      -> If the output.txt file is not created before then it gets created in the same file as the program is in.
    5. fclose(stdout)
      -> Closes the standard output stream file to avoid leaks.
      -> Always use this at the end of the program, i.e- just before return(0) in the int main() function.

    This article is contributed by Rachit Belwariar. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

    Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above

    competitive-programming-img




    My Personal Notes arrow_drop_up

    Recommended Posts:


    Article Tags :

    1


    Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.