Los casos de prueba son una parte extremadamente importante de cualquier “Proceso de prueba de software/proyecto”. Por lo tanto, este conjunto será muy importante para todos los aspirantes a desarrolladores de software. Los siguientes son los programas para generar casos de prueba.
- Generación de números aleatorios
C++
// 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); }
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; // minimum 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; # minimum 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; // minimum 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
Javascript
<script> let requiredNumbers = 5; let lowerBound = 0; let upperBound = 1000; for(let i = 0; i < requiredNumbers; i++) { let a = Math.floor(Math.random() * (upperBound - lowerBound)) + lowerBound; document.write(a+"<br>"); } // This code is contributed by rag2127 </script>
- Generación de arrays aleatorias
C++
// 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); }
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; // minimum range of random numbers static int lowerBound = 0; // maximum range of random numbers static int upperBound = 1000; // minimum 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; # minimum range of random numbers lowerBound = 0; # maximum range of random numbers upperBound = 1000; # minimum 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; // minimum range of random numbers static int lowerBound = 0; // maximum range of random numbers static int upperBound = 1000; // minimum 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
Javascript
<script> // A JavaScript Program to generate test cases // for array filled with random numbers // the number of runs // for the test data generated let RUN = 5; // minimum range of random numbers let lowerBound = 0; // maximum range of random numbers let upperBound = 1000; // minimum size of reqd array let minSize = 10; // maximum size of reqd array let maxSize = 20; for(let i = 0; i < RUN; i++) { let size = Math.floor(Math.random() * (maxSize - minSize)) + minSize; let array = new Array(size); document.write(size+"<br>"); for(let j = 0; j < size; j++) { let a = Math.floor(Math.random() * (upperBound - lowerBound)) + lowerBound; document.write(a + " "); } document.write("<br>"); } // This code is contributed by avanitrachhadiya2155 </script>
- Generación de array aleatoria
C++
// 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); }
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; // minimum range of random numbers static int lowerBound = 0; // maximum range of random numbers static int upperBound = 1000; // maximum size of column static int maxColumn = 10; // minimum size of column static int minColumn = 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 column = random.nextInt(maxColumn - minColumn) + minColumn; int[][] matrix = new int[row][column]; System.out.println(row + " " + column); for(int j = 0; j < row; j++) { for(int k = 0; k < column; k++) { int a = random.nextInt(upperBound - lowerBound) + lowerBound; System.out.print(a + " "); } System.out.println(); } System.out.println(); } } } // This code is contributed by Madfrost
Python3
# A Python3 Program to generate test cases # for matrix filled with random numbers import random # the number of runs # for the test data generated RUN = 5; # minimum range of random numbers lowerBound = 0; # maximum range of random numbers upperBound = 1000; # maximum size of column maxColumn = 10; # minimum size of column minColumn = 1; # minimum size of row minRow = 1; # maximum size of row maxRow = 10; # Driver Code if __name__ == '__main__': for i in range(RUN): row = random.randrange(0, maxRow - minRow) + minRow column = random.randrange(0, maxColumn - minColumn) + minColumn matrix = [[0 for i in range(column)] for j in range(row)] print(row, column) for j in range(row): for k in range(column): a = random.randrange(0, upperBound - lowerBound) + lowerBound print(a ,end = " ") print() print() # This code is contributed by Shubham Singh
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; // minimum range of random numbers static int lowerBound = 0; // maximum range of random numbers static int upperBound = 1000; // maximum size of column static int maxColumn = 10; // minimum size of column static int minColumn = 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 column = random.Next(maxColumn - minColumn) + minColumn; int[,] matrix = new int[row, column]; Console.WriteLine(row + " " + column); for(int j = 0; j < row; j++) { for(int k = 0; k < column; k++) { int a = random.Next(upperBound - lowerBound) + lowerBound; Console.Write(a + " "); } Console.WriteLine(); } Console.WriteLine(); } } } // This code is contributed by 29AjayKumar
- Función rand() –
-> Generar números aleatorios del rango 0 a RAND_MAX (32767)
-> Definido en el encabezado <stdlib.h>/<cstdlib>
-> Si queremos asignar un número aleatorio en el rango – m a n [n >= m] a una variable var, luego use-
var = m + ( rand() % ( n – m + 1 ) );
-> Esta función es una función de tiempo de ejecución. Entonces, los valores: n, m deben declararse antes de compilar, al igual que tenemos que declarar el tamaño de la array antes de compilar.
-> Llame a esta función cada vez que desee generar un número aleatorio - función time()
-> Devuelve el número de segundos desde [00:00:00 UTC, 1 de enero de 1970]
-> Definido en el encabezado <time.h> - srand (semilla)
-> Genera un número aleatorio de acuerdo con la semilla que se le pasó.
-> Si esta función no se usa y usamos la función rand(), entonces cada vez que ejecutamos el programa se generan los mismos números aleatorios.
-> Para superar la limitación anterior, pasamos el tiempo (NULL) como semilla. Por lo tanto, srand(time(NULL)) se utiliza para generar valores aleatorios cada vez que se ejecuta el programa.
-> Utilice siempre esto al comienzo del programa, es decir, justo después de int main() {
-> No es necesario llamar a esta función cada vez que genera un número aleatorio
-> Definido en el encabezado <stdlib.h>/<cstdlib> - freopen(“output.txt”, “w”, stdout)
-> Escribe (es por eso que pasamos “w” como segundo argumento) todos los datos al archivo output.txt (El archivo debe estar en el mismo archivo que el programa es en).
-> Se utiliza para redirigir stdout a un archivo.
-> Si el archivo output.txt no se crea antes, se crea en el mismo archivo en el que se encuentra el programa. - fclose(stdout)
-> Cierra el archivo de flujo de salida estándar para evitar fugas.
-> Utilice siempre esto al final del programa, es decir, justo antes de return(0) en la función int main().
Este artículo es una contribución de Rachit Belwariar . Si le gusta GeeksforGeeks y le gustaría contribuir, también puede escribir un artículo y enviarlo por correo a review-team@geeksforgeeks.org. Vea su artículo que aparece en la página principal de GeeksforGeeks y ayude a otros Geeks.
Escriba comentarios si encuentra algo incorrecto o si desea compartir más información sobre el tema tratado anteriormente.
Publicación traducida automáticamente
Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA