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
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
use crate::{
    bounded::{is_within, BoundedGenerator, BoundedValue},
    driver::DriverMode,
    Driver, TypeGenerator, TypeGeneratorWithParams, TypeValueGenerator, ValueGenerator,
};
use byteorder::{ByteOrder, LittleEndian};
use core::{
    mem::size_of,
    ops::{RangeBounds, RangeFrom},
};

trait NumBindWithin {
    fn bind_within<R: RangeBounds<Self>>(&mut self, range_bounds: &R);
}

macro_rules! impl_unsigned_bounded_integer {
    ($ty:ident) => {
        impl NumBindWithin for $ty {
            fn bind_within<R: RangeBounds<Self>>(&mut self, range_bounds: &R) {
                use core::ops::Bound::*;

                let start = match range_bounds.start_bound() {
                    Included(value) => *value,
                    Excluded(value) => value.saturating_add(1),
                    Unbounded => core::$ty::MIN,
                };

                let end = match range_bounds.end_bound() {
                    Included(value) => *value,
                    Excluded(value) => value.saturating_sub(1),
                    Unbounded => core::$ty::MAX,
                };

                let steps = (end - start).saturating_add(1);
                let values_per_step = core::$ty::MAX / steps;
                *self = core::cmp::min(start.saturating_add(*self / values_per_step), end);
            }
        }

        impl_bounded_integer!($ty);
    };
}

macro_rules! impl_bounded_integer {
    ($ty:ident) => {
        impl<R: RangeBounds<Self>> BoundedValue<R> for $ty {
            type BoundValue = $ty;

            #[inline(always)]
            fn is_within(&self, range_bounds: &R) -> bool {
                is_within(self, range_bounds)
            }

            #[inline(always)]
            fn bind_within(&mut self, range_bounds: &R) {
                NumBindWithin::bind_within(self, range_bounds)
            }
        }

        impl TypeGeneratorWithParams for $ty {
            type Output = BoundedGenerator<TypeValueGenerator<$ty>, RangeFrom<$ty>>;

            fn gen_with() -> Self::Output {
                BoundedGenerator::new(Default::default(), core::$ty::MIN..)
            }
        }
    };
}

impl TypeGenerator for u8 {
    fn generate<D: Driver>(driver: &mut D) -> Option<Self> {
        let mut bytes = [0; size_of::<u8>()];
        Driver::fill_bytes(driver, &mut bytes)?;
        Some(bytes[0])
    }
}

impl ValueGenerator for u8 {
    type Output = u8;

    fn generate<D: Driver>(&self, _driver: &mut D) -> Option<Self> {
        Some(*self)
    }
}

impl_unsigned_bounded_integer!(u8);

macro_rules! impl_unsigned_integer {
    ($ty:ident, $call:ident) => {
        impl TypeGenerator for $ty {
            fn generate<D: Driver>(driver: &mut D) -> Option<Self> {
                let mut bytes = [0; size_of::<$ty>()];
                Driver::fill_bytes(driver, &mut bytes)?;
                Some(LittleEndian::$call(&bytes))
            }
        }

        impl ValueGenerator for $ty {
            type Output = $ty;

            fn generate<D: Driver>(&self, _driver: &mut D) -> Option<Self> {
                Some(*self)
            }
        }

        impl_unsigned_bounded_integer!($ty);
    };
}

macro_rules! impl_signed_integer {
    ($ty:ident, $unsigned:ident) => {
        impl TypeGenerator for $ty {
            fn generate<D: Driver>(driver: &mut D) -> Option<Self> {
                let unsigned: $unsigned = driver.gen()?;

                // When using Direct, use simple conversions instead of zigzag
                let signed = if driver.mode() == DriverMode::Direct {
                    unsigned as $ty
                } else {
                    ((unsigned >> 1) as $ty) ^ (-((unsigned & 1) as $ty))
                };

                Some(signed)
            }
        }

        impl ValueGenerator for $ty {
            type Output = $ty;

            fn generate<D: Driver>(&self, _driver: &mut D) -> Option<Self> {
                Some(*self)
            }
        }

        impl NumBindWithin for $ty {
            #[inline]
            fn bind_within<R: RangeBounds<Self>>(&mut self, range_bounds: &R) {
                use core::ops::Bound::*;

                let to_unsigned = |value: $ty| {
                    if value == core::$ty::MIN {
                        return 0;
                    }

                    if value >= 0 {
                        return value as $unsigned + core::$ty::MAX as $unsigned + 1;
                    }

                    value as $unsigned - core::$ty::MAX as $unsigned - 1
                };

                let from_unsigned = |value: $unsigned| {
                    if value == 0 {
                        return core::$ty::MIN;
                    }

                    if value > core::$ty::MAX as $unsigned {
                        return (value - core::$ty::MAX as $unsigned - 1) as $ty;
                    }

                    (value + core::$ty::MAX as $unsigned) as $ty + 1
                };

                let start = match range_bounds.start_bound() {
                    Included(value) => Included(to_unsigned(*value)),
                    Excluded(value) => Excluded(to_unsigned(*value)),
                    Unbounded => Unbounded,
                };

                let end = match range_bounds.end_bound() {
                    Included(value) => Included(to_unsigned(*value)),
                    Excluded(value) => Excluded(to_unsigned(*value)),
                    Unbounded => Unbounded,
                };

                let mut unsigned = to_unsigned(*self);
                NumBindWithin::bind_within(&mut unsigned, &(start, end));
                *self = from_unsigned(unsigned);
            }
        }

        impl_bounded_integer!($ty);
    };
}

impl_signed_integer!(i8, u8);

impl_unsigned_integer!(u16, read_u16);
impl_signed_integer!(i16, u16);

impl_unsigned_integer!(u32, read_u32);
impl_signed_integer!(i32, u32);

impl_unsigned_integer!(u64, read_u64);
impl_signed_integer!(i64, u64);

impl_unsigned_integer!(u128, read_u128);
impl_signed_integer!(i128, u128);

impl TypeGenerator for usize {
    fn generate<D: Driver>(driver: &mut D) -> Option<Self> {
        let mut bytes = [0; size_of::<usize>()];
        Driver::fill_bytes(driver, &mut bytes)?;
        Some(LittleEndian::read_uint(&bytes, bytes.len()) as usize)
    }
}

impl ValueGenerator for usize {
    type Output = Self;

    fn generate<D: Driver>(&self, _driver: &mut D) -> Option<Self> {
        Some(*self)
    }
}

impl_unsigned_bounded_integer!(usize);
impl_signed_integer!(isize, usize);

macro_rules! impl_float {
    ($name:ident, $ty:ident, $call:ident) => {
        impl TypeGenerator for $ty {
            fn generate<D: Driver>(driver: &mut D) -> Option<Self> {
                let mut bytes = [0; size_of::<$ty>()];
                Driver::fill_bytes(driver, &mut bytes)?;
                Some(unsafe { core::mem::transmute(bytes) })
            }
        }

        impl ValueGenerator for $ty {
            type Output = $ty;

            fn generate<D: Driver>(&self, _driver: &mut D) -> Option<Self> {
                Some(*self)
            }
        }

        // TODO impl_bounded
    };
}

impl_float!(gen_f32, f32, read_f32);
impl_float!(gen_f64, f64, read_f64);

macro_rules! impl_non_zero_integer {
    ($ty:ident) => {
        impl TypeGenerator for core::num::$ty {
            fn generate<D: Driver>(driver: &mut D) -> Option<Self> {
                let value = (1..).generate(driver)?;
                Some(unsafe { Self::new_unchecked(value) })
            }
        }

        impl<R: RangeBounds<Self>> BoundedValue<R> for core::num::$ty {
            type BoundValue = core::num::$ty;

            #[inline(always)]
            fn is_within(&self, range_bounds: &R) -> bool {
                is_within(self, range_bounds)
            }

            #[inline(always)]
            fn bind_within(&mut self, range_bounds: &R) {
                use core::ops::Bound::*;

                let start = match range_bounds.start_bound() {
                    Included(value) => Included(value.get()),
                    Excluded(value) => Excluded(value.get()),
                    Unbounded => Unbounded,
                };

                let end = match range_bounds.end_bound() {
                    Included(value) => Included(value.get()),
                    Excluded(value) => Excluded(value.get()),
                    Unbounded => Unbounded,
                };

                let mut inner = self.get();

                // try a few times before giving up
                for _ in 0..=3 {
                    NumBindWithin::bind_within(&mut inner, &(start, end));
                    if let Some(value) = Self::new(inner) {
                        *self = value;
                        return;
                    } else {
                        inner = inner.wrapping_add(1);
                    }
                }

                panic!(concat!(
                    "could not satisfy bounded value for ",
                    stringify!($ty)
                ))
            }
        }

        impl TypeGeneratorWithParams for core::num::$ty {
            type Output =
                BoundedGenerator<TypeValueGenerator<core::num::$ty>, RangeFrom<core::num::$ty>>;

            fn gen_with() -> Self::Output {
                BoundedGenerator::new(
                    Default::default(),
                    unsafe { core::num::$ty::new_unchecked(1) }..,
                )
            }
        }
    };
}

impl_non_zero_integer!(NonZeroI8);
impl_non_zero_integer!(NonZeroU8);
impl_non_zero_integer!(NonZeroI16);
impl_non_zero_integer!(NonZeroU16);
impl_non_zero_integer!(NonZeroI32);
impl_non_zero_integer!(NonZeroU32);
impl_non_zero_integer!(NonZeroI64);
impl_non_zero_integer!(NonZeroU64);
impl_non_zero_integer!(NonZeroI128);
impl_non_zero_integer!(NonZeroU128);
impl_non_zero_integer!(NonZeroIsize);
impl_non_zero_integer!(NonZeroUsize);