OmniSciDB  04ee39c94c
TestHelpers.h
Go to the documentation of this file.
1 /*
2  * Copyright 2017 MapD Technologies, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef TEST_HELPERS_H_
18 #define TEST_HELPERS_H_
19 
20 #include "../QueryEngine/TargetValue.h"
21 #include "Shared/Logger.h"
22 
23 #include "LeafHostInfo.h"
24 
25 #include <gtest/gtest.h>
26 #include <boost/algorithm/string.hpp>
27 #include <boost/program_options.hpp>
28 #include <boost/variant.hpp>
29 
30 namespace TestHelpers {
31 
32 template <class T>
33 void compare_array(const TargetValue& r,
34  const std::vector<T>& arr,
35  const double tol = -1.) {
36  auto array_tv = boost::get<ArrayTargetValue>(&r);
37  CHECK(array_tv);
38  if (!array_tv->is_initialized()) {
39  ASSERT_EQ(size_t(0), arr.size());
40  return;
41  }
42  const auto& scalar_tv_vector = array_tv->get();
43  ASSERT_EQ(scalar_tv_vector.size(), arr.size());
44  size_t ctr = 0;
45  for (const ScalarTargetValue scalar_tv : scalar_tv_vector) {
46  auto p = boost::get<T>(&scalar_tv);
47  if (tol < 0.) {
48  ASSERT_EQ(*p, arr[ctr++]);
49  } else {
50  ASSERT_NEAR(*p, arr[ctr++], tol);
51  }
52  }
53 }
54 
55 template <>
56 void compare_array(const TargetValue& r,
57  const std::vector<std::string>& arr,
58  const double tol) {
59  auto array_tv = boost::get<ArrayTargetValue>(&r);
60  CHECK(array_tv);
61  if (!array_tv->is_initialized()) {
62  ASSERT_EQ(size_t(0), arr.size());
63  return;
64  }
65  const auto& scalar_tv_vector = array_tv->get();
66  ASSERT_EQ(scalar_tv_vector.size(), arr.size());
67  size_t ctr = 0;
68  for (const ScalarTargetValue scalar_tv : scalar_tv_vector) {
69  auto ns = boost::get<NullableString>(&scalar_tv);
70  CHECK(ns);
71  auto str = boost::get<std::string>(ns);
72  CHECK(str);
73  ASSERT_TRUE(*str == arr[ctr++]);
74  }
75 }
76 
77 template <class T>
78 void compare_array(const std::vector<T>& a,
79  const std::vector<T>& b,
80  const double tol = -1.) {
81  CHECK_EQ(a.size(), b.size());
82  for (size_t i = 0; i < a.size(); i++) {
83  if (tol < 0.) {
84  ASSERT_EQ(a[i], b[i]);
85  } else {
86  ASSERT_NEAR(a[i], b[i], tol);
87  }
88  }
89 }
90 
92  static void compare(const GeoPointTargetValue& a,
93  const GeoPointTargetValue& b,
94  const double tol = -1.) {
95  compare_array(*a.coords, *b.coords, tol);
96  }
97  static void compare(const GeoLineStringTargetValue& a,
98  const GeoLineStringTargetValue& b,
99  const double tol = -1.) {
100  compare_array(*a.coords, *b.coords, tol);
101  }
102  static void compare(const GeoPolyTargetValue& a,
103  const GeoPolyTargetValue& b,
104  const double tol = -1.) {
105  compare_array(*a.coords, *b.coords, tol);
107  }
108  static void compare(const GeoMultiPolyTargetValue& a,
109  const GeoMultiPolyTargetValue& b,
110  const double tol = -1.) {
111  compare_array(*a.coords, *b.coords, tol);
114  }
115 };
116 
117 template <class T>
118 T g(const TargetValue& r) {
119  auto geo_r = boost::get<GeoTargetValue>(&r);
120  CHECK(geo_r);
121  auto p = boost::get<T>(geo_r);
122  CHECK(p);
123  return *p;
124 }
125 
126 template <class T>
128  const T& geo_truth_target,
129  const double tol = -1.) {
130  const auto geo_value = g<T>(r);
131  GeoTargetComparator::compare(geo_value, geo_truth_target, tol);
132 }
133 
134 template <class T>
135 T v(const TargetValue& r) {
136  auto scalar_r = boost::get<ScalarTargetValue>(&r);
137  CHECK(scalar_r);
138  auto p = boost::get<T>(scalar_r);
139  CHECK(p);
140  return *p;
141 }
142 
143 template <typename T>
144 inline std::string convert(const T& t) {
145  return std::to_string(t);
146 }
147 
148 template <std::size_t N>
149 inline std::string convert(const char (&t)[N]) {
150  return std::string(t);
151 }
152 
153 template <>
154 inline std::string convert(const std::string& t) {
155  return t;
156 }
157 
159  ValuesGenerator(const std::string& table_name) : table_name_(table_name) {}
160 
161  template <typename... COL_ARGS>
162  std::string operator()(COL_ARGS&&... args) const {
163  std::vector<std::string> vals({convert(std::forward<COL_ARGS>(args))...});
164  return std::string("INSERT INTO " + table_name_ + " VALUES(" +
165  boost::algorithm::join(vals, ",") + ");");
166  }
167 
168  const std::string table_name_;
169 };
170 
171 LeafHostInfo to_leaf_host_info(std::string& server_info, NodeRole role) {
172  size_t pos = server_info.find(':');
173  if (pos == std::string::npos) {
174  throw std::runtime_error("Invalid host:port -> " + server_info);
175  }
176 
177  auto host = server_info.substr(0, pos);
178  auto port = server_info.substr(pos + 1);
179 
180  return LeafHostInfo(host, std::stoi(port), role);
181 }
182 
183 std::vector<LeafHostInfo> to_leaf_host_info(std::vector<std::string>& server_infos,
184  NodeRole role) {
185  std::vector<LeafHostInfo> host_infos;
186 
187  for (auto& server_info : server_infos) {
188  host_infos.push_back(to_leaf_host_info(server_info, role));
189  }
190 
191  return host_infos;
192 }
193 
194 void init_logger_stderr_only(int argc, char const* const* argv) {
195  logger::LogOptions log_options(argv[0]);
196  log_options.max_files_ = 0; // stderr only by default
197  log_options.parse_command_line(argc, argv);
198  logger::init(log_options);
199 }
200 
201 } // namespace TestHelpers
202 
203 #endif // TEST_HELPERS_H_
#define CHECK_EQ(x, y)
Definition: Logger.h:195
void compare_geo_target(const TargetValue &r, const T &geo_truth_target, const double tol=-1.)
Definition: TestHelpers.h:127
NodeRole
Definition: LeafHostInfo.h:32
T v(const TargetValue &r)
Definition: TestHelpers.h:135
std::string convert(const T &t)
Definition: TestHelpers.h:144
T g(const TargetValue &r)
Definition: TestHelpers.h:118
std::string join(T const &container, std::string const &delim)
std::shared_ptr< std::vector< int32_t > > ring_sizes
Definition: TargetValue.h:125
std::shared_ptr< std::vector< double > > coords
Definition: TargetValue.h:124
std::string to_string(char const *&&v)
std::shared_ptr< std::vector< int32_t > > poly_rings
Definition: TargetValue.h:126
const std::string table_name_
Definition: TestHelpers.h:168
std::shared_ptr< std::vector< double > > coords
Definition: TargetValue.h:107
static void compare(const GeoPolyTargetValue &a, const GeoPolyTargetValue &b, const double tol=-1.)
Definition: TestHelpers.h:102
void init(LogOptions const &log_opts)
Definition: Logger.cpp:260
static void compare(const GeoLineStringTargetValue &a, const GeoLineStringTargetValue &b, const double tol=-1.)
Definition: TestHelpers.h:97
std::string operator()(COL_ARGS &&... args) const
Definition: TestHelpers.h:162
std::shared_ptr< std::vector< int32_t > > ring_sizes
Definition: TargetValue.h:115
static void compare(const GeoMultiPolyTargetValue &a, const GeoMultiPolyTargetValue &b, const double tol=-1.)
Definition: TestHelpers.h:108
std::shared_ptr< std::vector< double > > coords
Definition: TargetValue.h:100
#define CHECK(condition)
Definition: Logger.h:187
void init_logger_stderr_only(int argc, char const *const *argv)
Definition: TestHelpers.h:194
ValuesGenerator(const std::string &table_name)
Definition: TestHelpers.h:159
boost::variant< ScalarTargetValue, ArrayTargetValue, GeoTargetValue, GeoTargetValuePtr > TargetValue
Definition: TargetValue.h:167
size_t max_files_
Definition: Logger.h:117
void compare_array(const TargetValue &r, const std::vector< T > &arr, const double tol=-1.)
Definition: TestHelpers.h:33
DEVICE ALWAYS_INLINE double tol(int32_t ic)
static void compare(const GeoPointTargetValue &a, const GeoPointTargetValue &b, const double tol=-1.)
Definition: TestHelpers.h:92
std::shared_ptr< std::vector< double > > coords
Definition: TargetValue.h:114
LeafHostInfo to_leaf_host_info(std::string &server_info, NodeRole role)
Definition: TestHelpers.h:171
boost::variant< int64_t, double, float, NullableString > ScalarTargetValue
Definition: TargetValue.h:156
void parse_command_line(int, char const *const *)
Definition: Logger.cpp:117