Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
invalid_input_tests.cc
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
15
16#include <memory>
17#include <ostream>
18#include <string>
19#include <utility>
20#include <vector>
21
22#include "absl/status/status.h"
23#include "absl/strings/str_join.h"
24#include "gtest/gtest.h"
25#include "ortools/base/gmock.h"
29#include "ortools/math_opt/model.pb.h"
30#include "ortools/math_opt/model_update.pb.h"
32
33namespace operations_research {
34namespace math_opt {
35
36using ::absl::StatusCode;
37using ::testing::HasSubstr;
38using ::testing::status::StatusIs;
39
40std::ostream& operator<<(std::ostream& out,
41 const InvalidInputTestParameters& params) {
42 out << "{ solver_type: " << params.solver_type
43 << " use_integer_variables: " << params.use_integer_variables << " }";
44 return out;
45}
46
48 : model_(), x_(model_.AddContinuousVariable(0.0, 1.0, "x")) {
49 model_.Maximize(2 * x_);
50}
51
53 const SolverType solver_type, SolveParameters solve_parameters,
54 std::vector<std::string> expected_error_substrings)
55 : solver_type(solver_type),
56 solve_parameters(std::move(solve_parameters)),
57 expected_error_substrings(std::move(expected_error_substrings)) {}
58
59std::ostream& operator<<(std::ostream& out,
60 const InvalidParameterTestParams& params) {
61 out << "{ solver_type: " << params.solver_type << " solve_params: "
63 << " expected_error_substrings: [ "
64 << absl::StrJoin(params.expected_error_substrings, "; ") << " ]";
65 return out;
66}
67
68namespace {
69
70TEST_P(InvalidInputTest, InvalidModel) {
71 ModelProto model;
72 model.set_name("simple_model");
73 model.mutable_variables()->add_ids(3);
74 model.mutable_variables()->add_lower_bounds(2.0);
75 model.mutable_variables()->add_upper_bounds(3.0);
76 model.mutable_variables()->add_upper_bounds(4.0);
77 model.mutable_variables()->add_integers(GetParam().use_integer_variables);
78 model.mutable_variables()->add_names("x3");
79
80 EXPECT_THAT(Solver::New(EnumToProto(TestedSolver()), model, /*arguments=*/{}),
81 StatusIs(StatusCode::kInvalidArgument));
82}
83
84TEST_P(InvalidInputTest, InvalidCommonParameters) {
86 const std::unique_ptr<Solver> solver,
87 Solver::New(EnumToProto(TestedSolver()), ModelProto{}, /*arguments=*/{}));
88 Solver::SolveArgs solve_args;
89 solve_args.parameters.set_threads(-1);
90 EXPECT_THAT(solver->Solve(solve_args),
91 StatusIs(StatusCode::kInvalidArgument));
92}
93
94TEST_P(InvalidInputTest, InvalidUpdate) {
95 ModelProto model;
96 model.set_name("simple_model");
97 model.mutable_variables()->add_ids(3);
98 model.mutable_variables()->add_lower_bounds(2.0);
99 model.mutable_variables()->add_upper_bounds(3.0);
100 model.mutable_variables()->add_integers(GetParam().use_integer_variables);
101 model.mutable_variables()->add_names("x3");
102
103 ASSERT_OK_AND_ASSIGN(auto solver,
104 Solver::New(EnumToProto(TestedSolver()), model,
105 /*arguments=*/{}));
106
107 ModelUpdateProto update;
108 update.add_deleted_variable_ids(2);
109
110 EXPECT_THAT(solver->Update(update), StatusIs(StatusCode::kInvalidArgument));
111}
112
113TEST_P(InvalidParameterTest, InvalidParameterNameAsError) {
114 SolveArguments args = {.parameters = GetParam().solve_parameters};
115 const auto result = SimpleSolve();
116 ASSERT_THAT(result, StatusIs(absl::StatusCode::kInvalidArgument));
117 for (const std::string& error : GetParam().expected_error_substrings) {
118 SCOPED_TRACE(error);
119 EXPECT_THAT(result.status().message(), HasSubstr(error));
120 }
121}
122
123} // namespace
124} // namespace math_opt
125} // namespace operations_research
void Maximize(double objective)
Sets the objective to maximize the provided expression.
Definition model.h:1389
static absl::StatusOr< std::unique_ptr< Solver > > New(SolverTypeProto solver_type, const ModelProto &model, const InitArgs &arguments)
Definition solver.cc:88
GRBmodel * model
EXPECT_THAT(ComputeInfeasibleSubsystem(model, GetParam().solver_type), IsOkAndHolds(IsInfeasible(true, ModelSubset{ .variable_bounds={{x, ModelSubset::Bounds{.lower=false,.upper=true}}},.linear_constraints={ {c, ModelSubset::Bounds{.lower=true,.upper=false}}}})))
TEST_P(InfeasibleSubsystemTest, CanComputeInfeasibleSubsystem)
SolverType
The solvers supported by MathOpt.
Definition parameters.h:42
ASSERT_THAT(solver->Update(), IsOkAndHolds(DidUpdate()))
std::ostream & operator<<(std::ostream &ostr, const IndicatorConstraint &constraint)
Enum< E >::Proto EnumToProto(std::optional< E > value)
Definition enums.h:270
In SWIG mode, we don't want anything besides these top-level includes.
std::string ProtobufShortDebugString(const P &message)
Definition proto_utils.h:41
STL namespace.
internal::StatusIsMatcher StatusIs(CodeMatcher code_matcher, MessageMatcher message_matcher)
#define ASSERT_OK_AND_ASSIGN(lhs, rexpr)
InvalidParameterTestParams(SolverType solver_type, SolveParameters solve_parameters, std::vector< std::string > expected_error_substrings)