Added support for per test suite helper functions

Added to generate_code.pl:
    - support for per test suite helper functions
    - description of the structure of the files the script uses to construct
      the test suite file
    - delimiters through the source code to make the machine generated code
      easier to understand
This commit is contained in:
SimonB 2016-02-15 23:27:28 +00:00 committed by Simon Butcher
parent 718548d5c9
commit 6fb9db3afd
2 changed files with 81 additions and 4 deletions

View file

@ -2,11 +2,47 @@
# generate_code.pl # generate_code.pl
# #
# Purpose
#
# Generates the test suite code given inputs of the test suite directory that # Generates the test suite code given inputs of the test suite directory that
# contain the test suites, and the test suite file names for the test code and # contain the test suites, and the test suite file names for the test code and
# test data. # test data.
# #
# Usage: generate_code.pl <suite dir> <code file> <data file> [main code file] # Usage: generate_code.pl <suite dir> <code file> <data file> [main code file]
#
# Structure of files
#
# - main code file - 'main_test.function'
# Template file that contains the main() function for the test suite,
# test dispatch code as well as support functions. It contains the
# following symbols which are substituted by this script during
# processing:
# TEST_FILENAME
# SUITE_PRE_DEP
# MAPPING_CODE
# FUNCTION CODE
# SUITE_POST_DEP
# DEP_CHECK_CODE
# DISPATCH_FUNCTION
#
# - common helper code file - 'helpers.function'
# Common helper functions
#
# - test suite code file - file name in the form 'test_suite_xxx.function'
# Code file that contains the actual test cases. The file contains a
# series of code sequences delimited by the following:
# BEGIN_HEADER / END_HEADER - list of headers files
# BEGIN_SUITE_HELPERS / END_SUITE_HELPERS - helper functions common to
# the test suite
# BEGIN_CASE / END_CASE - the test cases in the test suite. Each test
# case contains at least one function that is used to create the
# dispatch code.
#
# - test data file - file name in the form 'test_suite_xxxx.data'
# The test case parameters to to be used in execution of the test. The
# file name is used to replace the symbol 'TEST_FILENAME' in the main code
# file above.
#
use strict; use strict;
@ -15,15 +51,16 @@ my $suite_name = shift or die "Missing suite name";
my $data_name = shift or die "Missing data name"; my $data_name = shift or die "Missing data name";
my $test_main_file = do { my $arg = shift; defined($arg) ? $arg : $suite_dir."/main_test.function" }; my $test_main_file = do { my $arg = shift; defined($arg) ? $arg : $suite_dir."/main_test.function" };
my $test_file = $data_name.".c"; my $test_file = $data_name.".c";
my $test_helper_file = $suite_dir."/helpers.function"; my $test_common_helper_file = $suite_dir."/helpers.function";
my $test_case_file = $suite_dir."/".$suite_name.".function"; my $test_case_file = $suite_dir."/".$suite_name.".function";
my $test_case_data = $suite_dir."/".$data_name.".data"; my $test_case_data = $suite_dir."/".$data_name.".data";
my $line_separator = $/; my $line_separator = $/;
undef $/; undef $/;
open(TEST_HELPERS, "$test_helper_file") or die "Opening test helpers '$test_helper_file': $!"; open(TEST_HELPERS, "$test_common_helper_file") or die "Opening test helpers
my $test_helpers = <TEST_HELPERS>; '$test_common_helper_file': $!";
my $test_common_helpers = <TEST_HELPERS>;
close(TEST_HELPERS); close(TEST_HELPERS);
open(TEST_MAIN, "$test_main_file") or die "Opening test main '$test_main_file': $!"; open(TEST_MAIN, "$test_main_file") or die "Opening test main '$test_main_file': $!";
@ -40,6 +77,7 @@ close(TEST_DATA);
my ( $suite_header ) = $test_cases =~ /\/\* BEGIN_HEADER \*\/\n(.*?)\n\/\* END_HEADER \*\//s; my ( $suite_header ) = $test_cases =~ /\/\* BEGIN_HEADER \*\/\n(.*?)\n\/\* END_HEADER \*\//s;
my ( $suite_defines ) = $test_cases =~ /\/\* BEGIN_DEPENDENCIES\n \* (.*?)\n \* END_DEPENDENCIES/s; my ( $suite_defines ) = $test_cases =~ /\/\* BEGIN_DEPENDENCIES\n \* (.*?)\n \* END_DEPENDENCIES/s;
my ( $suite_helpers ) = $test_cases =~ /\/\* BEGIN_SUITE_HELPERS \*\/\n(.*?)\n\/\* END_SUITE_HELPERS \*\//s;
my $requirements; my $requirements;
if ($suite_defines =~ /^depends_on:/) if ($suite_defines =~ /^depends_on:/)
@ -67,16 +105,43 @@ $/ = $line_separator;
open(TEST_FILE, ">$test_file") or die "Opening destination file '$test_file': $!"; open(TEST_FILE, ">$test_file") or die "Opening destination file '$test_file': $!";
print TEST_FILE << "END"; print TEST_FILE << "END";
/*
* *** THIS FILE HAS BEEN MACHINE GENERATED ***
*
* This file has been machine generated using the script: $0
*
* Test file : $test_file
*
* The following files were used to create this file.
*
* Main code file : $test_main_file
* Helper file : $test_common_helper_file
* Test suite file : $test_case_file
* Test suite daya : $test_case_data
*
*
* This file is part of mbed TLS (https://tls.mbed.org)
*/
#if !defined(MBEDTLS_CONFIG_FILE) #if !defined(MBEDTLS_CONFIG_FILE)
#include <mbedtls/config.h> #include <mbedtls/config.h>
#else #else
#include MBEDTLS_CONFIG_FILE #include MBEDTLS_CONFIG_FILE
#endif #endif
$test_helpers
/*----------------------------------------------------------------------------*/
/* Common helper functions */
$test_common_helpers
/*----------------------------------------------------------------------------*/
/* Test Suite Code */
$suite_pre_code $suite_pre_code
$suite_header $suite_header
$suite_helpers
$suite_post_code $suite_post_code
END END

View file

@ -101,9 +101,17 @@ MAPPING_CODE
return( -1 ); return( -1 );
} }
/*----------------------------------------------------------------------------*/
/* Test Case code */
FUNCTION_CODE FUNCTION_CODE
SUITE_POST_DEP SUITE_POST_DEP
/*----------------------------------------------------------------------------*/
/* Test dispatch code */
int dep_check( char *str ) int dep_check( char *str )
{ {
if( str == NULL ) if( str == NULL )
@ -133,6 +141,10 @@ DISPATCH_FUNCTION
return( ret ); return( ret );
} }
/*----------------------------------------------------------------------------*/
/* Main Test code */
int get_line( FILE *f, char *buf, size_t len ) int get_line( FILE *f, char *buf, size_t len )
{ {
char *ret; char *ret;