Hegel 0.18.3
Property-based testing for C++
Loading...
Searching...
No Matches
temporal.h
1#pragma once
2
3#include <algorithm>
4#include <chrono>
5#include <cstdint>
6#include <optional>
7#include <stdexcept>
8#include <string>
9#include <type_traits>
10#include <utility>
11#include <vector>
12
13#include "hegel/core.h"
14#include "hegel/generators/temporal_types.h"
15#include "hegel/generators/zone_source.h"
16
18
19 // =============================================================================
20 // Parameter structs
21 // =============================================================================
22
26 struct DatesParams {
28 std::optional<Date> min_value;
30 std::optional<Date> max_value;
34 std::optional<std::vector<Weekday>> weekdays;
38 std::optional<std::vector<Month>> months;
39 };
40
44 struct TimesParams {
46 std::optional<Time> min_value;
48 std::optional<Time> max_value;
51 };
52
59 std::optional<DateTime> min_value;
62 std::optional<DateTime> max_value;
66 };
67
74 template <typename TimePoint = Instant> struct InstantsParams {
77 std::optional<TimePoint> min_value;
80 std::optional<TimePoint> max_value;
81 };
82
86 template <> struct InstantsParams<Instant> {
88 std::optional<Instant> min_value;
91 std::optional<Instant> max_value;
95 };
96
103 std::optional<UtcOffset> min_value;
106 std::optional<UtcOffset> max_value;
111 bool allow_unknown = false;
112 };
113
117 struct ZonesParams {
121 std::optional<std::vector<std::string>> names;
123 bool allow_links = true;
126 bool allow_legacy = false;
129 bool allow_unknown = false;
135 bool allow_opaque = false;
139 std::optional<ZoneSource> source;
140 };
141
161 std::optional<AnyGenerator<TimeZone>> zones;
169 std::optional<ZoneSource> source;
171 std::optional<DateTime> min_value;
173 std::optional<DateTime> max_value;
175 std::optional<Instant> min_instant;
177 std::optional<Instant> max_instant;
185 bool allow_imaginary = true;
188 };
189
193 struct UnitBounds {
194 int64_t min_value;
195 int64_t max_value;
196 };
197
205 struct SpansParams {
206 std::optional<UnitBounds> years;
207 std::optional<UnitBounds> months;
208 std::optional<UnitBounds> weeks;
209 std::optional<UnitBounds> days;
210 std::optional<UnitBounds> hours;
211 std::optional<UnitBounds> minutes;
212 std::optional<UnitBounds> seconds;
213 std::optional<UnitBounds> milliseconds;
214 std::optional<UnitBounds> microseconds;
215 std::optional<UnitBounds> nanoseconds;
218 };
219
226 template <typename Duration = SignedDuration> struct DurationsParams {
229 std::optional<Duration> min_value;
232 std::optional<Duration> max_value;
233 };
234
238 template <> struct DurationsParams<SignedDuration> {
241 std::optional<SignedDuration> min_value;
244 std::optional<SignedDuration> max_value;
247 };
248
250 namespace detail {
251
252 template <typename T> struct is_chrono_duration : std::false_type {};
253 template <typename Rep, typename Period>
254 struct is_chrono_duration<std::chrono::duration<Rep, Period>>
255 : std::true_type {};
256
257 // The engine precision of a chrono period, by its length in
258 // nanoseconds; only the standard units from nanoseconds to hours
259 // have one.
260 template <int64_t UnitNanos> struct unit_precision {
261 static constexpr bool known = false;
262 static constexpr Precision value = Precision::Nanoseconds;
263 };
264 template <> struct unit_precision<1> {
265 static constexpr bool known = true;
266 static constexpr Precision value = Precision::Nanoseconds;
267 };
268 template <> struct unit_precision<1'000> {
269 static constexpr bool known = true;
270 static constexpr Precision value = Precision::Microseconds;
271 };
272 template <> struct unit_precision<1'000'000> {
273 static constexpr bool known = true;
274 static constexpr Precision value = Precision::Milliseconds;
275 };
276 template <> struct unit_precision<1'000'000'000> {
277 static constexpr bool known = true;
278 static constexpr Precision value = Precision::Seconds;
279 };
280 template <> struct unit_precision<60'000'000'000> {
281 static constexpr bool known = true;
282 static constexpr Precision value = Precision::Minutes;
283 };
284 template <> struct unit_precision<3'600'000'000'000> {
285 static constexpr bool known = true;
286 static constexpr Precision value = Precision::Hours;
287 };
288
289 inline constexpr Instant lowest_instant{MIN_INSTANT, 0};
290 inline constexpr Instant highest_instant{MAX_INSTANT, 999'999'999};
291
292 // The engine precision of an instant draw for a time point type:
293 // the finest one that is a whole number of the type's ticks, so
294 // that the type holds every instant at it. `known` is false for
295 // a type that does not convert to Instant, and for a tick that
296 // does not divide an hour.
297 template <typename TimePoint, bool = holds_instants<TimePoint>::value>
298 struct time_point_precision {
299 static constexpr bool known = false;
300 static constexpr Precision value = Precision::Nanoseconds;
301 };
302 template <typename TimePoint>
303 struct time_point_precision<TimePoint, true> {
304 static constexpr int64_t tick =
305 period_nanos<typename TimePoint::period>::value;
306 static constexpr bool known = 3'600'000'000'000 % tick == 0;
307 static constexpr Precision value =
308 1 % tick == 0 ? Precision::Nanoseconds
309 : 1'000 % tick == 0 ? Precision::Microseconds
310 : 1'000'000 % tick == 0 ? Precision::Milliseconds
311 : 1'000'000'000 % tick == 0 ? Precision::Seconds
312 : 60'000'000'000 % tick == 0 ? Precision::Minutes
313 : Precision::Hours;
314 };
315
316 // The Instant at a time since the epoch, inside the engine's range
317 // or not; nullopt when its seconds do not fit in int64_t.
318 template <typename Rep, typename Period>
319 std::optional<Instant>
320 instant_at(std::chrono::duration<Rep, Period> since_epoch) {
321 std::optional<std::pair<int64_t, int64_t>> parts =
322 parts_from_duration(since_epoch, /*floor=*/true);
323 if (!parts) {
324 return std::nullopt;
325 }
326 return Instant{parts->first, static_cast<uint32_t>(parts->second)};
327 }
328
329 } // namespace detail
331
353 Generator<Date> dates(const DatesParams& params = {});
354
367
381
395
426
482
501
503 namespace detail {
504
506 engine_instants(const InstantsParams<Instant>& params);
507
509 signed_durations(const DurationsParams<SignedDuration>& params);
510
511 template <typename TimePoint>
512 class TimePointsGenerator
513 : public generators::detail::ValuePrinted<TimePoint> {
514 public:
515 explicit TimePointsGenerator(Generator<Instant> instants)
516 : instants_(std::move(instants)) {}
517
518 TimePoint do_draw(const TestCase& tc) const override {
519 return *instants_.do_draw(tc)
520 .template to_chrono<typename TimePoint::duration>();
521 }
522
523 uint64_t label() const override { return instants_.label(); }
524
525 private:
526 Generator<Instant> instants_;
527 };
528
529 template <typename TimePoint>
530 Generator<TimePoint>
531 time_points(const InstantsParams<TimePoint>& params) {
532 using Duration = typename TimePoint::duration;
533 InstantsParams<Instant> inner;
534 inner.precision = time_point_precision<TimePoint>::value;
535 if (params.min_value.has_value()) {
536 inner.min_value =
537 instant_at(params.min_value->time_since_epoch());
538 if (!inner.min_value.has_value()) {
539 throw std::invalid_argument(
540 "min_value lies outside the engine's instant range");
541 }
542 } else {
543 inner.min_value = std::max(
544 instant_at(Duration::min()).value_or(lowest_instant),
545 lowest_instant);
546 }
547 if (params.max_value.has_value()) {
548 inner.max_value =
549 instant_at(params.max_value->time_since_epoch());
550 if (!inner.max_value.has_value()) {
551 throw std::invalid_argument(
552 "max_value lies outside the engine's instant range");
553 }
554 } else {
555 inner.max_value = std::min(
556 instant_at(Duration::max()).value_or(highest_instant),
557 highest_instant);
558 }
559 return Generator<TimePoint>(
560 new TimePointsGenerator<TimePoint>(engine_instants(inner)));
561 }
562
563 } // namespace detail
565
616 template <typename TimePoint = Instant>
618 if constexpr (std::is_same_v<TimePoint, Instant>) {
619 return detail::engine_instants(params);
620 } else {
621 static_assert(detail::is_system_time_point<TimePoint>::value,
622 "instants<TimePoint>() requires Instant or a "
623 "std::chrono::time_point of "
624 "std::chrono::system_clock");
625 static_assert(detail::time_point_precision<TimePoint>::known,
626 "instants<TimePoint>() requires a duration with an "
627 "integer count of at most 64 bits and a tick that "
628 "is a whole number of nanoseconds and divides a "
629 "second, or a whole number of seconds that divides "
630 "an hour; for another duration draw "
631 "std::chrono::hours or finer and convert");
632 return detail::time_points(params);
633 }
634 }
635
667 template <typename Duration = SignedDuration>
669 if constexpr (std::is_same_v<Duration, SignedDuration>) {
670 return detail::signed_durations(params);
671 } else {
672 static_assert(detail::is_chrono_duration<Duration>::value,
673 "durations<Duration>() requires SignedDuration or a "
674 "std::chrono::duration");
675 constexpr int64_t unit_nanos =
676 detail::period_nanos<typename Duration::period>::value;
677 static_assert(detail::unit_precision<unit_nanos>::known,
678 "durations<Duration>() supports the periods from "
679 "nanoseconds to hours; map durations() for "
680 "another period");
681 DurationsParams<SignedDuration> inner;
682 inner.precision = detail::unit_precision<unit_nanos>::value;
683 if (params.min_value.has_value()) {
684 inner.min_value =
685 SignedDuration::from_chrono(*params.min_value);
686 if (!inner.min_value.has_value()) {
687 throw std::invalid_argument(
688 "min_value lies outside the engine's duration range");
689 }
690 } else {
691 inner.min_value = SignedDuration::from_chrono(Duration::min());
692 }
693 if (params.max_value.has_value()) {
694 inner.max_value =
695 SignedDuration::from_chrono(*params.max_value);
696 if (!inner.max_value.has_value()) {
697 throw std::invalid_argument(
698 "max_value lies outside the engine's duration range");
699 }
700 } else {
701 inner.max_value = SignedDuration::from_chrono(Duration::max());
702 }
703 return detail::signed_durations(inner).map(
704 [](const SignedDuration& d) {
705 return *d.template to_chrono<Duration>();
706 });
707 }
708 }
709
710} // namespace hegel::generators::temporal
Handle to the currently-executing test case.
Definition test_case.h:46
A printable generator: TestCase::draw() accepts it.
Definition core.h:470
Date and time generators, and the plain values they draw.
Definition temporal.h:17
Generator< Span > spans(SpansParams params={})
Generate Span values.
SpanSign
How the signs of a span's units are drawn.
Definition temporal_types.h:772
@ Uniform
One sign for every unit.
Definition temporal_types.h:773
Generator< TimePoint > instants(InstantsParams< TimePoint > params={})
Generate Instant values, or std::chrono::system_clock time points when TimePoint is one.
Definition temporal.h:617
constexpr int64_t MIN_INSTANT
The first second of MIN_YEAR, the lowest second of an Instant.
Definition temporal_types.h:74
Generator< UtcOffset > offsets(OffsetsParams params={})
Generate UtcOffset values.
constexpr int64_t MAX_INSTANT
The last second of MAX_YEAR, the highest second of an Instant.
Definition temporal_types.h:76
Generator< ZonedDateTime > zoned_datetimes(const ZonedDatetimesParams &params={})
Generate ZonedDateTime values: a zone, a wall-clock time in it and, when the clocks repeated that tim...
Ambiguous
Definition temporal_types.h:764
@ Both
Definition temporal_types.h:765
Granularity
The values a UTC offset draw restricts itself to.
Definition temporal_types.h:728
@ Seconds
Whole seconds.
Definition temporal_types.h:732
Precision
Definition temporal_types.h:718
@ Nanoseconds
Whole nanoseconds.
Definition temporal_types.h:719
Generator< TimeZone > zones(const ZonesParams &params={})
Generate TimeZone values.
Generator< Duration > durations(DurationsParams< Duration > params={})
Generate SignedDuration values, or std::chrono::duration values when Duration is one.
Definition temporal.h:668
Generator< std::string > datetimes()
Generate naive ISO 8601 datetime strings (YYYY-MM-DDTHH:MM:SS[.ffffff]) from 0001-01-01 to 9999-12-31...
Generator< std::string > times()
Generate ISO 8601 time strings (HH:MM:SS[.ffffff], the fraction present iff the microsecond is nonzer...
Generator< std::string > dates()
Generate ISO 8601 date strings (YYYY-MM-DD) from 0001-01-01 to 9999-12-31.
Parameters for dates() generator.
Definition temporal.h:26
std::optional< Date > min_value
Minimum date (inclusive). Default: January 1 of MIN_YEAR.
Definition temporal.h:28
std::optional< std::vector< Month > > months
Definition temporal.h:38
std::optional< Date > max_value
Maximum date (inclusive). Default: December 31 of MAX_YEAR.
Definition temporal.h:30
std::optional< std::vector< Weekday > > weekdays
Definition temporal.h:34
Parameters for datetimes() generator.
Definition temporal.h:56
std::optional< DateTime > min_value
Definition temporal.h:59
Precision precision
Definition temporal.h:65
std::optional< DateTime > max_value
Definition temporal.h:62
Precision precision
The drawn duration is a whole number of these units.
Definition temporal.h:246
std::optional< SignedDuration > max_value
Definition temporal.h:244
std::optional< SignedDuration > min_value
Definition temporal.h:241
Parameters for durations() generator over a std::chrono::duration.
Definition temporal.h:226
std::optional< Duration > min_value
Definition temporal.h:229
std::optional< Duration > max_value
Definition temporal.h:232
An instant.
Definition temporal_types.h:288
std::optional< Instant > max_value
Definition temporal.h:91
std::optional< Instant > min_value
Minimum instant (inclusive). Default: MIN_INSTANT.
Definition temporal.h:88
Parameters for instants() generator over a std::chrono::system_clock time point.
Definition temporal.h:74
std::optional< TimePoint > min_value
Definition temporal.h:77
std::optional< TimePoint > max_value
Definition temporal.h:80
Parameters for offsets() generator.
Definition temporal.h:100
bool allow_unknown
Definition temporal.h:111
std::optional< UtcOffset > max_value
Definition temporal.h:106
Granularity granularity
The drawn offset is a whole number of these units.
Definition temporal.h:108
std::optional< UtcOffset > min_value
Definition temporal.h:103
A signed duration: seconds plus nanoseconds of the same sign, the latter within ±999999999,...
Definition temporal_types.h:420
static std::optional< SignedDuration > from_chrono(std::chrono::duration< Rep, Period > duration)
The SignedDuration of a std::chrono::duration, or std::nullopt when its seconds do not fit in int64_t...
Definition temporal_types.h:447
Parameters for spans() generator.
Definition temporal.h:205
std::optional< UnitBounds > nanoseconds
Definition temporal.h:215
std::optional< UnitBounds > milliseconds
Bounds on milliseconds.
Definition temporal.h:213
std::optional< UnitBounds > hours
Bounds on hours.
Definition temporal.h:210
std::optional< UnitBounds > seconds
Bounds on seconds.
Definition temporal.h:212
std::optional< UnitBounds > minutes
Bounds on minutes.
Definition temporal.h:211
std::optional< UnitBounds > microseconds
Bounds on microseconds.
Definition temporal.h:214
std::optional< UnitBounds > days
Bounds on days.
Definition temporal.h:209
std::optional< UnitBounds > weeks
Bounds on weeks.
Definition temporal.h:208
std::optional< UnitBounds > years
Bounds on years.
Definition temporal.h:206
std::optional< UnitBounds > months
Bounds on months.
Definition temporal.h:207
SpanSign sign
How the units' signs are drawn.
Definition temporal.h:217
Parameters for times() generator.
Definition temporal.h:44
std::optional< Time > max_value
Maximum time (inclusive). Default: 23:59:59.999999999.
Definition temporal.h:48
std::optional< Time > min_value
Minimum time (inclusive). Default: midnight.
Definition temporal.h:46
Precision precision
The drawn time is a whole number of these units since midnight.
Definition temporal.h:50
Inclusive bounds on one unit of a span, for SpansParams.
Definition temporal.h:193
int64_t min_value
Minimum value (inclusive).
Definition temporal.h:194
int64_t max_value
Maximum value (inclusive).
Definition temporal.h:195
Parameters for zoned_datetimes() generator.
Definition temporal.h:149
Precision precision
The drawn wall time is a whole number of these units since midnight.
Definition temporal.h:179
bool allow_imaginary
Definition temporal.h:185
Ambiguous ambiguous
Which instant a wall time the clocks repeated means.
Definition temporal.h:187
std::optional< DateTime > min_value
Minimum wall-clock time (inclusive), in whatever zone is drawn.
Definition temporal.h:171
std::optional< Instant > max_instant
Maximum instant (inclusive).
Definition temporal.h:177
std::optional< ZoneSource > source
Definition temporal.h:169
std::optional< Instant > min_instant
Minimum instant (inclusive).
Definition temporal.h:175
std::optional< AnyGenerator< TimeZone > > zones
Definition temporal.h:161
std::optional< DateTime > max_value
Maximum wall-clock time (inclusive), in whatever zone is drawn.
Definition temporal.h:173
Parameters for zones() generator.
Definition temporal.h:117
bool allow_unknown
Definition temporal.h:129
bool allow_legacy
Definition temporal.h:126
bool allow_opaque
Definition temporal.h:135
bool allow_links
Whether the default set includes link names such as Asia/Calcutta.
Definition temporal.h:123
std::optional< ZoneSource > source
Definition temporal.h:139
std::optional< std::vector< std::string > > names
Definition temporal.h:121