void test_state_is_reset_when_if_fails() { Branch branch; Stack context; Term* c = branch.compile("c = true"); branch.compile("if c { state i = 0; i += 1 } else { 'hi' }"); evaluate_branch(&context, &branch); test_equals(&context.state, "{_if_block: [{i: 1}]}"); evaluate_branch(&context, &branch); test_equals(&context.state, "{_if_block: [{i: 2}]}"); evaluate_branch(&context, &branch); test_equals(&context.state, "{_if_block: [{i: 3}]}"); set_bool(c, false); evaluate_branch(&context, &branch); test_equals(&context.state, "{_if_block: [null, null]}"); set_bool(c, true); evaluate_branch(&context, &branch); test_equals(&context.state, "{_if_block: [{i: 1}]}"); }
void test_state_is_reset_when_if_fails2() { // Similar to test_state_is_reset_when_if_fails, but this one doesn't // have an 'else' block and it uses test_oracle. internal_debug_function::oracle_clear(); Branch branch; Term* a = branch.compile("a = true"); branch.compile("if a { state s = test_oracle() }"); internal_debug_function::oracle_send(1); internal_debug_function::oracle_send(2); internal_debug_function::oracle_send(3); Stack context; evaluate_branch(&context, &branch); test_equals(&context.state, "{_if_block: [{s: 1}]}"); evaluate_branch(&context, &branch); test_equals(&context.state, "{_if_block: [{s: 1}]}"); set_bool(a, false); evaluate_branch(&context, &branch); test_equals(&context.state, "{_if_block: [null, null]}"); set_bool(a, true); evaluate_branch(&context, &branch); test_equals(&context.state, "{_if_block: [{s: 2}]}"); }
void test_if_elif_else() { Branch branch; branch.compile("if true { a = 1 } elif true { a = 2 } else { a = 3 } a=a"); evaluate_branch(&branch); test_assert(branch.contains("a")); test_equals(branch["a"]->asInt(), 1); branch.compile( "if false { b = 'apple' } elif false { b = 'orange' } else { b = 'pineapple' } b=b"); evaluate_branch(&branch); test_assert(branch.contains("b")); test_assert(branch["b"]->asString() == "pineapple"); // try one without 'else' branch.clear(); branch.compile("c = 0"); branch.compile("if false { c = 7 } elif true { c = 8 }; c=c"); evaluate_branch(&branch); test_assert(branch.contains("c")); test_assert(branch["c"]->asInt() == 8); // try with some more complex conditions branch.clear(); branch.compile("x = 5"); branch.compile("if x > 6 { compare = 1 } elif x < 6 { compare = -1 } else { compare = 0}"); branch.compile("compare=compare"); evaluate_branch(&branch); test_assert(branch.contains("compare")); test_assert(branch["compare"]->asInt() == -1); }
void test_change_function() { Branch branch; // simple test Term* a = branch.compile("add(3,3)"); evaluate_branch(&branch); test_assert(as_int(a) == 6); change_function(a, KERNEL->get("mult_i")); evaluate_branch(&branch); test_assert(as_int(a) == 9); }
void bug_reproducing_list_after_eval() { // There once was a bug where source repro would fail when using a list // in a vectorized function, but only after that piece of code had been // evaluated. This was because vectorize_vv was calling apply() which // was changing the 'statement' property of its inputs. Branch branch; Term* sum = branch.compile("[1 1] + [1 1]"); Term* in0 = sum->input(0); Term* in1 = sum->input(0); test_equals(get_term_source_text(in0), "[1 1]"); test_equals(get_term_source_text(in1), "[1 1]"); test_equals(get_input_source_text(sum, 0), "[1 1] "); test_equals(get_input_source_text(sum, 1), " [1 1]"); test_equals(get_branch_source_text(&branch), "[1 1] + [1 1]"); evaluate_branch(&branch); test_equals(get_term_source_text(in0), "[1 1]"); test_equals(get_term_source_text(in1), "[1 1]"); test_equals(get_input_source_text(sum, 0), "[1 1] "); test_equals(get_input_source_text(sum, 1), " [1 1]"); test_equals(get_branch_source_text(&branch), "[1 1] + [1 1]"); }
void test_trimmed_state(std::string const& source, std::string const& dest, std::string const& expectedTrash) { Branch sourceBranch; sourceBranch.compile(source); if (test_fail_on_static_error(&sourceBranch)) return; Stack context; evaluate_branch(&context, &sourceBranch); if (test_fail_on_runtime_error(context)) return; Branch destBranch; destBranch.compile(dest); if (test_fail_on_static_error(&destBranch)) return; caValue trash; strip_orphaned_state(&destBranch, &context.state, &trash); if (expectedTrash != trash.toString()) { declare_current_test_failed(); std::cout << "In test " << get_current_test_name() << std::endl; std::cout << expectedTrash << " != " << trash.toString() << std::endl; } }
void test_snippet(std::string const& source) { Branch branch; branch.compile(source); if (test_fail_on_static_error(&branch)) return; Stack context; evaluate_branch(&context, &branch); if (test_fail_on_runtime_error(context)) return; // Try stripping orphaned state, this should not have an effect. caValue trash; strip_orphaned_state(&branch, &context.state, &trash); if (!is_null(&trash)) { std::cout << "Falsely orphaned state in " << get_current_test_name() << std::endl; std::cout << "Code = " << source << std::endl; std::cout << "Trash = " << trash.toString() << std::endl; declare_current_test_failed(); return; } }
std::vector<placement_problem> placement_problem::branch(branching_rule rule) const{ // Chose a good branch based simply on the positions of the cells std::vector<point> pos = get_positions(); int best_fc, best_sc; int best_cell_measure=-1; bool found_cell_overlap=false; // Branch to avoid overlaps between cells for(int i=0; i+1<cells.size(); ++i){ for(int j=i+1; j<cells.size(); ++j){ int measure = evaluate_branch(i, j, pos, rule); if(measure >= 0 and measure > best_cell_measure){ found_cell_overlap=true; best_cell_measure = measure; best_fc = i; best_sc = j; } } } rect best_fixed; int best_fixed_c; int best_fixed_measure=-1; bool found_fixed_overlap=false; for(rect const R : fixed_elts){ for(int i=0; i<cells.size(); ++i){ int measure = evaluate_branch(i, R, pos, rule); if(measure >= 0 and measure > best_fixed_measure){ found_fixed_overlap=true; best_fixed_measure = measure; best_fixed = R; best_fixed_c = i; } } } if(found_cell_overlap and (not found_fixed_overlap or best_cell_measure >= best_fixed_measure) ){ return branch_overlap_removal(best_fc, best_sc); } else if(found_fixed_overlap){ return branch_overlap_removal(best_fixed_c, best_fixed); } else{ assert(is_correct()); return std::vector<placement_problem>(); } }
void opcode_check_arg_count(void) { TRACE_LOG("Opcode: CHECK_ARG_COUNT.\n"); evaluate_branch( (uint8_t) ( ((int16_t)op[0]) <= ((int16_t)number_of_locals_from_function_call) ? 1 : 0) ); }
void test_dont_always_rebind_inner_names() { Branch branch; branch.compile("if false { b = 1 } elif false { c = 1 } elif false { d = 1 } else { e = 1 }"); evaluate_branch(&branch); test_assert(!branch.contains("b")); test_assert(!branch.contains("c")); test_assert(!branch.contains("d")); test_assert(!branch.contains("e")); }
void test_state_simple() { Branch branch; Stack context; // Simple test, condition never changes Term* block = branch.compile("if true { state i = 0; i += 1 }"); evaluate_branch(&context, &branch); caValue *i = context.state.getField("_if_block")->getIndex(0)->getField("i"); test_assert(i != NULL); test_assert(as_int(i) == 1); evaluate_branch(&context, &branch); i = context.state.getField("_if_block")->getIndex(0)->getField("i"); test_assert(as_int(i) == 2); evaluate_branch(&context, &branch); i = context.state.getField("_if_block")->getIndex(0)->getField("i"); test_assert(as_int(i) == 3); // Same test with elif branch.clear(); block = branch.compile("if false {} elif true { state i = 0; i += 1 }"); evaluate_branch(&context, &branch); i = context.state.getField("_if_block")->getIndex(1)->getField("i"); test_assert(as_int(i) == 1); evaluate_branch(&context, &branch); i = context.state.getField("_if_block")->getIndex(1)->getField("i"); test_assert(as_int(i) == 2); evaluate_branch(&context, &branch); i = context.state.getField("_if_block")->getIndex(1)->getField("i"); test_assert(as_int(i) == 3); // Same test with else branch.clear(); Stack context2; block = branch.compile("if false {} else { state i = 0; i += 1 }"); evaluate_branch(&context2, &branch); i = context2.state.getField("_if_block")->getIndex(1)->getField("i"); test_assert(as_int(i) == 1); evaluate_branch(&context2, &branch); i = context2.state.getField("_if_block")->getIndex(1)->getField("i"); test_assert(as_int(i) == 2); evaluate_branch(&context2, &branch); i = context2.state.getField("_if_block")->getIndex(1)->getField("i"); test_assert(as_int(i) == 3); }
void test_included_file_changed() { Branch branch; setup(branch); FakeFileSystem files; files["f"] = "state s; alloc_handle(@s)"; branch.compile("include('f')"); Stack context; evaluate_branch(&context, &branch); test_equals(&g_slots, "[true, false, false]"); files.set("f", ""); evaluate_branch(&context, &branch); test_assert(&branch); test_equals(&g_slots, "[false, false, false]"); }
void opcode_push_user_stack(void) { uint16_t spare_slots; uint8_t *user_stack; TRACE_LOG("Opcode: PUSH_USER_STACK.\n"); (void)read_z_result_variable(); user_stack = z_mem + (uint16_t)op[1]; if ((spare_slots = load_word(user_stack)) > 0) { store_word(user_stack + spare_slots, (uint16_t)op[0]); spare_slots--; store_word(user_stack, spare_slots); evaluate_branch((uint8_t)1); } else { evaluate_branch((uint8_t)0); } }
void test_with_state() { Branch branch; setup(branch); test_equals(&g_slots, "[false, false, false]"); branch.compile("state s"); branch.compile("alloc_handle(@s)"); set_branch_in_progress(&branch, false); Stack context; evaluate_branch(&context, &branch); test_equals(&g_slots, "[true, false, false]"); evaluate_branch(&context, &branch); test_equals(&g_slots, "[true, false, false]"); reset(&context.state); test_equals(&g_slots, "[false, false, false]"); }
void test_import_c() { Branch branch; Term* func = import_function(&branch, my_imported_function, "my_imported_func(int,int) -> int"); test_equals(function_get_output_type(func, 0)->name, "int"); Term* result = branch.compile("my_imported_func(4,5)"); evaluate_branch(&branch); test_assert(as_int(result) == 9); }
void test_parse_with_no_line_endings() { Branch branch; branch.compile("a = 4"); branch.compile("if a < 5 { a = 5 }"); branch.compile("a=a"); evaluate_branch(&branch); test_assert(&branch); test_assert(branch["a"]->asInt() == 5); branch.compile("if a > 7 { a = 5 } else { a = 3 }"); branch.compile("a=a"); evaluate_branch(&branch); test_assert(&branch); test_assert(branch["a"]->asInt() == 3); branch.compile("if a == 2 { a = 1 } elif a == 3 { a = 9 } else { a = 2 }"); branch.compile("a=a"); evaluate_branch(&branch); test_assert(&branch); test_assert(branch["a"]->asInt() == 9); }
void test_if_joining_on_bool() { // The following code once had a bug where cond wouldn't work // if one of its inputs was missing value. Branch branch; caValue* s = branch.eval("hey = true"); test_assert(s->value_data.ptr != NULL); branch.eval("if false { hey = false }"); evaluate_branch(&branch); test_assert(branch["hey"]->asBool() == true); }
void test_execution_with_elif() { Branch branch; branch.compile("x = 5"); branch.compile("if x > 5 { test_spy('Fail') } " "elif x < 5 { test_spy('Fail')} " "elif x == 5 { test_spy('Success')} " "else { test_spy('Fail') }"); internal_debug_function::spy_clear(); evaluate_branch(&branch); test_assert(&branch); test_equals(internal_debug_function::spy_results(), "['Success']"); }
void test_state_inside_if_block() { Branch branch; setup(branch); branch.compile("state s = null"); branch.compile("if is_null(s) { s = alloc_handle(s) }"); Stack context; evaluate_branch(&context, &branch); test_equals(&g_slots, "[true, false, false]"); clear_branch(&branch); strip_orphaned_state(&branch, &context.state); test_equals(&g_slots, "[false, false, false]"); }
void test_that_stripping_state_is_recursive() { Branch branch; setup(branch); branch.compile("if true { state a = 1; state s; s = alloc_handle(s) }"); Stack context; evaluate_branch(&context, &branch); test_equals(&g_slots, "[true, false, false]"); clear_branch(&branch); branch.compile("if true { state a = 1 }"); strip_orphaned_state(&branch, &context.state); test_equals(&g_slots, "[false, false, false]"); }
void test_in_subroutine_state() { Branch branch; setup(branch); branch.compile("def hi(any input) { state s = input }"); branch.compile("state s"); branch.compile("alloc_handle(@s)"); branch.compile("hi(s)"); Stack context; evaluate_branch(&context, &branch); test_equals(&g_slots, "[true, false, false]"); set_null(&context.state); test_equals(&g_slots, "[false, false, false]"); }
int run_debugger_repl(std::string const& filename) { Branch branch; load(&branch, filename); while (true) { std::cout << "> "; std::string input; if (!std::getline(std::cin, input)) break; if (input == "exit" || input == "/exit") break; if (input == "") continue; if (input == "p") { print_branch(std::cout, &branch); continue; } if (input == "e") { Stack stack; evaluate_branch(&stack, &branch); continue; } if (input == "c") { print_static_errors_formatted(&branch, std::cout); continue; } std::cout << "unrecognized command: " << input << std::endl; } return 0; }
void test_deleted_state() { Branch branch; setup(branch); branch.compile("state s"); branch.compile("alloc_handle(@s)"); branch.compile("state t"); branch.compile("alloc_handle(@t)"); Stack context; evaluate_branch(&context, &branch); test_equals(&g_slots, "[true, true, false]"); clear_branch(&branch); branch.compile("state t"); strip_orphaned_state(&branch, &context.state); test_equals(&g_slots, "[false, true, false]"); }
void test_execution() { Branch branch; internal_debug_function::spy_clear(); // Start off with some simple expressions branch.compile("if true { test_spy('Success 1') }"); branch.compile("if false { test_spy('Fail') }"); branch.compile("if (1 + 2) > 1 { test_spy('Success 2') }"); branch.compile("if (1 + 2) < 1 { test_spy('Fail') }"); evaluate_branch(&branch); test_assert(&branch); test_equals(internal_debug_function::spy_results(), "['Success 1', 'Success 2']"); // Use 'else' branch.clear(); internal_debug_function::spy_clear(); branch.compile("if true { test_spy('Success 1') } else { test_spy('Fail') }"); branch.compile("if false { test_spy('Fail') } else { test_spy('Success 2') }"); branch.compile("if true { test_spy('Success 3-1') test_spy('Success 3-2') test_spy('Success 3-3') } " "else { test_spy('Fail') }"); branch.compile("if false { test_spy('Fail') test_spy('Fail 2') } " "else { test_spy('Success 4-1') test_spy('Success 4-2') test_spy('Success 4-3') }"); evaluate_branch(&branch); test_assert(&branch); test_equals(internal_debug_function::spy_results(), "['Success 1', 'Success 2', 'Success 3-1', 'Success 3-2', 'Success 3-3', " "'Success 4-1', 'Success 4-2', 'Success 4-3']"); // Do some nested blocks branch.clear(); internal_debug_function::spy_clear(); branch.compile("if true { if false { test_spy('Error!') } else { test_spy('Nested 1') } } " "else { test_spy('Error!') }"); evaluate_branch(&branch); test_assert(&branch); test_equals(internal_debug_function::spy_results(), "['Nested 1']"); branch.clear(); internal_debug_function::spy_clear(); branch.compile("if false { test_spy('Error!') } else { if false { test_spy('Error!') } " "else { test_spy('Nested 2') } }"); evaluate_branch(&branch); test_assert(&branch); test_equals(internal_debug_function::spy_results(), "['Nested 2']"); branch.clear(); internal_debug_function::spy_clear(); branch.compile("if false { test_spy('Error!') }" "else { if true { test_spy('Nested 3') } else { test_spy('Error!') } }"); evaluate_branch(&branch); test_assert(&branch); test_equals(internal_debug_function::spy_results(), "['Nested 3']"); branch.clear(); internal_debug_function::spy_clear(); branch.compile("if true { if false { test_spy('Error!') } else { test_spy('Nested 4') } } " "else { test_spy('Error!') }"); evaluate_branch(&branch); test_assert(&branch); test_equals(internal_debug_function::spy_results(), "['Nested 4']"); branch.clear(); internal_debug_function::spy_clear(); branch.compile( "if (false)\n" " test_spy('Error!')\n" "else\n" " if (true)\n" " if (false)\n" " test_spy('Error!')\n" " else\n" " if (false)\n" " test_spy('Error!')\n" " else\n" " if (true)\n" " test_spy('Nested 5')\n" " else\n" " test_spy('Error!')\n" " test_spy('Error!')\n" ); evaluate_branch(&branch); test_assert(&branch); test_equals(internal_debug_function::spy_results(), "['Nested 5']"); }