2022-05-19 20:52:19 +08:00
|
|
|
#include "test_common.h"
|
2022-06-14 03:20:17 +00:00
|
|
|
|
2021-10-14 15:52:01 +08:00
|
|
|
extern "C" {
|
2022-03-08 12:29:30 +08:00
|
|
|
extern char log_buff[LOG_BUFF_MAX][LOG_SIZE];
|
2022-02-14 20:57:15 +08:00
|
|
|
/* test head */
|
2022-02-22 00:41:45 +08:00
|
|
|
VMParameters* pikaVM_runAsmWithPars(PikaObj* self,
|
2022-03-01 15:01:22 +08:00
|
|
|
VMParameters* locals,
|
|
|
|
VMParameters* globals,
|
|
|
|
char* pikaAsm);
|
2022-02-14 20:40:29 +08:00
|
|
|
char* Parser_LineToAsm(Args* buffs, char* line, Stack* blockStack);
|
2022-08-12 23:51:14 +08:00
|
|
|
char* Parser_linesToAsm(Args* outBuffs, char* multiLine);
|
2022-03-29 18:02:55 +08:00
|
|
|
int32_t __clearInvokeStackes(VMParameters* locals);
|
2022-02-14 20:57:15 +08:00
|
|
|
int32_t pikaVM_runAsmLine(PikaObj* self,
|
2022-02-22 00:41:45 +08:00
|
|
|
VMParameters* locals,
|
|
|
|
VMParameters* globals,
|
2022-02-14 20:57:15 +08:00
|
|
|
char* pikaAsm,
|
|
|
|
int32_t lineAddr);
|
2021-10-14 15:52:01 +08:00
|
|
|
}
|
|
|
|
|
2021-10-14 20:44:50 +08:00
|
|
|
TEST(VM, num1) {
|
2022-04-30 21:17:19 +08:00
|
|
|
char* line = "1";
|
2021-10-14 15:52:01 +08:00
|
|
|
Args* buffs = New_strBuff();
|
2021-11-27 20:21:04 +08:00
|
|
|
char* pikaAsm = Parser_LineToAsm(buffs, line, NULL);
|
2021-10-14 15:52:01 +08:00
|
|
|
printf("%s", pikaAsm);
|
2022-04-30 21:17:19 +08:00
|
|
|
PikaObj* self = newRootObj("root", New_PikaStdLib_SysObj);
|
|
|
|
__platform_printf("BEGIN\r\n");
|
2021-11-23 12:15:12 +08:00
|
|
|
pikaVM_runAsm(self, pikaAsm);
|
2022-04-30 21:17:19 +08:00
|
|
|
EXPECT_STREQ(log_buff[1], "BEGIN\r\n");
|
|
|
|
EXPECT_STREQ(log_buff[0], "1\r\n");
|
2021-10-14 19:45:39 +08:00
|
|
|
obj_deinit(self);
|
2021-10-14 15:52:01 +08:00
|
|
|
args_deinit(buffs);
|
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
2021-10-14 20:44:50 +08:00
|
|
|
}
|
|
|
|
|
2021-10-14 20:46:32 +08:00
|
|
|
TEST(VM, a_1) {
|
2022-04-30 21:17:19 +08:00
|
|
|
char* line = "a = 1";
|
2021-10-14 20:46:32 +08:00
|
|
|
Args* buffs = New_strBuff();
|
2021-11-27 20:21:04 +08:00
|
|
|
char* pikaAsm = Parser_LineToAsm(buffs, line, NULL);
|
2021-10-14 20:46:32 +08:00
|
|
|
printf("%s", pikaAsm);
|
2022-04-30 21:17:19 +08:00
|
|
|
PikaObj* self = newRootObj("root", New_PikaStdLib_SysObj);
|
2021-11-23 12:15:12 +08:00
|
|
|
pikaVM_runAsm(self, pikaAsm);
|
2022-04-30 21:17:19 +08:00
|
|
|
ASSERT_EQ(args_getInt(self->list, "a"), 1);
|
2021-10-14 20:49:43 +08:00
|
|
|
|
|
|
|
obj_deinit(self);
|
|
|
|
args_deinit(buffs);
|
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(VM, a_1d1) {
|
2022-04-30 21:17:19 +08:00
|
|
|
char* line = "a = 1.1";
|
2021-10-14 20:49:43 +08:00
|
|
|
Args* buffs = New_strBuff();
|
2021-11-27 20:21:04 +08:00
|
|
|
char* pikaAsm = Parser_LineToAsm(buffs, line, NULL);
|
2021-10-14 20:49:43 +08:00
|
|
|
printf("%s", pikaAsm);
|
2022-04-30 21:17:19 +08:00
|
|
|
PikaObj* self = newRootObj("root", New_PikaStdLib_SysObj);
|
2022-02-22 00:41:45 +08:00
|
|
|
VMParameters* globals = pikaVM_runAsm(self, pikaAsm);
|
2022-04-30 21:17:19 +08:00
|
|
|
ASSERT_FLOAT_EQ(args_getFloat(globals->list, "a"), 1.1);
|
2021-10-14 20:49:43 +08:00
|
|
|
|
2021-10-14 21:02:19 +08:00
|
|
|
obj_deinit(self);
|
2021-11-23 12:15:12 +08:00
|
|
|
// obj_deinit(globals);
|
2021-10-14 21:02:19 +08:00
|
|
|
args_deinit(buffs);
|
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(VM, str_xy) {
|
2022-04-30 21:17:19 +08:00
|
|
|
char* line = "a = 'xy'";
|
2021-10-14 21:02:19 +08:00
|
|
|
Args* buffs = New_strBuff();
|
2021-11-27 20:21:04 +08:00
|
|
|
char* pikaAsm = Parser_LineToAsm(buffs, line, NULL);
|
2021-10-14 21:02:19 +08:00
|
|
|
printf("%s", pikaAsm);
|
2022-04-30 21:17:19 +08:00
|
|
|
PikaObj* self = newRootObj("root", New_PikaStdLib_SysObj);
|
2022-02-22 00:41:45 +08:00
|
|
|
VMParameters* globals = pikaVM_runAsm(self, pikaAsm);
|
2021-10-14 21:02:19 +08:00
|
|
|
|
2022-04-30 21:17:19 +08:00
|
|
|
ASSERT_STREQ(args_getStr(globals->list, "a"), "xy");
|
2021-10-14 21:02:19 +08:00
|
|
|
|
|
|
|
obj_deinit(self);
|
2021-11-23 12:15:12 +08:00
|
|
|
// obj_deinit(globals);
|
2021-10-14 21:02:19 +08:00
|
|
|
args_deinit(buffs);
|
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(VM, str_xy_space) {
|
2022-04-30 21:17:19 +08:00
|
|
|
char* line = "a = 'xy '";
|
2021-10-14 21:02:19 +08:00
|
|
|
Args* buffs = New_strBuff();
|
2021-11-27 20:21:04 +08:00
|
|
|
char* pikaAsm = Parser_LineToAsm(buffs, line, NULL);
|
2021-10-14 21:02:19 +08:00
|
|
|
printf("%s", pikaAsm);
|
2022-04-30 21:17:19 +08:00
|
|
|
PikaObj* self = newRootObj("root", New_PikaStdLib_SysObj);
|
2022-02-22 00:41:45 +08:00
|
|
|
VMParameters* globals = pikaVM_runAsm(self, pikaAsm);
|
2021-10-14 21:02:19 +08:00
|
|
|
|
2022-04-30 21:17:19 +08:00
|
|
|
ASSERT_STREQ(args_getStr(globals->list, "a"), "xy ");
|
2021-10-14 21:02:19 +08:00
|
|
|
|
2021-10-14 22:54:46 +08:00
|
|
|
obj_deinit(self);
|
|
|
|
args_deinit(buffs);
|
2021-11-23 12:15:12 +08:00
|
|
|
// obj_deinit(globals);
|
2021-10-14 22:54:46 +08:00
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(VM, ref_a_b) {
|
2022-04-30 21:17:19 +08:00
|
|
|
PikaObj* self = newRootObj("root", New_PikaStdLib_SysObj);
|
2021-10-14 23:03:31 +08:00
|
|
|
Args* buffs = New_strBuff();
|
2021-10-14 22:54:46 +08:00
|
|
|
|
2022-04-30 21:17:19 +08:00
|
|
|
pikaVM_runAsm(self, Parser_LineToAsm(buffs, "a = 'xy '", NULL));
|
2021-10-14 22:54:46 +08:00
|
|
|
|
2021-10-14 23:03:31 +08:00
|
|
|
args_deinit(buffs);
|
2021-10-14 20:46:32 +08:00
|
|
|
obj_deinit(self);
|
2021-11-23 12:15:12 +08:00
|
|
|
// obj_deinit(globals);
|
2021-10-14 20:46:32 +08:00
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
2021-10-14 23:22:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(VM, Run_add) {
|
2022-04-30 21:17:19 +08:00
|
|
|
PikaObj* self = newRootObj("root", New_PikaMath_Operator);
|
2021-10-14 23:22:48 +08:00
|
|
|
Args* buffs = New_strBuff();
|
2022-04-30 21:17:19 +08:00
|
|
|
char* pikaAsm = Parser_LineToAsm(buffs, "a = plusInt(1,2)", NULL);
|
|
|
|
__platform_printf("%s", pikaAsm);
|
2022-03-29 16:41:45 +08:00
|
|
|
VMParameters* globals = pikaVM_runAsm(self, pikaAsm);
|
2021-10-14 23:39:24 +08:00
|
|
|
|
|
|
|
args_deinit(buffs);
|
2022-04-30 21:17:19 +08:00
|
|
|
int a = args_getInt(globals->list, "a");
|
2021-10-14 23:39:24 +08:00
|
|
|
ASSERT_EQ(a, 3);
|
|
|
|
obj_deinit(self);
|
2021-11-23 12:15:12 +08:00
|
|
|
// obj_deinit(globals);
|
2021-10-14 23:39:24 +08:00
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(VM, Run_add_multy) {
|
2022-04-30 21:17:19 +08:00
|
|
|
PikaObj* self = newRootObj("root", New_PikaMath_Operator);
|
2021-10-14 23:39:24 +08:00
|
|
|
Args* buffs = New_strBuff();
|
|
|
|
|
2022-04-30 21:17:19 +08:00
|
|
|
pikaVM_runAsm(self, Parser_LineToAsm(buffs, "b = 2", NULL));
|
2021-10-14 23:39:24 +08:00
|
|
|
|
|
|
|
args_deinit(buffs);
|
|
|
|
obj_deinit(self);
|
2021-11-23 12:15:12 +08:00
|
|
|
// obj_deinit(globals);
|
2021-10-14 23:39:24 +08:00
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(VM, Run_add_1_2_3) {
|
2022-04-30 21:17:19 +08:00
|
|
|
PikaObj* self = newRootObj("root", New_PikaMath_Operator);
|
2021-10-14 23:39:24 +08:00
|
|
|
Args* buffs = New_strBuff();
|
2022-03-29 16:41:45 +08:00
|
|
|
char* pikaAsm =
|
2022-04-30 21:17:19 +08:00
|
|
|
Parser_LineToAsm(buffs, "a = plusInt(1, plusInt(2,3) )", NULL);
|
|
|
|
__platform_printf("%s", pikaAsm);
|
2022-03-29 16:41:45 +08:00
|
|
|
VMParameters* globals = pikaVM_runAsm(self, pikaAsm);
|
2021-10-14 23:22:48 +08:00
|
|
|
|
|
|
|
args_deinit(buffs);
|
2022-04-30 21:17:19 +08:00
|
|
|
int a = args_getInt(globals->list, "a");
|
2021-10-14 23:39:24 +08:00
|
|
|
ASSERT_EQ(a, 6);
|
2021-10-14 23:22:48 +08:00
|
|
|
obj_deinit(self);
|
2021-11-23 12:15:12 +08:00
|
|
|
// obj_deinit(globals);
|
2021-10-14 23:22:48 +08:00
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
2021-10-17 20:34:40 +08:00
|
|
|
}
|
|
|
|
|
2021-10-17 22:38:27 +08:00
|
|
|
extern PikaMemInfo pikaMemInfo;
|
|
|
|
TEST(VM, WHILE) {
|
|
|
|
pikaMemInfo.heapUsedMax = 0;
|
|
|
|
Args* buffs = New_strBuff();
|
|
|
|
char* lines =(char *)
|
|
|
|
"a = 1\n"
|
|
|
|
"b = 0\n"
|
|
|
|
"while a:\n"
|
|
|
|
" b = 1\n"
|
|
|
|
" a = 0\n"
|
|
|
|
"\n";
|
|
|
|
printf("%s", lines);
|
2022-08-12 23:51:14 +08:00
|
|
|
char* pikaAsm = Parser_linesToAsm(buffs, lines);
|
2021-10-17 22:38:27 +08:00
|
|
|
printf("%s", pikaAsm);
|
|
|
|
pikaMemInfo.heapUsedMax = 0;
|
2021-10-18 17:33:55 +08:00
|
|
|
PikaObj* self = New_TinyObj(NULL);
|
2022-02-22 00:41:45 +08:00
|
|
|
VMParameters* globals = pikaVM_runAsm(self, pikaAsm);
|
2022-04-30 21:17:19 +08:00
|
|
|
EXPECT_EQ(args_getInt(globals->list, "a"), 0);
|
|
|
|
EXPECT_EQ(args_getInt(globals->list, "b"), 1);
|
2021-11-23 12:15:12 +08:00
|
|
|
// obj_deinit(globals);
|
2021-10-17 22:38:27 +08:00
|
|
|
args_deinit(buffs);
|
|
|
|
obj_deinit(self);
|
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
|
|
|
}
|
2021-10-21 18:02:11 +08:00
|
|
|
|
|
|
|
TEST(VM, a_1_1) {
|
2022-04-30 21:17:19 +08:00
|
|
|
char* line = "a = 1 + 1";
|
2021-10-21 18:02:11 +08:00
|
|
|
Args* buffs = New_strBuff();
|
2021-11-27 20:21:04 +08:00
|
|
|
char* pikaAsm = Parser_LineToAsm(buffs, line, NULL);
|
2021-10-21 18:02:11 +08:00
|
|
|
printf("%s", pikaAsm);
|
2022-04-30 21:17:19 +08:00
|
|
|
PikaObj* self = newRootObj("root", New_PikaStdLib_SysObj);
|
2022-02-22 00:41:45 +08:00
|
|
|
VMParameters* globals = pikaVM_runAsm(self, pikaAsm);
|
2021-10-21 18:02:11 +08:00
|
|
|
|
2022-04-30 21:17:19 +08:00
|
|
|
int res = args_getInt(globals->list, "a");
|
2021-10-21 18:02:11 +08:00
|
|
|
obj_deinit(self);
|
|
|
|
args_deinit(buffs);
|
|
|
|
ASSERT_EQ(res, 2);
|
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
2021-10-21 18:07:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(VM, a_1_1d1) {
|
2022-04-30 21:17:19 +08:00
|
|
|
char* line = "a = 1 + 1.1";
|
2021-10-21 18:07:29 +08:00
|
|
|
Args* buffs = New_strBuff();
|
2021-11-27 20:21:04 +08:00
|
|
|
char* pikaAsm = Parser_LineToAsm(buffs, line, NULL);
|
2021-10-21 18:07:29 +08:00
|
|
|
printf("%s", pikaAsm);
|
2022-04-30 21:17:19 +08:00
|
|
|
PikaObj* self = newRootObj("root", New_PikaStdLib_SysObj);
|
2022-02-22 00:41:45 +08:00
|
|
|
VMParameters* globals = pikaVM_runAsm(self, pikaAsm);
|
2021-10-21 18:07:29 +08:00
|
|
|
|
2022-04-30 21:17:19 +08:00
|
|
|
float res = args_getFloat(globals->list, "a");
|
2021-10-21 18:07:29 +08:00
|
|
|
obj_deinit(self);
|
2021-11-23 12:15:12 +08:00
|
|
|
// obj_deinit(globals);
|
2021-10-21 18:07:29 +08:00
|
|
|
args_deinit(buffs);
|
|
|
|
ASSERT_FLOAT_EQ(res, 2.1);
|
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
2021-10-21 18:28:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(VM, a_jjcc) {
|
2022-04-30 21:17:19 +08:00
|
|
|
char* line = "a = (1 + 1.1) * 3 - 2 /4.0";
|
2022-04-02 23:28:02 +08:00
|
|
|
printf("%s\r\n", line);
|
2021-10-21 18:28:52 +08:00
|
|
|
Args* buffs = New_strBuff();
|
2021-11-27 20:21:04 +08:00
|
|
|
char* pikaAsm = Parser_LineToAsm(buffs, line, NULL);
|
2021-10-21 18:28:52 +08:00
|
|
|
printf("%s", pikaAsm);
|
2022-04-30 21:17:19 +08:00
|
|
|
PikaObj* self = newRootObj("root", New_PikaStdLib_SysObj);
|
2022-02-22 00:41:45 +08:00
|
|
|
VMParameters* globals = pikaVM_runAsm(self, pikaAsm);
|
2021-10-21 18:28:52 +08:00
|
|
|
|
2022-04-30 21:17:19 +08:00
|
|
|
float res = args_getFloat(globals->list, "a");
|
2021-10-21 18:28:52 +08:00
|
|
|
obj_deinit(self);
|
|
|
|
args_deinit(buffs);
|
2021-11-23 12:15:12 +08:00
|
|
|
// obj_deinit(globals);
|
2021-10-21 18:28:52 +08:00
|
|
|
ASSERT_FLOAT_EQ(res, 5.8);
|
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
2021-10-21 18:38:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(VM, while_a_1to10) {
|
2022-07-14 08:36:02 +00:00
|
|
|
char* line =
|
|
|
|
"a = 0\n"
|
|
|
|
"while a < 10:\n"
|
|
|
|
" a += 1\n"
|
|
|
|
" print(a)\n"
|
|
|
|
"\n";
|
2021-10-21 18:38:28 +08:00
|
|
|
Args* buffs = New_strBuff();
|
2022-08-12 23:51:14 +08:00
|
|
|
char* pikaAsm = Parser_linesToAsm(buffs, line);
|
2021-10-21 18:38:28 +08:00
|
|
|
printf("%s", pikaAsm);
|
2022-04-30 21:17:19 +08:00
|
|
|
PikaObj* self = newRootObj("root", New_PikaStdLib_SysObj);
|
2022-02-22 00:41:45 +08:00
|
|
|
VMParameters* globals = pikaVM_runAsm(self, pikaAsm);
|
2021-10-21 18:38:28 +08:00
|
|
|
|
2022-04-30 21:17:19 +08:00
|
|
|
int res = args_getInt(globals->list, "a");
|
2021-10-21 18:38:28 +08:00
|
|
|
obj_deinit(self);
|
|
|
|
args_deinit(buffs);
|
2021-11-23 12:15:12 +08:00
|
|
|
// obj_deinit(globals);
|
2021-10-21 18:38:28 +08:00
|
|
|
ASSERT_FLOAT_EQ(res, 10);
|
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
2021-10-26 20:50:13 +08:00
|
|
|
}
|
|
|
|
|
2021-10-30 22:08:44 +08:00
|
|
|
TEST(VM, mem_x) {
|
2022-07-14 08:36:02 +00:00
|
|
|
char* line =
|
|
|
|
"mem = PikaStdLib.MemChecker()\n"
|
|
|
|
"mem.x = 1\n"
|
|
|
|
"print(mem.x)\n"
|
|
|
|
"mem.x = 2\n"
|
|
|
|
"print(mem.x)\n"
|
|
|
|
"\n";
|
2021-10-26 20:50:13 +08:00
|
|
|
Args* buffs = New_strBuff();
|
2022-08-12 23:51:14 +08:00
|
|
|
char* pikaAsm = Parser_linesToAsm(buffs, line);
|
2021-10-26 20:50:13 +08:00
|
|
|
printf("%s", pikaAsm);
|
2022-04-30 21:17:19 +08:00
|
|
|
PikaObj* self = newRootObj("", New_PikaMain);
|
2022-02-22 00:41:45 +08:00
|
|
|
VMParameters* globals = pikaVM_runAsm(self, pikaAsm);
|
2021-10-26 20:50:13 +08:00
|
|
|
|
2022-04-30 21:17:19 +08:00
|
|
|
int res = obj_getInt(globals, "mem.x");
|
2021-11-14 18:39:08 +08:00
|
|
|
EXPECT_EQ(2, res);
|
2021-10-26 20:50:13 +08:00
|
|
|
obj_deinit(self);
|
2021-11-23 12:15:12 +08:00
|
|
|
// obj_deinit(globals);
|
2021-10-26 20:50:13 +08:00
|
|
|
args_deinit(buffs);
|
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
2021-10-29 21:45:24 +08:00
|
|
|
}
|
|
|
|
|
2021-10-30 11:08:56 +08:00
|
|
|
TEST(VM, RET_instruct) {
|
2022-07-14 08:36:02 +00:00
|
|
|
char* pikaAsm =
|
|
|
|
"B1\n"
|
|
|
|
"0 NUM 13\n"
|
|
|
|
"0 RET \n"
|
|
|
|
"0 NUM 2\n"
|
|
|
|
"0 RET \n"
|
|
|
|
"B0\n";
|
2021-10-30 11:08:56 +08:00
|
|
|
PikaObj* self = New_TinyObj(NULL);
|
2022-02-22 00:41:45 +08:00
|
|
|
VMParameters* globals = pikaVM_runAsm(self, pikaAsm);
|
2022-04-30 21:17:19 +08:00
|
|
|
Arg* return_arg = args_getArg(globals->list, "return");
|
2022-04-26 14:04:19 +08:00
|
|
|
int num = arg_getInt(return_arg);
|
2021-10-30 11:49:55 +08:00
|
|
|
EXPECT_EQ(num, 13);
|
2021-10-30 11:08:56 +08:00
|
|
|
obj_deinit(self);
|
2021-11-23 12:15:12 +08:00
|
|
|
// obj_deinit(globals);
|
2021-10-30 11:08:56 +08:00
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
2021-10-30 11:49:55 +08:00
|
|
|
}
|
|
|
|
|
2021-10-30 16:51:05 +08:00
|
|
|
TEST(VM, RUN_DEF) {
|
2022-07-14 08:36:02 +00:00
|
|
|
char* pikaAsm =
|
|
|
|
"B0\n"
|
|
|
|
"0 DEF test()\n"
|
|
|
|
"0 JMP 1\n"
|
|
|
|
"B1\n"
|
|
|
|
"0 NUM 1\n"
|
|
|
|
"0 RET \n"
|
|
|
|
"B0\n"
|
|
|
|
"0 RUN test\n"
|
|
|
|
"0 OUT a\n";
|
2021-10-30 16:51:05 +08:00
|
|
|
PikaObj* self = New_TinyObj(NULL);
|
2022-02-22 00:41:45 +08:00
|
|
|
VMParameters* globals = pikaVM_runAsm(self, pikaAsm);
|
2022-04-30 21:17:19 +08:00
|
|
|
int num = obj_getInt(globals, "a");
|
2021-10-31 16:25:18 +08:00
|
|
|
EXPECT_EQ(num, 1);
|
|
|
|
obj_deinit(self);
|
2021-11-23 12:15:12 +08:00
|
|
|
// obj_deinit(globals);
|
2021-10-31 16:25:18 +08:00
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(VM, RUN_global) {
|
2022-07-14 08:36:02 +00:00
|
|
|
char* pikaAsm =
|
|
|
|
"B0\n"
|
|
|
|
"0 NUM 1\n"
|
|
|
|
"0 OUT a\n"
|
|
|
|
"B0\n"
|
|
|
|
"0 DEF test()\n"
|
|
|
|
"0 JMP 1\n"
|
|
|
|
"B1\n"
|
|
|
|
"0 REF a\n"
|
|
|
|
"0 RET \n"
|
|
|
|
"B0\n"
|
|
|
|
"0 RUN test\n"
|
|
|
|
"0 OUT b\n";
|
2021-10-31 16:25:18 +08:00
|
|
|
PikaObj* self = New_TinyObj(NULL);
|
2021-11-23 12:15:12 +08:00
|
|
|
pikaVM_runAsm(self, pikaAsm);
|
2022-04-30 21:17:19 +08:00
|
|
|
int a = obj_getInt(self, "a");
|
|
|
|
int b = obj_getInt(self, "b");
|
2021-10-31 16:25:18 +08:00
|
|
|
EXPECT_EQ(a, 1);
|
|
|
|
EXPECT_EQ(b, 1);
|
|
|
|
obj_deinit(self);
|
2021-11-23 12:15:12 +08:00
|
|
|
// obj_deinit(globals);
|
2021-10-31 16:25:18 +08:00
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(VM, RUN_local_b) {
|
2022-07-14 08:36:02 +00:00
|
|
|
char* pikaAsm =
|
|
|
|
"B0\n"
|
|
|
|
"0 NUM 1\n"
|
|
|
|
"0 OUT a\n"
|
|
|
|
"B0\n"
|
|
|
|
"0 DEF test()\n"
|
|
|
|
"0 JMP 1\n"
|
|
|
|
"B1\n"
|
|
|
|
"0 REF a\n"
|
|
|
|
"0 OUT b\n"
|
|
|
|
"1 REF b\n"
|
|
|
|
"1 REF a\n"
|
|
|
|
"0 OPT +\n"
|
|
|
|
"0 RET \n"
|
|
|
|
"B0\n"
|
|
|
|
"0 RUN test\n"
|
|
|
|
"0 OUT c\n";
|
2022-04-30 21:17:19 +08:00
|
|
|
PikaObj* self = newRootObj("", New_BaseObj);
|
2022-02-22 00:41:45 +08:00
|
|
|
VMParameters* globals = pikaVM_runAsm(self, pikaAsm);
|
2022-04-30 21:17:19 +08:00
|
|
|
int a = obj_getInt(globals, "a");
|
|
|
|
int b = obj_getInt(globals, "b");
|
|
|
|
int c = obj_getInt(globals, "c");
|
2021-10-31 16:25:18 +08:00
|
|
|
EXPECT_EQ(a, 1);
|
|
|
|
/* b is local, should not be exist in globals */
|
|
|
|
EXPECT_EQ(b, -999999999);
|
|
|
|
EXPECT_EQ(c, 2);
|
|
|
|
obj_deinit(self);
|
2021-11-23 12:15:12 +08:00
|
|
|
// obj_deinit(globals);
|
2021-10-31 16:25:18 +08:00
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(VM, RUN_DEF_add) {
|
2022-07-14 08:36:02 +00:00
|
|
|
char* pikaAsm =
|
|
|
|
"B0\n"
|
|
|
|
"0 DEF add(a,b)\n"
|
|
|
|
"0 JMP 1\n"
|
|
|
|
"B1\n"
|
|
|
|
"1 REF b\n"
|
|
|
|
"1 REF a\n"
|
|
|
|
"0 OPT +\n"
|
|
|
|
"0 RET \n"
|
|
|
|
"B0\n"
|
|
|
|
"1 NUM 1\n"
|
|
|
|
"1 NUM 2\n"
|
|
|
|
"0 RUN add\n"
|
|
|
|
"0 OUT c\n";
|
2022-04-30 21:17:19 +08:00
|
|
|
PikaObj* self = newRootObj("", New_BaseObj);
|
2022-02-22 00:41:45 +08:00
|
|
|
VMParameters* globals = pikaVM_runAsm(self, pikaAsm);
|
2022-04-30 21:17:19 +08:00
|
|
|
int a = obj_getInt(globals, "a");
|
|
|
|
int b = obj_getInt(globals, "b");
|
|
|
|
int c = obj_getInt(globals, "c");
|
2021-10-31 16:25:18 +08:00
|
|
|
/* a is local, should not be exist in globals */
|
|
|
|
EXPECT_EQ(a, -999999999);
|
|
|
|
/* b is local, should not be exist in globals */
|
|
|
|
EXPECT_EQ(b, -999999999);
|
|
|
|
EXPECT_EQ(c, 3);
|
2021-10-30 16:51:05 +08:00
|
|
|
obj_deinit(self);
|
2021-11-23 12:15:12 +08:00
|
|
|
// obj_deinit(globals);
|
2021-10-30 16:51:05 +08:00
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
2021-10-31 21:05:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(VM, run_def_add) {
|
2022-07-14 08:36:02 +00:00
|
|
|
char* line =
|
|
|
|
"def add(a, b):\n"
|
|
|
|
" return a + b\n"
|
|
|
|
"c = add(1,3)\n"
|
|
|
|
"\n";
|
2021-10-31 21:05:42 +08:00
|
|
|
Args* buffs = New_strBuff();
|
2022-08-12 23:51:14 +08:00
|
|
|
char* pikaAsm = Parser_linesToAsm(buffs, line);
|
2021-10-31 21:05:42 +08:00
|
|
|
printf("%s", pikaAsm);
|
2022-04-30 21:17:19 +08:00
|
|
|
PikaObj* self = newRootObj("root", New_PikaStdLib_SysObj);
|
2022-02-22 00:41:45 +08:00
|
|
|
VMParameters* globals = pikaVM_runAsm(self, pikaAsm);
|
2021-10-31 21:05:42 +08:00
|
|
|
|
2022-04-30 21:17:19 +08:00
|
|
|
int c = args_getInt(globals->list, "c");
|
2021-10-31 21:05:42 +08:00
|
|
|
obj_deinit(self);
|
|
|
|
args_deinit(buffs);
|
2021-11-23 12:15:12 +08:00
|
|
|
// obj_deinit(globals);
|
2021-10-31 21:05:42 +08:00
|
|
|
ASSERT_FLOAT_EQ(c, 4);
|
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
|
|
|
}
|
2021-11-28 01:21:24 +08:00
|
|
|
|
|
|
|
TEST(VM, equ) {
|
2022-07-14 08:36:02 +00:00
|
|
|
char* line =
|
|
|
|
"if -1 == -1:\n"
|
|
|
|
" a = 1\n"
|
|
|
|
"\n";
|
2021-11-28 01:21:24 +08:00
|
|
|
Args* buffs = New_strBuff();
|
2022-08-12 23:51:14 +08:00
|
|
|
char* pikaAsm = Parser_linesToAsm(buffs, line);
|
2021-11-28 01:21:24 +08:00
|
|
|
printf("%s", pikaAsm);
|
2022-04-30 21:17:19 +08:00
|
|
|
PikaObj* self = newRootObj("root", New_PikaStdLib_SysObj);
|
2022-02-22 00:41:45 +08:00
|
|
|
VMParameters* globals = pikaVM_runAsm(self, pikaAsm);
|
2021-11-28 01:21:24 +08:00
|
|
|
|
2022-04-30 21:17:19 +08:00
|
|
|
int c = args_getInt(globals->list, "a");
|
2021-11-28 01:21:24 +08:00
|
|
|
obj_deinit(self);
|
|
|
|
args_deinit(buffs);
|
|
|
|
// obj_deinit(globals);
|
|
|
|
ASSERT_FLOAT_EQ(c, 1);
|
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
|
|
|
}
|
2021-12-14 22:17:37 +08:00
|
|
|
|
|
|
|
TEST(VM, if_elif) {
|
2022-07-14 08:36:02 +00:00
|
|
|
char* line =
|
|
|
|
"a = 2\n"
|
|
|
|
"b = 0\n"
|
|
|
|
"if a > 1:\n"
|
|
|
|
" b = 1\n"
|
|
|
|
"elif a > 0:\n"
|
|
|
|
" b = 2\n"
|
|
|
|
"\n";
|
2021-12-14 22:17:37 +08:00
|
|
|
Args* buffs = New_strBuff();
|
2022-08-12 23:51:14 +08:00
|
|
|
char* pikaAsm = Parser_linesToAsm(buffs, line);
|
2021-12-14 22:17:37 +08:00
|
|
|
printf("%s", pikaAsm);
|
2022-04-30 21:17:19 +08:00
|
|
|
PikaObj* self = newRootObj("root", New_PikaStdLib_SysObj);
|
2022-02-22 00:41:45 +08:00
|
|
|
VMParameters* globals = pikaVM_runAsm(self, pikaAsm);
|
2021-12-14 22:17:37 +08:00
|
|
|
|
2022-04-30 21:17:19 +08:00
|
|
|
int b = args_getInt(globals->list, "b");
|
2021-12-14 22:17:37 +08:00
|
|
|
obj_deinit(self);
|
|
|
|
args_deinit(buffs);
|
|
|
|
ASSERT_FLOAT_EQ(b, 1);
|
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(VM, if_else) {
|
2022-07-14 08:36:02 +00:00
|
|
|
char* line =
|
|
|
|
"a = 0\n"
|
|
|
|
"b = 0\n"
|
|
|
|
"if a > 1:\n"
|
|
|
|
" b = 1\n"
|
|
|
|
"else:\n"
|
|
|
|
" b = 2\n"
|
|
|
|
"\n";
|
2021-12-14 22:17:37 +08:00
|
|
|
Args* buffs = New_strBuff();
|
2022-08-12 23:51:14 +08:00
|
|
|
char* pikaAsm = Parser_linesToAsm(buffs, line);
|
2021-12-14 22:17:37 +08:00
|
|
|
printf("%s", pikaAsm);
|
2022-04-30 21:17:19 +08:00
|
|
|
PikaObj* self = newRootObj("root", New_PikaStdLib_SysObj);
|
2022-02-22 00:41:45 +08:00
|
|
|
VMParameters* globals = pikaVM_runAsm(self, pikaAsm);
|
2021-12-14 22:17:37 +08:00
|
|
|
|
2022-04-30 21:17:19 +08:00
|
|
|
int b = args_getInt(globals->list, "b");
|
2021-12-14 22:17:37 +08:00
|
|
|
obj_deinit(self);
|
|
|
|
args_deinit(buffs);
|
|
|
|
// obj_deinit(globals);
|
|
|
|
ASSERT_FLOAT_EQ(b, 2);
|
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
|
|
|
}
|
|
|
|
|
2021-12-14 22:24:18 +08:00
|
|
|
TEST(VM, if_else_) {
|
2022-07-14 08:36:02 +00:00
|
|
|
char* line =
|
|
|
|
"a = 2\n"
|
|
|
|
"b = 0\n"
|
|
|
|
"if a > 1:\n"
|
|
|
|
" b = 1\n"
|
|
|
|
"else:\n"
|
|
|
|
" b = 2\n"
|
|
|
|
"\n";
|
2021-12-14 22:24:18 +08:00
|
|
|
Args* buffs = New_strBuff();
|
2022-08-12 23:51:14 +08:00
|
|
|
char* pikaAsm = Parser_linesToAsm(buffs, line);
|
2021-12-14 22:24:18 +08:00
|
|
|
printf("%s", pikaAsm);
|
2022-04-30 21:17:19 +08:00
|
|
|
PikaObj* self = newRootObj("root", New_PikaStdLib_SysObj);
|
2022-02-22 00:41:45 +08:00
|
|
|
VMParameters* globals = pikaVM_runAsm(self, pikaAsm);
|
2021-12-14 22:24:18 +08:00
|
|
|
|
2022-04-30 21:17:19 +08:00
|
|
|
int b = args_getInt(globals->list, "b");
|
2021-12-14 22:24:18 +08:00
|
|
|
obj_deinit(self);
|
|
|
|
args_deinit(buffs);
|
|
|
|
// obj_deinit(globals);
|
|
|
|
ASSERT_FLOAT_EQ(b, 1);
|
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
|
|
|
}
|
2021-12-24 21:59:00 +08:00
|
|
|
|
|
|
|
TEST(VM, EST) {
|
2022-07-14 08:36:02 +00:00
|
|
|
char* pikaAsm =
|
|
|
|
"B0\n"
|
|
|
|
"0 EST a\n"
|
|
|
|
"0 OUT b\n";
|
2022-04-30 21:17:19 +08:00
|
|
|
PikaObj* self = newRootObj("", New_BaseObj);
|
2021-12-24 21:59:00 +08:00
|
|
|
pikaVM_runAsm(self, pikaAsm);
|
2022-04-30 21:17:19 +08:00
|
|
|
int a = obj_getInt(self, "a");
|
|
|
|
int b = obj_getInt(self, "b");
|
2021-12-24 21:59:00 +08:00
|
|
|
/* a is local, should not be exist in globals */
|
|
|
|
EXPECT_EQ(a, -999999999);
|
|
|
|
/* b is local, should not be exist in globals */
|
|
|
|
EXPECT_EQ(b, 0);
|
|
|
|
obj_deinit(self);
|
|
|
|
// obj_deinit(globals);
|
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
2022-02-25 18:05:54 +08:00
|
|
|
}
|
|
|
|
|
2022-03-01 15:01:22 +08:00
|
|
|
TEST(VM, RUN_AS) {
|
2022-07-14 08:36:02 +00:00
|
|
|
char* pikaAsm =
|
2022-03-01 15:01:22 +08:00
|
|
|
"B0\n"
|
2022-04-28 17:35:11 +08:00
|
|
|
"0 RUN TinyObj\n"
|
2022-03-01 15:01:22 +08:00
|
|
|
"0 OUT as\n"
|
|
|
|
"B0\n"
|
|
|
|
"0 RAS as\n"
|
|
|
|
"B0\n"
|
|
|
|
"0 NUM 1\n"
|
|
|
|
"0 OUT x\n"
|
|
|
|
"B0\n"
|
|
|
|
"0 RAS $origin\n"
|
|
|
|
"B0\n"
|
|
|
|
"0 NUM 2\n"
|
2022-07-14 08:36:02 +00:00
|
|
|
"0 OUT x\n";
|
2022-04-30 21:17:19 +08:00
|
|
|
PikaObj* self = newRootObj("", New_PikaMain);
|
2022-03-01 15:01:22 +08:00
|
|
|
pikaVM_runAsm(self, pikaAsm);
|
2022-04-30 21:17:19 +08:00
|
|
|
PikaObj* as = obj_getObj(self, "as");
|
|
|
|
int x_as_ = obj_getInt(as, "x");
|
|
|
|
int x_as = obj_getInt(self, "as.x");
|
|
|
|
int x_origin = obj_getInt(self, "x");
|
2022-03-01 15:01:22 +08:00
|
|
|
EXPECT_EQ(x_as_, 1);
|
|
|
|
EXPECT_EQ(x_as, 1);
|
|
|
|
EXPECT_EQ(x_origin, 2);
|
|
|
|
obj_deinit(self);
|
|
|
|
// obj_deinit(globals);
|
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
|
|
|
}
|
2022-03-01 16:13:35 +08:00
|
|
|
|
|
|
|
TEST(VM, RUN_NEW) {
|
2022-07-14 08:36:02 +00:00
|
|
|
char* pikaAsm =
|
2022-03-01 16:13:35 +08:00
|
|
|
"B0\n"
|
2022-04-28 17:35:11 +08:00
|
|
|
"0 RUN TinyObj\n"
|
2022-03-01 16:13:35 +08:00
|
|
|
"0 OUT newObj\n"
|
2022-03-01 19:21:39 +08:00
|
|
|
"0 NUM 1\n"
|
|
|
|
"0 OUT newObj.x\n"
|
2022-03-01 16:13:35 +08:00
|
|
|
"B0\n"
|
|
|
|
"0 NEW newObj\n"
|
2022-07-14 08:36:02 +00:00
|
|
|
"0 OUT outObj\n";
|
2022-04-30 21:17:19 +08:00
|
|
|
PikaObj* self = newRootObj("", New_PikaMain);
|
2022-03-01 16:13:35 +08:00
|
|
|
pikaVM_runAsm(self, pikaAsm);
|
2022-04-30 21:17:19 +08:00
|
|
|
Arg* newObj = obj_getArg(self, "newObj");
|
|
|
|
Arg* outObj = obj_getArg(self, "outObj");
|
2022-03-01 19:21:39 +08:00
|
|
|
void* newObj_ptr = arg_getPtr(newObj);
|
|
|
|
void* outObj_ptr = arg_getPtr(outObj);
|
|
|
|
EXPECT_EQ(newObj_ptr, outObj_ptr);
|
2022-04-30 21:17:19 +08:00
|
|
|
int x = obj_getInt(self, "outObj.x");
|
2022-03-01 16:13:35 +08:00
|
|
|
ArgType newObj_type = arg_getType(newObj);
|
|
|
|
ArgType outObj_type = arg_getType(outObj);
|
2022-03-01 19:21:39 +08:00
|
|
|
EXPECT_EQ(x, 1);
|
2022-04-26 10:43:50 +08:00
|
|
|
EXPECT_EQ(newObj_type, ARG_TYPE_OBJECT);
|
2022-03-02 10:34:33 +08:00
|
|
|
EXPECT_EQ(outObj_type, ARG_TYPE_OBJECT);
|
2022-03-01 16:13:35 +08:00
|
|
|
obj_deinit(self);
|
|
|
|
// obj_deinit(globals);
|
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
|
|
|
}
|
|
|
|
|
2022-03-01 19:13:39 +08:00
|
|
|
TEST(VM, RUN_DEF_NEW) {
|
2022-07-14 08:36:02 +00:00
|
|
|
char* pikaAsm =
|
2022-03-01 19:13:39 +08:00
|
|
|
"B0\n"
|
|
|
|
"0 DEF testnew()\n"
|
|
|
|
"0 JMP 1\n"
|
|
|
|
"B1\n"
|
2022-04-28 17:35:11 +08:00
|
|
|
"0 RUN TinyObj\n"
|
2022-03-01 19:13:39 +08:00
|
|
|
"0 OUT newObj\n"
|
|
|
|
"B1\n"
|
2022-03-01 19:21:39 +08:00
|
|
|
"0 NUM 1\n"
|
2022-03-01 19:13:39 +08:00
|
|
|
"0 OUT newObj.x\n"
|
|
|
|
"B1\n"
|
|
|
|
"0 NEW newObj\n"
|
2022-03-16 14:40:51 +08:00
|
|
|
"0 RET \n"
|
2022-03-01 19:13:39 +08:00
|
|
|
"B1\n"
|
2022-03-16 14:40:51 +08:00
|
|
|
"0 RET \n"
|
2022-03-01 19:13:39 +08:00
|
|
|
"B0\n"
|
|
|
|
"0 RUN testnew\n"
|
2022-07-14 08:36:02 +00:00
|
|
|
"0 OUT outobj\n";
|
2022-04-30 21:17:19 +08:00
|
|
|
PikaObj* self = newRootObj("", New_PikaMain);
|
2022-03-01 19:13:39 +08:00
|
|
|
pikaVM_runAsm(self, pikaAsm);
|
|
|
|
/* assert */
|
2022-04-30 21:17:19 +08:00
|
|
|
PikaObj* outobj = obj_getObj(self, "outobj");
|
|
|
|
int x = obj_getInt(outobj, "x");
|
2022-03-01 19:21:39 +08:00
|
|
|
EXPECT_EQ(x, 1);
|
2022-03-01 19:13:39 +08:00
|
|
|
/* deinit */
|
|
|
|
obj_deinit(self);
|
|
|
|
// obj_deinit(globals);
|
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(VM, class_x_1) {
|
2022-07-14 08:36:02 +00:00
|
|
|
char* line =
|
2022-04-28 17:35:11 +08:00
|
|
|
"class Test(TinyObj):\n"
|
2022-03-01 19:13:39 +08:00
|
|
|
" x = 1\n"
|
|
|
|
"test = Test()\n"
|
2022-03-01 20:55:06 +08:00
|
|
|
"print('test.x: ' + str(test.x))\n"
|
2022-07-14 08:36:02 +00:00
|
|
|
"\n";
|
2022-03-01 19:13:39 +08:00
|
|
|
Args* buffs = New_strBuff();
|
2022-08-12 23:51:14 +08:00
|
|
|
char* pikaAsm = Parser_linesToAsm(buffs, line);
|
2022-03-01 19:13:39 +08:00
|
|
|
printf("%s", pikaAsm);
|
2022-04-30 21:17:19 +08:00
|
|
|
PikaObj* self = newRootObj("", New_PikaMain);
|
2022-03-01 19:21:39 +08:00
|
|
|
pikaVM_runAsm(self, pikaAsm);
|
2022-03-01 19:13:39 +08:00
|
|
|
|
2022-04-30 21:17:19 +08:00
|
|
|
PikaObj* test = obj_getObj(self, "test");
|
|
|
|
Arg* test_arg = obj_getArg(self, "test");
|
2022-03-01 19:13:39 +08:00
|
|
|
ArgType test_arg_type = arg_getType(test_arg);
|
2022-03-02 10:34:33 +08:00
|
|
|
EXPECT_EQ(test_arg_type, ARG_TYPE_OBJECT);
|
2022-04-30 21:17:19 +08:00
|
|
|
int x = obj_getInt(test, "x");
|
2022-03-01 20:52:50 +08:00
|
|
|
EXPECT_EQ(x, 1);
|
2022-03-01 19:13:39 +08:00
|
|
|
obj_deinit(self);
|
|
|
|
args_deinit(buffs);
|
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
|
|
|
}
|
2022-03-08 12:29:30 +08:00
|
|
|
|
|
|
|
TEST(VM, nag_a) {
|
2022-07-14 08:36:02 +00:00
|
|
|
char* line =
|
|
|
|
"a = 1\n"
|
|
|
|
"print(-a)\n"
|
|
|
|
"b = 0.5\n"
|
|
|
|
"print(-b)\n";
|
2022-03-08 12:29:30 +08:00
|
|
|
Args* buffs = New_strBuff();
|
2022-08-12 23:51:14 +08:00
|
|
|
char* pikaAsm = Parser_linesToAsm(buffs, line);
|
2022-03-08 12:29:30 +08:00
|
|
|
printf("%s", pikaAsm);
|
2022-04-30 21:17:19 +08:00
|
|
|
PikaObj* self = newRootObj("", New_PikaMain);
|
|
|
|
__platform_printf("BEGIN\r\n");
|
2022-03-08 12:29:30 +08:00
|
|
|
pikaVM_runAsm(self, pikaAsm);
|
|
|
|
/* assert */
|
2022-04-30 21:17:19 +08:00
|
|
|
EXPECT_STREQ(log_buff[2], "BEGIN\r\n");
|
|
|
|
EXPECT_STREQ(log_buff[1], "-1\r\n");
|
|
|
|
EXPECT_STREQ(log_buff[0], "-0.500000\r\n");
|
2022-03-08 12:29:30 +08:00
|
|
|
/* deinit */
|
|
|
|
obj_deinit(self);
|
|
|
|
args_deinit(buffs);
|
|
|
|
/* check mem */
|
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
|
|
|
}
|
2022-03-10 17:08:15 +08:00
|
|
|
|
2022-03-13 10:24:18 +08:00
|
|
|
TEST(InstructUnit, base) {
|
2022-04-30 21:17:19 +08:00
|
|
|
__platform_printf("BEGIN\r\n");
|
2022-03-13 12:54:44 +08:00
|
|
|
InstructUnit ins_unit;
|
|
|
|
instructUnit_init(&ins_unit);
|
|
|
|
instructUnit_setBlockDeepth(&ins_unit, 2);
|
|
|
|
instructUnit_setIsNewLine(&ins_unit, 1);
|
|
|
|
instructUnit_setInvokeDeepth(&ins_unit, 3);
|
|
|
|
instructUnit_setInstruct(&ins_unit, (Instruct)4);
|
|
|
|
instructUnit_setConstPoolIndex(&ins_unit, 12);
|
|
|
|
|
|
|
|
EXPECT_EQ(instructUnit_getBlockDeepth(&ins_unit), 2);
|
|
|
|
EXPECT_EQ(instructUnit_getIsNewLine(&ins_unit), 1);
|
|
|
|
EXPECT_EQ(instructUnit_getInvokeDeepth(&ins_unit), 3);
|
|
|
|
EXPECT_EQ(instructUnit_getInstruct(&ins_unit), 4);
|
|
|
|
EXPECT_EQ(instructUnit_getConstPoolIndex(&ins_unit), 12);
|
|
|
|
|
|
|
|
instructUnit_print(&ins_unit);
|
2022-04-30 21:17:19 +08:00
|
|
|
EXPECT_STREQ(log_buff[2], "BEGIN\r\n");
|
|
|
|
EXPECT_STREQ(log_buff[1], "B2\r\n");
|
|
|
|
EXPECT_STREQ(log_buff[0], "3 OUT #12\r\n");
|
2022-03-13 12:01:25 +08:00
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
2022-03-13 10:24:18 +08:00
|
|
|
}
|
|
|
|
|
2022-03-12 21:55:11 +08:00
|
|
|
TEST(ConstPool, get) {
|
2022-04-30 21:17:19 +08:00
|
|
|
__platform_printf("BEGIN\r\n");
|
2022-03-12 21:55:11 +08:00
|
|
|
ConstPool cp;
|
|
|
|
constPool_init(&cp);
|
|
|
|
char* next_content;
|
|
|
|
uint16_t next_offset;
|
|
|
|
next_offset = constPool_getLastOffset(&cp);
|
2022-04-30 21:17:19 +08:00
|
|
|
constPool_append(&cp, "hello");
|
2022-03-12 21:55:11 +08:00
|
|
|
next_content = constPool_getByOffset(&cp, next_offset);
|
2022-04-30 21:17:19 +08:00
|
|
|
EXPECT_STREQ(next_content, "hello");
|
2022-03-12 21:55:11 +08:00
|
|
|
next_offset = constPool_getLastOffset(&cp);
|
2022-04-30 21:17:19 +08:00
|
|
|
constPool_append(&cp, "world");
|
2022-03-12 21:55:11 +08:00
|
|
|
next_content = constPool_getByOffset(&cp, next_offset);
|
2022-04-30 21:17:19 +08:00
|
|
|
EXPECT_STREQ(next_content, "world");
|
2022-03-12 21:55:11 +08:00
|
|
|
char* first = constPool_getNow(&cp);
|
|
|
|
char* second = constPool_getNext(&cp);
|
|
|
|
char* third = constPool_getNext(&cp);
|
|
|
|
char* forth = constPool_getNext(&cp);
|
2022-04-30 21:17:19 +08:00
|
|
|
EXPECT_STREQ(first, "");
|
|
|
|
EXPECT_STREQ(second, "hello");
|
|
|
|
EXPECT_STREQ(third, "world");
|
2022-03-12 21:55:11 +08:00
|
|
|
EXPECT_EQ((uintptr_t)forth, (uintptr_t)NULL);
|
2022-04-30 21:17:19 +08:00
|
|
|
EXPECT_STREQ(constPool_getByIndex(&cp, 0), "");
|
|
|
|
EXPECT_STREQ(constPool_getByIndex(&cp, 1), "hello");
|
|
|
|
EXPECT_STREQ(constPool_getByIndex(&cp, 2), "world");
|
2022-03-12 21:55:11 +08:00
|
|
|
EXPECT_EQ((uintptr_t)constPool_getByIndex(&cp, 3), (uintptr_t)NULL);
|
2022-03-12 22:06:38 +08:00
|
|
|
constPool_print(&cp);
|
2022-04-30 21:17:19 +08:00
|
|
|
EXPECT_STREQ(log_buff[2], "BEGIN\r\n");
|
|
|
|
EXPECT_STREQ(log_buff[1], "1: hello\r\n");
|
|
|
|
EXPECT_STREQ(log_buff[0], "7: world\r\n");
|
2022-03-12 21:55:11 +08:00
|
|
|
constPool_deinit(&cp);
|
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
|
|
|
}
|
2022-03-13 10:24:18 +08:00
|
|
|
|
|
|
|
TEST(InstructArray, set) {
|
2022-04-30 21:17:19 +08:00
|
|
|
__platform_printf("BEGIN\r\n");
|
2022-03-13 12:54:44 +08:00
|
|
|
InstructUnit ins_unit;
|
|
|
|
instructUnit_init(&ins_unit);
|
|
|
|
instructUnit_setBlockDeepth(&ins_unit, 2);
|
|
|
|
instructUnit_setIsNewLine(&ins_unit, 1);
|
|
|
|
instructUnit_setInvokeDeepth(&ins_unit, 3);
|
|
|
|
instructUnit_setInstruct(&ins_unit, (Instruct)4);
|
|
|
|
instructUnit_setConstPoolIndex(&ins_unit, 12);
|
|
|
|
|
|
|
|
InstructArray ins_array;
|
|
|
|
instructArray_init(&ins_array);
|
|
|
|
instructArray_append(&ins_array, &ins_unit);
|
|
|
|
instructArray_print(&ins_array);
|
|
|
|
instructArray_deinit(&ins_array);
|
2022-04-30 21:17:19 +08:00
|
|
|
EXPECT_STREQ(log_buff[2], "BEGIN\r\n");
|
|
|
|
EXPECT_STREQ(log_buff[1], "B2\r\n");
|
|
|
|
EXPECT_STREQ(log_buff[0], "3 OUT #12\r\n");
|
2022-03-13 10:24:18 +08:00
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
|
|
|
}
|
2022-03-13 16:59:01 +08:00
|
|
|
|
|
|
|
TEST(VM, bytecode_jjcc) {
|
2022-04-30 21:17:19 +08:00
|
|
|
char* line = "a = (1 + 1.1) * 3 - 2 /4.0";
|
2022-03-13 16:59:01 +08:00
|
|
|
Args* buffs = New_strBuff();
|
|
|
|
char* pikaAsm = Parser_LineToAsm(buffs, line, NULL);
|
|
|
|
printf("%s", pikaAsm);
|
|
|
|
ByteCodeFrame byte_frame;
|
|
|
|
byteCodeFrame_init(&byte_frame);
|
|
|
|
byteCodeFrame_appendFromAsm(&byte_frame, pikaAsm);
|
|
|
|
byteCodeFrame_print(&byte_frame);
|
|
|
|
|
2022-04-30 21:17:19 +08:00
|
|
|
PikaObj* self = newRootObj("root", New_PikaStdLib_SysObj);
|
2022-03-13 16:59:01 +08:00
|
|
|
// pikaVM_runAsm(self, pikaAsm);
|
|
|
|
pikaVM_runByteCodeFrame(self, &byte_frame);
|
|
|
|
|
2022-04-30 21:17:19 +08:00
|
|
|
float res = obj_getFloat(self, "a");
|
2022-03-13 16:59:01 +08:00
|
|
|
|
|
|
|
obj_deinit(self);
|
|
|
|
args_deinit(buffs);
|
|
|
|
byteCodeFrame_deinit(&byte_frame);
|
|
|
|
|
|
|
|
ASSERT_FLOAT_EQ(res, 5.8);
|
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
|
|
|
}
|
2022-03-13 19:01:34 +08:00
|
|
|
|
|
|
|
TEST(VM, WHILE_byte) {
|
|
|
|
pikaMemInfo.heapUsedMax = 0;
|
|
|
|
Args* buffs = New_strBuff();
|
|
|
|
char* lines =(char *)
|
|
|
|
"a = 1\n"
|
|
|
|
"b = 0\n"
|
|
|
|
"while a:\n"
|
|
|
|
" b = 1\n"
|
|
|
|
" a = 0\n"
|
|
|
|
"\n";
|
|
|
|
printf("%s", lines);
|
2022-08-12 23:51:14 +08:00
|
|
|
char* pikaAsm = Parser_linesToAsm(buffs, lines);
|
2022-03-13 19:01:34 +08:00
|
|
|
printf("%s", pikaAsm);
|
|
|
|
pikaMemInfo.heapUsedMax = 0;
|
|
|
|
PikaObj* self = New_TinyObj(NULL);
|
2022-03-14 21:24:35 +08:00
|
|
|
pikaVM_run(self, lines);
|
2022-04-30 21:17:19 +08:00
|
|
|
EXPECT_EQ(obj_getInt(self, "a"), 0);
|
|
|
|
EXPECT_EQ(obj_getInt(self, "b"), 1);
|
2022-03-13 19:01:34 +08:00
|
|
|
args_deinit(buffs);
|
|
|
|
obj_deinit(self);
|
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
|
|
|
}
|
2022-03-14 12:12:04 +08:00
|
|
|
|
|
|
|
TEST(VM, for_break_byte) {
|
|
|
|
pikaMemInfo.heapUsedMax = 0;
|
|
|
|
Args* buffs = New_strBuff();
|
|
|
|
char* lines =(char *)
|
|
|
|
"a = 0\n"
|
|
|
|
"for i in range(0, 10):\n"
|
|
|
|
" if i == 5:\n"
|
|
|
|
" break\n"
|
|
|
|
" a = a + i\n"
|
|
|
|
"\n";
|
|
|
|
printf("%s", lines);
|
2022-08-12 23:51:14 +08:00
|
|
|
char* pikaAsm = Parser_linesToAsm(buffs, lines);
|
2022-03-14 12:12:04 +08:00
|
|
|
printf("%s", pikaAsm);
|
|
|
|
pikaMemInfo.heapUsedMax = 0;
|
2022-04-30 21:17:19 +08:00
|
|
|
PikaObj* self = newRootObj("pikaMain", New_PikaMain);
|
2022-03-14 21:24:35 +08:00
|
|
|
pikaVM_run(self, lines);
|
2022-03-14 12:12:04 +08:00
|
|
|
/* assert */
|
2022-04-30 21:17:19 +08:00
|
|
|
int a = obj_getInt(self, "a");
|
2022-03-14 12:12:04 +08:00
|
|
|
EXPECT_EQ(a, 10);
|
|
|
|
/* deinit */
|
|
|
|
args_deinit(buffs);
|
|
|
|
obj_deinit(self);
|
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
|
|
|
}
|
2022-03-17 15:43:26 +08:00
|
|
|
|
|
|
|
TEST(VM, load_static_bytes) {
|
|
|
|
const uint8_t bytes[] = {
|
|
|
|
0x08, 0x02, /* instruct array size */
|
|
|
|
0x00, 0x82, 0x01, 0x00, 0x00, 0x04, 0x0d, 0x00, 0x00, 0x83, 0x11, 0x00,
|
|
|
|
0x00, 0x04, 0x16, 0x00, 0x00, 0x85, 0x1b, 0x00, 0x00, 0x04, 0x1d, 0x00,
|
|
|
|
0x20, 0x85, 0x1b, 0x00, 0x20, 0x01, 0x0d, 0x00, 0x10, 0x02, 0x28, 0x00,
|
|
|
|
0x00, 0x02, 0x2e, 0x00, 0x00, 0x04, 0x33, 0x00, 0x00, 0x01, 0x37, 0x00,
|
|
|
|
0x00, 0x01, 0x3b, 0x00, 0x00, 0x01, 0x3f, 0x00, 0x00, 0x04, 0x43, 0x00,
|
|
|
|
0x00, 0x04, 0x4a, 0x00, 0x00, 0x04, 0x51, 0x00, 0x00, 0x82, 0x58, 0x00,
|
|
|
|
0x00, 0x04, 0x65, 0x00, 0x00, 0x0d, 0x65, 0x00, 0x00, 0x07, 0x67, 0x00,
|
|
|
|
0x11, 0x81, 0x0d, 0x00, 0x11, 0x05, 0x1b, 0x00, 0x01, 0x08, 0x69, 0x00,
|
|
|
|
0x01, 0x07, 0x6c, 0x00, 0x02, 0x8e, 0x6c, 0x00, 0x11, 0x81, 0x1d, 0x00,
|
|
|
|
0x11, 0x05, 0x1b, 0x00, 0x01, 0x08, 0x69, 0x00, 0x01, 0x07, 0x6c, 0x00,
|
|
|
|
0x12, 0x81, 0x6e, 0x00, 0x12, 0x01, 0x65, 0x00, 0x02, 0x02, 0x76, 0x00,
|
|
|
|
0x02, 0x04, 0x16, 0x00, 0x02, 0x85, 0x6c, 0x00, 0x02, 0x04, 0x1d, 0x00,
|
|
|
|
0x01, 0x8b, 0x6c, 0x00, 0x11, 0x01, 0x1d, 0x00, 0x11, 0x05, 0x6c, 0x00,
|
|
|
|
0x01, 0x08, 0x69, 0x00, 0x01, 0x07, 0x6c, 0x00, 0x12, 0x81, 0x16, 0x00,
|
|
|
|
0x12, 0x03, 0x7e, 0x00, 0x02, 0x08, 0x69, 0x00, 0x02, 0x07, 0x6c, 0x00,
|
|
|
|
0x13, 0x81, 0x6e, 0x00, 0x13, 0x01, 0x65, 0x00, 0x03, 0x02, 0x76, 0x00,
|
|
|
|
0x03, 0x04, 0x85, 0x00, 0x03, 0x82, 0x85, 0x00, 0x03, 0x85, 0x1b, 0x00,
|
|
|
|
0x03, 0x04, 0x1d, 0x00, 0x02, 0x8b, 0x6c, 0x00, 0x12, 0x01, 0x16, 0x00,
|
|
|
|
0x12, 0x03, 0x8a, 0x00, 0x02, 0x08, 0x69, 0x00, 0x02, 0x07, 0x6c, 0x00,
|
|
|
|
0x13, 0x81, 0x6e, 0x00, 0x13, 0x01, 0x65, 0x00, 0x03, 0x02, 0x76, 0x00,
|
|
|
|
0x03, 0x04, 0x8a, 0x00, 0x03, 0x85, 0x67, 0x00, 0x03, 0x04, 0x1d, 0x00,
|
|
|
|
0x02, 0x8b, 0x6c, 0x00, 0x12, 0x01, 0x16, 0x00, 0x12, 0x03, 0x8f, 0x00,
|
|
|
|
0x02, 0x08, 0x69, 0x00, 0x02, 0x07, 0x6c, 0x00, 0x13, 0x81, 0x6e, 0x00,
|
|
|
|
0x13, 0x01, 0x65, 0x00, 0x03, 0x02, 0x76, 0x00, 0x03, 0x04, 0x8f, 0x00,
|
|
|
|
0x03, 0x85, 0x67, 0x00, 0x03, 0x04, 0x1d, 0x00, 0x01, 0x8b, 0x6c, 0x00,
|
|
|
|
0x11, 0x01, 0x1d, 0x00, 0x11, 0x05, 0x67, 0x00, 0x01, 0x08, 0x69, 0x00,
|
|
|
|
0x01, 0x07, 0x6c, 0x00, 0x12, 0x81, 0x16, 0x00, 0x12, 0x03, 0x8a, 0x00,
|
|
|
|
0x02, 0x08, 0x69, 0x00, 0x02, 0x07, 0x6c, 0x00, 0x03, 0x82, 0x8a, 0x00,
|
|
|
|
0x03, 0x07, 0x6c, 0x00, 0x14, 0x81, 0x6e, 0x00, 0x14, 0x01, 0x65, 0x00,
|
|
|
|
0x04, 0x02, 0x76, 0x00, 0x04, 0x04, 0x85, 0x00, 0x04, 0x82, 0x85, 0x00,
|
|
|
|
0x03, 0x85, 0x1b, 0x00, 0x03, 0x04, 0x1d, 0x00, 0x02, 0x8b, 0x6c, 0x00,
|
|
|
|
0x12, 0x01, 0x16, 0x00, 0x12, 0x03, 0x8f, 0x00, 0x02, 0x08, 0x69, 0x00,
|
|
|
|
0x02, 0x07, 0x6c, 0x00, 0x13, 0x81, 0x6e, 0x00, 0x13, 0x01, 0x65, 0x00,
|
|
|
|
0x03, 0x02, 0x76, 0x00, 0x03, 0x04, 0x85, 0x00, 0x03, 0x85, 0x99, 0x00,
|
|
|
|
0x03, 0x04, 0x1d, 0x00, 0x01, 0x8b, 0x6c, 0x00, 0x11, 0x01, 0x1d, 0x00,
|
|
|
|
0x11, 0x05, 0x99, 0x00, 0x01, 0x08, 0x69, 0x00, 0x01, 0x07, 0x6c, 0x00,
|
|
|
|
0x12, 0x81, 0x16, 0x00, 0x12, 0x03, 0x8f, 0x00, 0x02, 0x08, 0x69, 0x00,
|
|
|
|
0x02, 0x07, 0x6c, 0x00, 0x13, 0x81, 0x9b, 0x00, 0x23, 0x01, 0x6e, 0x00,
|
|
|
|
0x23, 0x01, 0x65, 0x00, 0x13, 0x02, 0x76, 0x00, 0x03, 0x08, 0xa2, 0x00,
|
|
|
|
0x03, 0x07, 0x6c, 0x00, 0x04, 0x82, 0x85, 0x00, 0x14, 0x81, 0x6e, 0x00,
|
|
|
|
0x14, 0x01, 0x65, 0x00, 0x24, 0x01, 0x9b, 0x00, 0x24, 0x01, 0x8f, 0x00,
|
|
|
|
0x14, 0x08, 0xa4, 0x00, 0x14, 0x03, 0x6e, 0x00, 0x04, 0x02, 0xa6, 0x00,
|
|
|
|
0x03, 0x85, 0x1b, 0x00, 0x03, 0x04, 0x1d, 0x00, 0x00, 0x86, 0xae, 0x00,
|
|
|
|
0x00, 0x8c, 0x33, 0x00, /* instruct array */
|
|
|
|
0xb1, 0x00, /* const pool size */
|
2022-03-17 16:47:03 +08:00
|
|
|
0x00, 0x5f, 0x5f, 0x63, 0x61, 0x6c, 0x6c, 0x73, 0x2e, 0x6c, 0x65, 0x6e,
|
|
|
|
0x00, 0x6c, 0x65, 0x6e, 0x00, 0x6e, 0x6f, 0x6e, 0x65, 0x00, 0x6d, 0x6f,
|
|
|
|
0x64, 0x65, 0x00, 0x30, 0x00, 0x69, 0x6e, 0x66, 0x6f, 0x5f, 0x69, 0x6e,
|
|
|
|
0x64, 0x65, 0x78, 0x00, 0x72, 0x61, 0x6e, 0x67, 0x65, 0x00, 0x69, 0x74,
|
|
|
|
0x65, 0x72, 0x00, 0x5f, 0x6c, 0x30, 0x00, 0x5f, 0x72, 0x31, 0x00, 0x5f,
|
|
|
|
0x72, 0x32, 0x00, 0x5f, 0x72, 0x33, 0x00, 0x5f, 0x6c, 0x30, 0x2e, 0x61,
|
|
|
|
0x31, 0x00, 0x5f, 0x6c, 0x30, 0x2e, 0x61, 0x32, 0x00, 0x5f, 0x6c, 0x30,
|
|
|
|
0x2e, 0x61, 0x33, 0x00, 0x5f, 0x6c, 0x30, 0x2e, 0x5f, 0x5f, 0x6e, 0x65,
|
|
|
|
0x78, 0x74, 0x5f, 0x5f, 0x00, 0x69, 0x00, 0x32, 0x00, 0x3d, 0x3d, 0x00,
|
|
|
|
0x31, 0x00, 0x5f, 0x5f, 0x63, 0x61, 0x6c, 0x6c, 0x73, 0x00, 0x5f, 0x5f,
|
|
|
|
0x67, 0x65, 0x74, 0x5f, 0x5f, 0x00, 0x61, 0x6c, 0x77, 0x61, 0x79, 0x73,
|
|
|
|
0x00, 0x74, 0x6f, 0x64, 0x6f, 0x00, 0x77, 0x68, 0x65, 0x6e, 0x00, 0x70,
|
|
|
|
0x65, 0x72, 0x69, 0x6f, 0x64, 0x5f, 0x6d, 0x73, 0x00, 0x33, 0x00, 0x5f,
|
|
|
|
0x5f, 0x74, 0x69, 0x63, 0x6b, 0x00, 0x3e, 0x00, 0x2b, 0x00, 0x5f, 0x5f,
|
|
|
|
0x73, 0x65, 0x74, 0x5f, 0x5f, 0x00, 0x2d, 0x31, 0x00, /* const pool */
|
2022-03-17 15:43:26 +08:00
|
|
|
};
|
|
|
|
ByteCodeFrame bytecode_frame = {.const_pool =
|
|
|
|
{
|
|
|
|
.arg_buff = NULL,
|
|
|
|
.content_offset_now = 0,
|
|
|
|
.size = 0,
|
|
|
|
.content_start = NULL,
|
2022-03-18 13:44:40 +08:00
|
|
|
.output_redirect_fun = NULL,
|
2022-03-18 15:32:01 +08:00
|
|
|
.output_f = NULL,
|
2022-03-17 15:43:26 +08:00
|
|
|
},
|
|
|
|
.instruct_array = {
|
|
|
|
.arg_buff = NULL,
|
|
|
|
.content_offset_now = 0,
|
|
|
|
.size = 0,
|
|
|
|
.content_start = NULL,
|
2022-03-18 13:44:40 +08:00
|
|
|
.output_redirect_fun = NULL,
|
2022-03-18 15:32:01 +08:00
|
|
|
.output_f = NULL,
|
2022-03-17 15:43:26 +08:00
|
|
|
}};
|
|
|
|
|
2022-04-29 23:56:25 +08:00
|
|
|
byteCodeFrame_loadByteCode(&bytecode_frame, (uint8_t*)bytes);
|
2022-03-17 16:47:03 +08:00
|
|
|
byteCodeFrame_print(&bytecode_frame);
|
2022-03-17 15:43:26 +08:00
|
|
|
|
|
|
|
EXPECT_EQ(instructArray_getSize(&(bytecode_frame.instruct_array)), 520);
|
|
|
|
EXPECT_EQ(bytecode_frame.const_pool.size, 177);
|
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
|
|
|
}
|
2022-04-02 01:54:29 +08:00
|
|
|
|
2022-04-02 15:50:31 +08:00
|
|
|
TEST(VM, multi_jian) {
|
|
|
|
pikaMemInfo.heapUsedMax = 0;
|
|
|
|
Args* buffs = New_strBuff();
|
2022-04-30 21:17:19 +08:00
|
|
|
char* lines = "a = (3-4) - 4\n";
|
2022-04-02 15:50:31 +08:00
|
|
|
printf("%s", lines);
|
2022-08-12 23:51:14 +08:00
|
|
|
char* pikaAsm = Parser_linesToAsm(buffs, lines);
|
2022-04-02 15:50:31 +08:00
|
|
|
printf("%s", pikaAsm);
|
|
|
|
pikaMemInfo.heapUsedMax = 0;
|
2022-04-30 21:17:19 +08:00
|
|
|
PikaObj* self = newRootObj("pikaMain", New_PikaMain);
|
2022-04-02 15:50:31 +08:00
|
|
|
pikaVM_run(self, lines);
|
|
|
|
/* assert */
|
2022-04-30 21:17:19 +08:00
|
|
|
int a = obj_getInt(self, "a");
|
2022-04-02 15:50:31 +08:00
|
|
|
EXPECT_EQ(a, -5);
|
|
|
|
/* deinit */
|
|
|
|
args_deinit(buffs);
|
|
|
|
obj_deinit(self);
|
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
|
|
|
}
|
2022-04-02 22:37:41 +08:00
|
|
|
|
|
|
|
TEST(VM, _3_p_3) {
|
2022-04-30 21:17:19 +08:00
|
|
|
char* line = "-3+3";
|
2022-04-02 22:37:41 +08:00
|
|
|
Args* buffs = New_strBuff();
|
|
|
|
char* pikaAsm = Parser_LineToAsm(buffs, line, NULL);
|
|
|
|
printf("%s", pikaAsm);
|
2022-04-30 21:17:19 +08:00
|
|
|
PikaObj* self = newRootObj("root", New_PikaStdLib_SysObj);
|
|
|
|
__platform_printf("BEGIN\r\n");
|
2022-04-02 22:37:41 +08:00
|
|
|
pikaVM_runAsm(self, pikaAsm);
|
2022-04-30 21:17:19 +08:00
|
|
|
EXPECT_STREQ(log_buff[1], "BEGIN\r\n");
|
|
|
|
EXPECT_STREQ(log_buff[0], "0\r\n");
|
2022-04-02 22:37:41 +08:00
|
|
|
obj_deinit(self);
|
|
|
|
args_deinit(buffs);
|
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
|
|
|
}
|
2022-04-02 23:32:59 +08:00
|
|
|
|
|
|
|
TEST(VM, _3_3) {
|
2022-04-30 21:17:19 +08:00
|
|
|
char* line = "-3-3";
|
2022-04-02 23:32:59 +08:00
|
|
|
Args* buffs = New_strBuff();
|
|
|
|
char* pikaAsm = Parser_LineToAsm(buffs, line, NULL);
|
|
|
|
printf("%s", pikaAsm);
|
2022-04-30 21:17:19 +08:00
|
|
|
PikaObj* self = newRootObj("root", New_PikaStdLib_SysObj);
|
|
|
|
__platform_printf("BEGIN\r\n");
|
2022-04-02 23:32:59 +08:00
|
|
|
pikaVM_runAsm(self, pikaAsm);
|
2022-04-30 21:17:19 +08:00
|
|
|
EXPECT_STREQ(log_buff[1], "BEGIN\r\n");
|
|
|
|
EXPECT_STREQ(log_buff[0], "-6\r\n");
|
2022-04-02 23:32:59 +08:00
|
|
|
obj_deinit(self);
|
|
|
|
args_deinit(buffs);
|
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
|
|
|
}
|
2022-04-03 12:55:06 +08:00
|
|
|
|
|
|
|
TEST(VM, a_3) {
|
2022-04-30 21:17:19 +08:00
|
|
|
char* line = "a-3";
|
2022-04-03 12:55:06 +08:00
|
|
|
Args* buffs = New_strBuff();
|
|
|
|
char* pikaAsm = Parser_LineToAsm(buffs, line, NULL);
|
|
|
|
printf("%s", pikaAsm);
|
2022-04-30 21:17:19 +08:00
|
|
|
PikaObj* self = newRootObj("root", New_PikaStdLib_SysObj);
|
|
|
|
__platform_printf("BEGIN\r\n");
|
2022-04-03 12:55:06 +08:00
|
|
|
pikaVM_runAsm(self, pikaAsm);
|
2022-04-30 21:17:19 +08:00
|
|
|
EXPECT_STREQ(log_buff[3], "BEGIN\r\n");
|
|
|
|
EXPECT_STREQ(log_buff[2], "NameError: name 'a' is not defined\r\n");
|
2022-04-03 12:55:06 +08:00
|
|
|
obj_deinit(self);
|
|
|
|
args_deinit(buffs);
|
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
|
|
|
}
|
2022-04-19 19:16:53 +08:00
|
|
|
|
|
|
|
TEST(VM, hex_str) {
|
2022-04-30 21:17:19 +08:00
|
|
|
char* line = "a = '\\x33\\x35'";
|
2022-04-19 19:16:53 +08:00
|
|
|
Args* buffs = New_strBuff();
|
|
|
|
char* pikaAsm = Parser_LineToAsm(buffs, line, NULL);
|
|
|
|
printf("%s", pikaAsm);
|
2022-04-30 21:17:19 +08:00
|
|
|
PikaObj* self = newRootObj("root", New_PikaStdLib_SysObj);
|
|
|
|
__platform_printf("BEGIN\r\n");
|
2022-04-19 19:16:53 +08:00
|
|
|
pikaVM_runAsm(self, pikaAsm);
|
|
|
|
/* collect */
|
2022-04-30 21:17:19 +08:00
|
|
|
char* a = obj_getStr(self, "a");
|
2022-04-19 19:16:53 +08:00
|
|
|
/* assert */
|
2022-04-30 21:17:19 +08:00
|
|
|
EXPECT_STREQ(a, "35");
|
2022-04-19 19:16:53 +08:00
|
|
|
/* deinit */
|
|
|
|
obj_deinit(self);
|
|
|
|
args_deinit(buffs);
|
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
|
|
|
}
|
2022-04-20 14:00:05 +08:00
|
|
|
|
|
|
|
TEST(VM, hex_bytes) {
|
2022-07-14 08:36:02 +00:00
|
|
|
char* line =
|
2022-04-20 14:00:05 +08:00
|
|
|
"a = b'\\x03\\x05'\n"
|
2022-07-14 08:36:02 +00:00
|
|
|
"a\n";
|
2022-04-20 14:00:05 +08:00
|
|
|
Args* buffs = New_strBuff();
|
2022-08-12 23:51:14 +08:00
|
|
|
char* pikaAsm = Parser_linesToAsm(buffs, line);
|
2022-04-20 14:00:05 +08:00
|
|
|
printf("%s", pikaAsm);
|
2022-04-30 21:17:19 +08:00
|
|
|
PikaObj* self = newRootObj("root", New_PikaStdLib_SysObj);
|
|
|
|
__platform_printf("BEGIN\r\n");
|
2022-04-20 14:00:05 +08:00
|
|
|
pikaVM_runAsm(self, pikaAsm);
|
|
|
|
/* collect */
|
|
|
|
/* assert */
|
2022-09-04 21:19:41 +08:00
|
|
|
EXPECT_STREQ(log_buff[0], "b'\\x03\\x05'\r\n");
|
2022-04-20 14:00:05 +08:00
|
|
|
/* deinit */
|
|
|
|
obj_deinit(self);
|
|
|
|
args_deinit(buffs);
|
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
|
|
|
}
|
2022-05-24 15:08:46 +08:00
|
|
|
|
|
|
|
TEST(VM, bytes_equ) {
|
2022-07-14 08:36:02 +00:00
|
|
|
char* line =
|
2022-05-24 15:08:46 +08:00
|
|
|
"a = b'\\x03\\x05'\n"
|
|
|
|
"b = b'\\x03\\x05'\n"
|
|
|
|
"if a == b:\n"
|
|
|
|
" c = 1\n"
|
|
|
|
"b = b'\\x02\\x05'\n"
|
|
|
|
"if a != b:\n"
|
2022-07-14 08:36:02 +00:00
|
|
|
" d = 1\n";
|
2022-05-24 15:08:46 +08:00
|
|
|
Args* buffs = New_strBuff();
|
2022-08-12 23:51:14 +08:00
|
|
|
char* pikaAsm = Parser_linesToAsm(buffs, line);
|
2022-05-24 15:08:46 +08:00
|
|
|
printf("%s", pikaAsm);
|
|
|
|
PikaObj* self = newRootObj("root", New_PikaStdLib_SysObj);
|
|
|
|
__platform_printf("BEGIN\r\n");
|
|
|
|
pikaVM_runAsm(self, pikaAsm);
|
|
|
|
/* collect */
|
|
|
|
/* assert */
|
|
|
|
int c = obj_getInt(self, "c");
|
|
|
|
int d = obj_getInt(self, "d");
|
|
|
|
EXPECT_EQ(c, 1);
|
|
|
|
EXPECT_EQ(d, 1);
|
|
|
|
/* deinit */
|
|
|
|
obj_deinit(self);
|
|
|
|
args_deinit(buffs);
|
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
2022-07-14 08:36:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#if PIKA_BUILTIN_STRUCT_ENABLE
|
|
|
|
TEST(VM, tuple_literal) {
|
|
|
|
char* line = "(1,2,3)\n";
|
|
|
|
Args* buffs = New_strBuff();
|
2022-08-12 23:51:14 +08:00
|
|
|
char* pikaAsm = Parser_linesToAsm(buffs, line);
|
2022-07-14 08:36:02 +00:00
|
|
|
printf("%s", pikaAsm);
|
|
|
|
PikaObj* self = newRootObj("root", New_PikaStdLib_SysObj);
|
|
|
|
__platform_printf("BEGIN\r\n");
|
|
|
|
pikaVM_runAsm(self, pikaAsm);
|
|
|
|
/* collect */
|
|
|
|
/* assert */
|
|
|
|
EXPECT_STREQ(log_buff[0], "(1, 2, 3)\r\n");
|
|
|
|
EXPECT_STREQ(log_buff[1], "BEGIN\r\n");
|
|
|
|
/* deinit */
|
|
|
|
obj_deinit(self);
|
|
|
|
args_deinit(buffs);
|
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
|
|
|
}
|
|
|
|
#endif
|
2022-07-30 11:59:44 +08:00
|
|
|
|
|
|
|
TEST(VM, dvd_opt) {
|
2022-08-04 00:25:32 +08:00
|
|
|
char* line = "a = 10%4\n";
|
2022-07-30 11:59:44 +08:00
|
|
|
PikaObj* self = newRootObj("root", New_PikaStdLib_SysObj);
|
|
|
|
obj_run(self, line);
|
|
|
|
/* collect */
|
|
|
|
int a = obj_getInt(self, "a");
|
|
|
|
/* assert */
|
|
|
|
EXPECT_EQ(a, 2);
|
|
|
|
/* deinit */
|
|
|
|
obj_deinit(self);
|
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
|
|
|
}
|
2022-08-04 00:25:32 +08:00
|
|
|
|
|
|
|
TEST(VM, del_) {
|
|
|
|
char* line =
|
|
|
|
"a = 1\n"
|
|
|
|
"print(a)\n"
|
|
|
|
"del a\n"
|
|
|
|
"print(a)\n";
|
|
|
|
PikaObj* self = newRootObj("root", New_PikaStdLib_SysObj);
|
|
|
|
obj_run(self, line);
|
|
|
|
/* collect */
|
|
|
|
/* assert */
|
|
|
|
EXPECT_EQ(obj_isArgExist(self, "a"), 0);
|
|
|
|
/* deinit */
|
|
|
|
obj_deinit(self);
|
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
|
|
|
}
|
2022-08-08 20:09:10 +08:00
|
|
|
|
|
|
|
TEST(VM, is) {
|
|
|
|
char* line =
|
|
|
|
"a = 1\n"
|
|
|
|
"b = a\n"
|
|
|
|
"res1 = a is b\n"
|
|
|
|
"res2 = 1 is 1\n"
|
|
|
|
"c = TinyObj()\n"
|
|
|
|
"d = c\n"
|
|
|
|
"res3 = c is d\n"
|
|
|
|
"res4 = c is None\n";
|
|
|
|
;
|
|
|
|
PikaObj* self = newRootObj("root", New_PikaStdLib_SysObj);
|
|
|
|
obj_run(self, line);
|
|
|
|
/* collect */
|
|
|
|
/* assert */
|
|
|
|
EXPECT_EQ(obj_getInt(self, "res1"), 1);
|
|
|
|
EXPECT_EQ(obj_getInt(self, "res2"), 1);
|
|
|
|
EXPECT_EQ(obj_getInt(self, "res3"), 1);
|
|
|
|
EXPECT_EQ(obj_getInt(self, "res4"), 0);
|
|
|
|
/* deinit */
|
|
|
|
obj_deinit(self);
|
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
|
|
|
}
|
2022-08-09 10:58:22 +08:00
|
|
|
|
|
|
|
TEST(VM, in) {
|
|
|
|
char* line =
|
|
|
|
"res1 = 'a' in 'aaa'\n"
|
|
|
|
"res2 = 'aaa' in 'a'\n";
|
|
|
|
PikaObj* self = newRootObj("root", New_PikaStdLib_SysObj);
|
|
|
|
obj_run(self, line);
|
|
|
|
/* collect */
|
|
|
|
/* assert */
|
|
|
|
EXPECT_EQ(obj_getInt(self, "res1"), 1);
|
|
|
|
EXPECT_EQ(obj_getInt(self, "res2"), 0);
|
|
|
|
/* deinit */
|
|
|
|
obj_deinit(self);
|
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
2022-08-09 18:27:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(VM, assert) {
|
|
|
|
char* line =
|
|
|
|
"assert True\n"
|
|
|
|
"assert 1 == 1, 'testparser'\n"
|
|
|
|
"assert 1 == 2, 'testparser'\n"
|
|
|
|
"res1 = 0\n"
|
|
|
|
"try:\n"
|
|
|
|
" assert False\n"
|
|
|
|
"except:\n"
|
|
|
|
" res1 = 1\n";
|
|
|
|
PikaObj* self = newRootObj("root", New_PikaStdLib_SysObj);
|
|
|
|
obj_run(self, line);
|
|
|
|
/* collect */
|
|
|
|
int res1 = obj_getInt(self, "res1");
|
|
|
|
/* assert */
|
|
|
|
EXPECT_EQ(res1, 1);
|
|
|
|
/* deinit */
|
|
|
|
obj_deinit(self);
|
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
|
|
|
}
|
2022-08-09 22:13:21 +08:00
|
|
|
|
|
|
|
TEST(VM, issue_I5LHJG) {
|
|
|
|
char* line =
|
|
|
|
"res = (a, 2, 3)\n"
|
|
|
|
"[a, 2, 3]\n"
|
|
|
|
"{'x': a, 'y': 2, 'z': 3}\n";
|
|
|
|
PikaObj* self = newRootObj("root", New_PikaStdLib_SysObj);
|
|
|
|
obj_run(self, line);
|
|
|
|
/* collect */
|
|
|
|
/* assert */
|
|
|
|
EXPECT_EQ(obj_isArgExist(self, "res"), 0);
|
|
|
|
/* deinit */
|
|
|
|
obj_deinit(self);
|
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
|
|
|
}
|
2022-08-16 12:21:11 +08:00
|
|
|
|
|
|
|
TEST(VM, vars_runtime) {
|
|
|
|
char* line =
|
|
|
|
"def testvars(a, *b):\n"
|
|
|
|
" sum = 0\n"
|
|
|
|
" for i in b:\n"
|
|
|
|
" sum += i\n"
|
|
|
|
" return a * sum\n"
|
|
|
|
"res = testvars(6, 2, 3, 4, 5)\n";
|
|
|
|
PikaObj* self = newRootObj("root", New_PikaStdLib_SysObj);
|
|
|
|
obj_run(self, line);
|
|
|
|
/* collect */
|
|
|
|
int res = obj_getInt(self, "res");
|
|
|
|
/* assert */
|
|
|
|
EXPECT_EQ(res, 84);
|
|
|
|
/* deinit */
|
|
|
|
obj_deinit(self);
|
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
|
|
|
}
|
2022-08-16 17:40:11 +08:00
|
|
|
|
|
|
|
#if PIKA_BUILTIN_STRUCT_ENABLE
|
|
|
|
TEST(VM, list_add) {
|
|
|
|
char* line = "[1, 2, 3] + [4, 5, 6]";
|
|
|
|
PikaObj* self = newRootObj("root", New_PikaStdLib_SysObj);
|
|
|
|
obj_run(self, line);
|
|
|
|
/* collect */
|
|
|
|
/* assert */
|
2022-08-17 10:29:54 +08:00
|
|
|
EXPECT_STREQ(log_buff[0], "[1, 2, 3, 4, 5, 6]\r\n");
|
2022-08-16 17:40:11 +08:00
|
|
|
/* deinit */
|
|
|
|
obj_deinit(self);
|
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
|
|
|
}
|
|
|
|
#endif
|
2022-08-18 22:44:03 +08:00
|
|
|
|
2022-08-29 18:34:20 +08:00
|
|
|
TEST(VM, science_num) {
|
|
|
|
char* line =
|
|
|
|
"a = 1.0e-3\n"
|
|
|
|
"b = 2e-5\n"
|
|
|
|
"c = -3e-5\n"
|
|
|
|
"d = 0.4e2\n";
|
2022-08-18 22:44:03 +08:00
|
|
|
PikaObj* self = newRootObj("root", New_PikaStdLib_SysObj);
|
|
|
|
obj_run(self, line);
|
|
|
|
/* collect */
|
|
|
|
double a = obj_getFloat(self, "a");
|
|
|
|
double b = obj_getFloat(self, "b");
|
|
|
|
double c = obj_getFloat(self, "c");
|
|
|
|
double d = obj_getFloat(self, "d");
|
|
|
|
/* assert */
|
|
|
|
EXPECT_DOUBLE_EQ(a, 1.0e-3);
|
|
|
|
EXPECT_DOUBLE_EQ(b, 2.0e-5);
|
|
|
|
EXPECT_DOUBLE_EQ(c, -3e-5);
|
|
|
|
EXPECT_DOUBLE_EQ(d, 0.4e2);
|
|
|
|
/* deinit */
|
|
|
|
obj_deinit(self);
|
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
|
|
|
}
|
|
|
|
|
2022-08-29 18:34:20 +08:00
|
|
|
TEST(VM, issue_I5OJQB) {
|
|
|
|
char* line = "s = '\\\\'";
|
|
|
|
PikaObj* self = newRootObj("root", New_PikaStdLib_SysObj);
|
|
|
|
obj_run(self, line);
|
|
|
|
/* collect */
|
|
|
|
char* s = obj_getStr(self, "s");
|
|
|
|
/* assert */
|
2022-08-30 12:05:28 +08:00
|
|
|
EXPECT_STREQ(s, "\\");
|
2022-08-29 18:34:20 +08:00
|
|
|
/* deinit */
|
|
|
|
obj_deinit(self);
|
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
|
|
|
}
|
2022-08-30 12:17:14 +08:00
|
|
|
|
2022-08-31 00:44:45 +08:00
|
|
|
#if !PIKA_NANO_ENABLE
|
2022-08-30 12:17:14 +08:00
|
|
|
TEST(vm, keyword_2) {
|
|
|
|
char* line =
|
|
|
|
"def test(a, b):\n"
|
2022-08-31 00:15:23 +08:00
|
|
|
" print(__kwargs['a'], __kwargs['b'])\n"
|
2022-08-30 12:17:14 +08:00
|
|
|
"test(a=1, b= 2)";
|
|
|
|
PikaObj* self = newRootObj("root", New_PikaStdLib_SysObj);
|
|
|
|
obj_run(self, line);
|
|
|
|
/* collect */
|
|
|
|
/* assert */
|
|
|
|
EXPECT_STREQ(log_buff[0], "1 2\r\n");
|
|
|
|
/* deinit */
|
|
|
|
obj_deinit(self);
|
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
|
|
|
}
|
2022-08-31 00:38:51 +08:00
|
|
|
|
|
|
|
TEST(vm, keyword_3) {
|
|
|
|
char* line =
|
|
|
|
"def test(**keys):\n"
|
|
|
|
" print(keys['a'], keys['b'])\n"
|
|
|
|
"test(a=1, b= 2)";
|
|
|
|
PikaObj* self = newRootObj("root", New_PikaStdLib_SysObj);
|
|
|
|
obj_run(self, line);
|
|
|
|
/* collect */
|
|
|
|
/* assert */
|
|
|
|
EXPECT_STREQ(log_buff[0], "1 2\r\n");
|
|
|
|
/* deinit */
|
|
|
|
obj_deinit(self);
|
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
|
|
|
}
|
2022-09-02 01:17:00 +08:00
|
|
|
|
|
|
|
TEST(vm, keyword_4) {
|
|
|
|
char* line =
|
|
|
|
"def test(a, b, **keys):\n"
|
|
|
|
" print(keys['a'], keys['b'], a, b)\n"
|
|
|
|
"test(3, 4, a=1, b= 2)";
|
|
|
|
PikaObj* self = newRootObj("root", New_PikaStdLib_SysObj);
|
|
|
|
obj_run(self, line);
|
|
|
|
/* collect */
|
|
|
|
/* assert */
|
|
|
|
EXPECT_STREQ(log_buff[0], "1 2 3 4\r\n");
|
|
|
|
/* deinit */
|
|
|
|
obj_deinit(self);
|
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(vm, vars_keyward) {
|
2022-09-02 09:22:39 +08:00
|
|
|
/* init */
|
|
|
|
pikaMemInfo.heapUsedMax = 0;
|
|
|
|
PikaObj* pikaMain = newRootObj("pikaMain", New_PikaMain);
|
|
|
|
extern unsigned char pikaModules_py_a[];
|
|
|
|
obj_linkLibrary(pikaMain, pikaModules_py_a);
|
|
|
|
/* run */
|
|
|
|
__platform_printf("BEGIN\r\n");
|
|
|
|
pikaVM_runSingleFile(pikaMain, "../../examples/BuiltIn/function.py");
|
2022-09-02 01:17:00 +08:00
|
|
|
/* collect */
|
|
|
|
/* assert */
|
2022-09-03 10:15:31 +08:00
|
|
|
EXPECT_STREQ(log_buff[1], "1 2 3 4 5 10\r\n");
|
|
|
|
EXPECT_STREQ(log_buff[0], "1 2 3 4 5 12\r\n");
|
2022-09-02 01:17:00 +08:00
|
|
|
/* deinit */
|
2022-09-02 09:22:39 +08:00
|
|
|
obj_deinit(pikaMain);
|
2022-09-02 01:17:00 +08:00
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
|
|
|
}
|
2022-09-02 11:25:32 +00:00
|
|
|
|
|
|
|
TEST(vm, cb_1) {
|
|
|
|
/* init */
|
|
|
|
pikaMemInfo.heapUsedMax = 0;
|
|
|
|
PikaObj* pikaMain = newRootObj("pikaMain", New_PikaMain);
|
|
|
|
extern unsigned char pikaModules_py_a[];
|
|
|
|
obj_linkLibrary(pikaMain, pikaModules_py_a);
|
|
|
|
/* run */
|
|
|
|
__platform_printf("BEGIN\r\n");
|
|
|
|
pikaVM_runSingleFile(pikaMain, "../../examples/Callback/test1.py");
|
|
|
|
/* collect */
|
|
|
|
/* assert */
|
|
|
|
EXPECT_STREQ(log_buff[3], "__init__\r\n");
|
|
|
|
EXPECT_STREQ(log_buff[1], "a\r\n");
|
|
|
|
EXPECT_STREQ(log_buff[0], "b\r\n");
|
|
|
|
/* deinit */
|
|
|
|
obj_deinit(pikaMain);
|
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(vm, cb_2) {
|
|
|
|
/* init */
|
|
|
|
pikaMemInfo.heapUsedMax = 0;
|
|
|
|
PikaObj* pikaMain = newRootObj("pikaMain", New_PikaMain);
|
|
|
|
extern unsigned char pikaModules_py_a[];
|
|
|
|
obj_linkLibrary(pikaMain, pikaModules_py_a);
|
|
|
|
/* run */
|
|
|
|
__platform_printf("BEGIN\r\n");
|
|
|
|
pikaVM_runSingleFile(pikaMain, "../../examples/Callback/test2.py");
|
|
|
|
/* collect */
|
|
|
|
/* assert */
|
2022-09-02 16:16:31 +00:00
|
|
|
EXPECT_STREQ(log_buff[4], "__init__\r\n");
|
|
|
|
EXPECT_STREQ(log_buff[2], "a\r\n");
|
|
|
|
EXPECT_STREQ(log_buff[1], "b\r\n");
|
|
|
|
EXPECT_STREQ(log_buff[0], "ppp\r\n");
|
2022-09-02 11:25:32 +00:00
|
|
|
/* deinit */
|
|
|
|
obj_deinit(pikaMain);
|
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
|
|
|
}
|
|
|
|
|
2022-09-02 20:21:58 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
TEST(vm, default_no_input) {
|
|
|
|
/* init */
|
|
|
|
pikaMemInfo.heapUsedMax = 0;
|
|
|
|
PikaObj* pikaMain = newRootObj("pikaMain", New_PikaMain);
|
|
|
|
extern unsigned char pikaModules_py_a[];
|
|
|
|
obj_linkLibrary(pikaMain, pikaModules_py_a);
|
|
|
|
/* run */
|
|
|
|
__platform_printf("BEGIN\r\n");
|
2022-09-07 14:25:25 +08:00
|
|
|
obj_run(pikaMain,
|
|
|
|
"def test(a = 1):\n"
|
|
|
|
" print(a)\n"
|
|
|
|
"test()");
|
2022-09-02 20:21:58 +08:00
|
|
|
/* collect */
|
|
|
|
/* assert */
|
|
|
|
EXPECT_STREQ(log_buff[0], "1\r\n");
|
|
|
|
/* deinit */
|
|
|
|
obj_deinit(pikaMain);
|
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
|
|
|
}
|
2022-09-02 16:16:31 +00:00
|
|
|
|
|
|
|
TEST(vm, default_1) {
|
|
|
|
/* init */
|
|
|
|
pikaMemInfo.heapUsedMax = 0;
|
|
|
|
PikaObj* pikaMain = newRootObj("pikaMain", New_PikaMain);
|
|
|
|
extern unsigned char pikaModules_py_a[];
|
|
|
|
obj_linkLibrary(pikaMain, pikaModules_py_a);
|
|
|
|
/* run */
|
|
|
|
__platform_printf("BEGIN\r\n");
|
2022-09-07 14:25:25 +08:00
|
|
|
obj_run(pikaMain,
|
|
|
|
"def test(a = 1):\n"
|
|
|
|
" print(a)\n"
|
|
|
|
"test(a = 2)");
|
2022-09-02 16:16:31 +00:00
|
|
|
/* collect */
|
|
|
|
/* assert */
|
|
|
|
EXPECT_STREQ(log_buff[0], "2\r\n");
|
|
|
|
/* deinit */
|
|
|
|
obj_deinit(pikaMain);
|
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(vm, default_2) {
|
|
|
|
/* init */
|
|
|
|
pikaMemInfo.heapUsedMax = 0;
|
|
|
|
PikaObj* pikaMain = newRootObj("pikaMain", New_PikaMain);
|
|
|
|
extern unsigned char pikaModules_py_a[];
|
|
|
|
obj_linkLibrary(pikaMain, pikaModules_py_a);
|
|
|
|
/* run */
|
|
|
|
__platform_printf("BEGIN\r\n");
|
2022-09-07 14:25:25 +08:00
|
|
|
obj_run(pikaMain,
|
|
|
|
"def test(a, b = 1):\n"
|
|
|
|
" print(a, b)\n"
|
|
|
|
"test(1, b = 2)");
|
2022-09-02 16:16:31 +00:00
|
|
|
/* collect */
|
|
|
|
/* assert */
|
|
|
|
EXPECT_STREQ(log_buff[0], "1 2\r\n");
|
|
|
|
/* deinit */
|
|
|
|
obj_deinit(pikaMain);
|
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(vm, default_3) {
|
|
|
|
/* init */
|
|
|
|
pikaMemInfo.heapUsedMax = 0;
|
|
|
|
PikaObj* pikaMain = newRootObj("pikaMain", New_PikaMain);
|
|
|
|
extern unsigned char pikaModules_py_a[];
|
|
|
|
obj_linkLibrary(pikaMain, pikaModules_py_a);
|
|
|
|
/* run */
|
|
|
|
__platform_printf("BEGIN\r\n");
|
2022-09-07 14:25:25 +08:00
|
|
|
obj_run(pikaMain,
|
|
|
|
"def myprint(end='\\n', *var):\n"
|
|
|
|
" for v in var:\n"
|
|
|
|
" print(v, end = end)\n"
|
|
|
|
"myprint(1, 2)\n"
|
|
|
|
"myprint(1, 2, end=';\\n')\n");
|
2022-09-02 16:16:31 +00:00
|
|
|
/* collect */
|
|
|
|
/* assert */
|
|
|
|
EXPECT_STREQ(log_buff[3], "1\n");
|
|
|
|
EXPECT_STREQ(log_buff[2], "2\n");
|
|
|
|
EXPECT_STREQ(log_buff[1], "1;\n");
|
|
|
|
EXPECT_STREQ(log_buff[0], "2;\n");
|
|
|
|
/* deinit */
|
|
|
|
obj_deinit(pikaMain);
|
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(vm, default_4) {
|
|
|
|
/* init */
|
|
|
|
pikaMemInfo.heapUsedMax = 0;
|
|
|
|
PikaObj* pikaMain = newRootObj("pikaMain", New_PikaMain);
|
|
|
|
extern unsigned char pikaModules_py_a[];
|
|
|
|
obj_linkLibrary(pikaMain, pikaModules_py_a);
|
|
|
|
/* run */
|
|
|
|
__platform_printf("BEGIN\r\n");
|
2022-09-07 14:25:25 +08:00
|
|
|
obj_run(pikaMain,
|
|
|
|
"def test(end=5, *var):\n"
|
|
|
|
" res = end\n"
|
|
|
|
" print('end', end)\n"
|
|
|
|
" for v in var:\n"
|
|
|
|
" res += v\n"
|
|
|
|
" return res\n"
|
|
|
|
"test(1, 2, 3)\n"
|
|
|
|
"test(1, 2, 3, end=6)\n");
|
2022-09-02 16:16:31 +00:00
|
|
|
/* collect */
|
|
|
|
/* assert */
|
|
|
|
EXPECT_STREQ(log_buff[3], "end 5\r\n");
|
|
|
|
EXPECT_STREQ(log_buff[2], "11\r\n");
|
|
|
|
EXPECT_STREQ(log_buff[1], "end 6\r\n");
|
|
|
|
EXPECT_STREQ(log_buff[0], "12\r\n");
|
|
|
|
/* deinit */
|
|
|
|
obj_deinit(pikaMain);
|
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
|
|
|
}
|
2022-09-07 14:25:25 +08:00
|
|
|
|
|
|
|
TEST(vm, none) {
|
|
|
|
/* init */
|
|
|
|
pikaMemInfo.heapUsedMax = 0;
|
|
|
|
PikaObj* pikaMain = newRootObj("pikaMain", New_PikaMain);
|
|
|
|
extern unsigned char pikaModules_py_a[];
|
|
|
|
obj_linkLibrary(pikaMain, pikaModules_py_a);
|
|
|
|
/* run */
|
|
|
|
__platform_printf("BEGIN\r\n");
|
|
|
|
obj_run(pikaMain, "GTestTask.test(1, None)");
|
|
|
|
/* collect */
|
|
|
|
/* assert */
|
|
|
|
EXPECT_STREQ(log_buff[0], "GTestTask_test: 1, 0\r\n");
|
|
|
|
/* deinit */
|
|
|
|
obj_deinit(pikaMain);
|
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
|
|
|
}
|
2022-09-08 18:48:58 +08:00
|
|
|
|
|
|
|
TEST(vm, super_) {
|
|
|
|
/* init */
|
|
|
|
pikaMemInfo.heapUsedMax = 0;
|
|
|
|
PikaObj* pikaMain = newRootObj("pikaMain", New_PikaMain);
|
|
|
|
extern unsigned char pikaModules_py_a[];
|
|
|
|
obj_linkLibrary(pikaMain, pikaModules_py_a);
|
|
|
|
/* run */
|
|
|
|
__platform_printf("BEGIN\r\n");
|
|
|
|
obj_run(pikaMain,
|
|
|
|
"class test:\n"
|
|
|
|
" def __init__(self):\n"
|
|
|
|
" print('in test init')\n"
|
|
|
|
"class test2(test):\n"
|
|
|
|
" def __init__(self):\n"
|
|
|
|
" super().__init__()\n"
|
|
|
|
" print('in test2 init')\n"
|
|
|
|
"t = test2()");
|
|
|
|
/* collect */
|
|
|
|
/* assert */
|
|
|
|
EXPECT_STREQ(log_buff[0], "in test2 init\r\n");
|
|
|
|
EXPECT_STREQ(log_buff[1], "in test init\r\n");
|
|
|
|
EXPECT_STREQ(log_buff[2], "BEGIN\r\n");
|
|
|
|
/* deinit */
|
|
|
|
obj_deinit(pikaMain);
|
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(vm, super_val) {
|
|
|
|
/* init */
|
|
|
|
pikaMemInfo.heapUsedMax = 0;
|
|
|
|
PikaObj* pikaMain = newRootObj("pikaMain", New_PikaMain);
|
|
|
|
extern unsigned char pikaModules_py_a[];
|
|
|
|
obj_linkLibrary(pikaMain, pikaModules_py_a);
|
|
|
|
/* run */
|
|
|
|
__platform_printf("BEGIN\r\n");
|
|
|
|
obj_run(pikaMain,
|
|
|
|
"class test:\n"
|
|
|
|
" def __init__(self):\n"
|
|
|
|
" self.a = 1\n"
|
|
|
|
" self.b = 2\n"
|
|
|
|
"class test2(test):\n"
|
|
|
|
" def __init__(self):\n"
|
|
|
|
" super().__init__()\n"
|
|
|
|
" self.c = 3\n"
|
|
|
|
" self.d = 4\n"
|
|
|
|
"t = test2()\n"
|
|
|
|
"print(t.a, t.b, t.c, t.d)\n");
|
|
|
|
/* collect */
|
|
|
|
/* assert */
|
|
|
|
EXPECT_STREQ(log_buff[1], "BEGIN\r\n");
|
|
|
|
EXPECT_STREQ(log_buff[0], "1 2 3 4\r\n");
|
|
|
|
/* deinit */
|
|
|
|
obj_deinit(pikaMain);
|
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
|
|
|
}
|