Merge pull request #16292 from kaspar030/clist_is_empty
core: add clist_is_empty()
This commit is contained in:
commit
668e90adfc
@ -33,6 +33,7 @@
|
|||||||
* clist_remove() | O(n) | remove and return node
|
* clist_remove() | O(n) | remove and return node
|
||||||
* clist_sort() | O(NlogN)| sort list (stable)
|
* clist_sort() | O(NlogN)| sort list (stable)
|
||||||
* clist_count() | O(n) | count the number of elements in a list
|
* clist_count() | O(n) | count the number of elements in a list
|
||||||
|
* clist_is_empty() | O(1) | returns true if the list contains no elements
|
||||||
*
|
*
|
||||||
* clist can be used as a traditional list, a queue (FIFO) and a stack (LIFO) using
|
* clist can be used as a traditional list, a queue (FIFO) and a stack (LIFO) using
|
||||||
* fast O(1) operations.
|
* fast O(1) operations.
|
||||||
@ -87,6 +88,7 @@
|
|||||||
#ifndef CLIST_H
|
#ifndef CLIST_H
|
||||||
#define CLIST_H
|
#define CLIST_H
|
||||||
|
|
||||||
|
#include <stdbool.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include "list.h"
|
#include "list.h"
|
||||||
|
|
||||||
@ -102,6 +104,20 @@ extern "C" {
|
|||||||
*/
|
*/
|
||||||
typedef list_node_t clist_node_t;
|
typedef list_node_t clist_node_t;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Checks if *list is empty
|
||||||
|
*
|
||||||
|
* @note Complexity: O(1)
|
||||||
|
*
|
||||||
|
* @param[in] list Pointer to clist
|
||||||
|
*
|
||||||
|
* @returns true if list contains no elements, false otherwise
|
||||||
|
*/
|
||||||
|
static inline bool clist_is_empty(const clist_node_t *list)
|
||||||
|
{
|
||||||
|
return list->next == NULL;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Appends *new_node* at the end of *list
|
* @brief Appends *new_node* at the end of *list
|
||||||
*
|
*
|
||||||
|
|||||||
@ -331,6 +331,23 @@ static void test_clist_count(void)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void test_clist_is_empty(void)
|
||||||
|
{
|
||||||
|
TEST_ASSERT(clist_is_empty(&test_clist));
|
||||||
|
|
||||||
|
for (unsigned i = 1; i <= TEST_CLIST_LEN; i++) {
|
||||||
|
clist_rpush(&test_clist, &tests_clist_buf[i - 1]);
|
||||||
|
TEST_ASSERT(!clist_is_empty(&test_clist));
|
||||||
|
}
|
||||||
|
for (unsigned i = TEST_CLIST_LEN; i > 0; i--) {
|
||||||
|
clist_lpop(&test_clist);
|
||||||
|
/* when i == 1 at the beginning of the iteration, there's one element
|
||||||
|
left, which is then dropped in the line above.
|
||||||
|
So in all cases but that last one, the list is not empty. */
|
||||||
|
TEST_ASSERT(clist_is_empty(&test_clist) == (i == 1));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Test *tests_core_clist_tests(void)
|
Test *tests_core_clist_tests(void)
|
||||||
{
|
{
|
||||||
EMB_UNIT_TESTFIXTURES(fixtures) {
|
EMB_UNIT_TESTFIXTURES(fixtures) {
|
||||||
@ -349,6 +366,7 @@ Test *tests_core_clist_tests(void)
|
|||||||
new_TestFixture(test_clist_sort_empty),
|
new_TestFixture(test_clist_sort_empty),
|
||||||
new_TestFixture(test_clist_sort),
|
new_TestFixture(test_clist_sort),
|
||||||
new_TestFixture(test_clist_count),
|
new_TestFixture(test_clist_count),
|
||||||
|
new_TestFixture(test_clist_is_empty),
|
||||||
};
|
};
|
||||||
|
|
||||||
EMB_UNIT_TESTCALLER(core_clist_tests, set_up, NULL,
|
EMB_UNIT_TESTCALLER(core_clist_tests, set_up, NULL,
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user