BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bsls_fuzztestpreconditionexception.h
Go to the documentation of this file.
1/// @file bsls_fuzztestpreconditionexception.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bsls_fuzztestpreconditionexception.h -*-C++-*-
8#ifndef INCLUDED_BSLS_FUZZTESTPRECONDITIONEXCEPTION
9#define INCLUDED_BSLS_FUZZTESTPRECONDITIONEXCEPTION
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bsls_fuzztestpreconditionexception bsls_fuzztestpreconditionexception
15/// @brief Provide an exception type for handling failed preconditions.
16/// @addtogroup bsl
17/// @{
18/// @addtogroup bsls
19/// @{
20/// @addtogroup bsls_fuzztestpreconditionexception
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bsls_fuzztestpreconditionexception-purpose"> Purpose</a>
25/// * <a href="#bsls_fuzztestpreconditionexception-classes"> Classes </a>
26/// * <a href="#bsls_fuzztestpreconditionexception-description"> Description </a>
27/// * <a href="#bsls_fuzztestpreconditionexception-usage"> Usage </a>
28///
29/// # Purpose {#bsls_fuzztestpreconditionexception-purpose}
30/// Provide an exception type for handling failed preconditions.
31///
32/// # Classes {#bsls_fuzztestpreconditionexception-classes}
33///
34/// - bsls::FuzzTestPreconditionException: type describing a failed precondition
35///
36/// @see bsls_fuzztest
37///
38/// # Description {#bsls_fuzztestpreconditionexception-description}
39/// This component implements an exception class,
40/// `bsls::FuzzTestPreconditionException`, that provides a mechanism to convey
41/// context information from a failing precondition to a test handler. The
42/// context that is captured consists of the program source of the failing
43/// expression, the name of the file containing the assertion/review, the line
44/// number within that file where the asserted/reviewed expression may be found,
45/// and the level of the assertion/review that has failed.
46///
47/// ## Usage {#bsls_fuzztestpreconditionexception-usage}
48///
49///
50/// First we write a macro to act as a precondition testing `assert` facility
51/// that will throw an exception of type `bsls::FuzzTestPreconditionException`
52/// if the asserted expression fails. The thrown exception will capture the
53/// source-code of the expression, the filename and line number of the failing
54/// expression.
55/// @code
56/// #define TEST_PRECONDITION(EXPRESSION) \$
57/// if (!(EXPRESSION)) { \$
58/// throw bsls::FuzzTestPreconditionException(#EXPRESSION, __FILE__, \$
59/// __LINE__, "LEVEL", \$
60/// false); \$
61/// }
62/// @endcode
63/// Next we use the macro inside a try-block, so that we can catch the exception
64/// thrown if the tested expression fails.
65/// @code
66/// try {
67/// void *p = NULL;
68/// TEST_PRECONDITION(0 != p);
69/// }
70/// @endcode
71/// If the assertion fails, catch the exception and confirm that it correctly
72/// recorded the context of where the assertion failed.
73/// @code
74/// catch (const bsls::FuzzTestPreconditionException& exception) {
75/// assert(0 == strcmp("0 != p", exception.expression()));
76/// assert(0 == strcmp(__FILE__, exception.filename()));
77/// assert(9 == __LINE__ - exception.lineNumber());
78/// assert(0 == strcmp("LEVEL", exception.level()));
79/// assert(false == exception.isReview());
80/// }
81/// @endcode
82/// @}
83/** @} */
84/** @} */
85
86/** @addtogroup bsl
87 * @{
88 */
89/** @addtogroup bsls
90 * @{
91 */
92/** @addtogroup bsls_fuzztestpreconditionexception
93 * @{
94 */
95
96#include <bsls_assert.h>
98#include <bsls_keyword.h>
99#include <bsls_review.h>
100
101
102
103namespace bsls {
104
105 // ===================================
106 // class FuzzTestPreconditionException
107 // ===================================
108
109/// This class is an implementation detail of the `bsls` fuzz testing
110/// framework and should not be used directly in user code. It implements
111/// an immutable mechanism to communicate to a test-case handler information
112/// about the context of a precondition that fails.
113///
114/// See @ref bsls_fuzztestpreconditionexception
116
117 private:
118 // DATA
119 const char *d_expression; // expression that failed to assert as `true`
120 const char *d_filename; // name of file where the assert failed
121 const bool d_isReview; // flag indicating if the failure is a review
122 const char *d_level; // level of failed assertion or review
123 const int d_lineNumber; // line number in file where the assert failed
124
125 private:
126 // NOT IMPLEMENTED
129
130 public:
131 // CREATORS
132
133 /// Create a `FuzzTestPreconditionException` object with the specified
134 /// `expression`, `filename`, `lineNumber`, `level`, and `isReview`.
135 ///
136 /// \pre The behavior is undefined unless `0 < line` and all of `expression`,
137 /// `filename`, and `level` point to valid null-terminated character
138 /// strings that will remain unmodified for the lifetime of this object
139 /// (e.g., string literals).
142 const char *filename,
143 int lineNumber,
144 const char *level = "UNKNOWN",
145 const bool isReview = false);
146
147#ifdef BSLS_COMPILERFEATURES_SUPPORT_DEFAULTED_FUNCTIONS
148 // To avoid warnings about future incompatibility due to the deleted copy
149 // assignment operator we declare the copy constructor as implicitly
150 // generated. For consistency the destructor was also placed here and
151 // declared to be explicitly generated.
152
153 /// Create a `FuzzTestPreconditionException` object that is a copy of
154 /// the specified `original`, having the same values for the
155 /// `expression`, `filename`, `lineNumber`, `level`, and `isReview` attributes.
156 ///
157 /// \note Note that this trivial constructor's definition is
158 /// compiler generated.
160 const FuzzTestPreconditionException& original) = default;
161
162 /// Destroy this object.
163 /// \note Note that this trivial destructor's definition
164 /// is compiler generated.
166#endif
167
168 // ACCESSORS
169
170 /// Return a string containing the program source of the assertion that has
171 /// failed.
172 const char *expression() const;
173
174 /// Return a string containing the filename of the source file containing
175 /// the assertion that has failed.
176 const char *filename() const;
177
178 /// Return a string containing a representation of the level of assertion
179 /// or review macro that failed.
180 const char *level() const;
181
182 /// Return a flag indicating if the failure is a review.
183 bool isReview() const;
184
185 /// Return the number of the line within the file `filename` containing the
186 /// assertion that failed.
187 int lineNumber() const;
188};
189
190// ============================================================================
191// INLINE FUNCTION DEFINITIONS
192// ============================================================================
193
194 // ===================================
195 // class FuzzTestPreconditionException
196 // ===================================
197
198// CREATORS
200inline
202 const char *expression,
203 const char *filename,
204 const int lineNumber,
205 const char *level,
206 const bool isReview)
207: d_expression(expression)
208, d_filename(filename)
209, d_isReview(isReview)
210, d_level(level)
211, d_lineNumber(lineNumber)
212{
213}
214
215// ACCESSORS
216inline
218{
219 return d_expression;
220}
221
222inline
224{
225 return d_filename;
226}
227
228inline
230{
231 return d_level;
232}
233
234inline
236{
237 return d_isReview;
238}
239
240inline
242{
243 return d_lineNumber;
244}
245
246} // close package namespace
247
248
249#endif
250
251// ----------------------------------------------------------------------------
252// Copyright 2022 Bloomberg Finance L.P.
253//
254// Licensed under the Apache License, Version 2.0 (the "License");
255// you may not use this file except in compliance with the License.
256// You may obtain a copy of the License at
257//
258// http://www.apache.org/licenses/LICENSE-2.0
259//
260// Unless required by applicable law or agreed to in writing, software
261// distributed under the License is distributed on an "AS IS" BASIS,
262// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
263// See the License for the specific language governing permissions and
264// limitations under the License.
265// ----------------------------- END-OF-FILE ----------------------------------
266
267/** @} */
268/** @} */
269/** @} */
Definition bsls_fuzztestpreconditionexception.h:115
const char * filename() const
Definition bsls_fuzztestpreconditionexception.h:223
const char * expression() const
Definition bsls_fuzztestpreconditionexception.h:217
const char * level() const
Definition bsls_fuzztestpreconditionexception.h:229
bool isReview() const
Return a flag indicating if the failure is a review.
Definition bsls_fuzztestpreconditionexception.h:235
BSLS_KEYWORD_CONSTEXPR FuzzTestPreconditionException(const char *expression, const char *filename, int lineNumber, const char *level="UNKNOWN", const bool isReview=false)
Definition bsls_fuzztestpreconditionexception.h:201
int lineNumber() const
Definition bsls_fuzztestpreconditionexception.h:241
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
#define BSLS_KEYWORD_CONSTEXPR
Definition bsls_keyword.h:624
#define BSLS_KEYWORD_DELETED
Definition bsls_keyword.h:651
Definition bdlt_iso8601util.h:707