Google OR-Tools v9.15
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
sparse_collection_matchers.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_SPARSE_COLLECTION_MATCHERS_H_
15#define ORTOOLS_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 "gtest/gtest.h"
25#include "ortools/base/gmock.h"
28
29namespace operations_research {
30namespace math_opt {
31
33 std::initializer_list<std::pair<int64_t, double>> pairs);
34
36 std::initializer_list<std::pair<int64_t, bool>> pairs);
37
39 std::initializer_list<std::tuple<int64_t, int64_t, double>> values);
40
41// Type of the argument of SparseVectorMatcher.
42template <typename T>
43using 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}}));
50MATCHER_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.
61using Coefficient = std::tuple<int64_t, int64_t, const double>;
62using 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}}));
70MATCHER_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 // ORTOOLS_MATH_OPT_CORE_SPARSE_COLLECTION_MATCHERS_H_
std::initializer_list< std::pair< int64_t, const T > > Pairs
SparseBoolVectorProto MakeSparseBoolVector(std::initializer_list< std::pair< int64_t, bool > > pairs)
std::tuple< int64_t, int64_t, const double > Coefficient
SparseDoubleVectorProto MakeSparseDoubleVector(std::initializer_list< std::pair< int64_t, double > > pairs)
SparseVectorView< T > MakeView(absl::Span< const int64_t > ids, const Collection &values)
MATCHER_P(SparseVectorMatcher, pairs, "")
SparseDoubleMatrixProto MakeSparseDoubleMatrix(std::initializer_list< std::tuple< int64_t, int64_t, double > > values)
std::initializer_list< Coefficient > Coefficients
OR-Tools root namespace.