<commit_before>#!/usr/bin/env parrot-nqp

pir::load_bytecode('Tree/Optimizer.pbc');

plan(37);

{
    my $opt := Tree::Optimizer.new;
    my $test-input := [];
    ok($test-input =:= $opt.run($test-input),
       'Tree::Optimizer without any registered passes returns the input.');
}

{
    my $opt := Tree::Optimizer.new;
    my &transform := sub ($past) { $past; };
    $opt.register(&transform, :name<identity>);
    ok(1, 'A :name adverb can be supplied to Tree::Optimizer.register.');
    ok($opt.find-pass('identity') ~~ Tree::Optimizer::Pass,
       'Tree::Optimizer.find-pass returns a Tree::Optimizer::Pass, if found.');
    ok($opt.find-pass('identity').transformation =:= &transform,
       'Tree::Optimizer.find-pass returns a pass with correct .transformation.');

    ok(!pir::defined__IP($opt.find-pass('nonexistent-pass')),
       'Tree::Optimizer.find-pass with a non-existent name returns undef.');
}

{
    my $opt := Tree::Optimizer.new;
    my &transform := sub ($v) {
        -$v;
    };
    $opt.register(&transform);
    ok($opt.run(5) == -5,
       'Simple Sub pass runs correctly.');
    ok($opt.run(5) == -5,
       'Tree::Optimizer.run works repeatedly.');
}

{
    my &square := sub ($x) {
        $x * $x;
    }
    my &negate := sub ($x) {
        -$x;
    }
    {
        my $opt := Tree::Optimizer.new;
        $opt.register(&square, :name<square>);
        $opt.register(&negate, :depends-on<square>);
        ok($opt.run(2) == -4,
           'Correct order when registering a pass after its dependency.');
        ok($opt.run(2) == -4,
           'Repeated correct order when registering a pass after its dependency.');
    }
    {
        my $opt := Tree::Optimizer.new;
        $opt.register(&negate, :depends-on<square>);
        $opt.register(&square, :name<square>);
        ok($opt.run(2) == -4,
           'Correct order when registering a pass after its dependency.');
        ok($opt.run(2) == -4,
           'Repeated correct order when registering a pass after its dependency.');
    }
    {
        my $opt := Tree::Optimizer.new;
        $opt.register(&negate, :name<negate>);
        $opt.register(&square, :depends-on<negate>);
        ok($opt.run(2) == 4,
           'Correct order is not coincidental.');
    }
}

{
    my &inc := sub ($n) { $n + 1; };
    my $opt := Tree::Optimizer.new;
    $opt.register(&inc);
    $opt.register(&inc);
    ok($opt.run(0) == 2,
       'Multipled unnamed passes are allowed.');
}

{
    class Even {
        method ACCEPTS ($n) {
            if $n % 2 == 0 {
                $n / 2;
            } else {
                0;
            }
        }
    }
    my &inc := sub ($n) { 2 * $n + 1; };
    {
        my $opt := Tree::Optimizer.new;
        $opt.register(&inc, :when(Even.new));
        ok($opt.run(2) == 3,
           'A pass with :when runs correctly when matching.');
    }
    {
        my $opt := Tree::Optimizer.new;
        $opt.register(&inc, :when(Even.new));
        ok($opt.run(1) == 1,
           'A pass with :when correctly does nothing when not matching.');
    }
}

pir::load_bytecode('PCT.pbc');
pir::load_bytecode('PAST/Pattern.pbc');
{
    my $past := PAST::Stmts.new(PAST::Val.new(:value(6)));
    my &inc := sub transform ($node) {
        if $node.match(PAST::Pattern::Val.new, :exact(1)) {
            $node.value($node.value + 1);
        }<commit_msg>Fix test to run on modern parrot<commit_after>#!/usr/bin/env parrot-nqp

pir::load_bytecode('Tree/Optimizer.pbc');

plan(37);

{
    my $opt := Tree::Optimizer.new;
    my $test-input := [];
    ok($test-input =:= $opt.run($test-input),
       'Tree::Optimizer without any registered passes returns the input.');
}

{
    my $opt := Tree::Optimizer.new;
    my &transform := sub ($past) { $past; };
    $opt.register(&transform, :name<identity>);
    ok(1, 'A :name adverb can be supplied to Tree::Optimizer.register.');
    ok($opt.find-pass('identity') ~~ Tree::Optimizer::Pass,
       'Tree::Optimizer.find-pass returns a Tree::Optimizer::Pass, if found.');
    ok($opt.find-pass('identity').transformation =:= &transform,
       'Tree::Optimizer.find-pass returns a pass with correct .transformation.');

    ok(!pir::defined__IP($opt.find-pass('nonexistent-pass')),
       'Tree::Optimizer.find-pass with a non-existent name returns undef.');
}

{
    my $opt := Tree::Optimizer.new;
    my &transform := sub ($v) {
        -$v;
    };
    $opt.register(&transform);
    ok($opt.run(5) == -5,
       'Simple Sub pass runs correctly.');
    ok($opt.run(5) == -5,
       'Tree::Optimizer.run works repeatedly.');
}

{
    my &square := sub ($x) {
        $x * $x;
    }
    my &negate := sub ($x) {
        -$x;
    }
    {
        my $opt := Tree::Optimizer.new;
        $opt.register(&square, :name<square>);
        $opt.register(&negate, :depends-on<square>);
        ok($opt.run(2) == -4,
           'Correct order when registering a pass after its dependency.');
        ok($opt.run(2) == -4,
           'Repeated correct order when registering a pass after its dependency.');
    }
    {
        my $opt := Tree::Optimizer.new;
        $opt.register(&negate, :depends-on<square>);
        $opt.register(&square, :name<square>);
        ok($opt.run(2) == -4,
           'Correct order when registering a pass after its dependency.');
        ok($opt.run(2) == -4,
           'Repeated correct order when registering a pass after its dependency.');
    }
    {
        my $opt := Tree::Optimizer.new;
        $opt.register(&negate, :name<negate>);
        $opt.register(&square, :depends-on<negate>);
        ok($opt.run(2) == 4,
           'Correct order is not coincidental.');
    }
}

{
    my &inc := sub ($n) { $n + 1; };
    my $opt := Tree::Optimizer.new;
    $opt.register(&inc);
    $opt.register(&inc);
    ok($opt.run(0) == 2,
       'Multipled unnamed passes are allowed.');
}

{
    class Even {
        method ACCEPTS ($n, *%opts) {
            if $n % 2 == 0 {
                $n / 2;
            } else {
                0;
            }
        }
    }
    my &inc := sub ($n) { 2 * $n + 1; };
    {
        my $opt := Tree::Optimizer.new;
        $opt.register(&inc, :when(Even.new));
        ok($opt.run(2) == 3,
           'A pass with :when runs correctly when matching.');
    }
    {
        my $opt := Tree::Optimizer.new;
        $opt.register(&inc, :when(Even.new));
        ok($opt.run(1) == 1,
           'A pass with :when correctly does nothing when not matching.');
    }
}

pir::load_bytecode('PCT.pbc');
pir::load_bytecode('PAST/Pattern.pbc');
{
    my $past := PAST::Stmts.new(PAST::Val.new(:value(6)));
    my &inc := sub transform ($node) {
        if $node.match(PAST::Pattern::Val.new, :exact(1)) {
            $node.value($node.value + 1);
        }
{'fraction_non_alphanumeric': 0.186936604108172, 'fraction_numerical': 0.009753214127382887, 'mean_word_length': 2.2790697674418605, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6467642', 'n_tokens_mistral': 2364, 'n_tokens_neox': 2322, 'n_words': 515}