MySQL 9.1.0
Source Code Documentation
helpers.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2015, 2024, Oracle and/or its affiliates.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License, version 2.0,
6 as published by the Free Software Foundation.
7
8 This program is designed to work with certain software (including
9 but not limited to OpenSSL) that is licensed under separate terms,
10 as designated in a particular file or component or in included license
11 documentation. The authors of MySQL hereby grant you an additional
12 permission to link the program and your derivative works with the
13 separately licensed software that they have either included with
14 the program or referenced in the documentation.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24*/
25
26#ifndef MYSQL_HARNESS_HELPERS_INCLUDED
27#define MYSQL_HARNESS_HELPERS_INCLUDED
28
29// Standard headers
30#include <algorithm>
31#include <list>
32#include <string>
33#include <vector>
34
35// Third-party headers
36#include <gtest/gtest.h>
37
39
40template <typename T>
41std::ostream &operator<<(std::ostream &out, const std::vector<T> &v) {
42 out << "{";
43 for (auto &&elem : v) out << " " << elem;
44 out << " }";
45 return out;
46}
47
48template <typename T>
49std::ostream &operator<<(std::ostream &out, const std::list<T> &v) {
50 out << "{";
51 for (auto &&elem : v) out << " " << elem;
52 out << " }";
53 return out;
54}
55
56template <typename A, typename B>
57std::ostream &operator<<(std::ostream &out, const std::pair<A, B> &p) {
58 return out << p.first << ":" << p.second;
59}
60
61template <typename SeqCont1, typename SeqCont2>
62::testing::AssertionResult AssertSetEqual(const char *seq1_expr,
63 const char *seq2_expr,
64 const SeqCont1 &seq1,
65 const SeqCont2 &seq2) {
66 std::vector<typename SeqCont1::value_type> c1(seq1.begin(), seq1.end());
67 std::vector<typename SeqCont2::value_type> c2(seq2.begin(), seq2.end());
68 std::sort(c1.begin(), c1.end());
69 std::sort(c2.begin(), c2.end());
70
71 // Check for elements that are in the first range but not in the second.
72 std::vector<typename SeqCont2::value_type> c1_not_c2;
73 std::set_difference(c1.begin(), c1.end(), c2.begin(), c2.end(),
74 std::back_inserter(c1_not_c2));
75 if (c1_not_c2.size() > 0) {
76 auto result = ::testing::AssertionFailure();
77 result << seq1_expr << " had elements not in " << seq2_expr << ": ";
78 for (auto elem : c1_not_c2) result << elem << " ";
79 return result;
80 }
81
82 // Check for elements that are in the second range but not in the first.
83 std::vector<typename SeqCont2::value_type> c2_not_c1;
84 std::set_difference(c2.begin(), c2.end(), c1.begin(), c1.end(),
85 std::back_inserter(c2_not_c1));
86 if (c2_not_c1.size() > 0) {
87 auto result = ::testing::AssertionFailure();
88 result << seq2_expr << " had elements not in " << seq1_expr << ": ";
89 for (auto elem : c2_not_c1) result << elem << " ";
90 return result;
91 }
92
93 return ::testing::AssertionSuccess();
94}
95
96#define EXPECT_SETEQ(S1, S2) EXPECT_PRED_FORMAT2(AssertSetEqual, S1, S2)
97
98::testing::AssertionResult AssertLoaderSectionAvailable(
99 const char *loader_expr, const char *section_expr,
100 mysql_harness::Loader *loader, const std::string &section_name);
101
102#define EXPECT_SECTION_AVAILABLE(S, L) \
103 EXPECT_PRED_FORMAT2(AssertLoaderSectionAvailable, L, S)
104
105/**
106 * Just register logger with DIM for unit tests (unlike init_log(), which also
107 * initializes it)
108 */
110
111/**
112 * Register + init logger for unit tests
113 *
114 * Creates application ("main") logger, which will write all messages to the
115 * console. Almost all of our code relies on the fact of "main" logger being
116 * initialized, so it is necessary to provide one for unit tests. Also, some
117 * unit tests analyze log output, and expect that output to exist on stderr.
118 */
119void init_test_logger(const std::list<std::string> &additional_log_domains = {},
120 const std::string &log_folder = "",
121 const std::string &log_filename = "");
122
123#endif /* MYSQL_HARNESS_HELPERS_INCLUDED */
static Mysys_charset_loader * loader
Definition: charset.cc:185
Definition: loader.h:748
const char * p
Definition: ctype-mb.cc:1225
void init_test_logger(const std::list< std::string > &additional_log_domains={}, const std::string &log_folder="", const std::string &log_filename="")
Register + init logger for unit tests.
Definition: test_helpers.cc:63
void register_test_logger()
Just register logger with DIM for unit tests (unlike init_log(), which also initializes it)
Definition: test_helpers.cc:57
::testing::AssertionResult AssertLoaderSectionAvailable(const char *loader_expr, const char *section_expr, mysql_harness::Loader *loader, const std::string &section_name)
Definition: test_helpers.cc:32
std::ostream & operator<<(std::ostream &out, const std::vector< T > &v)
Definition: helpers.h:41
::testing::AssertionResult AssertSetEqual(const char *seq1_expr, const char *seq2_expr, const SeqCont1 &seq1, const SeqCont2 &seq2)
Definition: helpers.h:62
static const char * log_filename
Definition: myisamlog.cc:97
struct result result
Definition: result.h:34
Definition: result.h:30