OR-Tools  9.2
sparse_collection_matchers.h
Go to the documentation of this file.
1 // Copyright 2010-2021 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 OR_TOOLS_MATH_OPT_CORE_SPARSE_COLLECTION_MATCHERS_H_
15 #define OR_TOOLS_MATH_OPT_CORE_SPARSE_COLLECTION_MATCHERS_H_
16 
17 #include <cstdint>
18 #include <initializer_list>
19 #include <tuple>
20 #include <type_traits>
21 #include <utility>
22 #include <vector>
23 
24 #include "testing/base/public/gmock.h"
25 #include "testing/base/public/gunit.h"
27 #include "ortools/math_opt/sparse_containers.pb.h"
28 
29 namespace operations_research {
30 namespace math_opt {
31 
32 SparseDoubleVectorProto MakeSparseDoubleVector(
33  std::initializer_list<std::pair<int64_t, double>> pairs);
34 
35 SparseBoolVectorProto MakeSparseBoolVector(
36  std::initializer_list<std::pair<int64_t, bool>> pairs);
37 
38 SparseDoubleMatrixProto MakeSparseDoubleMatrix(
39  std::initializer_list<std::tuple<int64_t, int64_t, double>> values);
40 
41 // Type of the argument of SparseVectorMatcher.
42 template <typename T>
43 using Pairs = std::initializer_list<std::pair<int64_t, const T>>;
44 
45 // Here `pairs` must be a Pairs<T>.
46 //
47 // Usage:
48 // EXPECT_THAT(v, SparseVectorMatcher(Pairs<double>{}));
49 // EXPECT_THAT(v, SparseVectorMatcher(Pairs<double>{{2, 3.0}, {3, 2.0}}));
50 MATCHER_P(SparseVectorMatcher, pairs, "") {
51  const auto iterable = MakeView(arg);
52  const std::vector v(iterable.begin(), iterable.end());
53  const std::vector<typename decltype(v)::value_type> expected(pairs.begin(),
54  pairs.end());
55 
56  return ::testing::ExplainMatchResult(::testing::ContainerEq(expected), v,
57  result_listener);
58 }
59 
60 // Type of the argument of SparseDoubleMatrixMatcher.
61 using Coefficient = std::tuple<int64_t, int64_t, const double>;
62 using Coefficients = std::initializer_list<Coefficient>;
63 
64 // Here `coefficients` must be a Coefficients<T>.
65 //
66 // Usage:
67 // EXPECT_THAT(v, SparseDoubleMatrixMatcher(Coefficients{}));
68 // EXPECT_THAT(v, SparseDoubleMatrixMatcher(Coefficients{{2, 1, 3.0}, {3,
69 // 0, 2.0}}));
70 MATCHER_P(SparseDoubleMatrixMatcher, coefficients, "") {
71  std::vector<Coefficient> v;
72  for (int i = 0; i < arg.row_ids_size(); ++i) {
73  v.emplace_back(arg.row_ids(i), arg.column_ids(i), arg.coefficients(i));
74  }
75  const std::vector<Coefficient> expected(coefficients.begin(),
76  coefficients.end());
77 
78  return ::testing::ExplainMatchResult(::testing::ContainerEq(expected), v,
79  result_listener);
80 }
81 
82 } // namespace math_opt
83 } // namespace operations_research
84 
85 #endif // OR_TOOLS_MATH_OPT_CORE_SPARSE_COLLECTION_MATCHERS_H_
std::initializer_list< std::pair< int64_t, const T > > Pairs
SparseDoubleVectorProto MakeSparseDoubleVector(std::initializer_list< std::pair< int64_t, double >> pairs)
SparseVectorView< T > MakeView(absl::Span< const int64_t > ids, const Collection &values)
SparseDoubleMatrixProto MakeSparseDoubleMatrix(std::initializer_list< std::tuple< int64_t, int64_t, double >> values)
MATCHER_P(SparseVectorMatcher, pairs, "")
std::initializer_list< Coefficient > Coefficients
std::tuple< int64_t, int64_t, const double > Coefficient
SparseBoolVectorProto MakeSparseBoolVector(std::initializer_list< std::pair< int64_t, bool >> pairs)
Collection of objects used to extend the Constraint Solver library.
absl::Span< const double > coefficients