How to use make_test_case method in gabbi

Best Python code snippet using gabbi_python

create_test_cases.py

Source:create_test_cases.py Github

copy

Full Screen

...26import subprocess27import sys28sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..')) # For access to sexps.py, which is in parent dir29from sexps import *30def make_test_case(f_name, ret_type, body):31 """Create a simple optimization test case consisting of a single32 function with the given name, return type, and body.33 Global declarations are automatically created for any undeclared34 variables that are referenced by the function. All undeclared35 variables are assumed to be floats.36 """37 check_sexp(body)38 declarations = {}39 def make_declarations(sexp, already_declared = ()):40 if isinstance(sexp, list):41 if len(sexp) == 2 and sexp[0] == 'var_ref':42 if sexp[1] not in already_declared:43 declarations[sexp[1]] = [44 'declare', ['in'], 'float', sexp[1]]45 elif len(sexp) == 4 and sexp[0] == 'assign':46 assert sexp[2][0] == 'var_ref'47 if sexp[2][1] not in already_declared:48 declarations[sexp[2][1]] = [49 'declare', ['out'], 'float', sexp[2][1]]50 make_declarations(sexp[3], already_declared)51 else:52 already_declared = set(already_declared)53 for s in sexp:54 if isinstance(s, list) and len(s) >= 4 and \55 s[0] == 'declare':56 already_declared.add(s[3])57 else:58 make_declarations(s, already_declared)59 make_declarations(body)60 return declarations.values() + \61 [['function', f_name, ['signature', ret_type, ['parameters'], body]]]62# The following functions can be used to build expressions.63def const_float(value):64 """Create an expression representing the given floating point value."""65 return ['constant', 'float', ['{0:.6f}'.format(value)]]66def const_bool(value):67 """Create an expression representing the given boolean value.68 If value is not a boolean, it is converted to a boolean. So, for69 instance, const_bool(1) is equivalent to const_bool(True).70 """71 return ['constant', 'bool', ['{0}'.format(1 if value else 0)]]72def gt_zero(var_name):73 """Create Construct the expression var_name > 0"""74 return ['expression', 'bool', '>', ['var_ref', var_name], const_float(0)]75# The following functions can be used to build complex control flow76# statements. All of these functions return statement lists (even77# those which only create a single statement), so that statements can78# be sequenced together using the '+' operator.79def return_(value = None):80 """Create a return statement."""81 if value is not None:82 return [['return', value]]83 else:84 return [['return']]85def break_():86 """Create a break statement."""87 return ['break']88def continue_():89 """Create a continue statement."""90 return ['continue']91def simple_if(var_name, then_statements, else_statements = None):92 """Create a statement of the form93 if (var_name > 0.0) {94 <then_statements>95 } else {96 <else_statements>97 }98 else_statements may be omitted.99 """100 if else_statements is None:101 else_statements = []102 check_sexp(then_statements)103 check_sexp(else_statements)104 return [['if', gt_zero(var_name), then_statements, else_statements]]105def loop(statements):106 """Create a loop containing the given statements as its loop107 body.108 """109 check_sexp(statements)110 return [['loop', [], [], [], [], statements]]111def declare_temp(var_type, var_name):112 """Create a declaration of the form113 (declare (temporary) <var_type> <var_name)114 """115 return [['declare', ['temporary'], var_type, var_name]]116def assign_x(var_name, value):117 """Create a statement that assigns <value> to the variable118 <var_name>. The assignment uses the mask (x).119 """120 check_sexp(value)121 return [['assign', ['x'], ['var_ref', var_name], value]]122def complex_if(var_prefix, statements):123 """Create a statement of the form124 if (<var_prefix>a > 0.0) {125 if (<var_prefix>b > 0.0) {126 <statements>127 }128 }129 This is useful in testing jump lowering, because if <statements>130 ends in a jump, lower_jumps.cpp won't try to combine this131 construct with the code that follows it, as it might do for a132 simple if.133 All variables used in the if statement are prefixed with134 var_prefix. This can be used to ensure uniqueness.135 """136 check_sexp(statements)137 return simple_if(var_prefix + 'a', simple_if(var_prefix + 'b', statements))138def declare_execute_flag():139 """Create the statements that lower_jumps.cpp uses to declare and140 initialize the temporary boolean execute_flag.141 """142 return declare_temp('bool', 'execute_flag') + \143 assign_x('execute_flag', const_bool(True))144def declare_return_flag():145 """Create the statements that lower_jumps.cpp uses to declare and146 initialize the temporary boolean return_flag.147 """148 return declare_temp('bool', 'return_flag') + \149 assign_x('return_flag', const_bool(False))150def declare_return_value():151 """Create the statements that lower_jumps.cpp uses to declare and152 initialize the temporary variable return_value. Assume that153 return_value is a float.154 """155 return declare_temp('float', 'return_value')156def declare_break_flag():157 """Create the statements that lower_jumps.cpp uses to declare and158 initialize the temporary boolean break_flag.159 """160 return declare_temp('bool', 'break_flag') + \161 assign_x('break_flag', const_bool(False))162def lowered_return_simple(value = None):163 """Create the statements that lower_jumps.cpp lowers a return164 statement to, in situations where it does not need to clear the165 execute flag.166 """167 if value:168 result = assign_x('return_value', value)169 else:170 result = []171 return result + assign_x('return_flag', const_bool(True))172def lowered_return(value = None):173 """Create the statements that lower_jumps.cpp lowers a return174 statement to, in situations where it needs to clear the execute175 flag.176 """177 return lowered_return_simple(value) + \178 assign_x('execute_flag', const_bool(False))179def lowered_continue():180 """Create the statement that lower_jumps.cpp lowers a continue181 statement to.182 """183 return assign_x('execute_flag', const_bool(False))184def lowered_break_simple():185 """Create the statement that lower_jumps.cpp lowers a break186 statement to, in situations where it does not need to clear the187 execute flag.188 """189 return assign_x('break_flag', const_bool(True))190def lowered_break():191 """Create the statement that lower_jumps.cpp lowers a break192 statement to, in situations where it needs to clear the execute193 flag.194 """195 return lowered_break_simple() + assign_x('execute_flag', const_bool(False))196def if_execute_flag(statements):197 """Wrap statements in an if test so that they will only execute if198 execute_flag is True.199 """200 check_sexp(statements)201 return [['if', ['var_ref', 'execute_flag'], statements, []]]202def if_not_return_flag(statements):203 """Wrap statements in an if test so that they will only execute if204 return_flag is False.205 """206 check_sexp(statements)207 return [['if', ['var_ref', 'return_flag'], [], statements]]208def final_return():209 """Create the return statement that lower_jumps.cpp places at the210 end of a function when lowering returns.211 """212 return [['return', ['var_ref', 'return_value']]]213def final_break():214 """Create the conditional break statement that lower_jumps.cpp215 places at the end of a function when lowering breaks.216 """217 return [['if', ['var_ref', 'break_flag'], break_(), []]]218def bash_quote(*args):219 """Quote the arguments appropriately so that bash will understand220 each argument as a single word.221 """222 def quote_word(word):223 for c in word:224 if not (c.isalpha() or c.isdigit() or c in '@%_-+=:,./'):225 break226 else:227 if not word:228 return "''"229 return word230 return "'{0}'".format(word.replace("'", "'\"'\"'"))231 return ' '.join(quote_word(word) for word in args)232def create_test_case(doc_string, input_sexp, expected_sexp, test_name,233 pull_out_jumps=False, lower_sub_return=False,234 lower_main_return=False, lower_continue=False,235 lower_break=False):236 """Create a test case that verifies that do_lower_jumps transforms237 the given code in the expected way.238 """239 doc_lines = [line.strip() for line in doc_string.splitlines()]240 doc_string = ''.join('# {0}\n'.format(line) for line in doc_lines if line != '')241 check_sexp(input_sexp)242 check_sexp(expected_sexp)243 input_str = sexp_to_string(sort_decls(input_sexp))244 expected_output = sexp_to_string(sort_decls(expected_sexp))245 optimization = (246 'do_lower_jumps({0:d}, {1:d}, {2:d}, {3:d}, {4:d})'.format(247 pull_out_jumps, lower_sub_return, lower_main_return,248 lower_continue, lower_break))249 args = ['../../glsl_test', 'optpass', '--quiet', '--input-ir', optimization]250 test_file = '{0}.opt_test'.format(test_name)251 with open(test_file, 'w') as f:252 f.write('#!/bin/bash\n#\n# This file was generated by create_test_cases.py.\n#\n')253 f.write(doc_string)254 f.write('{0} <<EOF\n'.format(bash_quote(*args)))255 f.write('{0}\nEOF\n'.format(input_str))256 os.chmod(test_file, 0774)257 expected_file = '{0}.opt_test.expected'.format(test_name)258 with open(expected_file, 'w') as f:259 f.write('{0}\n'.format(expected_output))260def test_lower_returns_main():261 doc_string = """Test that do_lower_jumps respects the lower_main_return262 flag in deciding whether to lower returns in the main263 function.264 """265 input_sexp = make_test_case('main', 'void', (266 complex_if('', return_())267 ))268 expected_sexp = make_test_case('main', 'void', (269 declare_execute_flag() +270 declare_return_flag() +271 complex_if('', lowered_return())272 ))273 create_test_case(doc_string, input_sexp, expected_sexp, 'lower_returns_main_true',274 lower_main_return=True)275 create_test_case(doc_string, input_sexp, input_sexp, 'lower_returns_main_false',276 lower_main_return=False)277def test_lower_returns_sub():278 doc_string = """Test that do_lower_jumps respects the lower_sub_return flag279 in deciding whether to lower returns in subroutines.280 """281 input_sexp = make_test_case('sub', 'void', (282 complex_if('', return_())283 ))284 expected_sexp = make_test_case('sub', 'void', (285 declare_execute_flag() +286 declare_return_flag() +287 complex_if('', lowered_return())288 ))289 create_test_case(doc_string, input_sexp, expected_sexp, 'lower_returns_sub_true',290 lower_sub_return=True)291 create_test_case(doc_string, input_sexp, input_sexp, 'lower_returns_sub_false',292 lower_sub_return=False)293def test_lower_returns_1():294 doc_string = """Test that a void return at the end of a function is295 eliminated.296 """297 input_sexp = make_test_case('main', 'void', (298 assign_x('a', const_float(1)) +299 return_()300 ))301 expected_sexp = make_test_case('main', 'void', (302 assign_x('a', const_float(1))303 ))304 create_test_case(doc_string, input_sexp, expected_sexp, 'lower_returns_1',305 lower_main_return=True)306def test_lower_returns_2():307 doc_string = """Test that lowering is not performed on a non-void return at308 the end of subroutine.309 """310 input_sexp = make_test_case('sub', 'float', (311 assign_x('a', const_float(1)) +312 return_(const_float(1))313 ))314 create_test_case(doc_string, input_sexp, input_sexp, 'lower_returns_2',315 lower_sub_return=True)316def test_lower_returns_3():317 doc_string = """Test lowering of returns when there is one nested inside a318 complex structure of ifs, and one at the end of a function.319 In this case, the latter return needs to be lowered because it320 will not be at the end of the function once the final return321 is inserted.322 """323 input_sexp = make_test_case('sub', 'float', (324 complex_if('', return_(const_float(1))) +325 return_(const_float(2))326 ))327 expected_sexp = make_test_case('sub', 'float', (328 declare_execute_flag() +329 declare_return_value() +330 declare_return_flag() +331 complex_if('', lowered_return(const_float(1))) +332 if_execute_flag(lowered_return(const_float(2))) +333 final_return()334 ))335 create_test_case(doc_string, input_sexp, expected_sexp, 'lower_returns_3',336 lower_sub_return=True)337def test_lower_returns_4():338 doc_string = """Test that returns are properly lowered when they occur in339 both branches of an if-statement.340 """341 input_sexp = make_test_case('sub', 'float', (342 simple_if('a', return_(const_float(1)),343 return_(const_float(2)))344 ))345 expected_sexp = make_test_case('sub', 'float', (346 declare_execute_flag() +347 declare_return_value() +348 declare_return_flag() +349 simple_if('a', lowered_return(const_float(1)),350 lowered_return(const_float(2))) +351 final_return()352 ))353 create_test_case(doc_string, input_sexp, expected_sexp, 'lower_returns_4',354 lower_sub_return=True)355def test_lower_unified_returns():356 doc_string = """If both branches of an if statement end in a return, and357 pull_out_jumps is True, then those returns should be lifted358 outside the if and then properly lowered.359 Verify that this lowering occurs during the same pass as the360 lowering of other returns by checking that extra temporary361 variables aren't generated.362 """363 input_sexp = make_test_case('main', 'void', (364 complex_if('a', return_()) +365 simple_if('b', simple_if('c', return_(), return_()))366 ))367 expected_sexp = make_test_case('main', 'void', (368 declare_execute_flag() +369 declare_return_flag() +370 complex_if('a', lowered_return()) +371 if_execute_flag(simple_if('b', (simple_if('c', [], []) +372 lowered_return())))373 ))374 create_test_case(doc_string, input_sexp, expected_sexp, 'lower_unified_returns',375 lower_main_return=True, pull_out_jumps=True)376def test_lower_pulled_out_jump():377 doc_string = """If one branch of an if ends in a jump, and control cannot378 fall out the bottom of the other branch, and pull_out_jumps is379 True, then the jump is lifted outside the if.380 Verify that this lowering occurs during the same pass as the381 lowering of other jumps by checking that extra temporary382 variables aren't generated.383 """384 input_sexp = make_test_case('main', 'void', (385 complex_if('a', return_()) +386 loop(simple_if('b', simple_if('c', break_(), continue_()),387 return_())) +388 assign_x('d', const_float(1))389 ))390 # Note: optimization produces two other effects: the break391 # gets lifted out of the if statements, and the code after the392 # loop gets guarded so that it only executes if the return393 # flag is clear.394 expected_sexp = make_test_case('main', 'void', (395 declare_execute_flag() +396 declare_return_flag() +397 complex_if('a', lowered_return()) +398 if_execute_flag(399 loop(simple_if('b', simple_if('c', [], continue_()),400 lowered_return_simple()) +401 break_()) +402 if_not_return_flag(assign_x('d', const_float(1))))403 ))404 create_test_case(doc_string, input_sexp, expected_sexp, 'lower_pulled_out_jump',405 lower_main_return=True, pull_out_jumps=True)406def test_lower_breaks_1():407 doc_string = """If a loop contains an unconditional break at the bottom of408 it, it should not be lowered."""409 input_sexp = make_test_case('main', 'void', (410 loop(assign_x('a', const_float(1)) +411 break_())412 ))413 expected_sexp = input_sexp414 create_test_case(doc_string, input_sexp, expected_sexp, 'lower_breaks_1', lower_break=True)415def test_lower_breaks_2():416 doc_string = """If a loop contains a conditional break at the bottom of it,417 it should not be lowered if it is in the then-clause.418 """419 input_sexp = make_test_case('main', 'void', (420 loop(assign_x('a', const_float(1)) +421 simple_if('b', break_()))422 ))423 expected_sexp = input_sexp424 create_test_case(doc_string, input_sexp, expected_sexp, 'lower_breaks_2', lower_break=True)425def test_lower_breaks_3():426 doc_string = """If a loop contains a conditional break at the bottom of it,427 it should not be lowered if it is in the then-clause, even if428 there are statements preceding the break.429 """430 input_sexp = make_test_case('main', 'void', (431 loop(assign_x('a', const_float(1)) +432 simple_if('b', (assign_x('c', const_float(1)) +433 break_())))434 ))435 expected_sexp = input_sexp436 create_test_case(doc_string, input_sexp, expected_sexp, 'lower_breaks_3', lower_break=True)437def test_lower_breaks_4():438 doc_string = """If a loop contains a conditional break at the bottom of it,439 it should not be lowered if it is in the else-clause.440 """441 input_sexp = make_test_case('main', 'void', (442 loop(assign_x('a', const_float(1)) +443 simple_if('b', [], break_()))444 ))445 expected_sexp = input_sexp446 create_test_case(doc_string, input_sexp, expected_sexp, 'lower_breaks_4', lower_break=True)447def test_lower_breaks_5():448 doc_string = """If a loop contains a conditional break at the bottom of it,449 it should not be lowered if it is in the else-clause, even if450 there are statements preceding the break.451 """452 input_sexp = make_test_case('main', 'void', (453 loop(assign_x('a', const_float(1)) +454 simple_if('b', [], (assign_x('c', const_float(1)) +455 break_())))456 ))457 expected_sexp = input_sexp458 create_test_case(doc_string, input_sexp, expected_sexp, 'lower_breaks_5', lower_break=True)459def test_lower_breaks_6():460 doc_string = """If a loop contains conditional breaks and continues, and461 ends in an unconditional break, then the unconditional break462 needs to be lowered, because it will no longer be at the end463 of the loop after the final break is added.464 """465 input_sexp = make_test_case('main', 'void', (466 loop(simple_if('a', (complex_if('b', continue_()) +467 complex_if('c', break_()))) +468 break_())469 ))470 expected_sexp = make_test_case('main', 'void', (471 declare_break_flag() +472 loop(declare_execute_flag() +473 simple_if(474 'a',475 (complex_if('b', lowered_continue()) +476 if_execute_flag(477 complex_if('c', lowered_break())))) +478 if_execute_flag(lowered_break_simple()) +479 final_break())480 ))481 create_test_case(doc_string, input_sexp, expected_sexp, 'lower_breaks_6',482 lower_break=True, lower_continue=True)483def test_lower_guarded_conditional_break():484 doc_string = """Normally a conditional break at the end of a loop isn't485 lowered, however if the conditional break gets placed inside486 an if(execute_flag) because of earlier lowering of continues,487 then the break needs to be lowered.488 """489 input_sexp = make_test_case('main', 'void', (490 loop(complex_if('a', continue_()) +491 simple_if('b', break_()))492 ))493 expected_sexp = make_test_case('main', 'void', (494 declare_break_flag() +495 loop(declare_execute_flag() +496 complex_if('a', lowered_continue()) +497 if_execute_flag(simple_if('b', lowered_break())) +498 final_break())499 ))500 create_test_case(doc_string, input_sexp, expected_sexp, 'lower_guarded_conditional_break',501 lower_break=True, lower_continue=True)502def test_remove_continue_at_end_of_loop():503 doc_string = """Test that a redundant continue-statement at the end of a504 loop is removed.505 """506 input_sexp = make_test_case('main', 'void', (507 loop(assign_x('a', const_float(1)) +508 continue_())509 ))510 expected_sexp = make_test_case('main', 'void', (511 loop(assign_x('a', const_float(1)))512 ))513 create_test_case(doc_string, input_sexp, expected_sexp, 'remove_continue_at_end_of_loop')514def test_lower_return_void_at_end_of_loop():515 doc_string = """Test that a return of void at the end of a loop is properly516 lowered.517 """518 input_sexp = make_test_case('main', 'void', (519 loop(assign_x('a', const_float(1)) +520 return_()) +521 assign_x('b', const_float(2))522 ))523 expected_sexp = make_test_case('main', 'void', (524 declare_return_flag() +525 loop(assign_x('a', const_float(1)) +526 lowered_return_simple() +527 break_()) +528 if_not_return_flag(assign_x('b', const_float(2)))529 ))530 create_test_case(doc_string, input_sexp, input_sexp, 'return_void_at_end_of_loop_lower_nothing')531 create_test_case(doc_string, input_sexp, expected_sexp, 'return_void_at_end_of_loop_lower_return',532 lower_main_return=True)533 create_test_case(doc_string, input_sexp, expected_sexp, 'return_void_at_end_of_loop_lower_return_and_break',534 lower_main_return=True, lower_break=True)535def test_lower_return_non_void_at_end_of_loop():536 doc_string = """Test that a non-void return at the end of a loop is537 properly lowered.538 """539 input_sexp = make_test_case('sub', 'float', (540 loop(assign_x('a', const_float(1)) +541 return_(const_float(2))) +542 assign_x('b', const_float(3)) +543 return_(const_float(4))544 ))545 expected_sexp = make_test_case('sub', 'float', (546 declare_execute_flag() +547 declare_return_value() +548 declare_return_flag() +549 loop(assign_x('a', const_float(1)) +550 lowered_return_simple(const_float(2)) +551 break_()) +552 if_not_return_flag(assign_x('b', const_float(3)) +553 lowered_return(const_float(4))) +554 final_return()555 ))556 create_test_case(doc_string, input_sexp, input_sexp, 'return_non_void_at_end_of_loop_lower_nothing')557 create_test_case(doc_string, input_sexp, expected_sexp, 'return_non_void_at_end_of_loop_lower_return',558 lower_sub_return=True)559 create_test_case(doc_string, input_sexp, expected_sexp, 'return_non_void_at_end_of_loop_lower_return_and_break',...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run gabbi automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful