testlib icon indicating copy to clipboard operation
testlib copied to clipboard

Enhancing function println's range printing support

Open Lumine2024 opened this issue 1 month ago • 2 comments

Now the testlib's println function supports printing range such as println(arr.begin(), arr.end()), but it is not complete. For example, if we write code like:

println("?", arr.begin() + 2, arr.end());

It will result in a compile-time error, as we passed a std::vector<int>::iterator to __testlib_print_one, then std::cout::operator<<.

To overcome this issue, I modified the println function with more than 3 parameters. It will check the first two parameters, and

  • if they are convertible iterators, then print iterators and continue with the third
  • if they are not, then print the first parameter and continue with the second

Then, the code mentioned above outputs the expected result, as you can run the code below:

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

int main(int argc, char** argv) {
    registerGen(argc, argv, 1);

    // print chars
    println("Hello world");
    println("Hello", "world", "and TestLib");
    println("Hello TestLib", VERSION);

    // print ints
    println(1);
    println(2);
    println(1, 2, 3, 5, 6);

    // mix ints and chars
    const int start = 2010, end = 2025;
    println("Codeforces (c) Copyright", start, '-', end, "Mike Mirzayanov");

    // print arrays, std::array, and std::vector
    int a[5] = {1, 2, 3, 5, 6};
    println(a);
    std::array<int, 5> arr = {7, 8, 10, 11, 15};
    println(arr);
    std::vector<int> vec = {10, 20, 30, 50, 60};
    println(vec);

    // mix with chars
    println("a:", a);
    println("arr:", arr);
    println("vec:", vec);

    // print ranges
    println(a, a + sizeof(a) / sizeof(a[0]));
    println(arr.begin() + 1, arr.end());
    println(vec.begin(), vec.end() - 2);

    // print ranges mixed
    println(a, a + sizeof(a) / sizeof(a[0]), arr.begin(), arr.end());
    
    // print ranges mixed with chars
    println("a is", a, a + sizeof(a) / sizeof(a[0]), "while arr is", arr.begin(), arr.end());

    // print strings
    vector<string> cfKing = {"Yuchen Du", "Lingyu Jiang", "Qiwen Xu"};
    println(cfKing, "form the team CF King");
    println(cfKing[1]);

    return 0;
}

the ! at the end guarantees the processing go to the function I have modified, not the original println function.

Lumine2024 avatar Nov 22 '25 09:11 Lumine2024

upd: I unintentionally ignored the compilability in -std=c++11. Sorry for the inconvenience.

Lumine2024 avatar Nov 22 '25 14:11 Lumine2024

Thank you. I’m looking into it.

MikeMirzayanov avatar Dec 03 '25 11:12 MikeMirzayanov