Markus Pfeiffer | a26a005 | 2014-04-22 20:16:15 +0000 | [diff] [blame] | 1 | #!/usr/bin/env perl |
SimonB | e39088a | 2016-02-10 23:50:28 +0000 | [diff] [blame] | 2 | |
| 3 | # generate_code.pl |
| 4 | # |
Simon Butcher | 60411a8 | 2016-03-01 18:35:02 +0000 | [diff] [blame] | 5 | # Copyright (c) 2009-2016, ARM Limited, All Rights Reserved |
| 6 | # |
SimonB | 0284f58 | 2016-02-15 23:27:28 +0000 | [diff] [blame] | 7 | # Purpose |
| 8 | # |
SimonB | e39088a | 2016-02-10 23:50:28 +0000 | [diff] [blame] | 9 | # Generates the test suite code given inputs of the test suite directory that |
| 10 | # contain the test suites, and the test suite file names for the test code and |
| 11 | # test data. |
| 12 | # |
| 13 | # Usage: generate_code.pl <suite dir> <code file> <data file> [main code file] |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 14 | # |
SimonB | 0284f58 | 2016-02-15 23:27:28 +0000 | [diff] [blame] | 15 | # Structure of files |
| 16 | # |
| 17 | # - main code file - 'main_test.function' |
| 18 | # Template file that contains the main() function for the test suite, |
| 19 | # test dispatch code as well as support functions. It contains the |
| 20 | # following symbols which are substituted by this script during |
| 21 | # processing: |
| 22 | # TEST_FILENAME |
| 23 | # SUITE_PRE_DEP |
| 24 | # MAPPING_CODE |
| 25 | # FUNCTION CODE |
| 26 | # SUITE_POST_DEP |
| 27 | # DEP_CHECK_CODE |
| 28 | # DISPATCH_FUNCTION |
| 29 | # |
| 30 | # - common helper code file - 'helpers.function' |
| 31 | # Common helper functions |
| 32 | # |
| 33 | # - test suite code file - file name in the form 'test_suite_xxx.function' |
| 34 | # Code file that contains the actual test cases. The file contains a |
| 35 | # series of code sequences delimited by the following: |
| 36 | # BEGIN_HEADER / END_HEADER - list of headers files |
| 37 | # BEGIN_SUITE_HELPERS / END_SUITE_HELPERS - helper functions common to |
| 38 | # the test suite |
| 39 | # BEGIN_CASE / END_CASE - the test cases in the test suite. Each test |
| 40 | # case contains at least one function that is used to create the |
| 41 | # dispatch code. |
| 42 | # |
| 43 | # - test data file - file name in the form 'test_suite_xxxx.data' |
| 44 | # The test case parameters to to be used in execution of the test. The |
| 45 | # file name is used to replace the symbol 'TEST_FILENAME' in the main |
| 46 | # code file above. |
| 47 | # |
Gilles Peskine | e38900b | 2017-09-29 15:45:12 +0200 | [diff] [blame] | 48 | # A test data file consists of a sequence of paragraphs separated by |
| 49 | # a single empty line. Line breaks may be in Unix (LF) or Windows (CRLF) |
| 50 | # format. Lines starting with the character '#' are ignored |
| 51 | # (the parser behaves as if they were not present). |
| 52 | # |
| 53 | # Each paragraph describes one test case and must consist of: (1) one |
| 54 | # line which is the test case name; (2) an optional line starting with |
| 55 | # the 11-character prefix "depends_on:"; (3) a line containing the test |
| 56 | # function to execute and its parameters. |
| 57 | # |
| 58 | # A depends_on: line consists of a list of compile-time options |
| 59 | # separated by the character ':', with no whitespace. The test case |
| 60 | # is executed only if this compilation option is enabled in config.h. |
| 61 | # |
| 62 | # The last line of each paragraph contains a test function name and |
| 63 | # a list of parameters separated by the character ':'. Running the |
| 64 | # test case calls this function with the specified parameters. Each |
| 65 | # parameter may either be an integer written in decimal or hexadecimal, |
| 66 | # or a string surrounded by double quotes which may not contain the |
| 67 | # ':' character. |
SimonB | 0284f58 | 2016-02-15 23:27:28 +0000 | [diff] [blame] | 68 | # |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 69 | |
| 70 | use strict; |
| 71 | |
| 72 | my $suite_dir = shift or die "Missing suite directory"; |
| 73 | my $suite_name = shift or die "Missing suite name"; |
Paul Bakker | 46c1794 | 2011-07-13 14:54:54 +0000 | [diff] [blame] | 74 | my $data_name = shift or die "Missing data name"; |
Rich Evans | f4253c7 | 2015-01-14 19:23:00 +0000 | [diff] [blame] | 75 | my $test_main_file = do { my $arg = shift; defined($arg) ? $arg : $suite_dir."/main_test.function" }; |
Paul Bakker | 46c1794 | 2011-07-13 14:54:54 +0000 | [diff] [blame] | 76 | my $test_file = $data_name.".c"; |
SimonB | 0284f58 | 2016-02-15 23:27:28 +0000 | [diff] [blame] | 77 | my $test_common_helper_file = $suite_dir."/helpers.function"; |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 78 | my $test_case_file = $suite_dir."/".$suite_name.".function"; |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 79 | my $test_case_data = $suite_dir."/".$data_name.".data"; |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 80 | |
| 81 | my $line_separator = $/; |
| 82 | undef $/; |
| 83 | |
SimonB | 0284f58 | 2016-02-15 23:27:28 +0000 | [diff] [blame] | 84 | open(TEST_HELPERS, "$test_common_helper_file") or die "Opening test helpers |
| 85 | '$test_common_helper_file': $!"; |
| 86 | my $test_common_helpers = <TEST_HELPERS>; |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 87 | close(TEST_HELPERS); |
| 88 | |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 89 | open(TEST_MAIN, "$test_main_file") or die "Opening test main '$test_main_file': $!"; |
Gilles Peskine | be80726 | 2018-03-13 16:07:01 +0100 | [diff] [blame] | 90 | my @test_main_lines = split/^/, <TEST_MAIN>; |
| 91 | my $test_main; |
| 92 | my $index = 2; |
| 93 | for my $line (@test_main_lines) { |
| 94 | $line =~ s/!LINE_NO!/$index/; |
| 95 | $test_main = $test_main.$line; |
| 96 | $index++; |
| 97 | } |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 98 | close(TEST_MAIN); |
| 99 | |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 100 | open(TEST_CASES, "$test_case_file") or die "Opening test cases '$test_case_file': $!"; |
Gilles Peskine | be80726 | 2018-03-13 16:07:01 +0100 | [diff] [blame] | 101 | my @test_cases_lines = split/^/, <TEST_CASES>; |
| 102 | my $test_cases; |
| 103 | my $index = 2; |
| 104 | for my $line (@test_cases_lines) { |
| 105 | if ($line =~ /^\/\* BEGIN_SUITE_HELPERS .*\*\//) |
| 106 | { |
| 107 | $line = $line."#line $index \"$test_case_file\"\n"; |
| 108 | } |
| 109 | |
| 110 | if ($line =~ /^\/\* BEGIN_CASE .*\*\//) |
| 111 | { |
| 112 | $line = $line."#line $index \"$test_case_file\"\n"; |
| 113 | } |
| 114 | |
| 115 | $line =~ s/!LINE_NO!/$index/; |
| 116 | |
| 117 | $test_cases = $test_cases.$line; |
| 118 | $index++; |
| 119 | } |
| 120 | |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 121 | close(TEST_CASES); |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 122 | |
| 123 | open(TEST_DATA, "$test_case_data") or die "Opening test data '$test_case_data': $!"; |
| 124 | my $test_data = <TEST_DATA>; |
| 125 | close(TEST_DATA); |
| 126 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 127 | my ( $suite_header ) = $test_cases =~ /\/\* BEGIN_HEADER \*\/\n(.*?)\n\/\* END_HEADER \*\//s; |
| 128 | my ( $suite_defines ) = $test_cases =~ /\/\* BEGIN_DEPENDENCIES\n \* (.*?)\n \* END_DEPENDENCIES/s; |
SimonB | 0284f58 | 2016-02-15 23:27:28 +0000 | [diff] [blame] | 129 | my ( $suite_helpers ) = $test_cases =~ /\/\* BEGIN_SUITE_HELPERS \*\/\n(.*?)\n\/\* END_SUITE_HELPERS \*\//s; |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 130 | |
| 131 | my $requirements; |
| 132 | if ($suite_defines =~ /^depends_on:/) |
| 133 | { |
| 134 | ( $requirements ) = $suite_defines =~ /^depends_on:(.*)$/; |
| 135 | } |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 136 | |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 137 | my @var_req_arr = split(/:/, $requirements); |
| 138 | my $suite_pre_code; |
| 139 | my $suite_post_code; |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 140 | my $dispatch_code; |
| 141 | my $mapping_code; |
| 142 | my %mapping_values; |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 143 | |
| 144 | while (@var_req_arr) |
| 145 | { |
| 146 | my $req = shift @var_req_arr; |
Manuel Pégourié-Gonnard | e46c6c3 | 2015-03-23 13:59:10 +0100 | [diff] [blame] | 147 | $req =~ s/(!?)(.*)/$1defined($2)/; |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 148 | |
Manuel Pégourié-Gonnard | e46c6c3 | 2015-03-23 13:59:10 +0100 | [diff] [blame] | 149 | $suite_pre_code .= "#if $req\n"; |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 150 | $suite_post_code .= "#endif /* $req */\n"; |
| 151 | } |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 152 | |
| 153 | $/ = $line_separator; |
| 154 | |
| 155 | open(TEST_FILE, ">$test_file") or die "Opening destination file '$test_file': $!"; |
| 156 | print TEST_FILE << "END"; |
SimonB | 0284f58 | 2016-02-15 23:27:28 +0000 | [diff] [blame] | 157 | /* |
| 158 | * *** THIS FILE HAS BEEN MACHINE GENERATED *** |
| 159 | * |
| 160 | * This file has been machine generated using the script: $0 |
| 161 | * |
| 162 | * Test file : $test_file |
| 163 | * |
| 164 | * The following files were used to create this file. |
| 165 | * |
| 166 | * Main code file : $test_main_file |
| 167 | * Helper file : $test_common_helper_file |
| 168 | * Test suite file : $test_case_file |
Simon Butcher | 60411a8 | 2016-03-01 18:35:02 +0000 | [diff] [blame] | 169 | * Test suite data : $test_case_data |
SimonB | 0284f58 | 2016-02-15 23:27:28 +0000 | [diff] [blame] | 170 | * |
| 171 | * |
| 172 | * This file is part of mbed TLS (https://tls.mbed.org) |
| 173 | */ |
| 174 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 175 | #if !defined(MBEDTLS_CONFIG_FILE) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 176 | #include <mbedtls/config.h> |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 177 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 178 | #include MBEDTLS_CONFIG_FILE |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 179 | #endif |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 180 | |
SimonB | 0284f58 | 2016-02-15 23:27:28 +0000 | [diff] [blame] | 181 | |
| 182 | /*----------------------------------------------------------------------------*/ |
SimonB | 8bcd549 | 2016-02-17 23:34:30 +0000 | [diff] [blame] | 183 | /* Common helper code */ |
SimonB | 0284f58 | 2016-02-15 23:27:28 +0000 | [diff] [blame] | 184 | |
| 185 | $test_common_helpers |
| 186 | |
| 187 | |
| 188 | /*----------------------------------------------------------------------------*/ |
| 189 | /* Test Suite Code */ |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 190 | |
Paul Bakker | de56ca1 | 2013-09-15 17:05:21 +0200 | [diff] [blame] | 191 | $suite_pre_code |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 192 | $suite_header |
SimonB | 0284f58 | 2016-02-15 23:27:28 +0000 | [diff] [blame] | 193 | $suite_helpers |
Paul Bakker | de56ca1 | 2013-09-15 17:05:21 +0200 | [diff] [blame] | 194 | $suite_post_code |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 195 | |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 196 | END |
| 197 | |
Paul Bakker | b34fef2 | 2013-08-20 12:06:33 +0200 | [diff] [blame] | 198 | $test_main =~ s/SUITE_PRE_DEP/$suite_pre_code/; |
| 199 | $test_main =~ s/SUITE_POST_DEP/$suite_post_code/; |
| 200 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 201 | while($test_cases =~ /\/\* BEGIN_CASE *([\w:]*) \*\/\n(.*?)\n\/\* END_CASE \*\//msg) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 202 | { |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 203 | my $function_deps = $1; |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 204 | my $function_decl = $2; |
| 205 | |
| 206 | # Sanity checks of function |
Gilles Peskine | be80726 | 2018-03-13 16:07:01 +0100 | [diff] [blame] | 207 | if ($function_decl !~ /^#line\s*.*\nvoid /) |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 208 | { |
| 209 | die "Test function does not have 'void' as return type\n"; |
Gilles Peskine | be80726 | 2018-03-13 16:07:01 +0100 | [diff] [blame] | 210 | "Function declaration:\n" . |
| 211 | $function_decl; |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 212 | } |
Gilles Peskine | be80726 | 2018-03-13 16:07:01 +0100 | [diff] [blame] | 213 | if ($function_decl !~ /^(#line\s*.*)\nvoid (\w+)\(\s*(.*?)\s*\)\s*{(.*)}/ms) |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 214 | { |
| 215 | die "Function declaration not in expected format\n"; |
| 216 | } |
Gilles Peskine | be80726 | 2018-03-13 16:07:01 +0100 | [diff] [blame] | 217 | my $line_directive = $1; |
| 218 | my $function_name = $2; |
| 219 | my $function_params = $3; |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 220 | my $function_pre_code; |
| 221 | my $function_post_code; |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 222 | my $param_defs; |
| 223 | my $param_checks; |
| 224 | my @dispatch_params; |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 225 | my @var_def_arr = split(/,\s*/, $function_params); |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 226 | my $i = 1; |
| 227 | my $mapping_regex = "".$function_name; |
| 228 | my $mapping_count = 0; |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 229 | |
Gilles Peskine | be80726 | 2018-03-13 16:07:01 +0100 | [diff] [blame] | 230 | $function_decl =~ s/(^#line\s*.*)\nvoid /$1\nvoid test_suite_/; |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 231 | |
Paul Bakker | 318d0fe | 2014-07-10 14:59:25 +0200 | [diff] [blame] | 232 | # Add exit label if not present |
| 233 | if ($function_decl !~ /^exit:$/m) |
| 234 | { |
| 235 | $function_decl =~ s/}\s*$/\nexit:\n return;\n}/; |
| 236 | } |
| 237 | |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 238 | if ($function_deps =~ /^depends_on:/) |
Paul Bakker | ccff167 | 2009-10-03 19:57:10 +0000 | [diff] [blame] | 239 | { |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 240 | ( $function_deps ) = $function_deps =~ /^depends_on:(.*)$/; |
Paul Bakker | ccff167 | 2009-10-03 19:57:10 +0000 | [diff] [blame] | 241 | } |
| 242 | |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 243 | foreach my $req (split(/:/, $function_deps)) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 244 | { |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 245 | $function_pre_code .= "#ifdef $req\n"; |
| 246 | $function_post_code .= "#endif /* $req */\n"; |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 247 | } |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 248 | |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 249 | foreach my $def (@var_def_arr) |
| 250 | { |
| 251 | # Handle the different parameter types |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 252 | if( substr($def, 0, 4) eq "int " ) |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 253 | { |
| 254 | $param_defs .= " int param$i;\n"; |
| 255 | $param_checks .= " if( verify_int( params[$i], ¶m$i ) != 0 ) return( 2 );\n"; |
| 256 | push @dispatch_params, "param$i"; |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 257 | |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 258 | $mapping_regex .= ":([\\d\\w |\\+\\-\\(\\)]+)"; |
| 259 | $mapping_count++; |
| 260 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 261 | elsif( substr($def, 0, 6) eq "char *" ) |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 262 | { |
| 263 | $param_defs .= " char *param$i = params[$i];\n"; |
| 264 | $param_checks .= " if( verify_string( ¶m$i ) != 0 ) return( 2 );\n"; |
| 265 | push @dispatch_params, "param$i"; |
Andres AG | f083b31 | 2017-02-02 14:36:49 +0000 | [diff] [blame] | 266 | $mapping_regex .= ":(?:\\\\.|[^:\n])+"; |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 267 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 268 | else |
| 269 | { |
| 270 | die "Parameter declaration not of supported type (int, char *)\n"; |
| 271 | } |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 272 | $i++; |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 273 | |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | # Find non-integer values we should map for this function |
| 277 | if( $mapping_count) |
| 278 | { |
| 279 | my @res = $test_data =~ /^$mapping_regex/msg; |
| 280 | foreach my $value (@res) |
| 281 | { |
Manuel Pégourié-Gonnard | 18c443d | 2013-10-17 14:58:24 +0200 | [diff] [blame] | 282 | next unless ($value !~ /^\d+$/); |
| 283 | if ( $mapping_values{$value} ) { |
| 284 | ${ $mapping_values{$value} }{$function_pre_code} = 1; |
| 285 | } else { |
| 286 | $mapping_values{$value} = { $function_pre_code => 1 }; |
| 287 | } |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 288 | } |
| 289 | } |
| 290 | |
| 291 | my $call_params = join ", ", @dispatch_params; |
| 292 | my $param_count = @var_def_arr + 1; |
| 293 | $dispatch_code .= << "END"; |
| 294 | if( strcmp( params[0], "$function_name" ) == 0 ) |
| 295 | { |
| 296 | $function_pre_code |
| 297 | $param_defs |
| 298 | if( cnt != $param_count ) |
| 299 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 300 | mbedtls_fprintf( stderr, "\\nIncorrect argument count (%d != %d)\\n", cnt, $param_count ); |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 301 | return( 2 ); |
| 302 | } |
| 303 | |
| 304 | $param_checks |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 305 | test_suite_$function_name( $call_params ); |
| 306 | return ( 0 ); |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 307 | $function_post_code |
| 308 | return ( 3 ); |
| 309 | } |
| 310 | else |
| 311 | END |
| 312 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 313 | my $function_code = $function_pre_code . $function_decl . "\n" . $function_post_code; |
| 314 | $test_main =~ s/FUNCTION_CODE/$function_code\nFUNCTION_CODE/; |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | # Find specific case dependencies that we should be able to check |
| 318 | # and make check code |
| 319 | my $dep_check_code; |
| 320 | |
Hanno Becker | 276d530 | 2017-07-23 10:24:22 +0100 | [diff] [blame] | 321 | my @res = $test_data =~ /^depends_on:([!:\w]+)/msg; |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 322 | my %case_deps; |
| 323 | foreach my $deps (@res) |
| 324 | { |
| 325 | foreach my $dep (split(/:/, $deps)) |
| 326 | { |
| 327 | $case_deps{$dep} = 1; |
| 328 | } |
| 329 | } |
| 330 | while( my ($key, $value) = each(%case_deps) ) |
| 331 | { |
Hanno Becker | 276d530 | 2017-07-23 10:24:22 +0100 | [diff] [blame] | 332 | if( substr($key, 0, 1) eq "!" ) |
| 333 | { |
| 334 | my $key = substr($key, 1); |
| 335 | $dep_check_code .= << "END"; |
| 336 | if( strcmp( str, "!$key" ) == 0 ) |
| 337 | { |
| 338 | #if !defined($key) |
| 339 | return( 0 ); |
| 340 | #else |
| 341 | return( 1 ); |
| 342 | #endif |
| 343 | } |
| 344 | END |
| 345 | } |
| 346 | else |
| 347 | { |
| 348 | $dep_check_code .= << "END"; |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 349 | if( strcmp( str, "$key" ) == 0 ) |
| 350 | { |
| 351 | #if defined($key) |
| 352 | return( 0 ); |
| 353 | #else |
| 354 | return( 1 ); |
| 355 | #endif |
| 356 | } |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 357 | END |
Hanno Becker | 276d530 | 2017-07-23 10:24:22 +0100 | [diff] [blame] | 358 | } |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 359 | } |
| 360 | |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 361 | # Make mapping code |
| 362 | while( my ($key, $value) = each(%mapping_values) ) |
| 363 | { |
Manuel Pégourié-Gonnard | 18c443d | 2013-10-17 14:58:24 +0200 | [diff] [blame] | 364 | my $key_mapping_code = << "END"; |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 365 | if( strcmp( str, "$key" ) == 0 ) |
| 366 | { |
| 367 | *value = ( $key ); |
| 368 | return( 0 ); |
| 369 | } |
| 370 | END |
Manuel Pégourié-Gonnard | 18c443d | 2013-10-17 14:58:24 +0200 | [diff] [blame] | 371 | |
| 372 | # handle depenencies, unless used at least one without depends |
| 373 | if ($value->{""}) { |
| 374 | $mapping_code .= $key_mapping_code; |
| 375 | next; |
| 376 | } |
| 377 | for my $ifdef ( keys %$value ) { |
| 378 | (my $endif = $ifdef) =~ s!ifdef!endif //!g; |
| 379 | $mapping_code .= $ifdef . $key_mapping_code . $endif; |
| 380 | } |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | $dispatch_code =~ s/^(.+)/ $1/mg; |
| 384 | |
| 385 | $test_main =~ s/TEST_FILENAME/$test_case_data/; |
| 386 | $test_main =~ s/FUNCTION_CODE//; |
| 387 | $test_main =~ s/DEP_CHECK_CODE/$dep_check_code/; |
| 388 | $test_main =~ s/DISPATCH_FUNCTION/$dispatch_code/; |
| 389 | $test_main =~ s/MAPPING_CODE/$mapping_code/; |
| 390 | |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 391 | print TEST_FILE << "END"; |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 392 | $test_main |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 393 | END |
| 394 | |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 395 | close(TEST_FILE); |