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"
21
23
24// Configuration parameters for WithAlternateAllSolversRegistry.
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>
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// }
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.
65
67 delete;
69 const WithAlternateAllSolversRegistry&) = delete;
70
71 // Removes the temporary registry and restore the previous value of
72 // AllSolversRegistry::Instance().
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_
WithAlternateAllSolversRegistry & operator=(const WithAlternateAllSolversRegistry &)=delete
WithAlternateAllSolversRegistry(const WithAlternateAllSolversRegistry &)=delete
WithAlternateAllSolversRegistry(WithAlternateAllSolversRegistryConfiguration configuration)
absl::flat_hash_map< SolverTypeProto, absl_nonnull SolverInterface::Factory > overridden