How to create your own testcase generator?

There was this one question which always haunted me for a long time... How do these people manage to test our code vs different test-cases?!? Like so many possibilities with a wide range! With all sorts of characters, integers, etc. Well, I recently learnt a way to do it and hence I am here to share it with y'all!

So, basically we use a function to generate random integers. We can build a random function ourselves by simply writing a random expression and giving a start value (known as seed value) and the function repeats itself with the output as new input. But there are indeed inbuilt functions available for the same! I am a c++ guy, the function here is rand() which returns a random integer!

cout << rand(); //this line prints a random number between 0 and 32767

cout << (rand() % (max - min + 1)) + min;//this prints a random number between max and min

We also need to pass a seed value in such a way that program generates random numbers on every run. For this we use srand(time(0)); //this gives a seed value based on time at which program is executed.

That's all you need! Thanks for reading! :D

Here is an example code for Avengers Unite problem: (https://siesgstarena.herokuapp.com/contest/AUG18/problem/AUG18A)

#include <bits/stdc++.h>
using namespace std;

string cases[5] =
{
    "light",
    "magic",
    "strength",
    "stealth",
    "earth"
};

int main()
{
    srand(time(0));

    int max = 50, min=1;//as our testcase is 1<=t<=50

    int t =(rand() % (max - min + 1)) + min;
    cout << t << "\n";
    int m;
    while(t--)
    {
        m =(rand() % (4 - 0 + 1)) + 0; //since our array index lies between 0 and 4
        cout << cases[m] << "\n";
    }
}



  4    0  
Created On: November 14, 2018 1:20 AM


Comments
  • Nov 14, 2018 1:24 AM

    For outputting characters you can use their ascii values. For example, 'A' to 'Z' in ascii is 65-90. Hence you could do something like: static_cast(rand() % (90 - 65 + 1)) + 65;

      2     0
  • Nov 14, 2018 9:01 PM

    Liked it! Highly appreciated :)
    Adding more information to this, here's a link for what most of Codeforces Round uses and is used internationally. Check below:
    Click here to open GitHub Repository of testlib
    We at arena, use PHP language to generate our test cases giving us more freedom, variations, power and speed as of now. Over a period of time, we will be shifting to testlib for creating tests :)

      3     1

Supported by our Coding Partner