comparison tests/code_exec.cpp @ 0:a4671277546c tip

created the repository for the thymian project
author ferencd
date Tue, 17 Aug 2021 11:19:54 +0200
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:a4671277546c
1 #include "tests.h"
2 #include "gtest/gtest.h"
3
4 /*
5 * This file has the tests that are related to code execution in a
6 * virtual machine as triggered by the "outsisde" world (ie: C runtime)
7 */
8
9 /* Have some code in a VM.
10 * Execute some other code in the VM.
11 */
12 TEST(CodeExecution, ExecuteSomeCodeInAVm)
13 {
14 nap_runtime* runtime = nap_runtime_create(0);
15 int found_indicator = 0;
16
17 ASSERT_FALSE(runtime == NULL);
18
19 nap_bytecode_chunk* bytecode = nap_runtime_compile(runtime,
20 "int c = 0; \
21 int a = 1; \
22 int b = 2; \
23 "
24 ,0);
25
26 ASSERT_FALSE(bytecode == NULL);
27 nap_runtime_execute(runtime, bytecode);
28 nap_execute_code(runtime, "c = a + b");
29 ASSERT_EQ(3, VAR_INT(c));
30
31 nap_runtime_shutdown(&runtime);
32 ASSERT_TRUE(runtime == NULL);
33 }
34
35
36 /* Define a function. Call it via the runtime API
37 * Use the function, see that it returns the default return value (0).
38 */
39 TEST(CodeExecution, ExternalCallingOfInternalMethod)
40 {
41 nap_runtime* runtime = nap_runtime_create(0);
42 int found_indicator = 0;
43
44 ASSERT_FALSE(runtime == NULL);
45
46 nap_bytecode_chunk* bytecode = nap_runtime_compile(runtime,
47 "int c = 0; \
48 int some_fun(int a , int b) \
49 { \
50 c = a + b; \
51 }" ,0);
52
53 ASSERT_FALSE(bytecode == NULL);
54 nap_runtime_execute(runtime, bytecode);
55 nap_int_t p1 = 1;
56 nap_int_t p2 = 2;
57 nap_execute_method(runtime, 0, "some_fun", p1, p2);
58 ASSERT_EQ(3, VAR_INT(c));
59
60 nap_runtime_shutdown(&runtime);
61 ASSERT_TRUE(runtime == NULL);
62 }
63
64
65 /* Define a function. Call it via the runtime API
66 * Use the function, see that it returns the default return value (0).
67 */
68 TEST(CodeExecution, ExternalCallingOfInternalMethodWithIntReturnType)
69 {
70 nap_runtime* runtime = nap_runtime_create(0);
71 int found_indicator = 0;
72
73 ASSERT_FALSE(runtime == NULL);
74
75 nap_bytecode_chunk* bytecode = nap_runtime_compile(runtime,
76 "int c = 0; \
77 int some_fun(int a , int b) \
78 { \
79 c = a + b; \
80 return c; \
81 }" ,0);
82
83 ASSERT_FALSE(bytecode == NULL);
84 nap_runtime_execute(runtime, bytecode);
85 nap_int_t p1 = 1;
86 nap_int_t p2 = 2;
87 nap_int_t ret = 0;
88 nap_execute_method(runtime, &ret, "some_fun", p1, p2);
89 ASSERT_EQ(3, VAR_INT(c));
90 ASSERT_EQ(3, ret);
91
92 nap_runtime_shutdown(&runtime);
93 ASSERT_TRUE(runtime == NULL);
94 }
95
96 /* Define a function. Call it via the runtime API
97 * Use the function, see that it returns the default return value (0).
98 */
99 TEST(CodeExecution, ExternalCallingOfInternalMethodWithStringReturnType)
100 {
101 nap_runtime* runtime = nap_runtime_create(0);
102 int found_indicator = 0;
103
104 ASSERT_FALSE(runtime == NULL);
105
106 nap_bytecode_chunk* bytecode = nap_runtime_compile(runtime,
107 "string c = \"ABC\"; \
108 string some_fun(string a, string b) \
109 { \
110 c = a + b; \
111 return c; \
112 }" ,0);
113
114 ASSERT_FALSE(bytecode == NULL);
115 nap_runtime_execute(runtime, bytecode);
116 nap_string_t p1 = (nap_string_t)"DEF";
117 nap_string_t p2 = (nap_string_t)"GHI";
118 nap_string_t ret = 0;
119 nap_execute_method(runtime, &ret, "some_fun", p1, p2);
120 char *c = VAR_STRING(c);
121 ASSERT_STREQ("DEFGHI", c);
122 ASSERT_STREQ("DEFGHI", ret);
123 free(ret);
124 free(c);
125
126 nap_runtime_shutdown(&runtime);
127 ASSERT_TRUE(runtime == NULL);
128 }