添加函数注释

This commit is contained in:
建峰 2025-04-30 00:17:59 +08:00
parent 643d601c6e
commit e49f425e43
2 changed files with 25 additions and 4 deletions

View File

@ -189,6 +189,21 @@ static void list_print(struct _list* self)
} }
} }
/**
* @brief list slice
* if index < 0, from the end of list. for example:
* list[-1] is the last element in list.
*
* @param self
* @param start start index
* @param end end index
* @param step step, if step < 0, return a reverse list.
* @return struct _list*
* a copy of the list, from start to end, step by step.
* if step < 0, return a reverse list.
* if step > 0, return a forward list.
* if step == 0, return NULL.
*/
struct _list* list_slice(struct _list *self, int start, int end, int step) struct _list* list_slice(struct _list *self, int start, int end, int step)
{ {
assert(self != NULL); assert(self != NULL);

View File

@ -440,14 +440,20 @@ static void test_list_slice(void)
} }
// python: list[0:] -> [] // python: list[0:] -> []
list2 = list->slice(list, 0, 0, 1); // if start == end
TEST_ASSERT_NULL(list2);
list2 = list->slice(list, 1, 5, 0); // if step == 0 list2 = list->slice(list, 1, 5, 0); // if step == 0
TEST_ASSERT_NULL(list2); TEST_ASSERT_NULL(list2);
list2 = list->slice(list, 0, 0, 1); // if start == end
TEST_ASSERT_NOT_NULL(list2);
TEST_ASSERT_TRUE(list2->empty(list2));
list2 = list->slice(list, 1, 5, -1); // if start < end && step < 0 list2 = list->slice(list, 1, 5, -1); // if start < end && step < 0
TEST_ASSERT_NULL(list2); TEST_ASSERT_NULL(list2);
TEST_ASSERT_TRUE(list2->empty(list2));
list2 = list->slice(list, 5, 1, 1); // if start > end && step > 0
TEST_ASSERT_NULL(list2);
TEST_ASSERT_TRUE(list2->empty(list2));
// python: list[0:] // python: list[0:]
list2 = list->slice(list, 0, len, 1); list2 = list->slice(list, 0, len, 1);