44 requires std::is_integral_v<T>
45 class IntegerGenerator :
public IGenerator<T> {
48 : params_(std::move(params)) {
50 params_.min_value.value_or(std::numeric_limits<T>::min());
52 params_.max_value.value_or(std::numeric_limits<T>::max());
53 if (min_val > max_val) {
54 throw std::invalid_argument(
55 "Cannot have max_value < min_value");
59 std::optional<BasicGenerator<T>> as_basic()
const override {
61 params_.min_value.value_or(std::numeric_limits<T>::min());
63 params_.max_value.value_or(std::numeric_limits<T>::max());
64 return BasicGenerator<T>{{{
"type",
"integer"},
65 {
"min_value", min_val},
66 {
"max_value", max_val}},
67 &default_parse_raw<T>};
71 IntegersParams<T> params_;
76 requires std::is_floating_point_v<T>
79 explicit FloatGenerator(FloatsParams<T> params = {})
80 : params_(std::move(params)) {
81 bool has_min = params_.min_value.has_value();
82 bool has_max = params_.max_value.has_value();
83 bool nan = params_.allow_nan.value_or(!has_min && !has_max);
84 bool inf = params_.allow_infinity.value_or(!has_min || !has_max);
85 if (nan && (has_min || has_max)) {
86 throw std::invalid_argument(
87 "Cannot have allow_nan=true with min_value or max_value");
89 if (has_min && has_max && *params_.min_value > *params_.max_value) {
90 throw std::invalid_argument(
91 "Cannot have max_value < min_value");
93 if (inf && has_min && has_max) {
94 throw std::invalid_argument(
95 "Cannot have allow_infinity=true with both min_value and "
100 std::optional<BasicGenerator<T>> as_basic()
const override {
101 constexpr int width =
sizeof(T) * 8;
102 bool has_min = params_.min_value.has_value();
103 bool has_max = params_.max_value.has_value();
104 bool nan = params_.allow_nan.value_or(!has_min && !has_max);
105 bool inf = params_.allow_infinity.value_or(!has_min || !has_max);
107 hegel::internal::json::json schema = {
109 {
"exclude_min", params_.exclude_min},
110 {
"exclude_max", params_.exclude_max},
112 {
"allow_infinity", inf},
114 if (params_.min_value)
115 schema[
"min_value"] = *params_.min_value;
116 if (params_.max_value)
117 schema[
"max_value"] = *params_.max_value;
118 return BasicGenerator<T>{std::move(schema), &default_parse_raw<T>};
122 FloatsParams<T> params_;
144 template <
typename T =
int64_t>
145 requires std::is_integral_v<T>
147 return Generator<T>(
new IntegerGenerator<T>(std::move(params)));
166 template <
typename T =
double>
167 requires std::is_floating_point_v<T>
169 return Generator<T>(
new FloatGenerator<T>(std::move(params)));
The base class of all generators.
Definition core.h:157
Hegel generators.
Definition core.h:16
Generator< T > integers(IntegersParams< T > params={})
Generate random integers. For a given integral type T, produces values in the range [std::numeric_lim...
Definition numeric.h:146
Generator< T > floats(FloatsParams< T > params={})
Generate random floating point numbers.
Definition numeric.h:168
Parameters for floats() generator.
Definition numeric.h:28
bool exclude_min
If true, exclude min_value (exclusive bound).
Definition numeric.h:31
std::optional< T > min_value
Minimum value. Default: no minimum.
Definition numeric.h:29
std::optional< T > max_value
Maximum value. Default: no maximum.
Definition numeric.h:30
std::optional< bool > allow_nan
Allow NaN. Default: true if unbounded.
Definition numeric.h:36
bool exclude_max
If true, exclude max_value (exclusive bound).
Definition numeric.h:33
std::optional< bool > allow_infinity
Allow infinity. Default: true if unbounded.
Definition numeric.h:38
Parameters for integers() generator.
Definition numeric.h:17
std::optional< T > max_value
Maximum value (inclusive). Default: type maximum.
Definition numeric.h:21
std::optional< T > min_value
Minimum value (inclusive). Default: type minimum.
Definition numeric.h:19