1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
extern crate quickcheck;

use self::quickcheck::{Arbitrary, Gen};

use super::{PgDate, PgInterval, PgTime, PgTimestamp};

// see https://www.postgresql.org/docs/current/datatype-datetime.html
// for the specific limits

impl Arbitrary for PgDate {
    fn arbitrary(g: &mut Gen) -> Self {
        const MIN_DAY: i32 = (-4713 * 365) - (2000 * 365);
        const MAX_DAY: i32 = 5874897 * 365 - (2000 * 365);

        let mut day = i32::arbitrary(g);

        if day <= MIN_DAY {
            day %= MIN_DAY;
        }

        if day >= MAX_DAY {
            day %= MAX_DAY;
        }

        PgDate(day)
    }
}

impl Arbitrary for PgTime {
    fn arbitrary(g: &mut Gen) -> Self {
        // 24:00:00 in microseconds
        const MAX_TIME: i64 = 24 * 60 * 60 * 1_000_000;

        let time = u64::arbitrary(g);

        let mut time = if time > i64::MAX as u64 {
            (time / 2) as i64
        } else {
            time as i64
        };

        if time > MAX_TIME {
            time %= MAX_TIME;
        }

        PgTime(time)
    }
}

impl Arbitrary for PgTimestamp {
    fn arbitrary(g: &mut Gen) -> Self {
        const MIN_TIMESTAMP: i64 = -4713 * 365 * 24 * 60 * 60 * 100_000;
        const MAX_TIMESTAMP: i64 = 294276 * 365 * 24 * 60 * 60 * 100_000;

        let mut timestamp = i64::arbitrary(g);

        if timestamp <= MIN_TIMESTAMP {
            timestamp %= MIN_TIMESTAMP;
        }

        if timestamp >= MAX_TIMESTAMP {
            timestamp %= MAX_TIMESTAMP;
        }

        PgTimestamp(timestamp)
    }
}

impl Arbitrary for PgInterval {
    fn arbitrary(g: &mut Gen) -> Self {
        PgInterval {
            microseconds: i64::arbitrary(g),
            days: i32::arbitrary(g),
            months: i32::arbitrary(g),
        }
    }
}