testlib.h : A Comprehensive Guide

Srijanjain
Nybles
Published in
4 min readDec 7, 2023

Introduction

Testing is an essential part of software development, especially in competitive programming, where code correctness can make the difference between winning and losing. If you are developing a programming contest problem or a competitive programmer, you might have come across a powerful library called ‘testlib.h.’ This library simplifies the process of writing and running test cases for your C++ code, making it an invaluable tool for ensuring your solutions are well and efficient.

What is testlib.h?

Testlib.h is a C++ library specifically designed to streamline the creation and validation of test cases for competitive programming. Developed by Mikhail Mirzayanov, it has gained popularity among competitive programmers for its ability to simplify testing tasks and provide a rich set of functions for generating test cases, validating output, and comparing results.

Testlib.h is generally used for :-

  • writing generators ( programs to create test cases )
  • validators (verifies that it satisfies the constraints )
  • interactors ( for interactive problems )
  • checkers (check participant’s answer against jury’s answer)

Why use “testlib.h” ?

When generating test cases with random values and certain restrictions? One might argue that the built-in rand() function can achieve the same results. However, there’s a crucial distinction.

The rand() function provides random values on each run, making it inconsistent for collaborative work. In contrast, “testlib.h” ensures that random values are generated in a consistent order across

different systems. This consistency is invaluable for generating and validating test cases as a team.

In summary, “testlib.h” offers a level of reliability and consistency that the standard rand() function cannot provide, making it a valuable tool for collaborative test case generation.

How to Set Up testlib.h on Your System ?

  • Download the testlib.h header file from the official GitHub repository
  • Place file in directory where your C++ compiler is present or project directory
  • Include testlib.h in your code by adding #include “testlib.h” in the beginning of your code

How to generate Test Cases through testlib.h ?

In competitive programming, the need to test your code with various inputs and edge cases frequently arises. testlib.h simplifies this process by providing a variety of functions for generating test cases.

To initiate a testlib generator, the initial line of your generator should take the form registerGen(argc, argv, 1);

#include "testlib.h"
using namespace std;
int main(int argc, char *argv[]) {
registerGen(argc, argv, 1);
//your code goes here
}

To generate test cases, testlib.h offers a wide range of functions, as follows:

  • rnd.next (n) — generate equiprobable integer between 0 to n-1.
  • rnd.next (n1 , n2) — generate equiprobable integer between n1 to n2 inclusive.
  • rnd.next ( n.0 ) — generate equiprobable real number in the half interval [0,n) .
  • rnd.next(“one|two|three”) — generate equiprobable string out of given strings .
  • rnd.next(“[c1-c2] {n} [c3-c4] {m}”) — generate string of length n+m , str = str1+str2

str1 -> string having length n each char is equiprobable char having ASCII value between c1 to c2 .

str2 -> string having length m each char is equiprobable char having ASCII value between c3 to c4 .

* For having length 1 [c1-c2] will work instead of [c1-c2]{1}

  • rnd.wnext
  1. using rnd.wnext (a,b) — function serves as an efficient way to generate repeated values Specifically, it calculates the maximum value of rnd.next(a) for b times.

Example — rnd.wnext(3,2) = max(rnd.next(3),rnd.next(3) ;

2. Introducing bias with rnd.next(a,b,c) -

Another interesting feature of rnd.wnext is the ability to introduce a value bias, denoted as “c.” This value bias influences the probability of generating numbers, making the distribution of values closer to either the maximum or minimum value.

positive value-bias — larger the positive value of c more

likely the value will be closer to upper limit

negative value-bias — larger the negative value of c

more likely the value will be closer to upper limit

no bias — when c is equal to zero

** Here are some of the functions provided by the “testlib.h” library for generating test cases. Let’s focus on generating test cases for data structures like arrays and strings

String

To generate test cases for a specific question where we require strings consisting of the characters ‘0’ and ‘1’ (binary strings) and have constraints like the sum of the string lengths should be 1e5 or 2e5, we will calculate the number of test cases in a particular test set and generate them accordingly.

For example, the code for this task might look like the following:

#include "testlib.h"
using namespace std;
int main(int argc, char *argv[]) {
registerGen(argc, argv, 1);
//generate strings having length 100
for(int i=0;i<1000;i++){
println("[0–1]{100}");
}
}

Array

Imagine that you need to generate an array with random lengths, where the sum of the array’s lengths equals 1e5, and the elements within the array are less than or equal to 1e9. To achieve this, you can utilise an additional function provided by “testlib.h.”

  • vector<int> n_list = rnd.partition(a,sum,mi) — return

equiprobable vector having length a , which has sum of all its value equal to sum and every element is greater than equal to mi

code for above thing is as follows

#include "testlib.h"
#include <vector>
using namespace std;
int main(int argc, char** argv) {
registerGen(argc, argv, 1);
int test_count = opt<int>("test-count");
int sum_n = opt<int>("sum-n");
int min_n = opt<int>("min-n", 1);
int min_value = opt<int>("min-value", 1);
int max_value = opt<int>("max-value", 1000 * 1000 * 1000);
int value_bias = opt<int>("value-bias", 0);
vector<int> n_list = rnd.partition(test_count, sum_n, min_n);
println(test_count);
for (int test_case = 0; test_case < test_count; ++test_case) {
int n = n_list[test_case];
vector<int> arr(n);
for (int i = 0; i < n; ++i) {
arr[i] = rnd.wnext(min_value, max_value, value_bias);
}
println(n);
println(arr);
}
}

Thanks for Reading
This is my first blog, do give your views in the comment below.

About me

I am Srijan Jain, 2nd-year B.Tech IT student at IIIT Allahabad, Member at Competitive Coding Wing, Geekhaven, IIIT Allahabad. Connect on Linkedin.

--

--