BDE 4.14.0 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, the line number
44/// within that file where the asserted expression may be found, and the level
45/// of the assertion 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/// bsls::AssertViolation violation(#EXPRESSION, \$
59/// __FILE__, \$
60/// __LINE__, \$
61/// "LEVEL"); \$
62/// throw bsls::FuzzTestPreconditionException(violation); \$
63/// }
64/// @endcode
65/// Next we use the macro inside a try-block, so that we can catch the exception
66/// thrown if the tested expression fails.
67/// @code
68/// try {
69/// void *p = NULL;
70/// TEST_PRECONDITION(0 != p);
71/// }
72/// @endcode
73/// If the assertion fails, catch the exception and confirm that it correctly
74/// recorded the context of where the assertion failed.
75/// @code
76/// catch (const bsls::FuzzTestPreconditionException& exception) {
77/// assert(0 == strcmp("0 != p",
78/// exception.assertViolation().comment()));
79/// assert(0 == strcmp(__FILE__,
80/// exception.assertViolation().fileName()));
81/// assert(11 == __LINE__ - exception.assertViolation().lineNumber());
82/// assert(0 == strcmp("LEVEL",
83/// exception.assertViolation().assertLevel()));
84/// }
85/// @endcode
86/// @}
87/** @} */
88/** @} */
89
90/** @addtogroup bsl
91 * @{
92 */
93/** @addtogroup bsls
94 * @{
95 */
96/** @addtogroup bsls_fuzztestpreconditionexception
97 * @{
98 */
99
100#include <bsls_assert.h>
102#include <bsls_keyword.h>
103
104
105
106namespace bsls {
107
108 // ===================================
109 // class FuzzTestPreconditionException
110 // ===================================
111
112/// This class is an implementation detail of the `bsls` fuzz testing
113/// framework and should not be used directly in user code. It implements
114/// an immutable mechanism to communicate to a test-case handler information
115/// about the context of a precondition that fails.
116///
117/// See @ref bsls_fuzztestpreconditionexception
119
120 private:
121 // DATA
122 const AssertViolation d_assertViolation; // from a failed precondition
123 // check
124
125 private:
126 // NOT IMPLEMENTED
129
130 public:
131 // CREATORS
132
133 /// Create a `FuzzTestPreconditionException` object with the specified
134 /// `assertViolation`.
137
138#ifdef BSLS_COMPILERFEATURES_SUPPORT_DEFAULTED_FUNCTIONS
139 // To avoid warnings about future incompatibility due to the deleted copy
140 // assignment operator we declare the copy constructor as implicitly
141 // generated. For consistency the destructor was also placed here and
142 // declared to be explicitly generated.
143
144 /// Create a `FuzzTestPreconditionException` object that is a copy of
145 /// the specified `original`, having the same value for the
146 /// `assertViolation` attribute. Note that this trivial constructor's
147 /// definition is compiler generated.
149 const FuzzTestPreconditionException& original) = default;
150
151 /// Destroy this object. Note that this trivial destructor's definition
152 /// is compiler generated.
154#endif
155
156 // ACCESSORS
157
158 /// Return an `AssertViolation` containing the details of the
159 /// precondition that has failed.
160 const AssertViolation& assertViolation() const;
161};
162
163// ============================================================================
164// INLINE FUNCTION DEFINITIONS
165// ============================================================================
166
167 // ===================================
168 // class FuzzTestPreconditionException
169 // ===================================
170
171// CREATORS
173inline
175 const AssertViolation& assertViolation)
176: d_assertViolation(assertViolation)
177{
178}
179
180// ACCESSORS
181inline
183{
184 return d_assertViolation;
185}
186
187} // close package namespace
188
189
190#endif
191
192// ----------------------------------------------------------------------------
193// Copyright 2022 Bloomberg Finance L.P.
194//
195// Licensed under the Apache License, Version 2.0 (the "License");
196// you may not use this file except in compliance with the License.
197// You may obtain a copy of the License at
198//
199// http://www.apache.org/licenses/LICENSE-2.0
200//
201// Unless required by applicable law or agreed to in writing, software
202// distributed under the License is distributed on an "AS IS" BASIS,
203// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
204// See the License for the specific language governing permissions and
205// limitations under the License.
206// ----------------------------- END-OF-FILE ----------------------------------
207
208/** @} */
209/** @} */
210/** @} */
Definition bsls_assert.h:1929
Definition bsls_fuzztestpreconditionexception.h:118
BSLS_KEYWORD_CONSTEXPR FuzzTestPreconditionException(const AssertViolation &assertViolation)
Definition bsls_fuzztestpreconditionexception.h:174
const AssertViolation & assertViolation() const
Definition bsls_fuzztestpreconditionexception.h:182
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
#define BSLS_KEYWORD_CONSTEXPR
Definition bsls_keyword.h:588
#define BSLS_KEYWORD_DELETED
Definition bsls_keyword.h:609
Definition bdlt_iso8601util.h:691