ferencd@0: #include "tests.h" ferencd@0: #include "gtest/gtest.h" ferencd@0: ferencd@0: /* ferencd@0: * This file has the tests that are related to code execution in a ferencd@0: * virtual machine as triggered by the "outsisde" world (ie: C runtime) ferencd@0: */ ferencd@0: ferencd@0: /* Have some code in a VM. ferencd@0: * Execute some other code in the VM. ferencd@0: */ ferencd@0: TEST(CodeExecution, ExecuteSomeCodeInAVm) ferencd@0: { ferencd@0: nap_runtime* runtime = nap_runtime_create(0); ferencd@0: int found_indicator = 0; ferencd@0: ferencd@0: ASSERT_FALSE(runtime == NULL); ferencd@0: ferencd@0: nap_bytecode_chunk* bytecode = nap_runtime_compile(runtime, ferencd@0: "int c = 0; \ ferencd@0: int a = 1; \ ferencd@0: int b = 2; \ ferencd@0: " ferencd@0: ,0); ferencd@0: ferencd@0: ASSERT_FALSE(bytecode == NULL); ferencd@0: nap_runtime_execute(runtime, bytecode); ferencd@0: nap_execute_code(runtime, "c = a + b"); ferencd@0: ASSERT_EQ(3, VAR_INT(c)); ferencd@0: ferencd@0: nap_runtime_shutdown(&runtime); ferencd@0: ASSERT_TRUE(runtime == NULL); ferencd@0: } ferencd@0: ferencd@0: ferencd@0: /* Define a function. Call it via the runtime API ferencd@0: * Use the function, see that it returns the default return value (0). ferencd@0: */ ferencd@0: TEST(CodeExecution, ExternalCallingOfInternalMethod) ferencd@0: { ferencd@0: nap_runtime* runtime = nap_runtime_create(0); ferencd@0: int found_indicator = 0; ferencd@0: ferencd@0: ASSERT_FALSE(runtime == NULL); ferencd@0: ferencd@0: nap_bytecode_chunk* bytecode = nap_runtime_compile(runtime, ferencd@0: "int c = 0; \ ferencd@0: int some_fun(int a , int b) \ ferencd@0: { \ ferencd@0: c = a + b; \ ferencd@0: }" ,0); ferencd@0: ferencd@0: ASSERT_FALSE(bytecode == NULL); ferencd@0: nap_runtime_execute(runtime, bytecode); ferencd@0: nap_int_t p1 = 1; ferencd@0: nap_int_t p2 = 2; ferencd@0: nap_execute_method(runtime, 0, "some_fun", p1, p2); ferencd@0: ASSERT_EQ(3, VAR_INT(c)); ferencd@0: ferencd@0: nap_runtime_shutdown(&runtime); ferencd@0: ASSERT_TRUE(runtime == NULL); ferencd@0: } ferencd@0: ferencd@0: ferencd@0: /* Define a function. Call it via the runtime API ferencd@0: * Use the function, see that it returns the default return value (0). ferencd@0: */ ferencd@0: TEST(CodeExecution, ExternalCallingOfInternalMethodWithIntReturnType) ferencd@0: { ferencd@0: nap_runtime* runtime = nap_runtime_create(0); ferencd@0: int found_indicator = 0; ferencd@0: ferencd@0: ASSERT_FALSE(runtime == NULL); ferencd@0: ferencd@0: nap_bytecode_chunk* bytecode = nap_runtime_compile(runtime, ferencd@0: "int c = 0; \ ferencd@0: int some_fun(int a , int b) \ ferencd@0: { \ ferencd@0: c = a + b; \ ferencd@0: return c; \ ferencd@0: }" ,0); ferencd@0: ferencd@0: ASSERT_FALSE(bytecode == NULL); ferencd@0: nap_runtime_execute(runtime, bytecode); ferencd@0: nap_int_t p1 = 1; ferencd@0: nap_int_t p2 = 2; ferencd@0: nap_int_t ret = 0; ferencd@0: nap_execute_method(runtime, &ret, "some_fun", p1, p2); ferencd@0: ASSERT_EQ(3, VAR_INT(c)); ferencd@0: ASSERT_EQ(3, ret); ferencd@0: ferencd@0: nap_runtime_shutdown(&runtime); ferencd@0: ASSERT_TRUE(runtime == NULL); ferencd@0: } ferencd@0: ferencd@0: /* Define a function. Call it via the runtime API ferencd@0: * Use the function, see that it returns the default return value (0). ferencd@0: */ ferencd@0: TEST(CodeExecution, ExternalCallingOfInternalMethodWithStringReturnType) ferencd@0: { ferencd@0: nap_runtime* runtime = nap_runtime_create(0); ferencd@0: int found_indicator = 0; ferencd@0: ferencd@0: ASSERT_FALSE(runtime == NULL); ferencd@0: ferencd@0: nap_bytecode_chunk* bytecode = nap_runtime_compile(runtime, ferencd@0: "string c = \"ABC\"; \ ferencd@0: string some_fun(string a, string b) \ ferencd@0: { \ ferencd@0: c = a + b; \ ferencd@0: return c; \ ferencd@0: }" ,0); ferencd@0: ferencd@0: ASSERT_FALSE(bytecode == NULL); ferencd@0: nap_runtime_execute(runtime, bytecode); ferencd@0: nap_string_t p1 = (nap_string_t)"DEF"; ferencd@0: nap_string_t p2 = (nap_string_t)"GHI"; ferencd@0: nap_string_t ret = 0; ferencd@0: nap_execute_method(runtime, &ret, "some_fun", p1, p2); ferencd@0: char *c = VAR_STRING(c); ferencd@0: ASSERT_STREQ("DEFGHI", c); ferencd@0: ASSERT_STREQ("DEFGHI", ret); ferencd@0: free(ret); ferencd@0: free(c); ferencd@0: ferencd@0: nap_runtime_shutdown(&runtime); ferencd@0: ASSERT_TRUE(runtime == NULL); ferencd@0: }