Use switch instead if if-else for dependency and expression checks.

This commit is contained in:
Azim Khan 2017-07-07 17:14:02 +01:00 committed by Mohammad Azim Khan
parent d61a4384d8
commit b1c2d0f946
2 changed files with 36 additions and 33 deletions

View file

@ -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

View file

@ -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 );
}}