# 如何知道代码失败的测试用例？

1. 手动检查极端情况
2. 压力测试

### 检查角落情况:

int mn = arr [0]
for i：1-> arr_size：
if(arr [i] <mn)
mn = arr [i]
print mn

### 压力测试:

In压力测试的想法是生成随机测试用例, 并检查蛮力解和最优解的输出。虽然蛮力解决方案是慢仍然是正确而最佳的解决方案是快点但这在某些测试案例中是错误的。这使我们可以检查某些测试用例的最佳解决方案输出是否正确, 手动地检查, 它可以简单地检查是否输出天真的解决方案同意最佳解决方案输出。检查是自动完成的, 并且数千个代码的正确性取决于复杂的天真的解决方案数秒之内的测试用例, 因此可能性查找代码失败的测试用例的方法高.

1. 生成随机输入(点击这里了解如何)
2. 存储蛮力输出和最佳解
3. 如果两个输出(等于)大于打印正确
4. 否则打印输入和打破循环

## C ++

``````//C++ program to illustrate the stress
//testing for the above problem
#include <bits/stdc++.h>
using namespace std;

//Function that implements the Naive
//Solution for the given problem
int maxProductNaive( int a[], int n)
{
int ans;
for ( int i = 0; i <n; i++) {
for ( int j = i + 1; j <n; j++) {
if (i == 0 && j == 1)
ans = a[i] * a[j];
else if (a[i] * a[j]> ans)
ans = a[i] * a[j];
}
}
return ans;
}

//Function that implements the Optimal
//Solution for the given problem
int maxProductOptimal( int a[], int n)
{
//Sort the given array
sort(a, a + n);

//Find the maximum product
int ans = a[n - 1] * a[n - 2];

//Return the product
return ans;
}

//Function that performs the
//Stress Testing
void test()
{
//Seeding random number generator
//to get uniques values
srand ( time (0));

while (1) {

//Generate values for n
//from 2 to 10
int n = rand () % 10 + 2;
int a[n];

//Iterate over array a[]
for ( int i = 0; i <n; i++) {

//Subtracting -5 will
//generate -ve integers
a[i] = rand () % 10 - 5;
}

//Solution using Naive Approach
int ans_brute
= maxProductNaive(a, n);

//Solution using Optimal Approach
int ans_optimal
= maxProductOptimal(a, n);

//If ans is correct
if (ans_brute == ans_optimal)
cout <<"Correct\n" ;

//Else print the WA Test Case
else {

cout <<n <<endl;

//Print the array a[]
for ( int i = 0; i <n; i++)
cout <<a[i] <<" " ;

cout <<"\nCorrect Output: "
<<ans_brute <<endl;

cout <<"Wrong Output: "
<<ans_optimal <<endl;
break ;
}
}
}

//Driver Code
int main()
{
//Function Call for Stress Testing
test();
return 0;
}``````

``````Wrong Answer
4
-4 -3 -3 1
Correct Output: 12
Wrong Output: -3``````

• 回顶