Google OR-Tools v9.15
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-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
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"
31
32namespace operations_research {
33namespace math_opt {
34
35using ::absl::StatusCode;
36using ::testing::HasSubstr;
37using ::testing::status::StatusIs;
38
39std::ostream& operator<<(std::ostream& out,
40 const InvalidInputTestParameters& params) {
41 out << "{ solver_type: " << params.solver_type
42 << " use_integer_variables: " << params.use_integer_variables << " }";
43 return out;
44}
45
47 : model_(), x_(model_.AddContinuousVariable(0.0, 1.0, "x")) {
48 model_.Maximize(2 * x_);
49}
50
57
58std::ostream& operator<<(std::ostream& out,
59 const InvalidParameterTestParams& params) {
60 out << "{ solver_type: " << params.solver_type << " solve_params: "
62 << " expected_error_substrings: [ "
63 << absl::StrJoin(params.expected_error_substrings, "; ") << " ]";
64 return out;
65}
66
67namespace {
68
69TEST_P(InvalidInputTest, InvalidModel) {
70 ModelProto model;
71 model.set_name("simple_model");
72 model.mutable_variables()->add_ids(3);
76 model.mutable_variables()->add_integers(GetParam().use_integer_variables);
77 model.mutable_variables()->add_names("x3");
78
79 EXPECT_THAT(Solver::New(EnumToProto(TestedSolver()), model, /*arguments=*/{}),
80 StatusIs(StatusCode::kInvalidArgument));
81}
82
83TEST_P(InvalidInputTest, InvalidCommonParameters) {
85 const std::unique_ptr<Solver> solver,
86 Solver::New(EnumToProto(TestedSolver()), ModelProto{}, /*arguments=*/{}));
87 Solver::SolveArgs solve_args;
88 solve_args.parameters.set_threads(-1);
89 EXPECT_THAT(solver->Solve(solve_args),
90 StatusIs(StatusCode::kInvalidArgument));
91}
92
93TEST_P(InvalidInputTest, InvalidUpdate) {
94 ModelProto model;
95 model.set_name("simple_model");
96 model.mutable_variables()->add_ids(3);
97 model.mutable_variables()->add_lower_bounds(2.0);
98 model.mutable_variables()->add_upper_bounds(3.0);
99 model.mutable_variables()->add_integers(GetParam().use_integer_variables);
100 model.mutable_variables()->add_names("x3");
101
102 ASSERT_OK_AND_ASSIGN(auto solver,
103 Solver::New(EnumToProto(TestedSolver()), model,
104 /*arguments=*/{}));
105
106 ModelUpdateProto update;
107 update.add_deleted_variable_ids(2);
108
109 EXPECT_THAT(solver->Update(update), StatusIs(StatusCode::kInvalidArgument));
110}
111
112TEST_P(InvalidParameterTest, InvalidParameterNameAsError) {
113 SolveArguments args = {.parameters = GetParam().solve_parameters};
114 const auto result = SimpleSolve();
115 ASSERT_THAT(result, StatusIs(absl::StatusCode::kInvalidArgument));
116 for (const std::string& error : GetParam().expected_error_substrings) {
117 SCOPED_TRACE(error);
118 EXPECT_THAT(result.status().message(), HasSubstr(error));
119 }
120}
121
122} // namespace
123} // namespace math_opt
124} // namespace operations_research
::operations_research::math_opt::VariablesProto *PROTOBUF_NONNULL mutable_variables()
Definition model.pb.h:4518
void set_name(Arg_ &&arg, Args_... args)
static absl::StatusOr< std::unique_ptr< Solver > > New(SolverTypeProto solver_type, const ModelProto &model, const InitArgs &arguments)
Definition solver.cc:88
::std::string *PROTOBUF_NONNULL add_names()
Definition model.pb.h:2786
#define ASSERT_OK_AND_ASSIGN(lhs, rexpr)
Definition gmock.h:53
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)
std::ostream & operator<<(std::ostream &ostr, const SecondOrderConeConstraint &constraint)
Enum< E >::Proto EnumToProto(std::optional< E > value)
Definition enums.h:270
OR-Tools root namespace.
std::string ProtobufShortDebugString(const P &message)
Definition proto_utils.h:46
STL namespace.
InvalidInputTestParameters(const SolverType solver_type, const bool use_integer_variables)
InvalidParameterTestParams(SolverType solver_type, SolveParameters solve_parameters, std::vector< std::string > expected_error_substrings)