test append and get for list is ok

This commit is contained in:
lyon 2021-11-17 10:59:13 +08:00
parent f3e17a2a75
commit 4e67a1925f
2 changed files with 32 additions and 1 deletions

View File

@ -16,7 +16,7 @@ int PikaStdLib_List_len(PikaObj* self) {
Arg* PikaStdLib_List_get(PikaObj* self, int i) {
char buff[11];
char* index = fast_itoa(buff, i);
return obj_getArg(self, index);
return arg_copy(obj_getArg(self, index));
}
void PikaStdLib_List_init(PikaObj* self) {
/* set top index for append */

View File

@ -28,4 +28,35 @@ TEST(pikaMain, new_mem) {
obj_deinit(pikaMain);
obj_deinit(globals);
EXPECT_EQ(pikaMemNow(), 0);
}
TEST(pikaMain, list_new) {
pikaMemInfo.heapUsedMax = 0;
/* init */
PikaObj* pikaMain = newRootObj((char*)"pikaMain", New_PikaMain);
/* run */
Parameters* globals =
obj_runDirect(pikaMain, (char*)
"list = PikaStdLib.List()\n"
"list.init()\n"
"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");
/* deinit */
obj_deinit(globals);
obj_deinit(pikaMain);
EXPECT_EQ(pikaMemNow(), 0);
}