2 # Copyright (C) 2007, Parrot Foundation.
7 use lib qw( . lib ../lib ../../lib );
9 use Parrot::Test 'no_plan';
13 t/op/cc_params.t - Parrot Calling Conventions parameter matching tests
17 % prove t/op/cc_params.t
21 Tests Parrot calling conventions for parameter matching and mismatching.
25 my $t_testbody = <<'TESTBODY';
27 .include 'errors.pasm'
28 errorson .PARROT_ERRORS_PARAM_COUNT_FLAG
29 errorson .PARROT_ERRORS_RESULT_COUNT_FLAG
34 (@LIST_RESULTS@) = '@FUNC@'(@LIST_ARGS@)
42 .return(@LIST_RETURNS@)
46 my $t_expbody = <<'EXPBODY';
50 ## loop over test cases
52 for my $c_args ( 0 .. 1 ) {
54 for my $c_results ( 0 .. 1 ) {
56 for my $c_params ( 0 .. ( $c_args ? 2 : 1 ) ) {
58 for my $c_returns ( 0 .. ( $c_results ? 2 : 1 ) ) {
59 my $td = TemplateData->new;
61 ## initialize template keys
62 for ( $t_testbody =~ m/@(\w+)@/g ) { $td->addkey($_) }
64 for ( $t_expbody =~ m/@(\w+)@/g ) { $td->addkey($_) }
66 ## initialize template data
67 $td->initialize( $c_args, $c_params, $c_returns, $c_results );
69 ## generate tests and results
70 my $testbody = $td->generate($t_testbody);
71 my $expbody = $td->generate($t_expbody);
72 my $testhead = create_test_header( $c_args, $c_results, $c_params, $c_returns, );
75 if ( $expbody eq "ok\n" ) {
76 pir_output_like( $testbody, "/$expbody/", $testhead );
79 pir_error_output_like( $testbody, "/$expbody/", $testhead );
88 sub create_test_header {
89 return 'param mismatch: args:'
100 package TemplateData;
102 sub new { bless {} => shift; }
103 sub addkey { my $self = shift; $self->{$_} = '' for @_ }
109 $self->{C_ARGS} = $num;
111 if ( $self->{C_ARGS} ) {
112 $self->{_ARGS} = [ map { 'arg' . $_ } 1 .. $num ];
113 $self->{LIST_ARGS} = join ', ' => @{ $self->{_ARGS} };
114 $self->{INIT_ARGS} = ' .local int ' . $self->{LIST_ARGS} . $/;
115 $self->{INIT_ARGS} .= " ${ $self->{_ARGS} }[$_] = $_$/" for 0 .. $#{ $self->{_ARGS} };
123 $self->{C_PARAMS} = $num;
125 if ( $self->{C_PARAMS} ) {
126 $self->{_PARAMS} = [ map { 'param' . $_ } 1 .. $num ];
127 $self->{LIST_PARAMS} = join ', ' => @{ $self->{_PARAMS} };
128 $self->{INIT_PARAMS} =
129 join( "\n" => map { ' .param int ' . ${ $self->{_PARAMS} }[$_] }
130 0 .. $#{ $self->{_PARAMS} } );
133 $self->{INIT_PARAMS} = q{ get_params '()'};
141 $self->{C_RETURNS} = $num;
143 if ( $self->{C_RETURNS} ) {
144 $self->{_RETURNS} = [ map { 'return' . $_ } 1 .. $num ];
145 $self->{LIST_RETURNS} = join ', ' => @{ $self->{_RETURNS} };
146 $self->{INIT_RETURNS} = ' .local int ' . $self->{LIST_RETURNS} . $/;
147 $self->{INIT_RETURNS} .= " ${ $self->{_RETURNS} }[$_] = $_$/"
148 for 0 .. $#{ $self->{_RETURNS} };
156 $self->{C_RESULTS} = $num;
158 if ( $self->{C_RESULTS} ) {
159 $self->{_RESULTS} = [ map { 'result' . $_ } 1 .. $num ];
160 $self->{LIST_RESULTS} = join ', ' => @{ $self->{_RESULTS} };
161 $self->{INIT_RESULTS} =
162 join "\n" => ( map { ' .local int ' . $_ } @{ $self->{_RESULTS} } );
168 my ( $c_args, $c_params, $c_returns, $c_results ) = @_;
180 sub create_check_results {
182 my ( $c_args, $c_params, $c_returns, $c_results ) = @_;
184 $self->{CHECK_RESULTS} =
185 ( ( $c_args == $c_params ) and ( $c_results == $c_returns ) )
187 : 'too (many|few) ((positional|named) (arguments|returns)).*';
192 my ( $c_args, $c_params, $c_returns, $c_results ) = @_;
194 $self->create_args($c_args);
195 $self->create_params($c_params);
196 $self->create_returns($c_returns);
197 $self->create_results($c_results);
199 $self->create_func( $c_args, $c_params, $c_returns, $c_results );
201 $self->create_check_results( $c_args, $c_params, $c_returns, $c_results );
208 for ( $template =~ m/@(\w+)@/g ) {
209 my $replacement = ( exists $self->{$_} and defined $self->{$_} ) ? $self->{$_} : '';
211 $template =~ s/@(\w+)@/$replacement/;
218 # cperl-indent-level: 4
221 # vim: expandtab shiftwidth=4: