pikapython/test/gc-test.cpp

134 lines
3.7 KiB
C
Raw Normal View History

#include "test_common.h"
TEST_START
2022-04-25 23:39:16 +08:00
TEST(gc, root) {
2022-04-30 21:17:19 +08:00
PikaObj* root = newRootObj("root", New_PikaMain);
2022-04-25 23:39:16 +08:00
EXPECT_EQ(obj_refcntNow(root), 1);
2022-04-30 21:17:19 +08:00
Arg* refArg = arg_setRef(NULL, "", root);
2022-04-25 23:39:16 +08:00
EXPECT_EQ(obj_refcntNow(root), 2);
arg_deinit(refArg);
EXPECT_EQ(obj_refcntNow(root), 1);
obj_deinit(root);
EXPECT_EQ(pikaMemNow(), 0);
}
TEST(gc, ref1) {
/* init */
2022-04-30 21:17:19 +08:00
PikaObj* pikaMain = newRootObj("pikaMain", New_PikaMain);
2022-04-25 23:39:16 +08:00
/* run */
2022-04-30 21:17:19 +08:00
obj_run(pikaMain, "mem = PikaStdLib.MemChecker()");
PikaObj* mem = (PikaObj*)obj_getPtr(pikaMain, "mem");
2022-04-25 23:39:16 +08:00
EXPECT_EQ(obj_refcntNow(mem), 1);
/* deinit */
obj_deinit(pikaMain);
EXPECT_EQ(pikaMemNow(), 0);
}
TEST(gc, ref121) {
/* init */
2022-04-30 21:17:19 +08:00
PikaObj* pikaMain = newRootObj("pikaMain", New_PikaMain);
2022-04-25 23:39:16 +08:00
/* run */
2022-04-30 21:17:19 +08:00
obj_run(pikaMain, "mem = PikaStdLib.MemChecker()");
PikaObj* mem = (PikaObj*)obj_getPtr(pikaMain, "mem");
2022-04-25 23:39:16 +08:00
EXPECT_EQ(obj_refcntNow(mem), 1);
2022-04-30 21:17:19 +08:00
obj_run(pikaMain, "mem2 = mem");
2022-04-25 23:39:16 +08:00
EXPECT_EQ(obj_refcntNow(mem), 2);
2022-04-30 21:17:19 +08:00
obj_removeArg(pikaMain, "mem2");
2022-04-25 23:39:16 +08:00
EXPECT_EQ(obj_refcntNow(mem), 1);
/* deinit */
obj_deinit(pikaMain);
EXPECT_EQ(pikaMemNow(), 0);
}
2022-04-26 09:49:17 +08:00
TEST(gc, ref12) {
/* init */
2022-04-30 21:17:19 +08:00
PikaObj* pikaMain = newRootObj("pikaMain", New_PikaMain);
2022-04-26 09:49:17 +08:00
/* run */
2022-04-30 21:17:19 +08:00
obj_run(pikaMain, "mem = PikaStdLib.MemChecker()");
PikaObj* mem = (PikaObj*)obj_getPtr(pikaMain, "mem");
2022-04-26 09:49:17 +08:00
EXPECT_EQ(obj_refcntNow(mem), 1);
2022-04-30 21:17:19 +08:00
obj_run(pikaMain, "mem2 = mem");
2022-04-26 09:49:17 +08:00
EXPECT_EQ(obj_refcntNow(mem), 2);
/* deinit */
obj_deinit(pikaMain);
EXPECT_EQ(pikaMemNow(), 0);
}
2022-04-25 23:39:16 +08:00
TEST(gc, ref1210) {
/* init */
2022-04-30 21:17:19 +08:00
PikaObj* pikaMain = newRootObj("pikaMain", New_PikaMain);
2022-04-25 23:39:16 +08:00
/* run */
2022-04-30 21:17:19 +08:00
obj_run(pikaMain, "mem = PikaStdLib.MemChecker()");
PikaObj* mem = (PikaObj*)obj_getPtr(pikaMain, "mem");
2022-04-25 23:39:16 +08:00
EXPECT_EQ(obj_refcntNow(mem), 1);
2022-04-30 21:17:19 +08:00
obj_run(pikaMain, "mem2 = mem");
2022-04-25 23:39:16 +08:00
EXPECT_EQ(obj_refcntNow(mem), 2);
2022-04-30 21:17:19 +08:00
obj_removeArg(pikaMain, "mem2");
2022-04-25 23:39:16 +08:00
EXPECT_EQ(obj_refcntNow(mem), 1);
/* deinit */
obj_deinit(pikaMain);
EXPECT_EQ(pikaMemNow(), 0);
}
2022-04-26 12:43:41 +08:00
TEST(gc, ref_move) {
/* init */
2022-04-30 21:17:19 +08:00
PikaObj* pikaMain = newRootObj("pikaMain", New_PikaMain);
2022-04-26 12:43:41 +08:00
/* run */
2022-04-30 21:17:19 +08:00
obj_run(pikaMain, "mem = PikaStdLib.MemChecker()");
PikaObj* mem = (PikaObj*)obj_getPtr(pikaMain, "mem");
2022-04-26 12:43:41 +08:00
EXPECT_EQ(obj_refcntNow(mem), 1);
2022-04-30 21:17:19 +08:00
obj_run(pikaMain, "mem2 = mem");
2022-04-26 12:43:41 +08:00
EXPECT_EQ(obj_refcntNow(mem), 2);
2022-04-30 21:17:19 +08:00
obj_removeArg(pikaMain, "mem");
2022-04-26 12:43:41 +08:00
EXPECT_EQ(obj_refcntNow(mem), 1);
/* deinit */
obj_deinit(pikaMain);
EXPECT_EQ(pikaMemNow(), 0);
}
2022-04-26 14:04:19 +08:00
TEST(gc, factory) {
/* init */
2022-04-30 21:17:19 +08:00
PikaObj* pikaMain = newRootObj("pikaMain", New_PikaMain);
2022-04-26 14:04:19 +08:00
/* run */
2022-06-06 11:35:07 +08:00
obj_run(pikaMain,
"def factory():\n"
" _mem = PikaStdLib.MemChecker()\n"
" return _mem\n"
"mem = factory()\n");
2022-04-26 14:04:19 +08:00
/* collect */
2022-04-30 21:17:19 +08:00
PikaObj* mem = obj_getObj(pikaMain, "mem");
2022-04-26 14:52:09 +08:00
EXPECT_EQ(obj_refcntNow(mem), 1);
2022-04-26 14:04:19 +08:00
/* deinit */
obj_deinit(pikaMain);
EXPECT_EQ(pikaMemNow(), 0);
}
2022-04-26 17:20:08 +08:00
TEST(gc, not_out) {
/* init */
2022-04-30 21:17:19 +08:00
PikaObj* pikaMain = newRootObj("pikaMain", New_PikaMain);
2022-04-26 17:20:08 +08:00
/* run */
2022-06-06 11:35:07 +08:00
obj_run(pikaMain, "PikaStdLib.MemChecker()\n");
2022-04-26 17:20:08 +08:00
/* deinit */
obj_deinit(pikaMain);
EXPECT_EQ(pikaMemNow(), 0);
}
2022-06-06 11:35:07 +08:00
TEST(gc, heap_failed1) {
/* init */
PikaObj* pikaMain = newRootObj("pikaMain", New_PikaMain);
/* run */
obj_run(pikaMain,
"recv_buf = PikaStdData.List()\n"
"RECV_MAX_SIZE=128\n"
"iteri = 0\n"
"for i in range(0, int(RECV_MAX_SIZE)):\n"
" recv_buf.append(0)\n"
" iteri += 1\n"
"iteri");
/* assert */
EXPECT_STREQ("128\r\n", log_buff[0]);
/* deinit */
obj_deinit(pikaMain);
EXPECT_EQ(pikaMemNow(), 0);
}
TEST_END