OR-Tools  9.6
dump_vars_test.cc
Go to the documentation of this file.
1 // Copyright 2010-2022 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 #include "ortools/base/dump_vars.h"
15 
16 #include <ostream>
17 #include <sstream>
18 #include <string>
19 #include <utility>
20 #include <vector>
21 
22 #include "gtest/gtest.h"
23 
24 #if !defined(__APPLE__)
25 namespace operations_research::base {
26 namespace {
27 
28 template <class T>
29 ::std::string ToString(const T& t) {
30  ::std::ostringstream oss;
31  oss << t;
32  return oss.str();
33 }
34 
35 TEST(DumpVars, Empty) {
36  EXPECT_EQ(R"()", ToString(DUMP_VARS()));
37  EXPECT_EQ(R"()", DUMP_VARS().str());
38 }
39 
40 TEST(DumpVars, Lvalue) {
41  int a = 42;
42  EXPECT_EQ(R"(a = 42)", ToString(DUMP_VARS(a)));
43  ::std::string foo = "hello";
44  EXPECT_EQ(R"(foo = hello)", ToString(DUMP_VARS(foo)));
45  EXPECT_EQ(R"(foo = hello)", DUMP_VARS(foo).str());
46  EXPECT_EQ(R"(x = hello)", ToString(DUMP_VARS(foo).as("x")));
47 }
48 
49 TEST(DumpVars, Rvalue) {
50  EXPECT_EQ("2 + 2 = 4", ToString(DUMP_VARS(2 + 2)));
51  EXPECT_EQ("2 + 2 = 4", DUMP_VARS(2 + 2).str());
52  EXPECT_EQ("x = 4", ToString(DUMP_VARS(2 + 2).as("x")));
53 }
54 
55 #define FORTY_TWO 42
56 #define ONE_AND_TWO 1, 2
57 
58 TEST(DumpVars, Macro) {
59  // Macros get evaluated before they are stringized. It's not necessarily good,
60  // but we'll have a test for it to serve as a documentation of facts.
61  EXPECT_EQ("42 = 42", ToString(DUMP_VARS(FORTY_TWO)));
62  EXPECT_EQ("42 = 42", DUMP_VARS(FORTY_TWO).str());
63 
64  EXPECT_EQ("1 = 1, 2 = 2", ToString(DUMP_VARS(ONE_AND_TWO)));
65  EXPECT_EQ("1 = 1, 2 = 2", DUMP_VARS(ONE_AND_TWO).str());
66  EXPECT_EQ("one = 1, two = 2",
67  ToString(DUMP_VARS(ONE_AND_TWO).as("one", "two")));
68 }
69 
70 template <int A, int B>
71 int Plus() {
72  return A + B;
73 }
74 
75 TEST(DumpVars, Parens) {
76  EXPECT_EQ("x = 5", ToString(DUMP_VARS(Plus<2, 3>()).as("x")));
77  EXPECT_EQ("(Plus<2, 3>()) = 5", ToString(DUMP_VARS((Plus<2, 3>()))));
78  EXPECT_EQ("(Plus<2, 3>()) = 5", DUMP_VARS((Plus<2, 3>())).str());
79  EXPECT_EQ("((Plus<2, 3>())) = 5", ToString(DUMP_VARS(((Plus<2, 3>())))));
80  EXPECT_EQ("((Plus<2, 3>())) = 5", DUMP_VARS(((Plus<2, 3>()))).str());
81  EXPECT_EQ("Parens = 5", DUMP_VARS(((Plus<2, 3>()))).as("Parens").str());
82 }
83 
84 TEST(DumpVars, Bindings) {
85  // Using a unique_ptr to ensure there is no copy.
86  std::vector<std::pair<int, std::unique_ptr<std::string>>> v;
87  v.push_back({3, std::make_unique<std::string>("hello")});
88  const std::string foo = "bar";
89  for (const auto& [i, s] : v) {
90  EXPECT_EQ("i = 3, *s = hello, foo = bar",
91  ToString(DUMP_VARS_WITH_BINDINGS((i, s), i, *s, foo)));
92  }
93 }
94 
95 TEST(DumpVars, NamesOverride) {
96  EXPECT_EQ("z = 5", ToString(DUMP_VARS(5).as().as("x", "y").as("z")));
97 }
98 
99 TEST(DumpVars, TwoValues) {
100  int foo = 42;
101  int bar = 24;
102  EXPECT_EQ("foo = 42, bar = 24", ToString(DUMP_VARS(foo, bar)));
103  EXPECT_EQ("foo = 42, bar = 24", DUMP_VARS(foo, bar).str());
104  EXPECT_EQ("bar = 42, foo = 24", DUMP_VARS(foo, bar).as("bar", "foo").str());
105 }
106 
107 TEST(DumpVars, ManyArgs) {
108  int a = 1;
109  int b = 2;
110  int c = 3;
111  int d = 5;
112  int e = 7;
113  int f = 11;
114  EXPECT_EQ("a = 1, b = 2, c = 3, d = 5, e = 7, f = 11",
115  ToString(DUMP_VARS(a, b, c, d, e, f)));
116  EXPECT_EQ("a = 1, b = 2, c = 3, d = 5, e = 7, f = 11",
117  DUMP_VARS(a, b, c, d, e, f).str());
118 }
119 
120 TEST(DumpVars, LazyEvaluation) {
121  {
122  int n = 0;
123  auto F = [&]() { return ++n; };
124  auto vars = DUMP_VARS(F());
125  EXPECT_EQ(0, n);
126  EXPECT_EQ("F() = 1", ToString(vars));
127  EXPECT_EQ(1, n);
128  EXPECT_EQ("F() = 2", ToString(vars));
129  EXPECT_EQ(2, n);
130  EXPECT_EQ("F() = 3", vars.str());
131  EXPECT_EQ(3, n);
132  EXPECT_EQ("F() = 4", vars.str());
133  EXPECT_EQ(4, n);
134  EXPECT_EQ("5 = 5", vars.as("5").str());
135  EXPECT_EQ(5, n);
136  }
137  {
138  int n = 0;
139  auto F = [&]() { return ++n; };
140  auto vars = DUMP_VARS(F()).as("x");
141  EXPECT_EQ(0, n);
142  EXPECT_EQ("x = 1", ToString(vars));
143  EXPECT_EQ(1, n);
144  EXPECT_EQ("x = 2", ToString(vars));
145  EXPECT_EQ(2, n);
146  EXPECT_EQ("x = 3", vars.str());
147  EXPECT_EQ(3, n);
148  EXPECT_EQ("x = 4", vars.str());
149  EXPECT_EQ(4, n);
150  EXPECT_EQ("y = 5", vars.as("y").str());
151  EXPECT_EQ(5, n);
152  }
153 }
154 
155 TEST(DumpVars, TemporaryLifetime) {
156  EXPECT_EQ(R"(std::string_view(std::string("hello")) = hello)",
157  ToString(DUMP_VARS(std::string_view(std::string("hello")))));
158  auto v = DUMP_VARS(std::string_view(std::string("hello")));
159  EXPECT_EQ(R"(std::string_view(std::string("hello")) = hello)", ToString(v));
160  EXPECT_EQ(R"(temp = hello)", ToString(v.as("temp")));
161 }
162 
163 } // namespace
164 } // namespace operations_research::base
165 #endif // !defined(__APPLE__)
int64_t b
int64_t a
#define DUMP_VARS_WITH_BINDINGS(binding,...)
Definition: dump_vars.h:88
#define DUMP_VARS(...)
Definition: dump_vars.h:73
#define ONE_AND_TWO
#define FORTY_TWO
const absl::string_view ToString(MPSolver::OptimizationProblemType optimization_problem_type)
TEST(LinearAssignmentTest, NullMatrix)