MySQL 9.6.0
Source Code Documentation
unittest_assertions.h
Go to the documentation of this file.
1// Copyright (c) 2025, Oracle and/or its affiliates.
2//
3// This program is free software; you can redistribute it and/or modify
4// it under the terms of the GNU General Public License, version 2.0,
5// as published by the Free Software Foundation.
6//
7// This program is designed to work with certain software (including
8// but not limited to OpenSSL) that is licensed under separate terms,
9// as designated in a particular file or component or in included license
10// documentation. The authors of MySQL hereby grant you an additional
11// permission to link the program and your derivative works with the
12// separately licensed software that they have either included with
13// the program or referenced in the documentation.
14//
15// This program is distributed in the hope that it will be useful,
16// but WITHOUT ANY WARRANTY; without even the implied warranty of
17// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18// GNU General Public License, version 2.0, for more details.
19//
20// You should have received a copy of the GNU General Public License
21// along with this program; if not, write to the Free Software
22// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23
24#ifndef MYSQL_DEBUGGING_UNITTEST_ASSERTIONS_H
25#define MYSQL_DEBUGGING_UNITTEST_ASSERTIONS_H
26
27/// @file
28/// Experimental API header
29
30#include <gtest/gtest.h> // ASSERT_EQ
31#include "mysql/debugging/my_scoped_trace.h" // MY_SCOPED_TRACE
32#include "mysql/utils/return_status.h" // Return_status
33
34/// @addtogroup GroupLibsMysqlDebugging
35/// @{
36
37namespace mysql::debugging {
38
39// Use macros, because ASSERT_EQ is a macro that stringifies its argument for
40// the error message.
41// NOLINTBEGIN(cppcoreguidelines-macro-usage)
42
43/// Equivalent to ASSERT_EQ(EXPRESSION, Return_status::ok)
44#define ASSERT_OK(EXPRESSION) \
45 ASSERT_EQ((EXPRESSION), mysql::utils::Return_status::ok)
46
47/// Equivalent to ASSERT_EQ(EXPRESSION, Return_status::error)
48#define ASSERT_ERROR(EXPRESSION) \
49 ASSERT_EQ((EXPRESSION), mysql::utils::Return_status::error)
50
51/// Assert that the result type of the given expression is void, and invoke it.
52#define ASSERT_VOID(EXPRESSION) \
53 do { \
54 static_assert(std::same_as<decltype(EXPRESSION), void>); \
55 (EXPRESSION); \
56 } while (false)
57
58// NOLINTEND(cppcoreguidelines-macro-usage)
59
60/// Assert that both (left==right) and !(left!=right) have the same truth values
61/// as `equal`.
62///
63/// @param left Left-hand-side operand.
64///
65/// @param right Right-hand-side operand.
66///
67/// @param equal If true (the default), left and right are expected to be equal;
68/// otherwise they are expected to be different.
69void test_eq_one_way(const auto &left, const auto &right, bool equal = true) {
70 ASSERT_EQ(left == right, equal);
71 ASSERT_EQ(left != right, !equal);
72}
73
74/// Assert that (left==right), (right==left), !(left!=right), and !(right!=left)
75/// all have the same truth values as `equal`.
76///
77/// @param left Left-hand-side operand.
78///
79/// @param right Right-hand-side operand.
80///
81/// @param equal If true (the default), left and right are expected to be equal;
82/// otherwise they are expected to be different.
83///
84/// (Despite the parameter names, this will also test the case where left and
85/// right are swapped).
86void test_eq(const auto &left, const auto &right, bool equal = true) {
87 {
88 MY_SCOPED_TRACE("left OP right");
90 }
91 {
92 MY_SCOPED_TRACE("right OP left");
94 }
95}
96
97/// For all 7 comparison operators, assert that (left OP right) == (cmp OP 0).
98///
99/// @param left Left-hand-side operand.
100///
101/// @param right Right-hand-side operand.
102///
103/// @param cmp Expected outcome. This must be of a type that can be compared
104/// with `0`: typically either `int` or `std::strong_ordering`.
105void test_cmp_one_way(const auto &left, const auto &right, auto cmp) {
106 MY_SCOPED_TRACE(cmp < 0 ? "lt" : cmp == 0 ? "eq" : "gt");
108 ASSERT_EQ(left < right, cmp < 0);
109 ASSERT_EQ(left > right, cmp > 0);
110 ASSERT_EQ(left <= right, cmp <= 0);
111 ASSERT_EQ(left >= right, cmp >= 0);
112 ASSERT_EQ(left <=> right, cmp <=> 0);
113}
114
115/// For all 7 comparison operators, assert that (left OP right) == (cmp OP 0),
116/// and that (right OP left) == (0 OP cmp).
117///
118/// @param left Left-hand-side operand.
119///
120/// @param right Right-hand-side operand.
121///
122/// @param cmp Expected outcome.
123///
124/// (Despite the parameter names, this will also test the case where left and
125/// right are swapped and cmp reversed).
126void test_cmp(const auto &left, const auto &right, std::strong_ordering cmp) {
127 {
128 MY_SCOPED_TRACE("left OP right");
130 }
131 {
132 MY_SCOPED_TRACE("right OP left");
133 test_cmp_one_way(right, left, cmp < 0 ? 1 : cmp == 0 ? 0 : -1);
134 }
135}
136
137/// For all 7 comparison operators, assert that (left OP right) == (cmp OP 0),
138/// and that (right OP left) == (0 OP cmp). This overload is for when cmp is of
139/// type int.
140///
141/// @param left Left-hand-side operand.
142///
143/// @param right Right-hand-side operand.
144///
145/// @param cmp Expected outcome: -1 if left<right, 0 if left==right, and 1 if
146/// left>right.
147///
148/// (Despite the parameter names, this will also test the case where left and
149/// right are swapped and cmp reversed).
150void test_cmp(const auto &left, const auto &right, int cmp) {
151 test_cmp(left, right, cmp <=> 0);
152}
153
154} // namespace mysql::debugging
155
156// addtogroup GroupLibsMysqlDebugging
157/// @}
158
159#endif // ifndef MYSQL_DEBUGGING_UNITTEST_ASSERTIONS_H
static int cmp(Bigint *a, Bigint *b)
Definition: dtoa.cc:1064
#define MY_SCOPED_TRACE(...)
"Thread-safe", multi-argument replacement for SCOPED_TRACE.
Definition: my_scoped_trace.h:61
static bool equal(const Item *i1, const Item *i2, const Field *f2)
Definition: sql_select.cc:3935
Experimental API header.
void right(std::string *to_trim)
Definition: trim.h:41
void left(std::string *to_trim)
Definition: trim.h:35
Definition: object_lifetime_tracker.h:37
void test_eq_one_way(const auto &left, const auto &right, bool equal=true)
Assert that both (left==right) and !(left!=right) have the same truth values as equal.
Definition: unittest_assertions.h:69
void test_eq(const auto &left, const auto &right, bool equal=true)
Assert that (left==right), (right==left), !(left!=right), and !(right!=left) all have the same truth ...
Definition: unittest_assertions.h:86
void test_cmp_one_way(const auto &left, const auto &right, auto cmp)
For all 7 comparison operators, assert that (left OP right) == (cmp OP 0).
Definition: unittest_assertions.h:105
void test_cmp(const auto &left, const auto &right, std::strong_ordering cmp)
For all 7 comparison operators, assert that (left OP right) == (cmp OP 0), and that (right OP left) =...
Definition: unittest_assertions.h:126
Experimental API header.