Skip to main content

TESTGEN

TESTGEN is a simple Python package that is intended to be used by short scripts to generate test cases for competitive programming problems.

By using TESTGEN you are not required to store the generated test cases on disk; TESTGEN promises that the generated tests will be reproducible, meaning that if you generate them again you will get the exact same test cases.

Rational

In competitive programming, you are provided with an algorithmic problem that you have to solve and then write a piece of code that will solve the problem for you. Your solution is then executed with a predefined set of test cases, and if your solution outputs the expected output on each test case in the given amount of time, your solution is considered CORRECT.

Writing those test cases manually is not really practical, and often you write a program that generates those tests for you. Instead of writing those scripts from the ground up for every problem that you need to generate test cases for, you can think of TESTGEN as a framework that provides you with useful functions and boilerplate you will eventually use.

In addition, there my be hundreds of different test cases that you will have to generate to a single problem, and each test case can take up to a couple of megabytes of storage on the disk. Instead of storing all those test cases, TESTGEN allows you to write your test generation script in such a way that will always output the same test cases (even if they use randomness) by using a seed and a well-formed order of generation. By using TESTGEN you are only required to store a configuration file named testgen.toml with your test generation script, and TESTGEN will generate the tests every time you run the generation script.

Usage

TESTGEN is designed to be compatible with the ICPC problem package specification.

A details example generation script that uses the TESTGEN framework can be found with the source code of the project, here.

"""  The following script is an example of a test generation script that
uses the TESTGEN framework for the problem that sums an array of N given
integers. """

from __future__ import annotations
from testgen import TestCase, TestCollection
from dataclasses import dataclass


MAX_ARRAY_SIZE = int(2e5)
MAX_ELEMENT = int(1e9)


@dataclass
class ArraySum(TestCase):
    input: list[int]

    def write_input(self, input_f) -> None:
        print(len(self.input), file=input_f)
        print(' '.join(str(i) for i in self.input), file=input_f)

    def write_answer(self, answer_f, input_f) -> None:
        print(sum(self.input), file=answer_f)

    def validate(self) -> None:
        assert 1 <= len(self.input) <= MAX_ARRAY_SIZE
        for v in self.input:
            assert 1 <= v <= MAX_ELEMENT


tests = TestCollection('data/secret', ArraySum)


@tests.collect(desc='max sized array filled with ones')
def all_ones() -> ArraySum:
    return ArraySum([1] * MAX_ARRAY_SIZE)


@tests.collect(
    desc='array filled with same values',
    params={'length': range(1, 10), 'value': (8743, 12, 999_999)},
)
def same_values(length: int, value: int) -> ArraySum:
    return ArraySum([value] * length)


@tests.collect(desc='random max sized array', repeat=3)
def random_list(random) -> ArraySum:
    return ArraySum(
        [random.randint(1, MAX_ELEMENT) for _ in range(MAX_ARRAY_SIZE)]
    )


if __name__ == '__main__':
    tests.generate()

Release files for testgen 0.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for testgen 0.3
File Size Uploaded
testgen-0.3.tar.gz 5.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for testgen 0.3
File Interpreter ABI Platform
testgen-0.3-py3-none-any.whl Python 3 none any Details

Total release size: 10.8 kB

Release files / testgen-0.3.tar.gz

Download URL testgen-0.3.tar.gz
Size 5.5 kB
Tags Source
SHA-256 checksum
How to use checksums
d4b138e73cbf9e19c0486dcaf9e07f28d92e44a01b08542a0f10a3c04b9480c7
BLAKE2b-256 checksum
How to use checksums
1a6ffe21e90ed09fdf7dfa41262e37e8eba8bbb26929882414095605e8b11baf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.7.0 importlib_metadata/4.8.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.10

Release files / testgen-0.3-py3-none-any.whl

Download URL testgen-0.3-py3-none-any.whl
Size 5.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
736b1f5c62f9fcf93cad3f5f248bef9e5024c538e5df0d55821dcd0ec87f677b
BLAKE2b-256 checksum
How to use checksums
be2ae42dbf1006010cf8ad8e0b0b8a20c196cfd63b193473c19ffbd013b6c948
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.7.0 importlib_metadata/4.8.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.10

Release history Release notifications | RSS feed

This release

0.3 This release

2 release files

0.2

2 release files

0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page