protest 1.1.0

An ergonomic, powerful, and feature-rich property testing library with minimal boilerplate.
Documentation
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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
//! Performance optimizations for property testing including parallel execution,
//! lazy evaluation, and memory-efficient strategies.

use std::marker::PhantomData;
use std::sync::Arc;
use std::time::{Duration, Instant};

use crate::config::{GeneratorConfig, TestConfig};
use crate::error::{PropertyError, PropertyResult, TestFailure, TestSuccess};
use crate::generator::Generator;
use crate::property::Property;
use crate::rng::create_seeded_rng;

/// Configuration for parallel execution
#[derive(Debug, Clone)]
pub struct ParallelConfig {
    /// Number of threads to use for parallel execution
    pub num_threads: usize,
    /// Batch size for distributing work across threads
    pub batch_size: usize,
    /// Whether to enable parallel execution
    pub enabled: bool,
}

impl Default for ParallelConfig {
    fn default() -> Self {
        Self {
            num_threads: num_cpus::get(),
            batch_size: 10,
            enabled: true,
        }
    }
}

/// Lazy generator wrapper that defers expensive computations
pub struct LazyGenerator<T, F> {
    generator_fn: F,
    _phantom: PhantomData<T>,
}

impl<T, F> LazyGenerator<T, F>
where
    F: Fn() -> Box<dyn Generator<T> + Send + Sync>,
{
    /// Create a new lazy generator
    pub fn new(generator_fn: F) -> Self {
        Self {
            generator_fn,
            _phantom: PhantomData,
        }
    }
}

impl<T, F> Generator<T> for LazyGenerator<T, F>
where
    T: 'static,
    F: Fn() -> Box<dyn Generator<T> + Send + Sync> + Send + Sync,
{
    fn generate(&self, rng: &mut dyn rand::RngCore, config: &GeneratorConfig) -> T {
        let generator = (self.generator_fn)();
        generator.generate(rng, config)
    }

    fn shrink(&self, value: &T) -> Box<dyn Iterator<Item = T>> {
        let generator = (self.generator_fn)();
        generator.shrink(value)
    }
}

/// Memory-efficient shrinking strategy that uses streaming
pub struct StreamingShrinkStrategy<T> {
    max_memory_usage: usize,
    current_memory_usage: usize,
    _phantom: PhantomData<T>,
}

impl<T> StreamingShrinkStrategy<T> {
    /// Create a new streaming shrink strategy with memory limit
    pub fn new(max_memory_mb: usize) -> Self {
        Self {
            max_memory_usage: max_memory_mb * 1024 * 1024, // Convert MB to bytes
            current_memory_usage: 0,
            _phantom: PhantomData,
        }
    }

    /// Check if we're within memory limits
    pub fn within_memory_limit(&self, additional_bytes: usize) -> bool {
        self.current_memory_usage + additional_bytes <= self.max_memory_usage
    }

    /// Update memory usage tracking
    pub fn update_memory_usage(&mut self, bytes: usize) {
        self.current_memory_usage += bytes;
    }

    /// Reset memory usage tracking
    pub fn reset_memory_usage(&mut self) {
        self.current_memory_usage = 0;
    }
}

/// Parallel property test executor
pub struct ParallelPropertyTest<T, G, P> {
    generator: Arc<G>,
    property: Arc<P>,
    config: TestConfig,
    parallel_config: ParallelConfig,
    _phantom: PhantomData<T>,
}

impl<T, G, P> ParallelPropertyTest<T, G, P>
where
    T: Clone + Send + Sync + 'static + std::fmt::Debug + PartialEq,
    G: Generator<T> + Send + Sync + 'static,
    P: Property<T> + Send + Sync + 'static,
{
    /// Create a new parallel property test
    pub fn new(
        generator: G,
        property: P,
        config: TestConfig,
        parallel_config: ParallelConfig,
    ) -> Self {
        Self {
            generator: Arc::new(generator),
            property: Arc::new(property),
            config,
            parallel_config,
            _phantom: PhantomData,
        }
    }

    /// Execute the property test in parallel
    pub fn run(self) -> PropertyResult<T> {
        if !self.parallel_config.enabled || self.config.iterations < self.parallel_config.batch_size
        {
            // Fall back to sequential execution for small test counts
            return self.run_sequential();
        }

        let test_start = Instant::now();
        let num_threads = self.parallel_config.num_threads.min(self.config.iterations);
        let iterations_per_thread = self.config.iterations / num_threads;
        let remaining_iterations = self.config.iterations % num_threads;

        // Use crossbeam for scoped threads to avoid lifetime issues
        let result = crossbeam::scope(|s| {
            let mut handles = Vec::new();

            for thread_id in 0..num_threads {
                let generator = Arc::clone(&self.generator);
                let property = Arc::clone(&self.property);
                let config = self.config.clone();

                let thread_iterations = if thread_id < remaining_iterations {
                    iterations_per_thread + 1
                } else {
                    iterations_per_thread
                };

                let handle = s.spawn(move |_| {
                    Self::run_thread_batch(
                        generator,
                        property,
                        config,
                        thread_id,
                        thread_iterations,
                    )
                });

                handles.push(handle);
            }

            // Collect results from all threads
            for handle in handles {
                match handle.join() {
                    Ok(Ok(_)) => continue,                   // Thread succeeded
                    Ok(Err(failure)) => return Err(failure), // Thread found a failure
                    Err(_) => {
                        // Thread panicked
                        return Err(TestFailure::new(
                            PropertyError::execution_failed(
                                "Thread panicked during parallel execution",
                            ),
                            // We need a dummy value here - this is a limitation of the current design
                            self.generator
                                .generate(&mut create_seeded_rng(0), &self.config.generator_config),
                            None,
                            0,
                            self.config.clone(),
                            0,
                            test_start.elapsed(),
                            Duration::from_secs(0),
                        ));
                    }
                }
            }

            // All threads succeeded
            Ok(TestSuccess::new(
                self.config.iterations,
                self.config,
                None, // Stats aggregation would need to be implemented
            ))
        });

        result.unwrap() // crossbeam::scope guarantees this won't panic
    }

    /// Run a batch of iterations in a single thread
    fn run_thread_batch(
        generator: Arc<G>,
        property: Arc<P>,
        config: TestConfig,
        thread_id: usize,
        iterations: usize,
    ) -> PropertyResult<T> {
        let mut rng = if let Some(seed) = config.seed {
            // Create a unique seed for each thread to avoid correlation
            create_seeded_rng(seed.wrapping_add(thread_id as u64))
        } else {
            crate::rng::create_rng()
        };

        for iteration in 0..iterations {
            let global_iteration = thread_id * iterations + iteration;

            // Generate test input
            let input = generator.generate(&mut rng, &config.generator_config);

            // Test the property
            match property.test(input.clone()) {
                Ok(_) => continue,
                Err(mut error) => {
                    // Add iteration context
                    error = match error {
                        PropertyError::PropertyFailed {
                            message,
                            context,
                            iteration: None,
                        } => PropertyError::PropertyFailed {
                            message,
                            context,
                            iteration: Some(global_iteration),
                        },
                        other => other,
                    };

                    // For parallel execution, we don't shrink immediately to avoid complexity
                    // The caller can shrink the failure if needed
                    return Err(TestFailure::new(
                        error,
                        input,
                        None, // No shrinking in parallel mode for now
                        0,
                        config,
                        global_iteration,
                        Duration::from_secs(0), // Thread doesn't track total time
                        Duration::from_secs(0),
                    ));
                }
            }
        }

        Ok(TestSuccess::new(iterations, config, None))
    }

    /// Fall back to sequential execution
    fn run_sequential(self) -> PropertyResult<T> {
        // For sequential fallback, we need to work with the Arc directly
        // since we can't guarantee Clone is implemented for all generators/properties
        let generator = Arc::try_unwrap(self.generator)
            .map_err(|_| "Failed to unwrap generator Arc")
            .unwrap();
        let property = Arc::try_unwrap(self.property)
            .map_err(|_| "Failed to unwrap property Arc")
            .unwrap();

        let test = crate::execution::PropertyTest::new(generator, property, self.config);
        test.run()
    }
}

// Note: ParallelAsyncPropertyTest has been removed to keep the library runtime-agnostic.
// Use check_async() with your own async runtime instead.

/// Execute a property test with parallel optimization
pub fn check_parallel<T, G, P>(
    generator: G,
    property: P,
    config: TestConfig,
    parallel_config: ParallelConfig,
) -> PropertyResult<T>
where
    T: Clone + Send + Sync + 'static + std::fmt::Debug + PartialEq,
    G: Generator<T> + Send + Sync + 'static,
    P: Property<T> + Send + Sync + 'static,
{
    let test = ParallelPropertyTest::new(generator, property, config, parallel_config);
    test.run()
}

// Note: check_async_parallel() has been removed to keep the library runtime-agnostic.
// Use check_async() with your own async runtime instead.

/// Create a lazy generator that defers expensive computations
pub fn lazy<T, F>(generator_fn: F) -> LazyGenerator<T, F>
where
    F: Fn() -> Box<dyn Generator<T> + Send + Sync>,
{
    LazyGenerator::new(generator_fn)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::generator::ConstantGenerator;
    use std::sync::Arc;
    use std::sync::atomic::{AtomicUsize, Ordering};

    // Test property that counts invocations
    struct CountingProperty {
        counter: Arc<AtomicUsize>,
    }

    impl CountingProperty {
        fn new() -> (Self, Arc<AtomicUsize>) {
            let counter = Arc::new(AtomicUsize::new(0));
            (
                Self {
                    counter: counter.clone(),
                },
                counter,
            )
        }
    }

    impl Property<i32> for CountingProperty {
        type Output = ();
        fn test(&self, _input: i32) -> Result<Self::Output, PropertyError> {
            self.counter.fetch_add(1, Ordering::SeqCst);
            Ok(())
        }
    }

    // Async version of counting property - commented out since parallel async tests are disabled
    // struct AsyncCountingProperty {
    //     counter: Arc<AtomicUsize>,
    // }

    // impl AsyncCountingProperty {
    //     fn new() -> (Self, Arc<AtomicUsize>) {
    //         let counter = Arc::new(AtomicUsize::new(0));
    //         (
    //             Self {
    //                 counter: counter.clone(),
    //             },
    //             counter,
    //         )
    //     }
    // }

    // impl AsyncProperty<i32> for AsyncCountingProperty {
    //     type Output = ();
    //     async fn test(&self, _input: i32) -> Result<Self::Output, PropertyError> {
    //         tokio::time::sleep(std::time::Duration::from_millis(1)).await;
    //         self.counter.fetch_add(1, Ordering::SeqCst);
    //         Ok(())
    //     }
    // }

    #[test]
    fn test_parallel_config_default() {
        let config = ParallelConfig::default();
        assert!(config.enabled);
        assert!(config.num_threads > 0);
        assert_eq!(config.batch_size, 10);
    }

    #[test]
    fn test_lazy_generator() {
        let lazy_gen = lazy(|| Box::new(ConstantGenerator::new(42)));
        let mut rng = crate::rng::create_rng();
        let config = GeneratorConfig::default();

        let value = lazy_gen.generate(&mut rng, &config);
        assert_eq!(value, 42);

        let shrinks: Vec<_> = lazy_gen.shrink(&value).collect();
        assert!(shrinks.is_empty());
    }

    #[test]
    fn test_streaming_shrink_strategy() {
        let mut strategy = StreamingShrinkStrategy::<i32>::new(1); // 1MB limit

        assert!(strategy.within_memory_limit(1024)); // 1KB should be fine

        strategy.update_memory_usage(1024);
        assert!(!strategy.within_memory_limit(1024 * 1024)); // 1MB more would exceed limit

        strategy.reset_memory_usage();
        assert!(strategy.within_memory_limit(1024 * 1024)); // Should be fine after reset
    }

    #[test]
    fn test_parallel_property_test_sequential_fallback() {
        let generator = ConstantGenerator::new(42);
        let (property, counter) = CountingProperty::new();
        let config = TestConfig {
            iterations: 5, // Small number to trigger sequential fallback
            ..TestConfig::default()
        };
        let parallel_config = ParallelConfig {
            batch_size: 10, // Larger than iterations
            ..ParallelConfig::default()
        };

        let test = ParallelPropertyTest::new(generator, property, config, parallel_config);
        let result = test.run();

        assert!(result.is_ok());
        assert_eq!(counter.load(Ordering::SeqCst), 5);
    }

    #[test]
    fn test_parallel_property_test_disabled() {
        let generator = ConstantGenerator::new(42);
        let (property, counter) = CountingProperty::new();
        let config = TestConfig {
            iterations: 20,
            ..TestConfig::default()
        };
        let parallel_config = ParallelConfig {
            enabled: false,
            ..ParallelConfig::default()
        };

        let test = ParallelPropertyTest::new(generator, property, config, parallel_config);
        let result = test.run();

        assert!(result.is_ok());
        assert_eq!(counter.load(Ordering::SeqCst), 20);
    }

    #[test]
    fn test_parallel_property_test_enabled() {
        let generator = ConstantGenerator::new(42);
        let (property, counter) = CountingProperty::new();
        let config = TestConfig {
            iterations: 20,
            ..TestConfig::default()
        };
        let parallel_config = ParallelConfig {
            enabled: true,
            num_threads: 2,
            batch_size: 5,
        };

        let test = ParallelPropertyTest::new(generator, property, config, parallel_config);
        let result = test.run();

        assert!(result.is_ok());
        assert_eq!(counter.load(Ordering::SeqCst), 20);
    }

    #[test]
    fn test_check_parallel_function() {
        let generator = ConstantGenerator::new(42);
        let (property, counter) = CountingProperty::new();
        let config = TestConfig {
            iterations: 15,
            ..TestConfig::default()
        };
        let parallel_config = ParallelConfig::default();

        let result = check_parallel(generator, property, config, parallel_config);

        assert!(result.is_ok());
        assert_eq!(counter.load(Ordering::SeqCst), 15);
    }

    // Note: These tests use ParallelAsyncPropertyTest which requires tokio::spawn
    // Removed to keep library runtime-agnostic
    // #[tokio::test]
    // async fn test_parallel_async_property_test_sequential_fallback() {
    //     let generator = ConstantGenerator::new(42);
    //     let (property, counter) = AsyncCountingProperty::new();
    //     let config = TestConfig {
    //         iterations: 5,
    //         ..TestConfig::default()
    //     };
    //     let parallel_config = ParallelConfig {
    //         batch_size: 10,
    //         ..ParallelConfig::default()
    //     };
    //
    //     let test = ParallelAsyncPropertyTest::new(generator, property, config, parallel_config);
    //     let result = test.run().await;
    //
    //     assert!(result.is_ok());
    //     assert_eq!(counter.load(Ordering::SeqCst), 5);
    // }
    //
    // #[tokio::test]
    // async fn test_parallel_async_property_test_enabled() {
    //     let generator = ConstantGenerator::new(42);
    //     let (property, counter) = AsyncCountingProperty::new();
    //     let config = TestConfig {
    //         iterations: 20,
    //         ..TestConfig::default()
    //     };
    //     let parallel_config = ParallelConfig {
    //         enabled: true,
    //         num_threads: 2,
    //         batch_size: 5,
    //     };
    //
    //     let test = ParallelAsyncPropertyTest::new(generator, property, config, parallel_config);
    //     let result = test.run().await;
    //
    //     assert!(result.is_ok());
    //     assert_eq!(counter.load(Ordering::SeqCst), 20);
    // }

    // Note: check_async_parallel function removed as it required tokio::spawn
    // Users can implement their own parallel async execution if needed
    // #[tokio::test]
    // async fn test_check_async_parallel_function() {
    //     let generator = ConstantGenerator::new(42);
    //     let (property, counter) = AsyncCountingProperty::new();
    //     let config = TestConfig {
    //         iterations: 15,
    //         ..TestConfig::default()
    //     };
    //     let parallel_config = ParallelConfig::default();
    //
    //     let result = check_async_parallel(generator, property, config, parallel_config).await;
    //
    //     assert!(result.is_ok());
    //     assert_eq!(counter.load(Ordering::SeqCst), 15);
    // }
}