BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bdlt_fuzzutil.h
Go to the documentation of this file.
1/// @file bdlt_fuzzutil.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bdlt_fuzzutil.h -*-C++-*-
8#ifndef INCLUDED_BDLT_FUZZUTIL
9#define INCLUDED_BDLT_FUZZUTIL
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bdlt_fuzzutil bdlt_fuzzutil
15/// @brief Provide creation of `bdlt` data types from fuzz data.
16/// @addtogroup bdl
17/// @{
18/// @addtogroup bdlt
19/// @{
20/// @addtogroup bdlt_fuzzutil
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bdlt_fuzzutil-purpose"> Purpose</a>
25/// * <a href="#bdlt_fuzzutil-classes"> Classes </a>
26/// * <a href="#bdlt_fuzzutil-description"> Description </a>
27/// * <a href="#bdlt_fuzzutil-usage"> Usage </a>
28/// * <a href="#bdlt_fuzzutil-example-1-provide-bdlt-datetime-within-a-range"> Example 1: Provide bdlt::Datetime within a Range </a>
29///
30/// # Purpose {#bdlt_fuzzutil-purpose}
31/// Provide creation of `bdlt` data types from fuzz data.
32///
33/// # Classes {#bdlt_fuzzutil-classes}
34///
35/// - bdlt::FuzzUtil: functions to create `bdlt` data types from fuzz data
36///
37/// @see bslim_fuzzdataview, bslim_fuzzutil
38///
39/// # Description {#bdlt_fuzzutil-description}
40/// This component defines a struct, `bdlt::FuzzUtil`, which serves
41/// as a namespace for functions to create `bdlt` data types from supplied fuzz
42/// input data.
43///
44/// ## Usage {#bdlt_fuzzutil-usage}
45///
46///
47/// This section illustrates intended use of this component.
48///
49/// ### Example 1: Provide bdlt::Datetime within a Range {#bdlt_fuzzutil-example-1-provide-bdlt-datetime-within-a-range}
50///
51///
52/// The provided fuzz data is here represented by an array of bytes:
53/// @code
54/// const uint8_t data[] = {0x8A, 0x19, 0x0D, 0x44, 0x37, 0x0D,
55/// 0x38, 0x5E, 0x9B, 0xAA, 0xF3, 0xDA};
56/// @endcode
57/// First, we default construct a `bslim::FuzzDataView` object, `fdv`:
58/// @code
59/// bslim::FuzzDataView fdv(data, sizeof(data));
60///
61/// assert(12 == fdv.length());
62/// @endcode
63/// Next, we construct `Date` objects to represent the `begin` and `end` of the
64/// time interval in which we wish to construct our new `Date` from the fuzz
65/// data:
66/// @code
67/// bdlt::Date begin(1833, 5, 7);
68/// bdlt::Date end(1897, 4, 3);
69/// @endcode
70/// Finally, we create a `Date` object, `within`, by employing @ref bdlt_fuzzutil :
71/// @code
72/// bdlt::Date within = bdlt::FuzzUtil::consumeDateInRange(&fdv, begin, end);
73///
74/// assert(begin <= within);
75/// assert(within <= end);
76/// @endcode
77/// @}
78/** @} */
79/** @} */
80
81/** @addtogroup bdl
82 * @{
83 */
84/** @addtogroup bdlt
85 * @{
86 */
87/** @addtogroup bdlt_fuzzutil
88 * @{
89 */
90
91#include <bdlscm_version.h>
92
93#include <bdlt_date.h>
94
95#include <bslim_fuzzdataview.h>
96#include <bslim_fuzzutil.h>
97
98
99namespace bdlt {
100
101 // ===============
102 // struct FuzzUtil
103 // ===============
104
105/// This utility `struct` provides a namespace for a suite of functions that
106/// produce objects of date and time values from fuzz data.
107struct FuzzUtil {
108
109 private:
110 // PRIVATE CLASS METHODS
111
112 /// Return the first valid date represented by `Date` objects.
113 static Date firstValidDate();
114
115 /// Return the last valid date represented by `Date` objects.
116 static Date lastValidDate();
117
118 public:
119 // CLASS METHODS
120
121 /// Return a `Date` based on the next bytes from the specified
122 /// `fuzzDataView`, and update `fuzzDataView` to reflect the bytes
123 /// consumed.
124 static bdlt::Date consumeDate(bslim::FuzzDataView *fuzzDataView);
125
126 /// Return a `Date` between the specified `begin` and `end` dates based
127 /// on the next bytes from the specified `fuzzDataView`, and update
128 /// `fuzzDataView` to reflect the bytes consumed. The behavior is
129 /// undefined unless `begin <= end`.
131 const bdlt::Date& begin,
132 const bdlt::Date& end);
133};
134
135// ============================================================================
136// INLINE DEFINITIONS
137// ============================================================================
138
139 // ---------------
140 // struct FuzzUtil
141 // ---------------
142
143// PRIVATE CLASS METHODS
144inline
145Date FuzzUtil::firstValidDate()
146{
147 return Date();
148}
149
150inline
151Date FuzzUtil::lastValidDate()
152{
153 return Date(9999, 12, 31);
154}
155
156// CLASS METHODS
157inline
159{
160 return consumeDateInRange(fuzzDataView, firstValidDate(), lastValidDate());
161}
162
163inline
165 const bdlt::Date& begin,
166 const bdlt::Date& end)
167{
168 int beginDays = begin - firstValidDate();
169 int endDays = end - firstValidDate();
170
171 int dateBetween = bslim::FuzzUtil::consumeNumberInRange<int>(
172 fuzzDataView, beginDays, endDays);
173
174 return firstValidDate() + dateBetween;
175}
176
177} // close package namespace
178
179
180#endif
181
182// ----------------------------------------------------------------------------
183// Copyright 2021 Bloomberg Finance L.P.
184//
185// Licensed under the Apache License, Version 2.0 (the "License");
186// you may not use this file except in compliance with the License.
187// You may obtain a copy of the License at
188//
189// http://www.apache.org/licenses/LICENSE-2.0
190//
191// Unless required by applicable law or agreed to in writing, software
192// distributed under the License is distributed on an "AS IS" BASIS,
193// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
194// See the License for the specific language governing permissions and
195// limitations under the License.
196// ----------------------------- END-OF-FILE ----------------------------------
197
198/** @} */
199/** @} */
200/** @} */
Definition bdlt_date.h:294
Definition bslim_fuzzdataview.h:130
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition bbldc_basicisma30360.h:112
Definition bdlt_fuzzutil.h:107
static bdlt::Date consumeDate(bslim::FuzzDataView *fuzzDataView)
Definition bdlt_fuzzutil.h:158
static bdlt::Date consumeDateInRange(bslim::FuzzDataView *fuzzDataView, const bdlt::Date &begin, const bdlt::Date &end)
Definition bdlt_fuzzutil.h:164