From 4e67a1925f553babb3f604fd90fa69eba4a42146 Mon Sep 17 00:00:00 2001 From: lyon Date: Wed, 17 Nov 2021 10:59:13 +0800 Subject: [PATCH] test append and get for list is ok --- .../PikaStdLib/PikaStdLib_List.c | 2 +- port/linux/test/pikaMain-test.cpp | 31 +++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/port/linux/package/pikascript/pikascript-lib/PikaStdLib/PikaStdLib_List.c b/port/linux/package/pikascript/pikascript-lib/PikaStdLib/PikaStdLib_List.c index be82d88c4..640de16f4 100644 --- a/port/linux/package/pikascript/pikascript-lib/PikaStdLib/PikaStdLib_List.c +++ b/port/linux/package/pikascript/pikascript-lib/PikaStdLib/PikaStdLib_List.c @@ -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 */ diff --git a/port/linux/test/pikaMain-test.cpp b/port/linux/test/pikaMain-test.cpp index 2508f2392..8562702a0 100644 --- a/port/linux/test/pikaMain-test.cpp +++ b/port/linux/test/pikaMain-test.cpp @@ -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); } \ No newline at end of file