Test cases

 

A test case consists of an input to the code and an expected output to verify a program’s actual output against its expected output. It helps in validating your code by evaluating it automatically.

In programming questions, input data is read from the standard input stream (STDIN) and the results are printed to the standard output stream (STDOUT).

stdin_stdout.jpg

For example, in an online coding test in C, the following code is used to read an integer from STDIN and print the result to STDOUT.

c_example_stdin_stdout.jpg

The online judge compares your output to the expected output, therefore, if you print an output that is different from the expected output, then an error will be displayed on the screen.

Types of test cases

Your code is run against 2 types of test cases: Sample and internal

Sample test cases

  • Usually available after the Constraints section
  • Known as Sample input and Sample output

sample_input_output.jpg

  • Used to check the logic and validate your code instantly because you can see both the input and expected output values when your code is run against these test cases
  • Your code is run against these test cases when you click Compile & Test

Note: Your code should be able to convert the sample input into the sample output. However, this is not enough to pass the challenge, because the code will be run against multiple, internal test cases too. Therefore, it is important that your code solves the problem statement.

Internal test cases

  • Not visible on the platform
    • Contains multiple test cases that cover a number of scenarios in order to test your code in a holistic manner

      Example

internal_test_cases.jpg

  • Your code is run against these test cases when you click Submit

Note: You cannot edit or change your code after you have submitted it.

By default, when you compile your code, it is run against the sample test cases. However, if you want to test your code with more test cases, you can manually define these by using the custom input section. For more information, see Testing your code.