logo

Test Case Generation | Set 5 (generera slumpmässiga sorterade matriser och palindromer)

Genererar slumpmässigt Sorterade matriser Vi lagrar de slumpmässiga arrayelementen i en matris och sorterar sedan den och skriver ut den. 

CPP
// 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 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("%dn" 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)) 

Tidskomplexitet : O (n log n)



Rymdkomplexitet: PÅ)

  Genererar slumpmässigt Palindramar

  • Testfallsgenereringsplanen genererar udda såväl som till och med längd palindromer.
  • Test Case Generation Plan använder en av de mest underklassade datastrukturen- Och
  • Eftersom en palindrom läses samma sak från vänster såväl som höger så lägger vi helt enkelt samma slumpmässiga karaktärer på båda vänster sida (klar med push_front () ) och höger sida (klar med push_back ( )))
CPP
// A C++ Program to generate test cases for // random strings #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 // 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('%dn' 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();  } } } 
JavaScript
// JavaScript Program to generate test cases for // random strings // Define the number of runs for the test data // generated const RUN = 5; // Define the range of the test data generated // Here it is 'a' to 'z' const MAX = 25; // Define the maximum length of string const MAXLEN = 50; function main() {  // A container for storing the palindromes  let container = [];  for (let i=1; i<=RUN; i++) {  let LEN = 1 + Math.floor(Math.random() * MAXLEN);  // First print the length of string  console.log(LEN);  // If it is an odd-length palindrome  if (LEN % 2)  container.push(String.fromCharCode('a'.charCodeAt(0) + Math.floor(Math.random() * MAX)));  // Then print the characters of the palindromic  // string  for (let j=1; j<=LEN/2; j++) {  let ch = String.fromCharCode('a'.charCodeAt(0) + Math.floor(Math.random() * MAX));  container.push(ch);  container.unshift(ch);  }  // Print the characters of the palindromic string  console.log(container.join(''));  // Clear the container for the next run  container = [];  } } // Call the main function main(); 
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() 

Tidskomplexitet:  PÅ)



Rymdkomplexitet: PÅ)

Om du gillar GeeksForgeeks och vill bidra kan du också skriva en artikel med write.geeksforgeeks.org eller maila din artikel till [email protected]. Se din artikel som visas på GeeksForgeeks huvudsida och hjälpa andra nördar. Referenser: - http://spojtoolkit.com/testcasegenerator/ Skriv kommentarer om du hittar något felaktigt eller om du vill dela mer information om ämnet som diskuteras ovan.