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

210 lines
5.8 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-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"
}
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-10-16 19:09:19 +08:00
char* pikaAsm = pikaParseLineToAsm(buffs, line, NULL);
2021-10-14 15:52:01 +08:00
printf("%s", pikaAsm);
PikaObj* self = newRootObj((char*)"root", New_PikaStdLib_SysObj);
2021-10-15 11:00:13 +08:00
args_deinit(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-10-16 19:09:19 +08:00
char* pikaAsm = pikaParseLineToAsm(buffs, line, NULL);
2021-10-14 20:46:32 +08:00
printf("%s", pikaAsm);
PikaObj* self = newRootObj((char*)"root", New_PikaStdLib_SysObj);
2021-10-15 11:00:13 +08:00
args_deinit(pikaVM_runAsm(self, pikaAsm));
2021-10-14 20:49:43 +08:00
ASSERT_EQ(obj_getInt(self, (char*)"a"), 1);
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-10-16 19:09:19 +08:00
char* pikaAsm = pikaParseLineToAsm(buffs, line, NULL);
2021-10-14 20:49:43 +08:00
printf("%s", pikaAsm);
PikaObj* self = newRootObj((char*)"root", New_PikaStdLib_SysObj);
2021-10-15 11:00:13 +08:00
args_deinit(pikaVM_runAsm(self, pikaAsm));
2021-10-14 20:49:43 +08:00
ASSERT_FLOAT_EQ(obj_getFloat(self, (char*)"a"), 1.1);
2021-10-14 21:02:19 +08:00
obj_deinit(self);
args_deinit(buffs);
EXPECT_EQ(pikaMemNow(), 0);
}
TEST(VM, str_xy) {
char* line = (char*)"a = 'xy'";
Args* buffs = New_strBuff();
2021-10-16 19:09:19 +08:00
char* pikaAsm = pikaParseLineToAsm(buffs, line, NULL);
2021-10-14 21:02:19 +08:00
printf("%s", pikaAsm);
PikaObj* self = newRootObj((char*)"root", New_PikaStdLib_SysObj);
2021-10-15 11:00:13 +08:00
args_deinit(pikaVM_runAsm(self, pikaAsm));
2021-10-14 21:02:19 +08:00
ASSERT_STREQ(obj_getStr(self, (char*)"a"), (char*)"xy");
obj_deinit(self);
args_deinit(buffs);
EXPECT_EQ(pikaMemNow(), 0);
}
TEST(VM, str_xy_space) {
char* line = (char*)"a = 'xy '";
Args* buffs = New_strBuff();
2021-10-16 19:09:19 +08:00
char* pikaAsm = pikaParseLineToAsm(buffs, line, NULL);
2021-10-14 21:02:19 +08:00
printf("%s", pikaAsm);
PikaObj* self = newRootObj((char*)"root", New_PikaStdLib_SysObj);
2021-10-15 11:00:13 +08:00
args_deinit(pikaVM_runAsm(self, pikaAsm));
2021-10-14 21:02:19 +08:00
ASSERT_STREQ(obj_getStr(self, (char*)"a"), (char*)"xy ");
2021-10-14 22:54:46 +08:00
obj_deinit(self);
args_deinit(buffs);
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
2021-10-16 19:09:19 +08:00
args_deinit(pikaVM_runAsm(
self, pikaParseLineToAsm(buffs, (char*)"a = 'xy '", NULL)));
args_deinit(
pikaVM_runAsm(self, pikaParseLineToAsm(buffs, (char*)"b = a", NULL)));
2021-10-14 22:54:46 +08:00
2021-10-14 23:03:31 +08:00
args_deinit(buffs);
2021-10-14 22:54:46 +08:00
ASSERT_STREQ(obj_getStr(self, (char*)"b"), (char*)"xy ");
2021-10-14 20:46:32 +08:00
obj_deinit(self);
EXPECT_EQ(pikaMemNow(), 0);
2021-10-14 23:22:48 +08:00
}
TEST(VM, Run_add) {
PikaObj* self = newRootObj((char*)"root", New_PikaMath_Operator);
Args* buffs = New_strBuff();
2021-10-16 19:09:19 +08:00
args_deinit(pikaVM_runAsm(
self, pikaParseLineToAsm(buffs, (char*)"a = plusInt(1,2)", NULL)));
2021-10-14 23:39:24 +08:00
args_deinit(buffs);
int a = obj_getInt(self, (char*)"a");
ASSERT_EQ(a, 3);
obj_deinit(self);
EXPECT_EQ(pikaMemNow(), 0);
}
TEST(VM, Run_add_multy) {
PikaObj* self = newRootObj((char*)"root", New_PikaMath_Operator);
Args* buffs = New_strBuff();
2021-10-15 11:00:13 +08:00
args_deinit(
2021-10-16 19:09:19 +08:00
pikaVM_runAsm(self, pikaParseLineToAsm(buffs, (char*)"b = 2", NULL)));
args_deinit(pikaVM_runAsm(
self, pikaParseLineToAsm(buffs, (char*)"a = plusInt(1,b)", NULL)));
2021-10-14 23:39:24 +08:00
args_deinit(buffs);
int a = obj_getInt(self, (char*)"a");
ASSERT_EQ(a, 3);
obj_deinit(self);
EXPECT_EQ(pikaMemNow(), 0);
}
TEST(VM, Run_add_1_2_3) {
PikaObj* self = newRootObj((char*)"root", New_PikaMath_Operator);
Args* buffs = New_strBuff();
2021-10-15 11:00:13 +08:00
args_deinit(pikaVM_runAsm(
2021-10-16 19:09:19 +08:00
self, pikaParseLineToAsm(buffs, (char*)"a = plusInt(1, plusInt(2,3) )",
NULL)));
2021-10-14 23:22:48 +08:00
args_deinit(buffs);
2021-10-14 23:39:24 +08:00
int a = obj_getInt(self, (char*)"a");
ASSERT_EQ(a, 6);
2021-10-14 23:22:48 +08:00
obj_deinit(self);
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";
2021-10-17 20:34:40 +08:00
PikaObj* self = New_TinyObj(NULL);
int lineAddr = 0;
int size = strGetSize(pikaAsm);
Args* sysRes = New_args(NULL);
args_setErrorCode(sysRes, 0);
args_setSysOut(sysRes, (char*)"");
lineAddr = pikaVM_runAsmLine(self, pikaAsm, lineAddr, sysRes);
lineAddr = pikaVM_runAsmLine(self, pikaAsm, lineAddr, sysRes);
2021-10-17 22:18:20 +08:00
lineAddr = pikaVM_runAsmLine(self, pikaAsm, lineAddr, sysRes);
2021-10-17 20:34:40 +08:00
__clearInvokeQueues(self);
obj_deinit(self);
args_deinit(sysRes);
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);
int lineAddr = 0;
int size = strGetSize(pikaAsm);
Args* sysRes = New_args(NULL);
args_setErrorCode(sysRes, 0);
args_setSysOut(sysRes, (char*)"");
lineAddr = pikaVM_runAsmLine(self, pikaAsm, lineAddr, sysRes);
lineAddr = pikaVM_runAsmLine(self, pikaAsm, lineAddr, sysRes);
__clearInvokeQueues(self);
obj_deinit(self);
args_deinit(sysRes);
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);
int lineAddr = 0;
int size = strGetSize(pikaAsm);
Args* sysRes = New_args(NULL);
args_setErrorCode(sysRes, 0);
args_setSysOut(sysRes, (char*)"");
lineAddr = pikaVM_runAsmLine(self, pikaAsm, lineAddr, sysRes);
lineAddr = pikaVM_runAsmLine(self, pikaAsm, lineAddr, sysRes);
lineAddr = pikaVM_runAsmLine(self, pikaAsm, lineAddr, sysRes);
__clearInvokeQueues(self);
obj_deinit(self);
args_deinit(sysRes);
EXPECT_EQ(lineAddr, 0);
EXPECT_EQ(pikaMemNow(), 0);
2021-10-14 20:46:32 +08:00
}