1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
|
#include <gtest/gtest.h>
#include "elua.h"
static char buffer[4096];
static char *get_script_buffer(const char *script)
{
FILE *fp = fopen(script, "r");
if (fp)
{
int num = fread(buffer, 1, 4096, fp);
if (num != 0)
{
buffer[num] = 0;
fclose(fp);
return buffer;
}
fclose(fp);
}
return NULL;
}
int get_userdata(elua_vm *L)
{
void *ud = elua_get_execute_userdata(L);
struct elua_data edata;
edata.type = STRING;
edata.buff = (char *)ud;
edata.len = strlen((char *)ud);
elua_cbinding_append_output_params(L, &edata, 1);
return 1;
}
TEST(lua_execute_file, normal)
{
elua_vm *L = elua_create_vm("TEST");
const char *ud = "hello world.";
const char *script = "./script/exec_with_context.lua";
char *input = (char *)"This is a test";
int len = strlen(input);
struct elua_data out;
out.type = STRING;
out.len = 1024;
out.buff = (char *)calloc(1, 1024);
elua_register_cbinding(L, "get", "get_userdata", get_userdata);
elua_context *context = NULL;
context = elua_create_context(L, "context");
struct elua_script *escript = elua_cache_script_file(L, script, 0);
int ret = elua_execute_script(escript, input, len, (void *)ud, context, &out);
const char *str = "userdata:hello world., context.count:1, context.message:This is first called";
EXPECT_EQ(0, ret);
EXPECT_EQ(strlen(str), out.len);
EXPECT_EQ(STRING, out.type);
EXPECT_STREQ(str, out.buff);
bzero(out.buff, 1024);
ud = "hello lua.";
ret = elua_execute_script(escript, input, len, (void *)ud, context, &out);
str = "userdata:hello lua., context.count:2, context.message:This is not first called";
EXPECT_EQ(0, ret);
EXPECT_EQ(strlen(str), out.len);
EXPECT_EQ(STRING, out.type);
EXPECT_STREQ(str, out.buff);
bzero(out.buff, 1024);
elua_context *context2 = NULL;
context2 = elua_create_context(L, "context");
ret = elua_execute_script(escript, input, len, (void *)ud, context2, &out);
str = "userdata:hello lua., context.count:1, context.message:This is first called";
EXPECT_EQ(0, ret);
EXPECT_EQ(strlen(str), out.len);
EXPECT_EQ(STRING, out.type);
EXPECT_STREQ(str, out.buff);
bzero(out.buff, 1024);
elua_destroy_context(context);
elua_destroy_context(context2);
elua_destroy_vm(L);
free(out.buff);
}
TEST(lua_exec, normal)
{
elua_vm *L = elua_create_vm("TEST");
const char *ud = "hello world.";
char *input = (char *)"This is a test";
int len = strlen(input);
struct elua_data out;
out.type = STRING;
out.integer = 1024;
out.buff = (char *)calloc(1, 1024);
char *script = get_script_buffer("./script/exec_with_context.lua");
int script_len = strlen(script);
elua_script *escript = elua_cache_script(L, script, script_len, 0);
elua_register_cbinding(L, "get", "get_userdata", get_userdata);
elua_context *context = NULL;
context = elua_create_context(L, "context");
int ret = elua_execute_script(escript, input, len, (void *)ud, context, &out);
const char *str = "userdata:hello world., context.count:1, context.message:This is first called";
EXPECT_EQ(0, ret);
EXPECT_EQ(strlen(str), out.len);
EXPECT_EQ(STRING, out.type);
EXPECT_STREQ(str, out.buff);
bzero(out.buff, 1024);
ud = "hello lua.";
ret = elua_execute_script(escript, input, len, (void *)ud, context, &out);
str = "userdata:hello lua., context.count:2, context.message:This is not first called";
EXPECT_EQ(0, ret);
EXPECT_EQ(strlen(str), out.len);
EXPECT_EQ(STRING, out.type);
EXPECT_STREQ(str, out.buff);
bzero(out.buff, 1024);
elua_context *context2 = NULL;
context2 = elua_create_context(L, "context");
ret = elua_execute_script(escript, input, len, (void *)ud, context2, &out);
str = "userdata:hello lua., context.count:1, context.message:This is first called";
EXPECT_EQ(0, ret);
EXPECT_EQ(strlen(str), out.len);
EXPECT_EQ(STRING, out.type);
EXPECT_STREQ(str, out.buff);
bzero(out.buff, 1024);
elua_destroy_context(context);
elua_destroy_context(context2);
elua_destroy_vm(L);
free(out.buff);
}
TEST(elua_execute_script, normal)
{
elua_vm *L = elua_create_vm("TEST");
const char *ud = "hello world.";
char *input = (char *)"This is a test";
int len = strlen(input);
struct elua_data out;
out.type = STRING;
out.integer = 1024;
out.buff = (char *)calloc(1, 1024);
const char *script = get_script_buffer("./script/exec_with_context.lua");
struct elua_script *escript = elua_cache_script(L, script, strlen(script), 0);
elua_register_cbinding(L, "get", "get_userdata", get_userdata);
elua_context *context = NULL;
context = elua_create_context(L, "context");
int ret = elua_execute_script(escript, input, len, (void *)ud, context, &out);
const char *str = "userdata:hello world., context.count:1, context.message:This is first called";
EXPECT_EQ(0, ret);
EXPECT_EQ(strlen(str), out.len);
EXPECT_EQ(STRING, out.type);
EXPECT_STREQ(str, out.buff);
bzero(out.buff, 1024);
ud = "hello lua.";
ret = elua_execute_script(escript, input, len, (void *)ud, context, &out);
str = "userdata:hello lua., context.count:2, context.message:This is not first called";
EXPECT_EQ(0, ret);
EXPECT_EQ(strlen(str), out.len);
EXPECT_EQ(STRING, out.type);
EXPECT_STREQ(str, out.buff);
bzero(out.buff, 1024);
elua_context *context2 = NULL;
context2 = elua_create_context(L, "context");
ret = elua_execute_script(escript, input, len, (void *)ud, context2, &out);
str = "userdata:hello lua., context.count:1, context.message:This is first called";
EXPECT_EQ(0, ret);
EXPECT_EQ(strlen(str), out.len);
EXPECT_EQ(STRING, out.type);
EXPECT_STREQ(str, out.buff);
bzero(out.buff, 1024);
elua_destroy_context(context);
elua_destroy_context(context2);
elua_destroy_vm(L);
free(out.buff);
}
|