void test_custom_object() { g_currentlyAllocated = 0; g_totalAllocated = 0; Block block; block.compile( "type MyType; \n" "def create_object() -> MyType\n" "def check_object(MyType t)\n" "s = create_object()\n" "check_object(s)\n" ); circa_install_function(&block, "create_object", create_object); circa_install_function(&block, "check_object", check_object); circa_setup_object_type(circa_find_type_local(&block, "MyType"), sizeof(CustomObject), CustomObjectRelease); // Shouldn't allocate any objects before running. test_equals(g_currentlyAllocated, 0); test_equals(g_totalAllocated, 0); Stack stack; push_frame(&stack, &block); run_interpreter(&stack); test_assert(&stack); circa_clear_stack(&stack); // Running the script should only cause 1 object allocation. test_equals(g_currentlyAllocated, 0); test_equals(g_totalAllocated, 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 global_names() { Block* block = find_or_create_block(global_root_block(), "names_test"); Term* a = block->compile("a = 1"); circa::Value globalName; get_global_name(a, &globalName); test_equals(&globalName, "names_test:a"); Term* a_2 = block->compile("a = 2"); get_global_name(a, &globalName); test_equals(&globalName, "names_test:a#1"); get_global_name(a_2, &globalName); test_equals(&globalName, "names_test:a#2"); Block* block2 = create_block(block, "block2"); Term* b = block2->compile("b = 3"); Term* b_2 = block2->compile("b = 4"); get_global_name(b, &globalName); test_equals(&globalName, "names_test:block2:b#1"); get_global_name(b_2, &globalName); test_equals(&globalName, "names_test:block2:b#2"); // Now try finding terms via their global name. test_assert(find_from_global_name(global_world(), "names_test:a") == a_2); test_assert(find_from_global_name(global_world(), "names_test:a#1") == a); test_assert(find_from_global_name(global_world(), "names_test:a#2") == a_2); test_assert(find_from_global_name(global_world(), "names_test:block2:b") == b_2); test_assert(find_from_global_name(global_world(), "names_test:block2:b#1") == b); test_assert(find_from_global_name(global_world(), "names_test:block2:b#2") == b_2); }
void repro_source_after_rename() { Branch branch; // Simple test Term* a = branch.compile("a = 1"); rename(a, "apple"); test_equals(get_term_source_text(a), "apple = 1"); // Rename a function argument Term* b = branch.compile("b = 5"); Term* add = branch.compile("add( b , 10)"); rename(b, "banana"); test_equals(get_term_source_text(add), "add( banana , 10)"); // Rename a function Term* myfunc = import_function(&branch, NULL, "def myfunc(int)"); Term* myfunc_call = branch.compile("myfunc(555)"); rename(myfunc, "my_renamed_func"); test_equals(get_term_source_text(myfunc_call), "my_renamed_func(555)"); }
void test_simple() { Branch branch; setup(branch); test_equals(&g_slots, "[false, false, false]"); caValue handle; assign(&handle, 0); test_equals(&g_slots, "[true, false, false]"); set_null(&handle); test_equals(&g_slots, "[false, false, false]"); assign(&handle, 0); test_equals(&g_slots, "[true, false, false]"); assign(&handle, 1); test_equals(&g_slots, "[false, true, false]"); assign(&handle, 2); test_equals(&g_slots, "[false, false, true]"); caValue handle2; caValue handle3; assign(&handle2, 0); test_equals(&g_slots, "[true, false, true]"); assign(&handle3, 1); test_equals(&g_slots, "[true, true, true]"); set_null(&handle2); test_equals(&g_slots, "[false, true, true]"); set_null(&handle); test_equals(&g_slots, "[false, true, false]"); }
void test_lazy() { #ifdef DEFERRED_CALLS_FIRST_DRAFT Branch branch; Term* a = branch.compile("a = add(1 2)"); set_lazy_call(a, true); Stack context; push_frame(&context, &branch); run_interpreter(&context); test_equals(get_register(&context, a), ":Unevaluated"); Frame* frame = push_frame(&context, &branch); frame->pc = a->index; frame->startPc = frame->pc; frame->endPc = frame->pc + 1; frame->strategy = ByDemand; run_interpreter(&context); test_equals(get_register(&context, a), "3"); reset_stack(&context); Term* b = branch.compile("b = add(a a)"); push_frame(&context, &branch); run_interpreter(&context); test_equals(get_register(&context, a), "3"); test_equals(get_register(&context, b), "6"); #endif }
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]"); }
int test_write_header() { struct setec_astronomy_header w_header, r_header; char temp_file[] = TEST_DATA_DIR "temp"; init_header(&w_header); init_header(&r_header); create_header(&w_header, DEFAULT_IV_LEN, DEFAULT_SALT_LEN, DEFAULT_HASH_COUNT, DEFAULT_PASSWORD, DEFAULT_HASH_LEN); test_equals(write_header(&w_header, temp_file), SA_SUCCESS); test_equals(read_header(&r_header, temp_file), SA_SUCCESS); test_equals(w_header.salt_len, r_header.salt_len); test_equals(w_header.hash_count, r_header.hash_count); test_equals(w_header.hash_len, w_header.hash_len); test_equals(w_header.iv_len, r_header.iv_len); test_equals(strncmp(w_header.salt, r_header.salt, w_header.salt_len), 0); test_equals(strncmp(w_header.hash, r_header.hash, w_header.hash_len), 0); test_equals(strncmp(w_header.iv, r_header.iv, w_header.iv_len), 0); test_equals(strncmp(w_header.hash, r_header.hash, w_header.hash_len), 0); free_header(&w_header); free_header(&r_header); return 0; }
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 find_name() { Block block; block.compile("a = 1"); block.compile("namespace ns { a = 2; b = 3; } "); test_equals(term_value(find_name(&block, "a")), "1"); test_equals(term_value(find_name(&block, "ns:a")), "2"); test_equals(term_value(find_name(&block, "ns:b")), "3"); }
int test_list_names() { char ** name_list; int num_names; remove(NON_EXISTING_FILE); test_equals(add_name_pass(NON_EXISTING_FILE, TEST_PASSWORD, TEST_NAME, TEST_PASS),SA_SUCCESS); test_equals(get_name_list(NON_EXISTING_FILE, TEST_PASSWORD, &name_list, &num_names),SA_SUCCESS); test_equals(num_names,1); test_equals(strcmp(name_list[0], TEST_NAME),0); free(name_list[0]); free(name_list); test_equals(add_name_pass(NON_EXISTING_FILE, TEST_PASSWORD, TEST_NAME2, TEST_PASS2),SA_SUCCESS); test_equals(get_name_list(NON_EXISTING_FILE, TEST_PASSWORD, &name_list, &num_names),SA_SUCCESS); test_equals(num_names,2); test_equals(strcmp(name_list[0], TEST_NAME),0); test_equals(strcmp(name_list[1], TEST_NAME2),0); free(name_list[0]); free(name_list[1]); free(name_list); remove(NON_EXISTING_FILE); return UT_SUCCESS; }
int test_int_to_2bytes() { unsigned char buf[2]; int val; val = 52229; int_to_2bytes(val, buf); test_equals(buf[0], 204); test_equals(buf[1], 5); return 0; }
int test_lbb_read_write() { struct little_black_box lbb; struct name_pass_pair pair; test_equals(lbb_open(&lbb, SA_CRYPT_MODE, NON_EXISTING_FILE, TEST_PASSWORD), SA_SUCCESS); test_equals(lbb_write_name_pass(&lbb, "foo", "bar"), SA_SUCCESS); test_equals(lbb_close(&lbb), SA_SUCCESS); test_equals(lbb_open(&lbb, SA_DECRYPT_MODE, NON_EXISTING_FILE, TEST_PASSWORD), SA_SUCCESS); test_equals(lbb_read_pair(&lbb, &pair), SA_SUCCESS); test_equals(strcmp(pair.name, "foo"), 0); test_equals(strcmp(pair.pass, "bar"), 0); test_equals(remove(NON_EXISTING_FILE), 0); lbb_close(&lbb); return UT_SUCCESS; }
void name_visible_iterator_1() { Branch branch; branch.compile("a = 1"); Term* b = branch.compile("b = 1"); branch.compile("c = 1; d = 1; e = 1"); branch.compile("b = 2; f = 1; g = 1"); NameVisibleIterator it(b); test_equals(it.current()->name,"c"); test_assert(!it.finished()); ++it; test_equals(it.current()->name,"d"); test_assert(!it.finished()); ++it; test_equals(it.current()->name,"e"); test_assert(!it.finished()); ++it; test_assert(it.finished()); }
int test_import_file() { char ** name_list; int num_names, x; remove(NON_EXISTING_FILE); test_equals(add_name_pass(NON_EXISTING_FILE, TEST_PASSWORD, TEST_NAME, TEST_PASS),SA_SUCCESS); test_equals(import_name_pass(NON_EXISTING_FILE, TEST_PASSWORD, GOOD_IMPORT_FILE), SA_SUCCESS); test_equals(get_name_list(NON_EXISTING_FILE, TEST_PASSWORD, &name_list, &num_names),SA_SUCCESS); remove(NON_EXISTING_FILE); test_equals(num_names,4); test_equals(strcmp(name_list[0], TEST_NAME),0); test_equals(strcmp(name_list[1], "foo"),0); test_equals(strcmp(name_list[2], "bar"),0); test_equals(strcmp(name_list[3], "baz"),0); for(x = 0; x < num_names; ++x) free(name_list[x]); free(name_list); return UT_SUCCESS; }
int test_add_entry() { char pass_buff[MAX_PASS_LEN]; remove(NON_EXISTING_FILE); /* First test, new file single entry */ test_equals(add_name_pass(NON_EXISTING_FILE, TEST_PASSWORD, TEST_NAME, TEST_PASS),SA_SUCCESS); test_equals(get_pass_by_name(NON_EXISTING_FILE, TEST_PASSWORD, TEST_NAME, pass_buff),SA_SUCCESS); test_equals(strncmp(TEST_PASS, pass_buff, MAX_PASS_LEN),0); /* Second test, add a second entry to the file and check that both entries are there */ test_equals(add_name_pass(NON_EXISTING_FILE, TEST_PASSWORD, TEST_NAME2, TEST_PASS2),SA_SUCCESS); test_equals(get_pass_by_name(NON_EXISTING_FILE, TEST_PASSWORD, TEST_NAME2, pass_buff),SA_SUCCESS); test_equals(strncmp(TEST_PASS2, pass_buff, MAX_PASS_LEN),0); test_equals(get_pass_by_name(NON_EXISTING_FILE, TEST_PASSWORD, TEST_NAME, pass_buff),SA_SUCCESS); test_equals(strncmp(TEST_PASS, pass_buff, MAX_PASS_LEN),0); remove(NON_EXISTING_FILE); return UT_SUCCESS; }
void test_prepend() { Value hello, there; set_string(&hello, "hello"); set_string(&there, "there"); test_equals(&hello, "hello"); test_equals(&there, "there"); string_prepend(&there, " "); test_equals(&there, " there"); string_prepend(&there, &hello); string_prepend(&there, "hello there"); test_equals(&hello, "hello"); }
void nested_name_match() { Block block; Term* a = block.compile("a = { b = { c = 4 }}"); block.compile("a2 = { b2 = 5 }"); test_assert(a == find_term_from_path_expression(&block, "a")); Term* c = find_term_from_path_expression(&block, "a/b/c"); Term* b2 = find_term_from_path_expression(&block, "a2/b2"); test_assert(c != NULL); test_assert(b2 != NULL); test_equals(term_value(c), "4"); test_equals(term_value(b2), "5"); }
void simple_name_match() { Block block; block.compile("a = 1; b = 2; c = 3"); Term* a = find_term_from_path_expression(&block, "a"); Term* b = find_term_from_path_expression(&block, "b"); Term* c = find_term_from_path_expression(&block, "c"); test_assert(a != NULL); test_assert(b != NULL); test_assert(c != NULL); test_equals(term_value(a), "1"); test_equals(term_value(b), "2"); test_equals(term_value(c), "3"); }
void test_release() { Block block; block.compile("type T = handle_type()"); block.compile("def T.release(self)"); install_function(&block, "T.release", my_release_func); Type* T = find_type(&block, "T"); test_assert(T != NULL); test_assert(find_method(NULL, T, "release") != NULL); gTimesReleaseCalled = 0; Value value; make(T, &value); test_assert(is_handle(&value)); test_equals(gTimesReleaseCalled, 0); set_int(handle_get_value(&value), 5); set_null(&value); test_equals(gTimesReleaseCalled, 1); gTimesReleaseCalled = 0; Value value2; make(T, &value); copy(&value, &value2); test_assert(gTimesReleaseCalled == 0); set_null(&value2); test_assert(gTimesReleaseCalled == 0); set_null(&value); test_assert(gTimesReleaseCalled == 1); gTimesReleaseCalled = 0; make(T, &value); make(T, &value2); set_null(&value); test_assert(gTimesReleaseCalled == 1); set_null(&value2); test_assert(gTimesReleaseCalled == 2); }
void assign_output_type() { Branch branch; branch.compile("a = [1]"); Term* assign = branch.compile("a[0] = 2"); test_equals(assign->type->name, "List"); }
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 infer_type_of_append() { Branch branch; branch.compile("a = []"); branch.compile("a.append(1)"); Term* b = branch.compile("a[0]"); test_equals(b->type->name, "int"); }
void infer_length() { Branch branch; caValue result; statically_infer_result(branch.compile("length([])"), &result); test_equals(&result, "0"); statically_infer_result(branch.compile("length([1])"), &result); test_equals(&result, "1"); statically_infer_result(branch.compile("length([1 2 3])"), &result); test_equals(&result, "3"); return; // TEST_DISABLED statically_infer_result(branch.compile("length([1 2 3].append(3))"), &result); test_equals(&result, "4"); }
void for_loop_output_type() { Branch branch; branch.compile("a = 1"); branch.compile("for i in [1] { a = 2 }"); test_equals(branch["a"]->type->name, "int"); }
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 source_file_location() { test_write_fake_file("block.ca", 1, "a = 1"); Block* block = load_module_file(global_world(), temp_string("source_file_location"), "block.ca"); test_equals(block_get_source_filename(block), "block.ca"); }
void recursive_wildcard_match() { Block block; block.compile("a = 1"); block.compile("b = { c = 2 }"); block.compile("d = { e = { f = 3 } }"); Term* a = find_term_from_path_expression(&block, "**/a"); Term* c = find_term_from_path_expression(&block, "**/c"); Term* f = find_term_from_path_expression(&block, "** / f"); test_assert(a != NULL); test_assert(c != NULL); test_assert(f != NULL); test_equals(term_value(a), "1"); test_equals(term_value(c), "2"); test_equals(term_value(f), "3"); }
int main(int argc, char *argv[]) { test_str_cmp(); test_equals(); //assert(str_cmp("Hello World","Hello%20World") == 0); //assert(str_cmp("Hello ","Hello%20") == 0); //assert(str_cmp(" Hello","%20%20Hello") == 0); return 0; }