Google OR-Tools
v9.15
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
solver_interface_testing.h
Go to the documentation of this file.
1
// Copyright 2010-2025 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
#ifndef ORTOOLS_MATH_OPT_CORE_SOLVER_INTERFACE_TESTING_H_
15
#define ORTOOLS_MATH_OPT_CORE_SOLVER_INTERFACE_TESTING_H_
16
17
#include "absl/base/nullability.h"
18
#include "absl/container/flat_hash_map.h"
19
#include "absl/container/flat_hash_set.h"
20
#include "
ortools/math_opt/core/solver_interface.h
"
21
22
namespace
operations_research::math_opt
{
23
24
// Configuration parameters for WithAlternateAllSolversRegistry.
25
struct
WithAlternateAllSolversRegistryConfiguration
{
26
// The solver types to keep from the actual registry.
27
//
28
// By default keep nothing. If a solver type is listed but not registered a
29
// LOG(FATAL) ends the test.
30
absl::flat_hash_set<SolverTypeProto>
kept
;
31
32
// The solver types to override in the temporary registry.
33
//
34
// They must not appear in the `kept` set or else it will LOG(FATAL) on
35
// construction of WithAlternateAllSolversRegistry.
36
absl::flat_hash_map<SolverTypeProto, absl_nonnull SolverInterface::Factory>
37
overridden
;
38
};
39
40
// Scoped object for temporarily replacing the AllSolversRegistry::Instance() in
41
// a unit test.
42
//
43
// Usage:
44
//
45
// TEST(MyTest, Something) {
46
// const WithAlternateAllSolversRegistry alternate_registry({
47
// .kept={SOLVER_TYPE_GSCIP},
48
// .overridden={{SOLVER_TYPE_GLOP, FakeGlopFactory}},
49
// });
50
// // At this point we have two registered solvers:
51
// // * SOLVER_TYPE_GSCIP: using the usual factory
52
// // * SOLVER_TYPE_GLOP: using FakeGlopFactory()
53
// ...
54
// }
55
class
WithAlternateAllSolversRegistry
{
56
public
:
57
// After the constructor returns and until this object is destroyed, the
58
// AllSolversRegistry::Instance() will be replaced by a new instance with the
59
// changes described in the configuration.
60
//
61
// It is an error to have two instances alive at the same time. If this
62
// happens a LOG(FATAL) will stop the test.
63
explicit
WithAlternateAllSolversRegistry
(
64
WithAlternateAllSolversRegistryConfiguration
configuration);
65
66
WithAlternateAllSolversRegistry
(
const
WithAlternateAllSolversRegistry
&) =
67
delete
;
68
WithAlternateAllSolversRegistry
&
operator=
(
69
const
WithAlternateAllSolversRegistry
&) =
delete
;
70
71
// Removes the temporary registry and restore the previous value of
72
// AllSolversRegistry::Instance().
73
~WithAlternateAllSolversRegistry
();
74
75
private
:
76
AllSolversRegistry
temporary_registry_;
77
};
78
79
}
// namespace operations_research::math_opt
80
81
#endif
// ORTOOLS_MATH_OPT_CORE_SOLVER_INTERFACE_TESTING_H_
operations_research::math_opt::AllSolversRegistry
Definition
solver_interface.h:181
operations_research::math_opt::WithAlternateAllSolversRegistry::~WithAlternateAllSolversRegistry
~WithAlternateAllSolversRegistry()
Definition
solver_interface_testing.cc:32
operations_research::math_opt::WithAlternateAllSolversRegistry::operator=
WithAlternateAllSolversRegistry & operator=(const WithAlternateAllSolversRegistry &)=delete
operations_research::math_opt::WithAlternateAllSolversRegistry::WithAlternateAllSolversRegistry
WithAlternateAllSolversRegistry(const WithAlternateAllSolversRegistry &)=delete
operations_research::math_opt::WithAlternateAllSolversRegistry::WithAlternateAllSolversRegistry
WithAlternateAllSolversRegistry(WithAlternateAllSolversRegistryConfiguration configuration)
Definition
solver_interface_testing.cc:22
operations_research::math_opt
Definition
gurobi_isv.cc:28
solver_interface.h
operations_research::math_opt::WithAlternateAllSolversRegistryConfiguration
Definition
solver_interface_testing.h:25
operations_research::math_opt::WithAlternateAllSolversRegistryConfiguration::kept
absl::flat_hash_set< SolverTypeProto > kept
Definition
solver_interface_testing.h:30
operations_research::math_opt::WithAlternateAllSolversRegistryConfiguration::overridden
absl::flat_hash_map< SolverTypeProto, absl_nonnull SolverInterface::Factory > overridden
Definition
solver_interface_testing.h:37
ortools
math_opt
core
solver_interface_testing.h
Generated by
1.15.0