perl-corpus 0.13.3

Test corpus management and generators for Perl parsers
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
//! Randomized Perl code generation utilities.

use proptest::prelude::*;
use proptest::strategy::BoxedStrategy;
use proptest::test_runner::{Config, RngAlgorithm, TestRng, TestRunner};
use rand::rngs::StdRng;
use rand::seq::SliceRandom;
use rand::{RngExt, SeedableRng};
use std::time::{SystemTime, UNIX_EPOCH};

use crate::r#gen;

const DEFAULT_PREAMBLE: &str = "use strict;\nuse warnings;\n\n";

/// Statement categories for randomized code generation.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum StatementKind {
    /// Minimal valid statements (assignments, conditionals, subs).
    Basic,
    /// Package/subroutine declarations and method calls.
    Declarations,
    /// Object-oriented constructs (bless, inheritance, overload).
    ObjectOriented,
    /// qw(...) and related list constructs.
    Qw,
    /// Quote-like operators (q/qq/qx/qr).
    QuoteLike,
    /// Heredoc syntax in common contexts.
    Heredoc,
    /// Whitespace and comment stress cases.
    Whitespace,
    /// Loop control flow statements.
    ControlFlow,
    /// Format statements and sections.
    Format,
    /// Glob expressions and patterns.
    Glob,
    /// Tie/untie statements.
    Tie,
    /// I/O and filehandle statements.
    Io,
    /// Filetest operators and stacked checks.
    Filetest,
    /// Built-in function calls (pack/unpack, split/join, etc).
    Builtins,
    /// Map/grep/sort list operators.
    ListOps,
    /// Operator-focused expressions.
    Expressions,
    /// Regex match/substitution/transliteration.
    Regex,
    /// Parser ambiguity and stress cases.
    Ambiguity,
    /// Sigil-heavy variable and dereference patterns.
    Sigils,
    /// Compile-time phase blocks (BEGIN/CHECK/UNITCHECK/INIT/END).
    Phasers,
    /// Special variables and punctuation variables.
    SpecialVars,
}

const STATEMENT_KINDS_ALL: [StatementKind; 21] = [
    StatementKind::Basic,
    StatementKind::Declarations,
    StatementKind::ObjectOriented,
    StatementKind::Qw,
    StatementKind::QuoteLike,
    StatementKind::Heredoc,
    StatementKind::Whitespace,
    StatementKind::ControlFlow,
    StatementKind::Format,
    StatementKind::Glob,
    StatementKind::Tie,
    StatementKind::Io,
    StatementKind::Filetest,
    StatementKind::Builtins,
    StatementKind::ListOps,
    StatementKind::Expressions,
    StatementKind::Regex,
    StatementKind::Ambiguity,
    StatementKind::Sigils,
    StatementKind::Phasers,
    StatementKind::SpecialVars,
];

impl StatementKind {
    /// Return all available statement kinds.
    pub fn all() -> &'static [StatementKind] {
        &STATEMENT_KINDS_ALL
    }
}

/// Options for randomized Perl code generation.
#[derive(Debug, Clone)]
pub struct CodegenOptions {
    /// Number of statements to generate.
    pub statements: usize,
    /// Seed for deterministic output.
    pub seed: u64,
    /// Optional preamble prepended to output (e.g., `use strict;`).
    pub preamble: Option<String>,
    /// Ensure each selected statement kind appears at least once when possible.
    pub ensure_coverage: bool,
    /// Statement kinds to include in generation.
    pub kinds: Vec<StatementKind>,
}

impl Default for CodegenOptions {
    fn default() -> Self {
        Self {
            statements: 20,
            seed: default_seed(),
            preamble: Some(DEFAULT_PREAMBLE.to_string()),
            ensure_coverage: false,
            kinds: StatementKind::all().to_vec(),
        }
    }
}

/// Generate random Perl code with a default statement count.
pub fn generate_perl_code() -> String {
    generate_perl_code_with_options(CodegenOptions::default())
}

/// Generate random Perl code with a specific statement count.
pub fn generate_perl_code_with_statements(statements: usize) -> String {
    generate_perl_code_with_options(CodegenOptions { statements, ..Default::default() })
}

/// Generate random Perl code with explicit statement count and seed.
pub fn generate_perl_code_with_seed(statements: usize, seed: u64) -> String {
    generate_perl_code_with_options(CodegenOptions { statements, seed, ..Default::default() })
}

/// Generate random Perl code with explicit options.
pub fn generate_perl_code_with_options(options: CodegenOptions) -> String {
    let mut rng = StdRng::seed_from_u64(options.seed);
    let mut runner = TestRunner::new_with_rng(
        Config::default(),
        TestRng::from_seed(RngAlgorithm::ChaCha, &proptest_seed(options.seed)),
    );
    let strategies = build_strategies_for(&options.kinds);

    let mut output = String::new();
    if let Some(preamble) = options.preamble.as_deref() {
        output.push_str(preamble);
    }

    if strategies.is_empty() || options.statements == 0 {
        return output;
    }

    let indices = build_strategy_indices(
        strategies.len(),
        options.statements,
        options.ensure_coverage,
        &mut rng,
    );

    for (i, idx) in indices.into_iter().enumerate() {
        let fallback = format!("my $var{} = {};", i, i);
        let mut snippet = sample_strategy(&strategies[idx], &mut runner, &fallback);

        if !snippet.ends_with('\n') {
            snippet.push('\n');
        }

        output.push_str(&snippet);
        output.push('\n');
    }

    output
}

fn default_seed() -> u64 {
    SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .unwrap_or_else(|_| std::time::Duration::from_secs(0))
        .as_secs()
}

fn proptest_seed(seed: u64) -> [u8; 32] {
    let mut bytes = [0u8; 32];
    for (i, chunk) in bytes.chunks_mut(8).enumerate() {
        let mixed = seed.wrapping_add((i as u64).wrapping_mul(0x9E3779B97F4A7C15));
        chunk.copy_from_slice(&mixed.to_le_bytes());
    }
    bytes
}

fn build_strategies_for(kinds: &[StatementKind]) -> Vec<BoxedStrategy<String>> {
    let mut strategies = Vec::new();

    for kind in kinds {
        match kind {
            StatementKind::Basic => strategies.push(basic_statement().boxed()),
            StatementKind::Declarations => {
                strategies.push(r#gen::declarations::declaration_in_context().boxed());
            }
            StatementKind::ObjectOriented => {
                strategies.push(r#gen::object_oriented::object_oriented_in_context().boxed());
            }
            StatementKind::Qw => {
                let qw = r#gen::qw::qw_in_context();
                let constants = r#gen::qw::use_constant_qw().prop_map(|(src, _)| src);
                strategies.push(prop_oneof![qw, constants].boxed());
            }
            StatementKind::QuoteLike => {
                let quote = r#gen::quote_like::quote_like_single()
                    .prop_map(|expr| format!("my $text = {};\n", expr));
                strategies.push(quote.boxed());
            }
            StatementKind::Heredoc => strategies.push(r#gen::heredoc::heredoc_in_context().boxed()),
            StatementKind::Whitespace => {
                let whitespace = r#gen::whitespace::whitespace_stress_test();
                let commented = r#gen::whitespace::commented_code();
                strategies.push(prop_oneof![whitespace, commented].boxed());
            }
            StatementKind::ControlFlow => {
                strategies.push(r#gen::control_flow::loop_with_control().boxed());
            }
            StatementKind::Format => {
                strategies.push(r#gen::format_statements::format_statement().boxed());
            }
            StatementKind::Glob => strategies.push(r#gen::glob::glob_in_context().boxed()),
            StatementKind::Tie => strategies.push(r#gen::tie::tie_in_context().boxed()),
            StatementKind::Io => strategies.push(r#gen::io::io_in_context().boxed()),
            StatementKind::Filetest => {
                strategies.push(r#gen::filetest::filetest_in_context().boxed());
            }
            StatementKind::Builtins => {
                strategies.push(r#gen::builtins::builtin_in_context().boxed());
            }
            StatementKind::ListOps => {
                strategies.push(r#gen::list_ops::list_op_in_context().boxed());
            }
            StatementKind::Expressions => {
                strategies.push(r#gen::expressions::expression_in_context().boxed());
            }
            StatementKind::Regex => {
                strategies.push(r#gen::regex::regex_in_context().boxed());
            }
            StatementKind::Ambiguity => {
                strategies.push(r#gen::ambiguity::ambiguity_in_context().boxed());
            }
            StatementKind::Sigils => {
                strategies.push(r#gen::sigils::sigil_in_context().boxed());
            }
            StatementKind::Phasers => {
                strategies.push(r#gen::phasers::phaser_block().boxed());
            }
            StatementKind::SpecialVars => {
                strategies.push(r#gen::special_vars::special_vars_in_context().boxed());
            }
        }
    }

    strategies
}

fn build_strategy_indices(
    strategy_len: usize,
    statements: usize,
    ensure_coverage: bool,
    rng: &mut StdRng,
) -> Vec<usize> {
    if strategy_len == 0 || statements == 0 {
        return Vec::new();
    }

    let mut indices = Vec::with_capacity(statements);

    if ensure_coverage {
        let mut all_indices: Vec<usize> = (0..strategy_len).collect();
        all_indices.shuffle(rng);

        if statements <= strategy_len {
            indices.extend(all_indices.into_iter().take(statements));
            return indices;
        }

        indices.extend(all_indices);
    }

    while indices.len() < statements {
        indices.push(rng.random_range(0..strategy_len));
    }

    indices
}

fn sample_strategy(
    strategy: &BoxedStrategy<String>,
    runner: &mut TestRunner,
    fallback: &str,
) -> String {
    match strategy.new_tree(runner) {
        Ok(tree) => tree.current(),
        Err(_) => fallback.to_string(),
    }
}

fn basic_statement() -> impl Strategy<Value = String> {
    prop_oneof![
        Just("my $x = 1;".to_string()),
        Just("my @items = (1, 2, 3);".to_string()),
        Just("my %map = (a => 1, b => 2);".to_string()),
        Just("sub add { return $_[0] + $_[1]; }".to_string()),
        Just("if ($x) { print $x; }".to_string()),
        Just("my $msg = \"hello\"; print $msg;".to_string()),
    ]
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::collections::HashSet;

    #[test]
    fn generated_code_is_stable_for_seed() {
        let first = generate_perl_code_with_seed(5, 42);
        let second = generate_perl_code_with_seed(5, 42);
        assert_eq!(first, second);
    }

    #[test]
    fn codegen_respects_empty_kinds() {
        let options = CodegenOptions {
            statements: 5,
            seed: 123,
            preamble: None,
            ensure_coverage: false,
            kinds: Vec::new(),
        };
        let code = generate_perl_code_with_options(options);
        assert!(code.is_empty());
    }

    #[test]
    fn strategy_indices_cover_all_kinds_when_requested() {
        let mut rng = StdRng::seed_from_u64(7);
        let strategy_len = 6;
        let statements = 30;

        let indices = build_strategy_indices(strategy_len, statements, true, &mut rng);
        assert_eq!(indices.len(), statements);

        let unique: HashSet<usize> = indices.iter().copied().collect();
        assert_eq!(
            unique.len(),
            strategy_len,
            "expected all strategy indices to appear at least once"
        );
        assert!(unique.iter().all(|idx| *idx < strategy_len));
    }

    #[test]
    fn strategy_indices_do_not_repeat_when_budget_is_smaller_than_kind_count() {
        let mut rng = StdRng::seed_from_u64(11);
        let strategy_len = 10;
        let statements = 4;

        let indices = build_strategy_indices(strategy_len, statements, true, &mut rng);
        assert_eq!(indices.len(), statements);

        let unique: HashSet<usize> = indices.iter().copied().collect();
        assert_eq!(
            unique.len(),
            statements,
            "expected unique indices when sampling fewer statements than kinds"
        );
    }

    proptest! {
        #[test]
        fn prop_strategy_indices_stay_in_bounds(
            strategy_len in 1usize..32,
            statements in 1usize..128,
            ensure_coverage in any::<bool>(),
            seed in any::<u64>(),
        ) {
            let mut rng = StdRng::seed_from_u64(seed);
            let indices = build_strategy_indices(strategy_len, statements, ensure_coverage, &mut rng);

            prop_assert_eq!(indices.len(), statements);
            prop_assert!(indices.iter().all(|idx| *idx < strategy_len));
        }

        #[test]
        fn prop_strategy_indices_cover_all_when_requested(
            strategy_len in 1usize..24,
            extra_statements in 0usize..48,
            seed in any::<u64>(),
        ) {
            let statements = strategy_len + extra_statements;
            let mut rng = StdRng::seed_from_u64(seed);
            let indices = build_strategy_indices(strategy_len, statements, true, &mut rng);

            let mut seen = vec![false; strategy_len];
            for idx in indices {
                seen[idx] = true;
            }

            prop_assert!(seen.into_iter().all(|v| v));
        }

        #[test]
        fn prop_seeded_codegen_is_deterministic_for_options(
            statements in 0usize..40,
            seed in any::<u64>(),
            ensure_coverage in any::<bool>(),
        ) {
            let options = CodegenOptions {
                statements,
                seed,
                preamble: Some("use strict;\n".to_string()),
                ensure_coverage,
                kinds: vec![
                    StatementKind::Basic,
                    StatementKind::ControlFlow,
                    StatementKind::Regex,
                ],
            };

            let first = generate_perl_code_with_options(options.clone());
            let second = generate_perl_code_with_options(options);
            prop_assert_eq!(first, second);
        }

        #[test]
        fn prop_basic_only_codegen_emits_requested_statement_count(
            statements in 0usize..50,
            seed in any::<u64>(),
        ) {
            let code = generate_perl_code_with_options(CodegenOptions {
                statements,
                seed,
                preamble: None,
                ensure_coverage: true,
                kinds: vec![StatementKind::Basic],
            });

            let non_empty_line_count = code.lines().filter(|line| !line.trim().is_empty()).count();
            prop_assert_eq!(non_empty_line_count, statements);
        }
    }
}