测试用例生成|集2(随机字符、字符串和随机字符串数组)

集合1(随机数、数组和矩阵)

null
  • 生成随机字符

CPP

// 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", 'a' + rand () % MAX);
// Uncomment the below line to store
// the test data in a file
// fclose(stdout);
return (0);
}


Javascript

<script>
let requiredNumbers = 5;
let lowerBound = 0;
let upperBound = 25;
for (let i = 0; i < requiredNumbers; i++)
{
let a = String.fromCharCode(97 + Math.floor(Math.random() *
(upperBound - lowerBound)) + lowerBound);
document.write(a+ "<br>" );
}
// This code is contributed by Shubham Singh
</script>


  • 生成随机字符串

CPP

// 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", LEN);
// Then print the characters of the string
for ( int j=1; j<=LEN; j++)
printf ("%c", 'a' + rand () % MAX);
printf ("");
}
// Uncomment the below line to store
// the test data in a file
// fclose(stdout);
return (0);
}


  • 生成随机字符串数组

CPP

// 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", 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 ("");
}
// Uncomment the below line to store
// the test data in a file
// fclose(stdout);
return (0);
}


本文由 拉希特·贝尔维里亚 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞13 分享