From b1c2d0f9468bfcddc0622a19372c43c921532f90 Mon Sep 17 00:00:00 2001 From: Azim Khan Date: Fri, 7 Jul 2017 17:14:02 +0100 Subject: [PATCH] Use switch instead if if-else for dependency and expression checks. --- tests/scripts/generate_code.py | 42 +++++++++++---------------------- tests/suites/main_test.function | 27 +++++++++++++++++---- 2 files changed, 36 insertions(+), 33 deletions(-) diff --git a/tests/scripts/generate_code.py b/tests/scripts/generate_code.py index 7af6fdf29..f81ec91cb 100644 --- a/tests/scripts/generate_code.py +++ b/tests/scripts/generate_code.py @@ -427,16 +427,15 @@ def gen_dep_check(dep_id, dep): noT, dep = ('!', dep[1:]) if dep[0] == '!' else ('', dep) assert len(dep) > 0, "Dependency should not be an empty string." dep_check = ''' -if ( dep_id == {id} ) -{{ -#if {noT}defined({macro}) - return( DEPENDENCY_SUPPORTED ); -#else - return( DEPENDENCY_NOT_SUPPORTED ); -#endif -}} -else -'''.format(noT=noT, macro=dep, id=dep_id) + case {id}: + {{ + #if {noT}defined({macro}) + ret = DEPENDENCY_SUPPORTED; + #else + ret = DEPENDENCY_NOT_SUPPORTED; + #endif + }} + break;'''.format(noT=noT, macro=dep, id=dep_id) return dep_check @@ -451,12 +450,11 @@ def gen_expression_check(exp_id, exp): assert exp_id > -1, "Expression Id should be a positive integer." assert len(exp) > 0, "Expression should not be an empty string." exp_code = ''' -if ( exp_id == {exp_id} ) -{{ - *out_value = {expression}; -}} -else -'''.format(exp_id=exp_id, expression=exp) + case {exp_id}: + {{ + *out_value = {expression}; + }} + break;'''.format(exp_id=exp_id, expression=exp) return exp_code @@ -527,28 +525,16 @@ def gen_suite_deps_checks(suite_deps, dep_check_code, expression_code): :param expression_code: :return: """ - # void unused params - if len(dep_check_code) == 0: - dep_check_code = '(void) dep_id;\n' - if len(expression_code) == 0: - expression_code = '(void) exp_id;\n' - expression_code += '(void) out_value;\n' - if len(suite_deps): ifdef = gen_deps_one_line(suite_deps) dep_check_code = ''' {ifdef} {code} -#else -(void) dep_id; #endif '''.format(ifdef=ifdef, code=dep_check_code) expression_code = ''' {ifdef} {code} -#else -(void) exp_id; -(void) out_value; #endif '''.format(ifdef=ifdef, code=expression_code) return dep_check_code, expression_code diff --git a/tests/suites/main_test.function b/tests/suites/main_test.function index e294e3621..93b32cc31 100644 --- a/tests/suites/main_test.function +++ b/tests/suites/main_test.function @@ -63,12 +63,22 @@ */ int get_expression( int32_t exp_id, int32_t * out_value ) {{ + int ret = KEY_VALUE_MAPPING_FOUND; + + (void) exp_id; + (void) out_value; + + switch( exp_id ) + {{ {expression_code} #line {line_no} "suites/main_test.function" - {{ - return( KEY_VALUE_MAPPING_NOT_FOUND ); + default: + {{ + ret = KEY_VALUE_MAPPING_NOT_FOUND; + }} + break; }} - return( KEY_VALUE_MAPPING_FOUND ); + return( ret ); }} @@ -85,11 +95,18 @@ int get_expression( int32_t exp_id, int32_t * out_value ) */ int dep_check( int dep_id ) {{ + int ret = DEPENDENCY_NOT_SUPPORTED; + + (void) dep_id; + + switch( dep_id ) + {{ {dep_check_code} #line {line_no} "suites/main_test.function" - {{ - return( DEPENDENCY_NOT_SUPPORTED ); + default: + break; }} + return( ret ); }}