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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
//! `proptest`-related features for `nalgebra` data structures.
//!
//! **This module is only available when the `proptest-support` feature is enabled in `nalgebra`**.
//!
//! `proptest` is a library for *property-based testing*. While similar to `QuickCheck`,
//! which may be more familiar to some users, it has a more sophisticated design that
//! provides users with automatic invariant-preserving shrinking. This means that when using
//! `proptest`, you rarely need to write your own shrinkers - which is usually very difficult -
//! and can instead get this "for free". Moreover, `proptest` does not rely on a canonical
//! `Arbitrary` trait implementation like `QuickCheck`, though it does also provide this. For
//! more information, check out the [proptest docs](https://docs.rs/proptest/0.10.1/proptest/)
//! and the [proptest book](https://altsysrq.github.io/proptest-book/intro.html).
//!
//! This module provides users of `nalgebra` with tools to work with `nalgebra` types in
//! `proptest` tests. At present, this integration is at an early stage, and only
//! provides tools for generating matrices and vectors, and not any of the geometry types.
//! There are essentially two ways of using this functionality:
//!
//! - Using the [matrix](fn.matrix.html) function to generate matrices with constraints
//!   on dimensions and elements.
//! - Relying on the `Arbitrary` implementation of `OMatrix`.
//!
//! The first variant is almost always preferred in practice. Read on to discover why.
//!
//! ### Using free function strategies
//!
//! In `proptest`, it is usually preferable to have free functions that generate *strategies*.
//! Currently, the [matrix](fn.matrix.html) function fills this role. The analogous function for
//! column vectors is [vector](fn.vector.html). Let's take a quick look at how it may be used:
//! ```
//! use nalgebra::proptest::matrix;
//! use proptest::prelude::*;
//!
//! proptest! {
//!     # /*
//!     #[test]
//!     # */
//!     fn my_test(a in matrix(-5 ..= 5, 2 ..= 4, 1..=4)) {
//!         // Generates matrices with elements in the range -5 ..= 5, rows in 2..=4 and
//!         // columns in 1..=4.
//!     }
//! }
//!
//! # fn main() { my_test(); }
//! ```
//!
//! In the above example, we generate matrices with constraints on the elements, as well as the
//! on the allowed dimensions. When a failing example is found, the resulting shrinking process
//! will preserve these invariants. We can use this to compose more advanced strategies.
//! For example, let's consider a toy example where we need to generate pairs of matrices
//! with exactly 3 rows fixed at compile-time and the same number of columns, but we want the
//! number of columns to vary. One way to do this is to use `proptest` combinators in combination
//! with [matrix](fn.matrix.html) as follows:
//!
//! ```
//! use nalgebra::{Dynamic, OMatrix, Const};
//! use nalgebra::proptest::matrix;
//! use proptest::prelude::*;
//!
//! type MyMatrix = OMatrix<i32, Const::<3>, Dynamic>;
//!
//! /// Returns a strategy for pairs of matrices with `U3` rows and the same number of
//! /// columns.
//! fn matrix_pairs() -> impl Strategy<Value=(MyMatrix, MyMatrix)> {
//!     matrix(-5 ..= 5, Const::<3>, 0 ..= 10)
//!         // We first generate the initial matrix `a`, and then depending on the concrete
//!         // instances of `a`, we pick a second matrix with the same number of columns
//!         .prop_flat_map(|a| {
//!             let b = matrix(-5 .. 5, Const::<3>, a.ncols());
//!             // This returns a new tuple strategy where we keep `a` fixed while
//!             // the second item is a strategy that generates instances with the same
//!             // dimensions as `a`
//!             (Just(a), b)
//!         })
//! }
//!
//! proptest! {
//!     # /*
//!     #[test]
//!     # */
//!     fn my_test((a, b) in matrix_pairs()) {
//!         // Let's double-check that the two matrices do indeed have the same number of
//!         // columns
//!         prop_assert_eq!(a.ncols(), b.ncols());
//!     }
//! }
//!
//! # fn main() { my_test(); }
//! ```
//!
//! ### The `Arbitrary` implementation
//!
//! If you don't care about the dimensions of matrices, you can write tests like these:
//!
//! ```
//! use nalgebra::{DMatrix, DVector, Dynamic, Matrix3, OMatrix, Vector3, U3};
//! use proptest::prelude::*;
//!
//! proptest! {
//!     # /*
//!     #[test]
//!     # */
//!     fn test_dynamic(matrix: DMatrix<i32>) {
//!         // This will generate arbitrary instances of `DMatrix` and also attempt
//!         // to shrink/simplify them when test failures are encountered.
//!     }
//!
//!     # /*
//!     #[test]
//!     # */
//!     fn test_static_and_mixed(matrix: Matrix3<i32>, matrix2: OMatrix<i32, U3, Dynamic>) {
//!         // Test some property involving these matrices
//!     }
//!
//!     # /*
//!     #[test]
//!     # */
//!     fn test_vectors(fixed_size_vector: Vector3<i32>, dyn_vector: DVector<i32>) {
//!         // Test some property involving these vectors
//!     }
//! }
//!
//! # fn main() { test_dynamic(); test_static_and_mixed(); test_vectors(); }
//! ```
//!
//! While this may be convenient, the default strategies for built-in types in `proptest` can
//! generate *any* number, including integers large enough to easily lead to overflow when used in
//! matrix operations, or even infinity or NaN values for floating-point types. Therefore
//! `Arbitrary` is rarely the method of choice for writing property-based tests.
//!
//! ### Notes on shrinking
//!
//! Due to some limitations of the current implementation, shrinking takes place by first
//! shrinking the matrix elements before trying to shrink the dimensions of the matrix.
//! This unfortunately often leads to the fact that a large number of shrinking iterations
//! are necessary to find a (nearly) minimal failing test case. As a workaround for this,
//! you can increase the maximum number of shrinking iterations when debugging. To do this,
//! simply set the `PROPTEST_MAX_SHRINK_ITERS` variable to a high number. For example:
//!
//! ```text
//! PROPTEST_MAX_SHRINK_ITERS=100000 cargo test my_failing_test
//! ```
use crate::allocator::Allocator;
use crate::{Const, DefaultAllocator, Dim, DimName, Dynamic, OMatrix, Scalar, U1};
use proptest::arbitrary::Arbitrary;
use proptest::collection::vec;
use proptest::strategy::{BoxedStrategy, Just, NewTree, Strategy, ValueTree};
use proptest::test_runner::TestRunner;

use std::ops::RangeInclusive;

/// Parameters for arbitrary matrix generation.
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct MatrixParameters<NParameters, R, C> {
    /// The range of rows that may be generated.
    pub rows: DimRange<R>,
    /// The range of columns that may be generated.
    pub cols: DimRange<C>,
    /// Parameters for the `Arbitrary` implementation of the scalar values.
    pub value_parameters: NParameters,
}

/// A range of allowed dimensions for use in generation of matrices.
///
/// The `DimRange` type is used to encode the range of dimensions that can be used for generation
/// of matrices with `proptest`. In most cases, you do not need to concern yourself with
/// `DimRange` directly, as it supports conversion from other types such as `U3` or inclusive
/// ranges such as `5 ..= 6`. The latter example corresponds to dimensions from (inclusive)
/// `Dynamic::new(5)` to `Dynamic::new(6)` (inclusive).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DimRange<D = Dynamic>(RangeInclusive<D>);

impl<D: Dim> DimRange<D> {
    /// The lower bound for dimensions generated.
    pub fn lower_bound(&self) -> D {
        *self.0.start()
    }

    /// The upper bound for dimensions generated.
    pub fn upper_bound(&self) -> D {
        *self.0.end()
    }
}

impl<D: Dim> From<D> for DimRange<D> {
    fn from(dim: D) -> Self {
        DimRange(dim..=dim)
    }
}

impl<D: Dim> From<RangeInclusive<D>> for DimRange<D> {
    fn from(range: RangeInclusive<D>) -> Self {
        DimRange(range)
    }
}

impl From<RangeInclusive<usize>> for DimRange<Dynamic> {
    fn from(range: RangeInclusive<usize>) -> Self {
        DimRange::from(Dynamic::new(*range.start())..=Dynamic::new(*range.end()))
    }
}

impl<D: Dim> DimRange<D> {
    /// Converts the `DimRange` into an instance of `RangeInclusive`.
    pub fn to_range_inclusive(&self) -> RangeInclusive<usize> {
        self.lower_bound().value()..=self.upper_bound().value()
    }
}

impl From<usize> for DimRange<Dynamic> {
    fn from(dim: usize) -> Self {
        DimRange::from(Dynamic::new(dim))
    }
}

/// The default range used for Dynamic dimensions when generating arbitrary matrices.
fn dynamic_dim_range() -> DimRange<Dynamic> {
    DimRange::from(0..=6)
}

/// Create a strategy to generate matrices containing values drawn from the given strategy,
/// with rows and columns in the provided ranges.
///
/// ## Examples
/// ```
/// use nalgebra::proptest::matrix;
/// use nalgebra::{OMatrix, Const, Dynamic};
/// use proptest::prelude::*;
///
/// proptest! {
///     # /*
///     #[test]
///     # */
///     fn my_test(a in matrix(0 .. 5i32, Const::<3>, 0 ..= 5)) {
///         // Let's make sure we've got the correct type first
///         let a: OMatrix<_, Const::<3>, Dynamic> = a;
///         prop_assert!(a.nrows() == 3);
///         prop_assert!(a.ncols() <= 5);
///         prop_assert!(a.iter().all(|x_ij| *x_ij >= 0 && *x_ij < 5));
///     }
/// }
///
/// # fn main() { my_test(); }
/// ```
///
/// ## Limitations
/// The current implementation has some limitations that lead to suboptimal shrinking behavior.
/// See the [module-level documentation](index.html) for more.
pub fn matrix<R, C, ScalarStrategy>(
    value_strategy: ScalarStrategy,
    rows: impl Into<DimRange<R>>,
    cols: impl Into<DimRange<C>>,
) -> MatrixStrategy<ScalarStrategy, R, C>
where
    ScalarStrategy: Strategy + Clone + 'static,
    ScalarStrategy::Value: Scalar,
    R: Dim,
    C: Dim,
    DefaultAllocator: Allocator<ScalarStrategy::Value, R, C>,
{
    matrix_(value_strategy, rows.into(), cols.into())
}

/// Same as `matrix`, but without the additional anonymous generic types
fn matrix_<R, C, ScalarStrategy>(
    value_strategy: ScalarStrategy,
    rows: DimRange<R>,
    cols: DimRange<C>,
) -> MatrixStrategy<ScalarStrategy, R, C>
where
    ScalarStrategy: Strategy + Clone + 'static,
    ScalarStrategy::Value: Scalar,
    R: Dim,
    C: Dim,
    DefaultAllocator: Allocator<ScalarStrategy::Value, R, C>,
{
    let nrows = rows.lower_bound().value()..=rows.upper_bound().value();
    let ncols = cols.lower_bound().value()..=cols.upper_bound().value();

    // Even though we can use this function to generate fixed-size matrices,
    // we currently generate all matrices with heap allocated Vec data.
    // TODO: Avoid heap allocation for fixed-size matrices.
    // Doing this *properly* would probably require us to implement a custom
    // strategy and valuetree with custom shrinking logic, which is not trivial

    // Perhaps more problematic, however, is the poor shrinking behavior the current setup leads to.
    // Shrinking in proptest basically happens in "reverse" of the combinators, so
    // by first generating the dimensions and then the elements, we get shrinking that first
    // tries to completely shrink the individual elements before trying to reduce the dimension.
    // This is clearly the opposite of what we want. I can't find any good way around this
    // short of writing our own custom value tree, which we should probably do at some point.
    // TODO: Custom implementation of value tree for better shrinking behavior.

    let strategy = nrows
        .prop_flat_map(move |nrows| (Just(nrows), ncols.clone()))
        .prop_flat_map(move |(nrows, ncols)| {
            (
                Just(nrows),
                Just(ncols),
                vec(value_strategy.clone(), nrows * ncols),
            )
        })
        .prop_map(|(nrows, ncols, values)| {
            // Note: R/C::from_usize will panic if nrows/ncols does not fit in the dimension type.
            // However, this should never fail, because we should only be generating
            // this stuff in the first place
            OMatrix::from_iterator_generic(R::from_usize(nrows), C::from_usize(ncols), values)
        })
        .boxed();

    MatrixStrategy { strategy }
}

/// Create a strategy to generate column vectors containing values drawn from the given strategy,
/// with length in the provided range.
///
/// This is a convenience function for calling
/// [`matrix(value_strategy, length, U1)`](fn.matrix.html) and should
/// be used when you only want to generate column vectors, as it's simpler and makes the intent
/// clear.
pub fn vector<D, ScalarStrategy>(
    value_strategy: ScalarStrategy,
    length: impl Into<DimRange<D>>,
) -> MatrixStrategy<ScalarStrategy, D, U1>
where
    ScalarStrategy: Strategy + Clone + 'static,
    ScalarStrategy::Value: Scalar,
    D: Dim,
    DefaultAllocator: Allocator<ScalarStrategy::Value, D>,
{
    matrix_(value_strategy, length.into(), Const::<1>.into())
}

impl<NParameters, R, C> Default for MatrixParameters<NParameters, R, C>
where
    NParameters: Default,
    R: DimName,
    C: DimName,
{
    fn default() -> Self {
        Self {
            rows: DimRange::from(R::name()),
            cols: DimRange::from(C::name()),
            value_parameters: NParameters::default(),
        }
    }
}

impl<NParameters, R> Default for MatrixParameters<NParameters, R, Dynamic>
where
    NParameters: Default,
    R: DimName,
{
    fn default() -> Self {
        Self {
            rows: DimRange::from(R::name()),
            cols: dynamic_dim_range(),
            value_parameters: NParameters::default(),
        }
    }
}

impl<NParameters, C> Default for MatrixParameters<NParameters, Dynamic, C>
where
    NParameters: Default,
    C: DimName,
{
    fn default() -> Self {
        Self {
            rows: dynamic_dim_range(),
            cols: DimRange::from(C::name()),
            value_parameters: NParameters::default(),
        }
    }
}

impl<NParameters> Default for MatrixParameters<NParameters, Dynamic, Dynamic>
where
    NParameters: Default,
{
    fn default() -> Self {
        Self {
            rows: dynamic_dim_range(),
            cols: dynamic_dim_range(),
            value_parameters: NParameters::default(),
        }
    }
}

impl<T, R, C> Arbitrary for OMatrix<T, R, C>
where
    T: Scalar + Arbitrary,
    <T as Arbitrary>::Strategy: Clone,
    R: Dim,
    C: Dim,
    MatrixParameters<T::Parameters, R, C>: Default,
    DefaultAllocator: Allocator<T, R, C>,
{
    type Parameters = MatrixParameters<T::Parameters, R, C>;

    fn arbitrary_with(args: Self::Parameters) -> Self::Strategy {
        let value_strategy = T::arbitrary_with(args.value_parameters);
        matrix(value_strategy, args.rows, args.cols)
    }

    type Strategy = MatrixStrategy<T::Strategy, R, C>;
}

/// A strategy for generating matrices.
#[derive(Debug, Clone)]
pub struct MatrixStrategy<NStrategy, R: Dim, C: Dim>
where
    NStrategy: Strategy,
    NStrategy::Value: Scalar,
    DefaultAllocator: Allocator<NStrategy::Value, R, C>,
{
    // For now we only internally hold a boxed strategy. The reason for introducing this
    // separate wrapper struct is so that we can replace the strategy logic with custom logic
    // later down the road without introducing significant breaking changes
    strategy: BoxedStrategy<OMatrix<NStrategy::Value, R, C>>,
}

impl<NStrategy, R, C> Strategy for MatrixStrategy<NStrategy, R, C>
where
    NStrategy: Strategy,
    NStrategy::Value: Scalar,
    R: Dim,
    C: Dim,
    DefaultAllocator: Allocator<NStrategy::Value, R, C>,
{
    type Tree = MatrixValueTree<NStrategy::Value, R, C>;
    type Value = OMatrix<NStrategy::Value, R, C>;

    fn new_tree(&self, runner: &mut TestRunner) -> NewTree<Self> {
        let underlying_tree = self.strategy.new_tree(runner)?;
        Ok(MatrixValueTree {
            value_tree: underlying_tree,
        })
    }
}

/// A value tree for matrices.
pub struct MatrixValueTree<T, R, C>
where
    T: Scalar,
    R: Dim,
    C: Dim,
    DefaultAllocator: Allocator<T, R, C>,
{
    // For now we only wrap a boxed value tree. The reason for wrapping is that this allows us
    // to swap out the value tree logic down the road without significant breaking changes.
    value_tree: Box<dyn ValueTree<Value = OMatrix<T, R, C>>>,
}

impl<T, R, C> ValueTree for MatrixValueTree<T, R, C>
where
    T: Scalar,
    R: Dim,
    C: Dim,
    DefaultAllocator: Allocator<T, R, C>,
{
    type Value = OMatrix<T, R, C>;

    fn current(&self) -> Self::Value {
        self.value_tree.current()
    }

    fn simplify(&mut self) -> bool {
        self.value_tree.simplify()
    }

    fn complicate(&mut self) -> bool {
        self.value_tree.complicate()
    }
}