2021-11-03 21:28:37 +08:00
|
|
|
#include "gtest/gtest.h"
|
|
|
|
extern "C" {
|
|
|
|
#include "PikaMain.h"
|
|
|
|
#include "PikaStdLib_MemChecker.h"
|
|
|
|
#include "dataArgs.h"
|
|
|
|
#include "dataMemory.h"
|
2022-01-03 21:12:05 +08:00
|
|
|
#include "mem_pool_config.h"
|
2022-01-04 11:24:34 +08:00
|
|
|
#include "pikaScript.h"
|
2021-11-03 21:28:37 +08:00
|
|
|
}
|
|
|
|
extern PikaMemInfo pikaMemInfo;
|
|
|
|
TEST(pikaMain, init) {
|
|
|
|
pikaMemInfo.heapUsedMax = 0;
|
|
|
|
PikaObj* pikaMain = pikaScriptInit();
|
|
|
|
obj_deinit(pikaMain);
|
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(pikaMain, new_mem) {
|
|
|
|
pikaMemInfo.heapUsedMax = 0;
|
|
|
|
PikaObj* pikaMain = newRootObj((char*)"pikaMain", New_PikaMain);
|
2021-12-30 19:55:09 +08:00
|
|
|
VM_Parameters* globals =
|
2021-11-03 21:28:37 +08:00
|
|
|
obj_runDirect(pikaMain, (char*)
|
|
|
|
"mem = PikaStdLib.MemChecker()\n"
|
|
|
|
"mem.max()\n"
|
|
|
|
);
|
2021-11-14 11:42:13 +08:00
|
|
|
PikaObj* mem = (PikaObj*)args_getPtr(globals->list, (char*)"mem");
|
2021-11-03 21:28:37 +08:00
|
|
|
PikaObj* memClassPtr = (PikaObj*)obj_getPtr(mem, (char*)"_clsptr");
|
|
|
|
EXPECT_EQ((void*)memClassPtr, (void*)New_PikaStdLib_MemChecker);
|
|
|
|
obj_deinit(pikaMain);
|
2021-11-23 12:15:12 +08:00
|
|
|
// obj_deinit(globals);
|
2021-11-03 21:28:37 +08:00
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
2021-11-17 10:59:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(pikaMain, list_new) {
|
|
|
|
/* init */
|
2021-11-17 11:03:56 +08:00
|
|
|
pikaMemInfo.heapUsedMax = 0;
|
2021-11-17 10:59:13 +08:00
|
|
|
PikaObj* pikaMain = newRootObj((char*)"pikaMain", New_PikaMain);
|
|
|
|
/* run */
|
2021-12-30 19:55:09 +08:00
|
|
|
VM_Parameters* globals =
|
2021-11-17 10:59:13 +08:00
|
|
|
obj_runDirect(pikaMain, (char*)
|
2021-12-13 21:24:48 +08:00
|
|
|
"list = PikaStdData.List()\n"
|
2021-11-17 10:59:13 +08:00
|
|
|
"list.append(7)\n"
|
|
|
|
"list.append('eee')\n"
|
|
|
|
"len = list.len()\n"
|
|
|
|
"a1 = list.get(0)\n"
|
|
|
|
"a2 = list.get(1)\n"
|
|
|
|
);
|
|
|
|
/* collect */
|
|
|
|
int len = obj_getInt(globals, (char*)"len");
|
|
|
|
int a1 = obj_getInt(globals, (char*)"a1");
|
|
|
|
char* a2 = obj_getStr(globals, (char*)"a2");
|
|
|
|
|
|
|
|
/* assert */
|
|
|
|
EXPECT_EQ(len, 2);
|
|
|
|
EXPECT_EQ(a1, 7);
|
|
|
|
EXPECT_STREQ(a2, "eee");
|
|
|
|
|
2021-11-20 09:28:21 +08:00
|
|
|
/* deinit */
|
2021-11-23 12:15:12 +08:00
|
|
|
// obj_deinit(globals);
|
2021-11-20 09:28:21 +08:00
|
|
|
obj_deinit(pikaMain);
|
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(pikaMain, int_float_convert) {
|
|
|
|
/* init */
|
|
|
|
pikaMemInfo.heapUsedMax = 0;
|
|
|
|
PikaObj* pikaMain = newRootObj((char*)"pikaMain", New_PikaMain);
|
|
|
|
/* run */
|
2021-12-30 19:55:09 +08:00
|
|
|
VM_Parameters* globals =
|
2021-11-20 09:28:21 +08:00
|
|
|
obj_runDirect(pikaMain, (char*)
|
|
|
|
"a = 1\n"
|
|
|
|
"a_f = float(a)\n"
|
|
|
|
"b = 1.3\n"
|
|
|
|
"b_i = int(b)\n"
|
|
|
|
);
|
|
|
|
/* collect */
|
|
|
|
int a = obj_getInt(globals, (char*)"a");
|
|
|
|
float a_f = obj_getFloat(globals, (char*)"a_f");
|
|
|
|
float b = obj_getFloat(globals, (char*)"b");
|
|
|
|
int b_i = obj_getInt(globals, (char*)"b_i");
|
|
|
|
|
|
|
|
/* assert */
|
|
|
|
EXPECT_EQ(a, 1);
|
|
|
|
EXPECT_FLOAT_EQ(a_f, 1);
|
|
|
|
EXPECT_FLOAT_EQ(b, 1.3);
|
|
|
|
EXPECT_FLOAT_EQ(b_i, 1);
|
|
|
|
|
2021-11-20 09:33:38 +08:00
|
|
|
/* deinit */
|
2021-11-23 12:15:12 +08:00
|
|
|
// obj_deinit(globals);
|
2021-11-20 09:33:38 +08:00
|
|
|
obj_deinit(pikaMain);
|
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(pikaMain, type_) {
|
|
|
|
/* init */
|
|
|
|
pikaMemInfo.heapUsedMax = 0;
|
|
|
|
PikaObj* pikaMain = newRootObj((char*)"pikaMain", New_PikaMain);
|
|
|
|
/* run */
|
2021-11-24 11:07:25 +08:00
|
|
|
obj_runDirect(pikaMain, (char*)
|
2021-11-20 09:33:38 +08:00
|
|
|
"type(1)\n"
|
|
|
|
"b = 1.4\n"
|
|
|
|
"type(b)\n"
|
|
|
|
);
|
|
|
|
/* collect */
|
|
|
|
|
|
|
|
/* assert */
|
|
|
|
|
2021-11-24 11:07:25 +08:00
|
|
|
/* deinit */
|
|
|
|
// obj_deinit(globals);
|
|
|
|
obj_deinit(pikaMain);
|
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(pikaMain, a_signed) {
|
|
|
|
/* init */
|
|
|
|
pikaMemInfo.heapUsedMax = 0;
|
|
|
|
PikaObj* pikaMain = newRootObj((char*)"pikaMain", New_PikaMain);
|
|
|
|
/* run */
|
|
|
|
obj_runDirect(pikaMain, (char*)"a = -1\n");
|
|
|
|
/* collect */
|
|
|
|
int a = obj_getInt(pikaMain, (char*)"a");
|
|
|
|
|
|
|
|
/* assert */
|
|
|
|
EXPECT_EQ(-1, a);
|
|
|
|
|
2021-11-28 01:29:47 +08:00
|
|
|
/* deinit */
|
|
|
|
obj_deinit(pikaMain);
|
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(pikaMain, a_mm) {
|
|
|
|
/* init */
|
|
|
|
pikaMemInfo.heapUsedMax = 0;
|
|
|
|
PikaObj* pikaMain = newRootObj((char*)"pikaMain", New_PikaMain);
|
|
|
|
/* run */
|
|
|
|
obj_runDirect(pikaMain, (char*)"a = -1.1 ** 3\n");
|
|
|
|
/* collect */
|
|
|
|
float a = obj_getFloat(pikaMain, (char*)"a");
|
|
|
|
|
|
|
|
/* assert */
|
|
|
|
EXPECT_FLOAT_EQ(-1.331, a);
|
|
|
|
|
2021-11-28 01:42:14 +08:00
|
|
|
/* deinit */
|
|
|
|
obj_deinit(pikaMain);
|
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(pikaMain, a_n1) {
|
|
|
|
/* init */
|
|
|
|
pikaMemInfo.heapUsedMax = 0;
|
|
|
|
PikaObj* pikaMain = newRootObj((char*)"pikaMain", New_PikaMain);
|
|
|
|
/* run */
|
|
|
|
obj_runDirect(pikaMain, (char*)"a = ~1\n");
|
|
|
|
/* collect */
|
|
|
|
int a = obj_getInt(pikaMain, (char*)"a");
|
|
|
|
|
|
|
|
/* assert */
|
|
|
|
EXPECT_EQ(-2, a);
|
|
|
|
|
2021-11-28 01:44:17 +08:00
|
|
|
/* deinit */
|
|
|
|
obj_deinit(pikaMain);
|
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(pikaMain, a_l4) {
|
|
|
|
/* init */
|
|
|
|
pikaMemInfo.heapUsedMax = 0;
|
|
|
|
PikaObj* pikaMain = newRootObj((char*)"pikaMain", New_PikaMain);
|
|
|
|
/* run */
|
|
|
|
obj_runDirect(pikaMain, (char*)"a = 1<<4\n");
|
|
|
|
/* collect */
|
|
|
|
int a = obj_getInt(pikaMain, (char*)"a");
|
|
|
|
|
|
|
|
/* assert */
|
|
|
|
EXPECT_EQ(16, a);
|
|
|
|
|
2021-11-28 11:51:41 +08:00
|
|
|
/* deinit */
|
|
|
|
obj_deinit(pikaMain);
|
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(pikaMain, less_equ) {
|
|
|
|
/* init */
|
|
|
|
pikaMemInfo.heapUsedMax = 0;
|
|
|
|
PikaObj* pikaMain = newRootObj((char*)"pikaMain", New_PikaMain);
|
|
|
|
/* run */
|
|
|
|
obj_runDirect(pikaMain, (char*)
|
|
|
|
"a = 0\n"
|
|
|
|
"while a <= 10:\n"
|
|
|
|
" a = a + 1\n"
|
|
|
|
"\n"
|
|
|
|
);
|
|
|
|
/* collect */
|
|
|
|
int a = obj_getInt(pikaMain, (char*)"a");
|
|
|
|
|
|
|
|
/* assert */
|
|
|
|
EXPECT_EQ(11, a);
|
|
|
|
|
2021-11-28 17:26:17 +08:00
|
|
|
/* deinit */
|
|
|
|
obj_deinit(pikaMain);
|
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(pikaMain, and_or_not) {
|
|
|
|
/* init */
|
|
|
|
pikaMemInfo.heapUsedMax = 0;
|
|
|
|
PikaObj* pikaMain = newRootObj((char*)"pikaMain", New_PikaMain);
|
|
|
|
/* run */
|
|
|
|
obj_runDirect(pikaMain, (char*)
|
|
|
|
"a = 0\n"
|
|
|
|
"b = 0\n"
|
|
|
|
"c = 0\n"
|
|
|
|
"if (1>2) or (2>1):\n"
|
|
|
|
" a = 1\n"
|
|
|
|
"\n"
|
|
|
|
"if (1>2) and (2>1):\n"
|
|
|
|
" b = 1\n"
|
|
|
|
"\n"
|
|
|
|
"if not (1>2):\n"
|
|
|
|
" c = 1\n"
|
|
|
|
"\n"
|
|
|
|
);
|
|
|
|
/* collect */
|
|
|
|
int a = obj_getInt(pikaMain, (char*)"a");
|
|
|
|
int b = obj_getInt(pikaMain, (char*)"b");
|
|
|
|
int c = obj_getInt(pikaMain, (char*)"c");
|
|
|
|
|
|
|
|
/* assert */
|
|
|
|
EXPECT_EQ(1, a);
|
|
|
|
EXPECT_EQ(0, b);
|
|
|
|
EXPECT_EQ(1, c);
|
|
|
|
|
2021-12-10 11:41:33 +08:00
|
|
|
/* deinit */
|
|
|
|
obj_deinit(pikaMain);
|
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(pikaMain, err_scop) {
|
|
|
|
/* init */
|
|
|
|
pikaMemInfo.heapUsedMax = 0;
|
|
|
|
PikaObj* pikaMain = newRootObj((char*)"pikaMain", New_PikaMain);
|
|
|
|
/* run */
|
2021-12-24 23:34:34 +08:00
|
|
|
obj_run(pikaMain, (char*)"print('test'");
|
2021-12-10 11:41:33 +08:00
|
|
|
/* collect */
|
|
|
|
|
|
|
|
/* assert */
|
|
|
|
|
2021-12-10 22:53:06 +08:00
|
|
|
/* deinit */
|
|
|
|
obj_deinit(pikaMain);
|
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(pikaMain, __init__) {
|
|
|
|
/* init */
|
|
|
|
pikaMemInfo.heapUsedMax = 0;
|
|
|
|
PikaObj* pikaMain = newRootObj((char*)"pikaMain", New_PikaMain);
|
|
|
|
/* run */
|
2021-12-24 23:34:34 +08:00
|
|
|
obj_run(pikaMain, (char*)"pin = PikaStdDevice.GPIO()\n");
|
2021-12-10 22:53:06 +08:00
|
|
|
/* collect */
|
2021-12-24 23:34:34 +08:00
|
|
|
char* a = obj_getStr(pikaMain, (char*)"pin.pin");
|
2021-12-10 22:53:06 +08:00
|
|
|
/* assert */
|
|
|
|
EXPECT_STREQ(a, "PA0");
|
|
|
|
|
2021-12-13 22:01:42 +08:00
|
|
|
/* deinit */
|
|
|
|
obj_deinit(pikaMain);
|
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
|
|
|
}
|
|
|
|
|
2021-12-24 23:34:34 +08:00
|
|
|
TEST(pikaMain, PikaStdData) {
|
2021-12-13 22:01:42 +08:00
|
|
|
/* init */
|
|
|
|
pikaMemInfo.heapUsedMax = 0;
|
|
|
|
PikaObj* pikaMain = newRootObj((char*)"pikaMain", New_PikaMain);
|
|
|
|
/* run */
|
|
|
|
obj_run(pikaMain, (char*)
|
|
|
|
"dict = PikaStdData.Dict()\n"
|
|
|
|
"dict.set('a', 1)\n"
|
2021-12-13 22:03:59 +08:00
|
|
|
"dict.set('b', 2)\n"
|
|
|
|
"dict.remove('b')\n"
|
2021-12-13 22:01:42 +08:00
|
|
|
"a = dict.get('a')\n"
|
2021-12-13 22:03:59 +08:00
|
|
|
"b = dict.get('b')\n"
|
2021-12-13 22:01:42 +08:00
|
|
|
);
|
|
|
|
/* collect */
|
2021-12-24 23:34:34 +08:00
|
|
|
int a = obj_getInt(pikaMain, (char*)"a");
|
|
|
|
int b = obj_getInt(pikaMain, (char*)"b");
|
2021-12-13 22:01:42 +08:00
|
|
|
/* assert */
|
|
|
|
EXPECT_EQ(a, 1);
|
2022-01-11 22:22:53 +08:00
|
|
|
EXPECT_EQ(b, -999999999);
|
2021-12-13 22:01:42 +08:00
|
|
|
|
2021-11-17 10:59:13 +08:00
|
|
|
/* deinit */
|
|
|
|
obj_deinit(pikaMain);
|
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
2021-12-24 23:10:50 +08:00
|
|
|
}
|
|
|
|
|
2021-12-24 23:34:34 +08:00
|
|
|
TEST(pikaMain, iter) {
|
|
|
|
/* init */
|
|
|
|
pikaMemInfo.heapUsedMax = 0;
|
|
|
|
PikaObj* pikaMain = newRootObj((char*)"pikaMain", New_PikaMain);
|
|
|
|
/* run */
|
2021-12-24 23:39:32 +08:00
|
|
|
obj_runDirect(pikaMain, (char*)
|
2021-12-24 23:34:34 +08:00
|
|
|
"list = PikaStdData.List()\n"
|
|
|
|
"list.append(7)\n"
|
|
|
|
"list.append('eee')\n"
|
|
|
|
"len = list.len()\n"
|
|
|
|
"a1 = list.get(0)\n"
|
|
|
|
"a2 = list.get(1)\n"
|
|
|
|
"res = iter(list)\n"
|
|
|
|
"\n"
|
|
|
|
);
|
|
|
|
/* collect */
|
2021-12-24 23:39:32 +08:00
|
|
|
PikaObj* res = (PikaObj*)obj_getPtr(pikaMain, (char*)"res");
|
|
|
|
PikaObj* list = (PikaObj*)obj_getPtr(pikaMain, (char*)"list");
|
2021-12-24 23:34:34 +08:00
|
|
|
/* assert */
|
2021-12-24 23:39:32 +08:00
|
|
|
EXPECT_EQ(res, list);
|
2021-12-24 23:34:34 +08:00
|
|
|
/* deinit */
|
|
|
|
// obj_deinit(globals);
|
|
|
|
obj_deinit(pikaMain);
|
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
|
|
|
}
|
|
|
|
|
2021-12-25 00:25:23 +08:00
|
|
|
TEST(pikaMain, list_for_loop) {
|
|
|
|
/* init */
|
|
|
|
pikaMemInfo.heapUsedMax = 0;
|
|
|
|
PikaObj* pikaMain = newRootObj((char*)"pikaMain", New_PikaMain);
|
|
|
|
/* run */
|
2021-12-25 00:33:55 +08:00
|
|
|
obj_runDirect(pikaMain, (char*)
|
2021-12-25 00:25:23 +08:00
|
|
|
"list = PikaStdData.List()\n"
|
|
|
|
"list.append(7)\n"
|
|
|
|
"list.append('eee')\n"
|
|
|
|
"len = list.len()\n"
|
|
|
|
"for item in list:\n"
|
|
|
|
" print(item)\n"
|
2021-12-25 00:33:55 +08:00
|
|
|
" a = item\n"
|
2021-12-25 00:25:23 +08:00
|
|
|
"\n"
|
|
|
|
);
|
|
|
|
/* collect */
|
2021-12-25 00:33:55 +08:00
|
|
|
char* a = obj_getStr(pikaMain, (char*)"a");
|
2021-12-25 00:25:23 +08:00
|
|
|
/* assert */
|
2021-12-25 00:33:55 +08:00
|
|
|
EXPECT_STREQ(a, (char*)"eee");
|
2021-12-25 00:25:23 +08:00
|
|
|
/* deinit */
|
|
|
|
// obj_deinit(globals);
|
|
|
|
obj_deinit(pikaMain);
|
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
2021-12-27 21:57:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(pikaMain, range) {
|
|
|
|
/* init */
|
|
|
|
pikaMemInfo.heapUsedMax = 0;
|
|
|
|
PikaObj* pikaMain = newRootObj((char*)"pikaMain", New_PikaMain);
|
|
|
|
/* run */
|
|
|
|
obj_runDirect(pikaMain, (char*)
|
|
|
|
"r = range(10, 0)\n"
|
2021-12-27 23:17:21 +08:00
|
|
|
"ir = iter(r)\n"
|
2021-12-27 21:57:08 +08:00
|
|
|
"\n"
|
|
|
|
);
|
|
|
|
/* collect */
|
|
|
|
/* assert */
|
|
|
|
obj_deinit(pikaMain);
|
|
|
|
/* mem check */
|
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
2021-12-28 01:04:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(pikaMain, for_in_range) {
|
|
|
|
/* init */
|
|
|
|
pikaMemInfo.heapUsedMax = 0;
|
|
|
|
PikaObj* pikaMain = newRootObj((char*)"pikaMain", New_PikaMain);
|
|
|
|
/* run */
|
|
|
|
obj_runDirect(pikaMain, (char*)
|
|
|
|
"a = 0\n"
|
|
|
|
"for i in range(0, 10):\n"
|
|
|
|
" print(i)\n"
|
|
|
|
" a = a + i\n"
|
|
|
|
"\n"
|
|
|
|
);
|
|
|
|
/* collect */
|
|
|
|
int a = obj_getInt(pikaMain, (char*)"a");
|
|
|
|
/* assert */
|
|
|
|
EXPECT_EQ(a, 45);
|
|
|
|
/* deinit */
|
|
|
|
obj_deinit(pikaMain);
|
|
|
|
/* mem check */
|
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
2021-12-28 09:20:04 +08:00
|
|
|
}
|
|
|
|
|
2021-12-29 10:06:48 +08:00
|
|
|
TEST(pikaMain, for_for_in_range) {
|
|
|
|
/* init */
|
|
|
|
pikaMemInfo.heapUsedMax = 0;
|
|
|
|
PikaObj* pikaMain = newRootObj((char*)"pikaMain", New_PikaMain);
|
|
|
|
/* run */
|
|
|
|
obj_runDirect(pikaMain, (char*)
|
|
|
|
"a = 0\n"
|
|
|
|
"for i in range(0, 10):\n"
|
|
|
|
" for k in range(0, 3):\n"
|
|
|
|
" print(k)\n"
|
|
|
|
" a = a + k\n"
|
|
|
|
"\n"
|
|
|
|
);
|
|
|
|
/* collect */
|
|
|
|
int a = obj_getInt(pikaMain, (char*)"a");
|
|
|
|
/* assert */
|
|
|
|
EXPECT_EQ(a, 30);
|
|
|
|
/* deinit */
|
|
|
|
obj_deinit(pikaMain);
|
|
|
|
/* mem check */
|
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
|
|
|
}
|
2021-12-29 22:28:40 +08:00
|
|
|
|
|
|
|
TEST(pikaMain, for_if_break) {
|
|
|
|
/* init */
|
|
|
|
pikaMemInfo.heapUsedMax = 0;
|
|
|
|
PikaObj* pikaMain = newRootObj((char*)"pikaMain", New_PikaMain);
|
|
|
|
/* run */
|
|
|
|
obj_runDirect(pikaMain, (char*)
|
|
|
|
"a = 0\n"
|
|
|
|
"for i in range(0, 10):\n"
|
2021-12-29 22:51:26 +08:00
|
|
|
" if i == 5:\n"
|
2021-12-29 22:28:40 +08:00
|
|
|
" break\n"
|
2021-12-29 22:51:26 +08:00
|
|
|
" a = a + i\n"
|
|
|
|
"\n"
|
|
|
|
);
|
|
|
|
/* collect */
|
|
|
|
int a = obj_getInt(pikaMain, (char*)"a");
|
|
|
|
/* assert */
|
|
|
|
EXPECT_EQ(a, 10);
|
|
|
|
/* deinit */
|
|
|
|
obj_deinit(pikaMain);
|
|
|
|
/* mem check */
|
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(pikaMain, for_if_continue) {
|
|
|
|
/* init */
|
|
|
|
pikaMemInfo.heapUsedMax = 0;
|
|
|
|
PikaObj* pikaMain = newRootObj((char*)"pikaMain", New_PikaMain);
|
|
|
|
/* run */
|
2022-01-04 00:29:11 +08:00
|
|
|
obj_run(pikaMain, (char*)
|
2021-12-29 22:51:26 +08:00
|
|
|
"a = 0\n"
|
|
|
|
"for i in range(0, 10):\n"
|
|
|
|
" if i == 5:\n"
|
|
|
|
" continue\n"
|
|
|
|
" a = a + i\n"
|
2021-12-29 22:28:40 +08:00
|
|
|
"\n"
|
|
|
|
);
|
|
|
|
/* collect */
|
|
|
|
int a = obj_getInt(pikaMain, (char*)"a");
|
|
|
|
/* assert */
|
2021-12-29 22:51:26 +08:00
|
|
|
EXPECT_EQ(a, 40);
|
2021-12-29 22:28:40 +08:00
|
|
|
/* deinit */
|
|
|
|
obj_deinit(pikaMain);
|
|
|
|
/* mem check */
|
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
|
|
|
}
|
2022-01-03 21:12:05 +08:00
|
|
|
|
|
|
|
/* the log_buff of printf */
|
|
|
|
extern char log_buff[LOG_BUFF_MAX][LOG_SIZE];
|
|
|
|
TEST(pikaMain, print_in_def) {
|
|
|
|
/* init */
|
|
|
|
pikaMemInfo.heapUsedMax = 0;
|
|
|
|
PikaObj* pikaMain = newRootObj((char*)"pikaMain", New_PikaMain);
|
|
|
|
/* run */
|
|
|
|
/* the test is used to fix too many print in def */
|
|
|
|
/* clear the log_buff */
|
|
|
|
memset(log_buff, 0, LOG_BUFF_MAX * LOG_SIZE);
|
|
|
|
obj_runDirect(pikaMain, (char*)
|
|
|
|
"def test_print():\n"
|
|
|
|
" print('test')\n"
|
|
|
|
"test_print()\n"
|
|
|
|
);
|
|
|
|
/* collect */
|
|
|
|
/* assert */
|
|
|
|
/* should only print once, so the second log (log_buff[1]) shuold be '\0' */
|
2022-01-03 21:51:42 +08:00
|
|
|
EXPECT_STREQ(log_buff[0], "test\r\n");
|
|
|
|
EXPECT_STREQ(log_buff[1], "");
|
2022-01-03 21:12:05 +08:00
|
|
|
/* deinit */
|
|
|
|
obj_deinit(pikaMain);
|
|
|
|
/* mem check */
|
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
2022-01-04 00:16:43 +08:00
|
|
|
}
|
2022-01-04 11:24:34 +08:00
|
|
|
|
2022-01-04 11:25:28 +08:00
|
|
|
TEST(pikaMain, prime_100) {
|
|
|
|
/* init */
|
|
|
|
pikaMemInfo.heapUsedMax = 0;
|
|
|
|
PikaObj* pikaMain = newRootObj((char*)"pikaMain", New_PikaMain);
|
|
|
|
/* run */
|
|
|
|
obj_run(pikaMain, (char*)
|
|
|
|
"num = 0\n"
|
|
|
|
"i = 2\n"
|
|
|
|
"for i in range(2 , 100):\n"
|
|
|
|
" j=2\n"
|
|
|
|
" is_prime = 1\n"
|
|
|
|
" for j in range(2 , i):\n"
|
|
|
|
" if i%j==0 :\n"
|
|
|
|
" is_prime = 0\n"
|
|
|
|
" break\n"
|
|
|
|
" if is_prime:\n"
|
|
|
|
" num = num + i\n"
|
|
|
|
"\n");
|
|
|
|
/* collect */
|
|
|
|
int num = obj_getInt(pikaMain, (char*)"num");
|
|
|
|
/* assert */
|
|
|
|
EXPECT_EQ(num, 1060);
|
|
|
|
/* deinit */
|
|
|
|
obj_deinit(pikaMain);
|
|
|
|
/* mem check */
|
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
2022-01-06 11:13:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* the log_buff of printf */
|
|
|
|
extern char log_buff[LOG_BUFF_MAX][LOG_SIZE];
|
|
|
|
TEST(pikaMain, for_in_dict) {
|
|
|
|
/* init */
|
|
|
|
pikaMemInfo.heapUsedMax = 0;
|
|
|
|
PikaObj* pikaMain = newRootObj((char*)"pikaMain", New_PikaMain);
|
|
|
|
/* run */
|
|
|
|
obj_run(pikaMain, (char*)
|
|
|
|
"print('__start__')\n"
|
|
|
|
"dict = PikaStdData.Dict()\n"
|
|
|
|
"dict.set('a', 1)\n"
|
|
|
|
"dict.set('b', 2)\n"
|
|
|
|
"dict.remove('b')\n"
|
|
|
|
"for item in dict:\n"
|
|
|
|
" print(item)\n"
|
|
|
|
"\n"
|
|
|
|
);
|
|
|
|
/* collect */
|
|
|
|
/* assert */
|
|
|
|
EXPECT_STREQ(log_buff[0], "1\r\n");
|
|
|
|
EXPECT_STREQ(log_buff[1], "__start__\r\n");
|
|
|
|
/* deinit */
|
|
|
|
obj_deinit(pikaMain);
|
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
2022-01-08 23:40:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(pikaMain, str_add) {
|
|
|
|
/* init */
|
|
|
|
pikaMemInfo.heapUsedMax = 0;
|
|
|
|
PikaObj* pikaMain = newRootObj((char*)"pikaMain", New_PikaMain);
|
|
|
|
/* run */
|
|
|
|
obj_run(pikaMain, (char*)
|
|
|
|
"a = 'a'\n"
|
|
|
|
"b = 'b'\n"
|
|
|
|
"c = a + b\n"
|
|
|
|
"d = 1\n"
|
|
|
|
"e = 1.2\n"
|
2022-01-09 00:16:01 +08:00
|
|
|
"g = c + str(d)\n"
|
|
|
|
"h = c + str(d) + str(e)\n"
|
2022-01-08 23:40:10 +08:00
|
|
|
"\n"
|
|
|
|
);
|
|
|
|
/* collect */
|
|
|
|
char* c = obj_getStr(pikaMain, (char*)"c");
|
|
|
|
char* g = obj_getStr(pikaMain, (char*)"g");
|
2022-01-10 21:49:52 +08:00
|
|
|
char* h = obj_getStr(pikaMain, (char*)"h");
|
2022-01-08 23:40:10 +08:00
|
|
|
/* assert */
|
2022-01-10 21:49:52 +08:00
|
|
|
EXPECT_STREQ(c, (char*)"ab");
|
|
|
|
EXPECT_STREQ(g, (char*)"ab1");
|
|
|
|
EXPECT_STREQ(h, (char*)"ab11.200000");
|
2022-01-08 23:40:10 +08:00
|
|
|
/* deinit */
|
|
|
|
obj_deinit(pikaMain);
|
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(pikaMain, str_add_print) {
|
|
|
|
/* init */
|
|
|
|
pikaMemInfo.heapUsedMax = 0;
|
|
|
|
PikaObj* pikaMain = newRootObj((char*)"pikaMain", New_PikaMain);
|
|
|
|
/* run */
|
|
|
|
obj_run(pikaMain, (char*)
|
2022-01-09 00:16:01 +08:00
|
|
|
"print('test: ' + str(2233))\n"
|
2022-01-08 23:40:10 +08:00
|
|
|
"\n"
|
|
|
|
);
|
|
|
|
/* collect */
|
|
|
|
/* assert */
|
|
|
|
EXPECT_STREQ(log_buff[0], "test: 2233\r\n");
|
|
|
|
/* deinit */
|
|
|
|
obj_deinit(pikaMain);
|
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
2022-01-09 00:12:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(pikaMain, int_float_to_str) {
|
|
|
|
/* init */
|
|
|
|
pikaMemInfo.heapUsedMax = 0;
|
|
|
|
PikaObj* pikaMain = newRootObj((char*)"pikaMain", New_PikaMain);
|
|
|
|
/* run */
|
|
|
|
obj_run(pikaMain, (char*)
|
|
|
|
"a = str(1)\n"
|
|
|
|
"b = str(1.2)\n"
|
|
|
|
"\n"
|
|
|
|
);
|
|
|
|
/* collect */
|
|
|
|
char* a = obj_getStr(pikaMain, (char*)"a");
|
|
|
|
char* b = obj_getStr(pikaMain, (char*)"b");
|
|
|
|
/* assert */
|
|
|
|
EXPECT_STREQ(a, "1");
|
|
|
|
EXPECT_STREQ(b, "1.200000");
|
|
|
|
/* deinit */
|
|
|
|
obj_deinit(pikaMain);
|
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
2022-01-09 22:17:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(pikaMain, str_eq) {
|
|
|
|
/* init */
|
|
|
|
pikaMemInfo.heapUsedMax = 0;
|
|
|
|
PikaObj* pikaMain = newRootObj((char*)"pikaMain", New_PikaMain);
|
|
|
|
/* run */
|
|
|
|
obj_run(pikaMain, (char*)
|
|
|
|
"a = '1ee'\n"
|
|
|
|
"b = '1ee'\n"
|
|
|
|
"c = '1e'\n"
|
|
|
|
"if a==b:\n"
|
|
|
|
" res1 = 1\n"
|
|
|
|
"else:\n"
|
|
|
|
" res1 = 0\n"
|
|
|
|
"if a==c:\n"
|
|
|
|
" res2 = 1\n"
|
|
|
|
"else:\n"
|
|
|
|
" res2 = 0\n"
|
|
|
|
"\n"
|
|
|
|
);
|
|
|
|
/* collect */
|
|
|
|
int res1 = obj_getInt(pikaMain, (char*)"res1");
|
|
|
|
int res2 = obj_getInt(pikaMain, (char*)"res2");
|
|
|
|
/* assert */
|
|
|
|
EXPECT_EQ(res1, 1);
|
|
|
|
EXPECT_EQ(res2, 0);
|
|
|
|
/* deinit */
|
|
|
|
obj_deinit(pikaMain);
|
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
2022-01-10 21:49:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(pikaMain, print_with_enter) {
|
|
|
|
/* init */
|
|
|
|
pikaMemInfo.heapUsedMax = 0;
|
|
|
|
PikaObj* pikaMain = newRootObj((char*)"pikaMain", New_PikaMain);
|
|
|
|
/* run */
|
|
|
|
__platform_printf((char*)"BEGIN\n");
|
|
|
|
obj_run(pikaMain, (char*)"print('test\\n')\n");
|
|
|
|
/* collect */
|
|
|
|
/* assert */
|
|
|
|
EXPECT_STREQ(log_buff[0], (char*)"test\n\r\n");
|
|
|
|
EXPECT_STREQ(log_buff[1], (char*)"BEGIN\n");
|
|
|
|
/* deinit */
|
|
|
|
obj_deinit(pikaMain);
|
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
2022-01-11 12:21:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(pikaMain, print_with_2enter) {
|
|
|
|
/* init */
|
|
|
|
pikaMemInfo.heapUsedMax = 0;
|
|
|
|
PikaObj* pikaMain = newRootObj((char*)"pikaMain", New_PikaMain);
|
|
|
|
/* run */
|
|
|
|
__platform_printf((char*)"BEGIN\n");
|
|
|
|
obj_run(pikaMain, (char*)"print('test\\r\\n\\n')\n");
|
|
|
|
/* collect */
|
|
|
|
/* assert */
|
|
|
|
EXPECT_STREQ(log_buff[0], (char*)"test\r\n\n\r\n");
|
|
|
|
EXPECT_STREQ(log_buff[1], (char*)"BEGIN\n");
|
|
|
|
/* deinit */
|
|
|
|
obj_deinit(pikaMain);
|
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
2022-01-12 22:38:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(pikaMain, print_ddd) {
|
|
|
|
/* init */
|
|
|
|
pikaMemInfo.heapUsedMax = 0;
|
|
|
|
PikaObj* pikaMain = newRootObj((char*)"pikaMain", New_PikaMain);
|
|
|
|
/* run */
|
|
|
|
__platform_printf((char*)"BEGIN\n");
|
2022-01-12 22:41:55 +08:00
|
|
|
obj_run(pikaMain, (char*)"print(\"[Info]: in Python config ...\")\n");
|
2022-01-12 22:38:35 +08:00
|
|
|
/* collect */
|
|
|
|
/* assert */
|
2022-01-12 22:41:55 +08:00
|
|
|
EXPECT_STREQ(log_buff[0], (char*)"[Info]: in Python config ...\r\n");
|
2022-01-12 22:38:35 +08:00
|
|
|
EXPECT_STREQ(log_buff[1], (char*)"BEGIN\n");
|
|
|
|
/* deinit */
|
|
|
|
obj_deinit(pikaMain);
|
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
|
|
|
}
|
2022-01-13 17:07:07 +08:00
|
|
|
|
|
|
|
TEST(pikaMain, for_in_string) {
|
|
|
|
/* init */
|
|
|
|
pikaMemInfo.heapUsedMax = 0;
|
|
|
|
PikaObj* pikaMain = newRootObj((char*)"pikaMain", New_PikaMain);
|
|
|
|
/* run */
|
|
|
|
__platform_printf((char*)"BEGIN\n");
|
|
|
|
obj_run(pikaMain, (char*)
|
|
|
|
"s = PikaStdData.String()\n"
|
|
|
|
"s.set('test')\n"
|
|
|
|
"for c in s:\n"
|
|
|
|
" print(c)\n"
|
|
|
|
"\n"
|
|
|
|
);
|
|
|
|
/* collect */
|
|
|
|
/* assert */
|
|
|
|
EXPECT_STREQ(log_buff[4], (char*)"BEGIN\n");
|
|
|
|
EXPECT_STREQ(log_buff[3], (char*)"t\r\n");
|
|
|
|
EXPECT_STREQ(log_buff[2], (char*)"e\r\n");
|
|
|
|
EXPECT_STREQ(log_buff[1], (char*)"s\r\n");
|
|
|
|
EXPECT_STREQ(log_buff[0], (char*)"t\r\n");
|
|
|
|
/* deinit */
|
|
|
|
obj_deinit(pikaMain);
|
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
2022-01-13 19:39:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(pikaMain, obj_no_free) {
|
|
|
|
/* init */
|
|
|
|
pikaMemInfo.heapUsedMax = 0;
|
|
|
|
PikaObj* pikaMain = newRootObj((char*)"pikaMain", New_PikaMain);
|
|
|
|
/* run */
|
|
|
|
__platform_printf((char*)"BEGIN\n");
|
|
|
|
obj_run(pikaMain, (char*)
|
|
|
|
"s = PikaStdData.String()\n"
|
|
|
|
"s.set('test')\n"
|
|
|
|
"s = PikaStdData.String()\n"
|
|
|
|
"s.set('test')\n"
|
|
|
|
"\n"
|
|
|
|
);
|
|
|
|
/* collect */
|
|
|
|
/* assert */
|
|
|
|
/* deinit */
|
|
|
|
obj_deinit(pikaMain);
|
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
2022-01-13 21:56:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(pikaMain, list__set__) {
|
|
|
|
/* init */
|
|
|
|
pikaMemInfo.heapUsedMax = 0;
|
|
|
|
PikaObj* pikaMain = newRootObj((char*)"pikaMain", New_PikaMain);
|
|
|
|
/* run */
|
|
|
|
__platform_printf((char*)"BEGIN\n");
|
|
|
|
obj_run(pikaMain, (char*)
|
|
|
|
"list = PikaStdData.List()\n"
|
|
|
|
"__set__(list, 0, 2)\n"
|
|
|
|
"res = __get__(list, 0)\n"
|
|
|
|
"\n"
|
|
|
|
);
|
|
|
|
/* collect */
|
|
|
|
int res = obj_getInt(pikaMain, (char*)"res");
|
|
|
|
/* assert */
|
|
|
|
EXPECT_EQ(res, 2);
|
|
|
|
/* deinit */
|
|
|
|
obj_deinit(pikaMain);
|
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(pikaMain, string__get__) {
|
|
|
|
/* init */
|
|
|
|
pikaMemInfo.heapUsedMax = 0;
|
|
|
|
PikaObj* pikaMain = newRootObj((char*)"pikaMain", New_PikaMain);
|
|
|
|
/* run */
|
|
|
|
__platform_printf((char*)"BEGIN\n");
|
|
|
|
obj_run(pikaMain, (char*)
|
|
|
|
"s = PikaStdData.String()\n"
|
|
|
|
"s.set('test')\n"
|
|
|
|
"res = __get__(s, 2)\n"
|
|
|
|
"\n"
|
|
|
|
);
|
|
|
|
/* collect */
|
|
|
|
char* res = obj_getStr(pikaMain, (char*)"res");
|
|
|
|
/* assert */
|
|
|
|
EXPECT_STREQ(res, "s");
|
|
|
|
/* deinit */
|
|
|
|
obj_deinit(pikaMain);
|
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(pikaMain, dict__set__get) {
|
|
|
|
/* init */
|
|
|
|
pikaMemInfo.heapUsedMax = 0;
|
|
|
|
PikaObj* pikaMain = newRootObj((char*)"pikaMain", New_PikaMain);
|
|
|
|
/* run */
|
|
|
|
__platform_printf((char*)"BEGIN\n");
|
|
|
|
obj_run(pikaMain, (char*)
|
|
|
|
"list = PikaStdData.Dict()\n"
|
|
|
|
"__set__(list, 'a', 2)\n"
|
|
|
|
"res = __get__(list, 'a')\n"
|
|
|
|
"\n"
|
|
|
|
);
|
|
|
|
/* collect */
|
|
|
|
int res = obj_getInt(pikaMain, (char*)"res");
|
|
|
|
/* assert */
|
|
|
|
EXPECT_EQ(res, 2);
|
|
|
|
/* deinit */
|
|
|
|
obj_deinit(pikaMain);
|
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
2022-01-18 21:55:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(pikaMain, str___get__) {
|
|
|
|
/* init */
|
|
|
|
pikaMemInfo.heapUsedMax = 0;
|
|
|
|
PikaObj* pikaMain = newRootObj((char*)"pikaMain", New_PikaMain);
|
|
|
|
/* run */
|
|
|
|
__platform_printf((char*)"BEGIN\n");
|
|
|
|
obj_run(pikaMain, (char*)
|
|
|
|
"s = 'test'\n"
|
|
|
|
"res = __get__(s, 2)\n"
|
|
|
|
"__set__(s, 2, 'q')\n"
|
|
|
|
"\n"
|
|
|
|
);
|
|
|
|
/* collect */
|
|
|
|
char* res = obj_getStr(pikaMain, (char*)"res");
|
|
|
|
char* s= obj_getStr(pikaMain, (char*)"s");
|
|
|
|
/* assert */
|
|
|
|
EXPECT_STREQ(res, (char*)"s");
|
|
|
|
// EXPECT_STREQ(s, (char*)"teqt");
|
|
|
|
/* deinit */
|
|
|
|
obj_deinit(pikaMain);
|
|
|
|
EXPECT_EQ(pikaMemNow(), 0);
|
2022-01-13 17:07:07 +08:00
|
|
|
}
|