Test Case Generation | Set 2 ( Random Characters, Strings and Arrays of Random Strings)

Set 1 (Random Numbers, Arrays and Matrices)

  • Generating Random Characters
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // A C++ Program to generate test cases for
    // random characters
    #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
      
    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);
    }

    chevron_right

    
    

  • Generating Random Strings
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // 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 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);
    }

    chevron_right

    
    

  • Generating Array of Random Strings
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // 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 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);
    }

    chevron_right

    
    

This article is contributed by Rachit Belweriar. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or 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.



My Personal Notes arrow_drop_up


Article Tags :

Be the First to upvote.


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