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
//! Tests for generation statistics and coverage functionality.

use protest::generator::ConstantGenerator;
use protest::primitives::{BoolGenerator, IntGenerator, StringGenerator, VecGenerator};
use protest::{
    CoverageThresholdsBuilder, Property, PropertyError, PropertyTestBuilder, StatisticsCollector,
    TestConfig, check, check_with_config,
};
use std::time::Duration;

/// Simple property that always passes
struct AlwaysPassProperty;
impl Property<i32> for AlwaysPassProperty {
    type Output = ();
    fn test(&self, _input: i32) -> Result<Self::Output, PropertyError> {
        Ok(())
    }
}

/// Property that always passes for any type
struct AlwaysPassAnyProperty<T>(std::marker::PhantomData<T>);
impl<T> AlwaysPassAnyProperty<T> {
    fn new() -> Self {
        Self(std::marker::PhantomData)
    }
}
impl<T> Property<T> for AlwaysPassAnyProperty<T> {
    type Output = ();
    fn test(&self, _input: T) -> Result<Self::Output, PropertyError> {
        Ok(())
    }
}

#[test]
fn test_basic_statistics_collection() {
    let result = PropertyTestBuilder::new()
        .iterations(50)
        .enable_statistics()
        .run(IntGenerator::<i32>::new(1, 100), AlwaysPassProperty);

    assert!(result.is_ok());
    let success = result.unwrap();

    // Check that statistics were collected
    assert!(success.stats.is_some());
    let stats = success.stats.unwrap();
    assert_eq!(stats.total_generated, 50);

    // Check that numeric coverage was recorded
    assert!(!stats.coverage_info.numeric_coverage.is_empty());

    // Check performance metrics
    assert!(stats.performance_metrics.total_generation_time > Duration::from_nanos(0));
    assert!(stats.performance_metrics.average_generation_time > Duration::from_nanos(0));
}

#[test]
fn test_statistics_disabled() {
    let result = PropertyTestBuilder::new()
        .iterations(50)
        .disable_statistics()
        .run(IntGenerator::<i32>::new(1, 100), AlwaysPassProperty);

    assert!(result.is_ok());
    let success = result.unwrap();

    // Statistics should be None when disabled
    assert!(success.stats.is_none());
}

#[test]
fn test_numeric_coverage_tracking() {
    let result = PropertyTestBuilder::new()
        .iterations(100)
        .enable_statistics()
        .run(IntGenerator::<i32>::new(0, 50), AlwaysPassProperty);

    assert!(result.is_ok());
    let success = result.unwrap();
    let stats = success.stats.unwrap();

    // Check numeric coverage
    assert!(!stats.coverage_info.numeric_coverage.is_empty());

    if let Some(numeric_coverage) = stats.coverage_info.numeric_coverage.values().next() {
        assert_eq!(numeric_coverage.total_count, 100);
        assert!(numeric_coverage.min_value >= 0.0);
        assert!(numeric_coverage.max_value <= 50.0);
        assert!(numeric_coverage.statistics.mean >= 0.0);
        assert!(numeric_coverage.statistics.mean <= 50.0);
    }
}

#[test]
fn test_boolean_coverage_tracking() {
    let result = PropertyTestBuilder::new()
        .iterations(100)
        .enable_statistics()
        .run(BoolGenerator, AlwaysPassAnyProperty::<bool>::new());

    assert!(result.is_ok());
    let success = result.unwrap();
    let stats = success.stats.unwrap();

    // Check boolean coverage
    assert!(!stats.coverage_info.boolean_coverage.is_empty());

    if let Some(boolean_coverage) = stats.coverage_info.boolean_coverage.values().next() {
        assert_eq!(boolean_coverage.total_count, 100);
        // With 100 iterations, we should have both true and false values
        assert!(boolean_coverage.has_full_coverage());
        assert!(boolean_coverage.true_ratio > 0.0 && boolean_coverage.true_ratio < 1.0);
    }
}

#[test]
fn test_string_coverage_tracking() {
    let result = PropertyTestBuilder::new()
        .iterations(50)
        .enable_statistics()
        .run(
            StringGenerator::ascii_printable(1, 20),
            AlwaysPassAnyProperty::<String>::new(),
        );

    assert!(result.is_ok());
    let success = result.unwrap();
    let stats = success.stats.unwrap();

    // Check string coverage
    assert!(!stats.coverage_info.string_coverage.is_empty());

    if let Some(string_coverage) = stats.coverage_info.string_coverage.values().next() {
        assert_eq!(string_coverage.total_count, 50);
        assert!(string_coverage.average_length >= 0.0);
        assert!(!string_coverage.length_distribution.is_empty());
        assert!(!string_coverage.character_distribution.is_empty());
    }
}

#[test]
fn test_collection_coverage_tracking() {
    let result = PropertyTestBuilder::new()
        .iterations(30)
        .enable_statistics()
        .run(
            VecGenerator::new(IntGenerator::<i32>::new(1, 100), 0, 10),
            AlwaysPassAnyProperty::<Vec<i32>>::new(),
        );

    assert!(result.is_ok());
    let success = result.unwrap();
    let stats = success.stats.unwrap();

    // Check collection coverage
    assert!(!stats.coverage_info.collection_coverage.is_empty());

    if let Some(collection_coverage) = stats.coverage_info.collection_coverage.values().next() {
        assert_eq!(collection_coverage.total_count, 30);
        assert!(collection_coverage.average_size >= 0.0);
        assert!(!collection_coverage.size_distribution.is_empty());
    }
}

#[test]
fn test_statistics_report_generation() {
    let result = PropertyTestBuilder::new()
        .iterations(20)
        .enable_statistics()
        .run(IntGenerator::<i32>::new(1, 10), AlwaysPassProperty);

    assert!(result.is_ok());
    let success = result.unwrap();
    let stats = success.stats.unwrap();

    // Test report generation
    let report = stats.generate_report();
    assert!(report.contains("GENERATION STATISTICS"));
    assert!(report.contains("Total values generated: 20"));
    assert!(report.contains("COVERAGE INFORMATION"));
    assert!(report.contains("PERFORMANCE METRICS"));

    // Test summary
    let summary = stats.get_summary();
    assert!(summary.contains("Generated 20 values"));
}

#[test]
fn test_coverage_thresholds() {
    let result = PropertyTestBuilder::new()
        .iterations(100)
        .enable_statistics()
        .run(BoolGenerator, AlwaysPassAnyProperty::<bool>::new());

    assert!(result.is_ok());
    let success = result.unwrap();
    let stats = success.stats.unwrap();

    // Create coverage thresholds
    let thresholds = CoverageThresholdsBuilder::new()
        .require_full_boolean_coverage()
        .build();

    // Check coverage against thresholds
    let report = stats.check_coverage_thresholds(&thresholds);

    // With 100 iterations, boolean coverage should be complete
    assert!(report.overall_pass);
    if let Some(&passed) = report.boolean_results.values().next() {
        assert!(passed);
    }
}

#[test]
fn test_statistics_collector_direct_usage() {
    let mut collector = StatisticsCollector::new();

    // Record some values directly
    collector.record_generated_value(&42i32, "i32");
    collector.record_generated_value(&true, "bool");
    collector.record_generated_value(&"hello".to_string(), "String");

    let stats = collector.get_stats();
    assert_eq!(stats.total_generated, 3);
    assert!(stats.coverage_info.numeric_coverage.contains_key("i32"));
    assert!(stats.coverage_info.boolean_coverage.contains_key("bool"));
    assert!(stats.coverage_info.string_coverage.contains_key("String"));
}

#[test]
fn test_statistics_collector_timing() {
    let mut collector = StatisticsCollector::new();

    // Test timing functionality
    collector.start_generation_timing();
    std::thread::sleep(Duration::from_millis(1));
    collector.end_generation_timing();
    collector.record_generated_value(&42, "i32");

    let stats = collector.get_stats();
    assert!(stats.performance_metrics.total_generation_time > Duration::from_nanos(0));
    assert!(stats.performance_metrics.average_generation_time > Duration::from_nanos(0));
}

#[test]
fn test_statistics_collector_reset() {
    let mut collector = StatisticsCollector::new();

    collector.record_generated_value(&42, "i32");
    assert_eq!(collector.get_stats().total_generated, 1);

    collector.reset();
    assert_eq!(collector.get_stats().total_generated, 0);
    assert!(
        collector
            .get_stats()
            .coverage_info
            .numeric_coverage
            .is_empty()
    );
}

#[test]
fn test_enum_coverage_tracking() {
    let mut collector = StatisticsCollector::new();

    // Simulate enum variants
    collector.record_enum_variant("Color", "Red", 3);
    collector.record_enum_variant("Color", "Green", 3);
    collector.record_enum_variant("Color", "Red", 3);

    let stats = collector.get_stats();
    let enum_coverage = stats.coverage_info.enum_coverage.get("Color").unwrap();

    assert_eq!(enum_coverage.total_count, 3);
    assert_eq!(enum_coverage.variant_distribution.len(), 2); // Red and Green
    assert!((enum_coverage.coverage_percentage - 2.0 / 3.0).abs() < 0.01); // 2 out of 3 variants
}

#[test]
fn test_performance_analysis() {
    let mut collector = StatisticsCollector::new();

    // Generate some data with timing
    for i in 0..10 {
        collector.start_generation_timing();
        std::thread::sleep(Duration::from_millis(1));
        collector.end_generation_timing();
        collector.record_generated_value(&i, "i32");
    }

    let report = collector.generate_analysis_report();
    assert!(report.contains("GENERATION ANALYSIS"));
    assert!(report.contains("Generation Time Distribution"));
    assert!(report.contains("Coverage Pattern Analysis"));
    assert!(report.contains("Performance Insights"));
}

#[test]
fn test_coverage_recommendations() {
    let mut collector = StatisticsCollector::new();

    // Generate only true values (incomplete boolean coverage)
    for _ in 0..10 {
        collector.record_generated_value(&true, "bool");
    }

    let thresholds = CoverageThresholdsBuilder::new()
        .require_full_boolean_coverage()
        .build();

    let (report, recommendations) = collector.check_coverage_and_recommend(&thresholds);

    assert!(!report.overall_pass); // Should fail due to incomplete boolean coverage
    assert!(!recommendations.is_empty()); // Should have recommendations

    // Check that recommendations mention boolean coverage
    let recommendations_text = recommendations.join(" ");
    assert!(recommendations_text.to_lowercase().contains("bool"));
}

#[test]
fn test_statistics_with_different_types() {
    let mut collector = StatisticsCollector::new();

    // Test with various numeric types
    collector.record_generated_value(&42u8, "u8");
    collector.record_generated_value(&1000u16, "u16");
    collector.record_generated_value(&-42i32, "i32");
    collector.record_generated_value(&3.15f64, "f64");

    let stats = collector.get_stats();
    assert_eq!(stats.total_generated, 4);

    // Should have numeric coverage for different types
    assert!(!stats.coverage_info.numeric_coverage.is_empty()); // At least one type should be tracked
}

#[test]
fn test_statistics_integration_with_check_functions() {
    // Test with basic check function
    let result = check(IntGenerator::<i32>::new(1, 10), AlwaysPassProperty);
    assert!(result.is_ok());

    // Statistics should be collected by default
    let success = result.unwrap();
    assert!(success.stats.is_some());

    // Test with custom config
    let config = TestConfig {
        iterations: 25,
        ..TestConfig::default()
    };

    let result = check_with_config(IntGenerator::<i32>::new(1, 10), AlwaysPassProperty, config);
    assert!(result.is_ok());

    let success = result.unwrap();
    assert!(success.stats.is_some());
    let stats = success.stats.unwrap();
    assert_eq!(stats.total_generated, 25);
}

#[test]
fn test_memory_tracking() {
    let mut collector = StatisticsCollector::new();

    // Record some values to trigger memory tracking
    for i in 0..10 {
        collector.record_generated_value(&vec![i; 100], "Vec<i32>");
    }

    let stats = collector.get_stats();

    // Memory stats should be recorded
    assert!(stats.performance_metrics.memory_stats.total_allocations > 0);
    // Peak memory usage should be greater than 0
    assert!(stats.performance_metrics.memory_stats.peak_memory_usage > 0);
}

#[test]
fn test_statistics_with_custom_generator() {
    // Use the existing ConstantGenerator
    let custom_generator = ConstantGenerator::new(42);

    let result = PropertyTestBuilder::new()
        .iterations(20)
        .enable_statistics()
        .run(custom_generator, AlwaysPassProperty);

    assert!(result.is_ok());
    let success = result.unwrap();
    let stats = success.stats.unwrap();

    assert_eq!(stats.total_generated, 20);

    // All values should be the same (42)
    if let Some(numeric_coverage) = stats.coverage_info.numeric_coverage.values().next() {
        assert_eq!(numeric_coverage.min_value, 42.0);
        assert_eq!(numeric_coverage.max_value, 42.0);
        assert_eq!(numeric_coverage.statistics.mean, 42.0);
        assert_eq!(numeric_coverage.statistics.variance, 0.0);
    }
}