pikapython/port/linux/test/VM-test.cpp

662 lines
18 KiB
C
Raw Normal View History

2021-10-14 15:52:01 +08:00
#include "gtest/gtest.h"
extern "C" {
#include "BaseObj.h"
2021-10-26 20:50:13 +08:00
#include "PikaMain.h"
2021-10-14 23:39:24 +08:00
#include "PikaMath_Operator.h"
2021-10-14 15:52:01 +08:00
#include "PikaParser.h"
2021-10-14 15:58:32 +08:00
#include "PikaStdLib_SysObj.h"
2021-10-14 15:52:01 +08:00
#include "PikaVM.h"
#include "dataMemory.h"
#include "dataQueue.h"
2021-10-14 15:52:01 +08:00
#include "dataStrs.h"
2022-02-14 20:40:29 +08:00
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-02-14 20:49:37 +08:00
char* Parser_multiLineToAsm(Args* outBuffs, char* multiLine);
2022-02-22 00:41:45 +08:00
int32_t __clearInvokeQueues(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) {
char* line = (char*)"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);
PikaObj* self = newRootObj((char*)"root", New_PikaStdLib_SysObj);
2021-11-23 12:15:12 +08:00
pikaVM_runAsm(self, pikaAsm);
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) {
char* line = (char*)"a = 1";
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);
PikaObj* self = newRootObj((char*)"root", New_PikaStdLib_SysObj);
2021-11-23 12:15:12 +08:00
pikaVM_runAsm(self, pikaAsm);
ASSERT_EQ(args_getInt(self->list, (char*)"a"), 1);
2021-10-14 20:49:43 +08:00
obj_deinit(self);
args_deinit(buffs);
EXPECT_EQ(pikaMemNow(), 0);
}
TEST(VM, a_1d1) {
char* line = (char*)"a = 1.1";
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);
PikaObj* self = newRootObj((char*)"root", New_PikaStdLib_SysObj);
2022-02-22 00:41:45 +08:00
VMParameters* globals = pikaVM_runAsm(self, pikaAsm);
ASSERT_FLOAT_EQ(args_getFloat(globals->list, (char*)"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) {
char* line = (char*)"a = 'xy'";
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);
PikaObj* self = newRootObj((char*)"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
ASSERT_STREQ(args_getStr(globals->list, (char*)"a"), (char*)"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) {
char* line = (char*)"a = 'xy '";
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);
PikaObj* self = newRootObj((char*)"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
ASSERT_STREQ(args_getStr(globals->list, (char*)"a"), (char*)"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) {
PikaObj* self = newRootObj((char*)"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-02-22 00:41:45 +08:00
VMParameters* globals =
2021-11-28 01:21:24 +08:00
pikaVM_runAsm(self, Parser_LineToAsm(buffs, (char*)"a = 'xy '", NULL));
globals = pikaVM_runAsmWithPars(
self, globals, globals, Parser_LineToAsm(buffs, (char*)"b = a", NULL));
2021-10-14 22:54:46 +08:00
2021-10-14 23:03:31 +08:00
args_deinit(buffs);
ASSERT_STREQ(args_getStr(globals->list, (char*)"b"), (char*)"xy ");
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) {
PikaObj* self = newRootObj((char*)"root", New_PikaMath_Operator);
2021-10-14 23:22:48 +08:00
Args* buffs = New_strBuff();
2022-02-22 00:41:45 +08:00
VMParameters* globals = pikaVM_runAsm(
2021-11-27 20:21:04 +08:00
self, Parser_LineToAsm(buffs, (char*)"a = plusInt(1,2)", NULL));
2021-10-14 23:39:24 +08:00
args_deinit(buffs);
int a = args_getInt(globals->list, (char*)"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) {
PikaObj* self = newRootObj((char*)"root", New_PikaMath_Operator);
2021-10-14 23:39:24 +08:00
Args* buffs = New_strBuff();
2022-02-22 00:41:45 +08:00
VMParameters* globals =
2021-11-27 20:21:04 +08:00
pikaVM_runAsm(self, Parser_LineToAsm(buffs, (char*)"b = 2", NULL));
2021-10-31 11:05:09 +08:00
globals = pikaVM_runAsmWithPars(
2021-10-31 11:01:14 +08:00
self, globals, globals,
2021-11-27 20:21:04 +08:00
Parser_LineToAsm(buffs, (char*)"a = plusInt(1,b)", NULL));
2021-10-14 23:39:24 +08:00
args_deinit(buffs);
int a = args_getInt(globals->list, (char*)"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_1_2_3) {
PikaObj* self = newRootObj((char*)"root", New_PikaMath_Operator);
2021-10-14 23:39:24 +08:00
Args* buffs = New_strBuff();
2022-02-22 00:41:45 +08:00
VMParameters* globals = pikaVM_runAsm(
2021-11-28 01:21:24 +08:00
self,
Parser_LineToAsm(buffs, (char*)"a = plusInt(1, plusInt(2,3) )", NULL));
2021-10-14 23:22:48 +08:00
args_deinit(buffs);
int a = args_getInt(globals->list, (char*)"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:18:20 +08:00
TEST(VM, JEZ) {
2021-10-17 20:34:40 +08:00
char* pikaAsm = (char*)
"B0\n"
2021-10-17 22:18:20 +08:00
"0 REF False\n"
"0 JEZ 2\n"
"B0\n"
2021-10-17 20:58:27 +08:00
"B0\n";
PikaObj* self = New_TinyObj(NULL);
2021-10-17 20:34:40 +08:00
int lineAddr = 0;
Args* sysRes = New_args(NULL);
args_setErrorCode(sysRes, 0);
args_setSysOut(sysRes, (char*)"");
2022-02-22 00:41:45 +08:00
VMParameters* globals = self;
2021-10-31 11:08:40 +08:00
lineAddr = pikaVM_runAsmLine(self, globals, globals, pikaAsm, lineAddr);
lineAddr = pikaVM_runAsmLine(self, globals, globals, pikaAsm, lineAddr);
lineAddr = pikaVM_runAsmLine(self, globals, globals, pikaAsm, lineAddr);
2021-10-30 22:20:26 +08:00
__clearInvokeQueues(globals);
2021-10-17 20:34:40 +08:00
obj_deinit(self);
args_deinit(sysRes);
2021-11-23 12:15:12 +08:00
// obj_deinit(globals);
2021-10-17 22:18:20 +08:00
EXPECT_EQ(lineAddr, 26);
2021-10-17 20:34:40 +08:00
EXPECT_EQ(pikaMemNow(), 0);
2021-10-17 20:58:27 +08:00
}
2021-10-17 22:18:20 +08:00
TEST(VM, JMP) {
2021-10-17 20:58:27 +08:00
char* pikaAsm = (char*)
"B0\n"
2021-10-17 22:18:20 +08:00
"0 JMP 2\n"
"B0\n"
2021-10-17 20:58:27 +08:00
"B0\n";
PikaObj* self = New_TinyObj(NULL);
2021-10-17 20:58:27 +08:00
int lineAddr = 0;
Args* sysRes = New_args(NULL);
args_setErrorCode(sysRes, 0);
args_setSysOut(sysRes, (char*)"");
2022-02-22 00:41:45 +08:00
VMParameters* globals = self;
2021-10-31 11:08:40 +08:00
lineAddr = pikaVM_runAsmLine(self, globals, globals, pikaAsm, lineAddr);
lineAddr = pikaVM_runAsmLine(self, globals, globals, pikaAsm, lineAddr);
2021-10-30 22:20:26 +08:00
__clearInvokeQueues(globals);
2021-10-17 20:58:27 +08:00
obj_deinit(self);
args_deinit(sysRes);
2021-11-23 12:15:12 +08:00
// obj_deinit(globals);
2021-10-17 22:18:20 +08:00
EXPECT_EQ(lineAddr, 14);
2021-10-17 20:58:27 +08:00
EXPECT_EQ(pikaMemNow(), 0);
2021-10-17 22:24:20 +08:00
}
TEST(VM, JMP_back1) {
char* pikaAsm = (char*)
"B0\n"
"B0\n"
"0 JMP -1\n"
"B0\n"
"B0\n";
PikaObj* self = New_TinyObj(NULL);
2021-10-17 22:24:20 +08:00
int lineAddr = 0;
Args* sysRes = New_args(NULL);
args_setErrorCode(sysRes, 0);
args_setSysOut(sysRes, (char*)"");
2022-02-22 00:41:45 +08:00
VMParameters* globals = self;
2021-10-31 11:08:40 +08:00
lineAddr = pikaVM_runAsmLine(self, globals, globals, pikaAsm, lineAddr);
lineAddr = pikaVM_runAsmLine(self, globals, globals, pikaAsm, lineAddr);
lineAddr = pikaVM_runAsmLine(self, globals, globals, pikaAsm, lineAddr);
2021-10-30 22:20:26 +08:00
__clearInvokeQueues(globals);
2021-10-17 22:24:20 +08:00
obj_deinit(self);
args_deinit(sysRes);
2021-11-23 12:15:12 +08:00
// obj_deinit(globals);
2021-10-17 22:24:20 +08:00
EXPECT_EQ(lineAddr, 0);
EXPECT_EQ(pikaMemNow(), 0);
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);
2021-11-27 20:21:04 +08:00
char* pikaAsm = Parser_multiLineToAsm(buffs, (char*)lines);
2021-10-17 22:38:27 +08:00
printf("%s", pikaAsm);
pikaMemInfo.heapUsedMax = 0;
PikaObj* self = New_TinyObj(NULL);
2022-02-22 00:41:45 +08:00
VMParameters* globals = pikaVM_runAsm(self, pikaAsm);
EXPECT_EQ(args_getInt(globals->list, (char*)"a"), 0);
EXPECT_EQ(args_getInt(globals->list, (char*)"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) {
char* line = (char*)"a = 1 + 1";
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);
PikaObj* self = newRootObj((char*)"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
int res = args_getInt(globals->list, (char*)"a");
2021-10-21 18:02:11 +08:00
obj_deinit(self);
2021-11-23 12:15:12 +08:00
// obj_deinit(globals);
2021-10-21 18:02:11 +08:00
args_deinit(buffs);
ASSERT_EQ(res, 2);
EXPECT_EQ(pikaMemNow(), 0);
2021-10-21 18:07:29 +08:00
}
TEST(VM, a_1_1d1) {
char* line = (char*)"a = 1 + 1.1";
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);
PikaObj* self = newRootObj((char*)"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
float res = args_getFloat(globals->list, (char*)"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) {
char* line = (char*)"a = (1 + 1.1) * 3 - 2 /4.0";
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);
PikaObj* self = newRootObj((char*)"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
float res = args_getFloat(globals->list, (char*)"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) {
char* line = (char*)
"a = 0\n"
"while a < 10:\n"
" a = a + 1\n"
" print(a)\n"
"\n";
Args* buffs = New_strBuff();
2021-11-27 20:21:04 +08:00
char* pikaAsm = Parser_multiLineToAsm(buffs, line);
2021-10-21 18:38:28 +08:00
printf("%s", pikaAsm);
PikaObj* self = newRootObj((char*)"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
int res = args_getInt(globals->list, (char*)"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) {
2021-10-26 20:50:13 +08:00
char* line = (char*)
"mem = PikaStdLib.MemChecker()\n"
"mem.x = 1\n"
"print(mem.x)\n"
"mem.x = 2\n"
"print(mem.x)\n"
"\n";
Args* buffs = New_strBuff();
2021-11-27 20:21:04 +08:00
char* pikaAsm = Parser_multiLineToAsm(buffs, line);
2021-10-26 20:50:13 +08:00
printf("%s", pikaAsm);
PikaObj* self = newRootObj((char*)"", New_PikaMain);
2022-02-22 00:41:45 +08:00
VMParameters* globals = pikaVM_runAsm(self, pikaAsm);
2021-10-26 20:50:13 +08:00
2021-10-30 22:20:26 +08:00
int res = obj_getInt(globals, (char*)"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
}
TEST(VM, DEF_instruct) {
char* pikaAsm = (char*)
"B0\n"
"0 DEF test()\n"
"0 JMP 1\n"
"B1\n"
"0 NUM 1\n"
"0 RET\n"
"B0\n";
2021-10-29 22:13:46 +08:00
char* methodCode = (char*)
"B1\n"
"0 NUM 1\n"
"0 RET\n"
"B0\n";
2021-10-29 21:45:24 +08:00
PikaObj* self = New_TinyObj(NULL);
2021-11-23 12:15:12 +08:00
pikaVM_runAsm(self, pikaAsm);
2021-10-29 22:13:46 +08:00
char* methodPtr = (char*)obj_getPtr(self, (char*)"test");
EXPECT_STREQ(methodCode, methodPtr);
2021-10-29 21:45:24 +08:00
obj_deinit(self);
2021-11-23 12:15:12 +08:00
// obj_deinit(globals);
2021-10-29 21:45:24 +08:00
EXPECT_EQ(pikaMemNow(), 0);
2021-10-30 11:08:56 +08:00
}
TEST(VM, RET_instruct) {
char* pikaAsm = (char*)
"B1\n"
"0 NUM 13\n"
"0 RET\n"
"0 NUM 2\n"
"0 RET\n"
"B0\n";
PikaObj* self = New_TinyObj(NULL);
2022-02-22 00:41:45 +08:00
VMParameters* globals = pikaVM_runAsm(self, pikaAsm);
Arg* returnArg = args_getArg(globals->list, (char*)"return");
2021-10-30 11:08:56 +08:00
int num = arg_getInt(returnArg);
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) {
char* pikaAsm = (char*)
"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";
PikaObj* self = New_TinyObj(NULL);
2022-02-22 00:41:45 +08:00
VMParameters* globals = pikaVM_runAsm(self, pikaAsm);
2021-10-31 16:25:18 +08:00
int num = obj_getInt(globals, (char*)"a");
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) {
char* pikaAsm = (char*)
"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";
PikaObj* self = New_TinyObj(NULL);
2021-11-23 12:15:12 +08:00
pikaVM_runAsm(self, pikaAsm);
int a = obj_getInt(self, (char*)"a");
int b = obj_getInt(self, (char*)"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) {
char* pikaAsm = (char*)
"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";
PikaObj* self = newRootObj((char*)"", New_BaseObj);
2022-02-22 00:41:45 +08:00
VMParameters* globals = pikaVM_runAsm(self, pikaAsm);
2021-10-31 16:25:18 +08:00
int a = obj_getInt(globals, (char*)"a");
int b = obj_getInt(globals, (char*)"b");
int c = obj_getInt(globals, (char*)"c");
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) {
char* pikaAsm = (char*)
"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";
PikaObj* self = newRootObj((char*)"", New_BaseObj);
2022-02-22 00:41:45 +08:00
VMParameters* globals = pikaVM_runAsm(self, pikaAsm);
2021-10-31 16:25:18 +08:00
int a = obj_getInt(globals, (char*)"a");
int b = obj_getInt(globals, (char*)"b");
int c = obj_getInt(globals, (char*)"c");
/* 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) {
char* line = (char*)
"def add(a, b):\n"
" return a + b\n"
"c = add(1,3)\n"
"\n";
Args* buffs = New_strBuff();
2021-11-27 20:21:04 +08:00
char* pikaAsm = Parser_multiLineToAsm(buffs, line);
2021-10-31 21:05:42 +08:00
printf("%s", pikaAsm);
PikaObj* self = newRootObj((char*)"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
int c = args_getInt(globals->list, (char*)"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) {
char* line = (char*)
"if -1 == -1:\n"
" a = 1\n"
"\n"
;
Args* buffs = New_strBuff();
char* pikaAsm = Parser_multiLineToAsm(buffs, line);
printf("%s", pikaAsm);
PikaObj* self = newRootObj((char*)"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
int c = args_getInt(globals->list, (char*)"a");
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) {
char* line = (char*)
"a = 2\n"
"b = 0\n"
"if a > 1:\n"
" b = 1\n"
"elif a > 0:\n"
" b = 2\n"
"\n"
;
Args* buffs = New_strBuff();
char* pikaAsm = Parser_multiLineToAsm(buffs, line);
printf("%s", pikaAsm);
PikaObj* self = newRootObj((char*)"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
int b = args_getInt(globals->list, (char*)"b");
obj_deinit(self);
args_deinit(buffs);
// obj_deinit(globals);
ASSERT_FLOAT_EQ(b, 1);
EXPECT_EQ(pikaMemNow(), 0);
}
TEST(VM, if_else) {
char* line = (char*)
"a = 0\n"
"b = 0\n"
"if a > 1:\n"
" b = 1\n"
"else:\n"
" b = 2\n"
"\n"
;
Args* buffs = New_strBuff();
char* pikaAsm = Parser_multiLineToAsm(buffs, line);
printf("%s", pikaAsm);
PikaObj* self = newRootObj((char*)"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
int b = args_getInt(globals->list, (char*)"b");
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_) {
char* line = (char*)
"a = 2\n"
"b = 0\n"
"if a > 1:\n"
" b = 1\n"
"else:\n"
" b = 2\n"
"\n"
;
Args* buffs = New_strBuff();
char* pikaAsm = Parser_multiLineToAsm(buffs, line);
printf("%s", pikaAsm);
PikaObj* self = newRootObj((char*)"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
int b = args_getInt(globals->list, (char*)"b");
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) {
char* pikaAsm = (char*)
"B0\n"
"0 EST a\n"
"0 OUT b\n"
;
PikaObj* self = newRootObj((char*)"", New_BaseObj);
pikaVM_runAsm(self, pikaAsm);
int a = obj_getInt(self, (char*)"a");
int b = obj_getInt(self, (char*)"b");
/* 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) {
char* pikaAsm = (char*)
"B0\n"
"0 RUN PikaStdLib.PikaObj\n"
"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"
"0 OUT x\n"
;
PikaObj* self = newRootObj((char*)"", New_PikaMain);
pikaVM_runAsm(self, pikaAsm);
PikaObj* as = obj_getObj(self, (char*)"as", 0);
int x_as_ = obj_getInt(as, (char*)"x");
int x_as = obj_getInt(self, (char*)"as.x");
int x_origin = obj_getInt(self, (char*)"x");
/* a is local, should not be exist in globals */
EXPECT_EQ(x_as_, 1);
EXPECT_EQ(x_as, 1);
/* b is local, should not be exist in globals */
EXPECT_EQ(x_origin, 2);
obj_deinit(self);
// obj_deinit(globals);
EXPECT_EQ(pikaMemNow(), 0);
}