Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
generic_tests.h
Go to the documentation of this file.
1// Copyright 2010-2024 Google LLC
2// Licensed under the Apache License, Version 2.0 (the "License");
3// you may not use this file except in compliance with the License.
4// You may obtain a copy of the License at
5//
6// http://www.apache.org/licenses/LICENSE-2.0
7//
8// Unless required by applicable law or agreed to in writing, software
9// distributed under the License is distributed on an "AS IS" BASIS,
10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11// See the License for the specific language governing permissions and
12// limitations under the License.
13
14// This header groups parameteric tests to validates behaviors common to MIP and
15// LP solvers.
16#ifndef OR_TOOLS_MATH_OPT_SOLVER_TESTS_GENERIC_TESTS_H_
17#define OR_TOOLS_MATH_OPT_SOLVER_TESTS_GENERIC_TESTS_H_
18
19#include <optional>
20#include <ostream>
21#include <string>
22
23#include "absl/status/statusor.h"
24#include "gtest/gtest.h"
26
27namespace operations_research {
28namespace math_opt {
29
32 bool integer_variables, std::string expected_log,
34
35 // The tested solver.
37
38 // True if the solver support SolveInterrupter.
40
41 // True if the tests should be performed with integer variables.
43
44 // A message included in the solver logs when an optimal solution is found.
45 std::string expected_log;
46
47 // Additional parameters to control the solve.
49
50 friend std::ostream& operator<<(std::ostream& out,
51 const GenericTestParameters& params);
52};
53
54// A suite of unit tests to validate that mandatory behavior for all (MIP and
55// LP) solvers.
56//
57// To use these tests, in file <solver>_test.cc, write:
58// INSTANTIATE_TEST_SUITE_P(
59// <Solver>GenericTest, GenericTest,
60// testing::Values(GenericTestParameters(
61// SolverType::k<Solver>,
62// /*support_interrupter=*/true,
63// /*integer_variables=*/true
64// )));
65class GenericTest : public ::testing::TestWithParam<GenericTestParameters> {
66 protected:
67 static absl::StatusOr<SolveResult> SimpleSolve(const Model& model) {
68 return Solve(model, GetParam().solver_type,
69 {.parameters = GetParam().solve_parameters});
70 }
71};
72
76 const std::optional<CallbackEvent> supported_event = std::nullopt)
79 event(supported_event) {}
80
81 // The tested solver.
83
84 // The test problem will be a 0-1 IP if true, otherwise will be an LP.
86
87 // A supported callback event, or nullopt if no event is supported.
88 std::optional<CallbackEvent> event;
89
90 friend std::ostream& operator<<(std::ostream& out,
91 const TimeLimitTestParameters& params);
92};
93
94// A suite of unit tests to show that time limits are handled correctly.
95//
96// These tests require that the underlying solver supports a callback. The tests
97// will create either a small LP or IP, depending on the bool
98// integer_variables below.
99//
100// To use these tests, in file <solver>_test.cc write:
101// INSTANTIATE_TEST_SUITE_P(<Solver>TimeLimitTest, TimeLimitTest,
102// testing::Values(TimeLimitTestParameters(
103// SolverType::k<Solver>, <integer_variables>, CALLBACK_EVENT_<EVENT>)));
104class TimeLimitTest : public ::testing::TestWithParam<TimeLimitTestParameters> {
105 public:
106 SolverType TestedSolver() const { return GetParam().solver_type; }
107};
108
109} // namespace math_opt
110} // namespace operations_research
111
112#endif // OR_TOOLS_MATH_OPT_SOLVER_TESTS_GENERIC_TESTS_H_
static absl::StatusOr< SolveResult > SimpleSolve(const Model &model)
GRBmodel * model
SolverType
The solvers supported by MathOpt.
Definition parameters.h:42
absl::StatusOr< SolveResult > Solve(const Model &model, const SolverType solver_type, const SolveArguments &solve_args, const SolverInitArguments &init_args)
Definition solve.cc:62
In SWIG mode, we don't want anything besides these top-level includes.
bool support_interrupter
True if the solver support SolveInterrupter.
SolveParameters solve_parameters
Additional parameters to control the solve.
std::string expected_log
A message included in the solver logs when an optimal solution is found.
bool integer_variables
True if the tests should be performed with integer variables.
friend std::ostream & operator<<(std::ostream &out, const GenericTestParameters &params)
GenericTestParameters(SolverType solver_type, bool support_interrupter, bool integer_variables, std::string expected_log, SolveParameters solve_parameters={})
friend std::ostream & operator<<(std::ostream &out, const TimeLimitTestParameters &params)
bool integer_variables
The test problem will be a 0-1 IP if true, otherwise will be an LP.
TimeLimitTestParameters(const SolverType solver_type, const bool integer_variables, const std::optional< CallbackEvent > supported_event=std::nullopt)
std::optional< CallbackEvent > event
A supported callback event, or nullopt if no event is supported.