Module testing

Module testing 

Source
Expand description

Testing utilities and macros for problem implementations.

This module provides macros and helpers to reduce test boilerplate by ~90% when implementing new problems. Instead of writing 300+ lines of tests per problem, you can use these macros to generate comprehensive test suites.

§Macros

§graph_problem_tests!

Generates a complete test suite for graph problems:

use problemreductions::graph_problem_tests;

graph_problem_tests! {
    problem_type: IndependentSetT<i32>,
    constraint_type: IndependentSetConstraint,
    test_cases: [
        // (name, num_vertices, edges, valid_solution, expected_size, is_maximization)
        (triangle, 3, [(0, 1), (1, 2), (0, 2)], [1, 0, 0], 1, true),
        (path, 3, [(0, 1), (1, 2)], [1, 0, 1], 2, true),
    ]
}

This generates tests for:

  • Problem creation and metadata
  • Solution validity and size computation
  • Energy mode (maximization vs minimization)
  • CSP interface (constraints, objectives)
  • Brute force solving (for small instances)

§complement_test!

Tests that two problems are complements (e.g., IS + VC = n):

use problemreductions::complement_test;

complement_test! {
    name: test_is_vc_complement,
    problem_a: IndependentSetT<i32>,
    problem_b: VertexCoverT<i32>,
    test_graphs: [
        (3, [(0, 1), (1, 2)]),
        (4, [(0, 1), (1, 2), (2, 3)]),
    ]
}

§quick_problem_test!

Quick single-instance validation:

use problemreductions::quick_problem_test;

quick_problem_test!(
    IndependentSetT<i32>,
    new(3, vec![(0, 1)]),
    solution: [0, 0, 1],
    expected_size: 1,
    is_valid: true
);

§Test Case Types

Structs§

GraphTestCase
A test case for a graph problem.
SatTestCase
A test case for a SAT problem.