📜  测试用例生成|集合1(随机数,数组和矩阵)

📅  最后修改于: 2021-06-25 18:28:59             🧑  作者: Mango

测试用例是任何“软件/项目测试过程”中极其重要的部分。因此,对于所有有抱负的软件开发人员来说,此套件将非常重要。以下是生成测试用例的程序。

  • 产生随机数
    C++
    // A C++ Program to generate test cases for
    // random number
    #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 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);
    }


    Java
    // 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


    Python3
    # 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


    C#
    // 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


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


    Java
    // 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


    Python3
    # 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


    C#
    // 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


    C++
    // A C++ Program to generate test cases for
    // matrix filled with random numbers
    #include
    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);
    }


    Java
    // 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


    C#
    // 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


  • 生成随机数组

    C++

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

    Java

    // 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
    

    Python3

    # 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
    

    C#

    // 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
    
  • 生成随机矩阵

    C++

    // A C++ Program to generate test cases for
    // matrix filled with random numbers
    #include
    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);
    }
    

    Java

    // 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
    

    C#

    // 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
    
    使用的库函数
    1. rand()函数
      ->生成从0到RAND_MAX范围内的随机数(32767)
      ->在 / 标头中定义
      ->如果我们要为变量var分配一个范围为– m到n [n> = m]的随机数,请使用-

      var = m +(rand()%(n – m + 1));
      ->此函数是运行时函数。因此,必须在编译之前声明值– n,m,就像我们必须在编译之前声明数组的大小一样。
      ->每次要生成随机数时都调用此函数

    2. time()函数
      ->从[00:00:00 UTC,1970年1月1日]返回秒数
      ->在标头中定义
    3. 雪兰特(种子)
      ->根据传递给它的种子生成随机数。
      ->如果不使用此函数,而我们使用rand()函数,则每次运行程序时,都会生成相同的随机数。
      ->为了克服以上限制,我们将time(NULL)作为种子。因此,每次使程序运行时,srand(time(NULL))都会用于生成随机值。
      ->始终在程序开始时使用它,即-在int main(){之后
      ->每次生成随机数时都无需调用此函数
      ->在 / 标头中定义
    4. freopen(“ output.txt”,“ w”,stdout)
      ->将所有数据写入(这就是我们将“ w”作为第二个参数传递的原因)到output.txt文件(该文件必须与程序所在的文件位于同一文件中)。
      ->用于将标准输出重定向到文件。
      ->如果未在之前创建output.txt文件,则将在与程序所在的文件相同的文件中创建该文件。
    5. fclose(标准输出)
      ->关闭标准输出流文件以避免泄漏。
      ->始终在程序末尾使用它,即-在int main()函数return(0)之前。

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