Mon, 14 Apr 2025 19:36:43 +0200
add test and documentation for cxListContains() - fixes #643
--- a/CHANGELOG Sun Apr 13 18:01:29 2025 +0200 +++ b/CHANGELOG Mon Apr 14 19:36:43 2025 +0200 @@ -3,6 +3,7 @@ * adds cxMempoolTransfer() and cxMempoolTransferObject() * adds cxListSet() + * adds cxListContains() * adds cxBufferShrink() * changes grow strategy for the mempory pool to reduce reallocations * changes grow strategy for CxBuffer, which does now take the page size into account
--- a/docs/Writerside/topics/about.md Sun Apr 13 18:01:29 2025 +0200 +++ b/docs/Writerside/topics/about.md Mon Apr 14 19:36:43 2025 +0200 @@ -30,6 +30,7 @@ * adds cxMempoolTransfer() and cxMempoolTransferObject() * adds cxListSet() +* adds cxListContains() * adds cxBufferShrink() * changes grow strategy for the mempory pool to reduce reallocations * changes grow strategy for CxBuffer, which does now take the page size into account
--- a/docs/Writerside/topics/list.h.md Sun Apr 13 18:01:29 2025 +0200 +++ b/docs/Writerside/topics/list.h.md Mon Apr 14 19:36:43 2025 +0200 @@ -168,6 +168,8 @@ size_t cxListFindRemove(CxList *list, const void *elem); +bool cxListContains(const CxList *list, const void *elem); + bool cxListIndexValid(const CxList *list, size_t index); size_t cxListSize(const CxList *list); @@ -191,6 +193,8 @@ The function `cxListFindRemove()` behaves like `cxListFind()`, except that it also removes the first occurrence of the element from the list. This will _also_ call destructor functions, if specified, so take special care when you continue to use `elem`, or when the list is storing pointers and the element appears more than once in the list. +The function `cxListContains()` returns `true`, if and only if `cxListFind()` would return a valid index. + With `cxListIndexValid()` you can check the index returned by `cxListFind()` or `cxListFindRemove()`, which is more convenient than comparing the return value if the return value of `cxListSize()`.
--- a/tests/test_list.c Sun Apr 13 18:01:29 2025 +0200 +++ b/tests/test_list.c Mon Apr 14 19:36:43 2025 +0200 @@ -1607,6 +1607,24 @@ free(testdata); }) +roll_out_test_combos(contains, { + int a = 37; + int b = 42; + int c = 55; + cxListAdd(list, &a); + cxListAdd(list, &b); + cxListAdd(list, &c); + int x; + x = 37; + CX_TEST_ASSERT(cxListContains(list, &x)); + x = 42; + CX_TEST_ASSERT(cxListContains(list, &x)); + x = 55; + CX_TEST_ASSERT(cxListContains(list, &x)); + x = 47; + CX_TEST_ASSERT(!cxListContains(list, &x)); +}) + roll_out_test_combos(clear, { int *testdata = int_test_data_added_to_list(list, isptrlist, 8); CX_TEST_ASSERT(cxListSize(list) > 0); @@ -2001,6 +2019,8 @@ cx_test_register(suite, test_list_parl_find_remove); cx_test_register(suite, test_list_arl_find_remove_sorted); cx_test_register(suite, test_list_parl_find_remove_sorted); + cx_test_register(suite, test_list_arl_contains); + cx_test_register(suite, test_list_parl_contains); cx_test_register(suite, test_list_arl_clear); cx_test_register(suite, test_list_parl_clear); cx_test_register(suite, test_list_arl_at); @@ -2102,6 +2122,8 @@ cx_test_register(suite, test_list_pll_find_remove); cx_test_register(suite, test_list_ll_find_remove_sorted); cx_test_register(suite, test_list_pll_find_remove_sorted); + cx_test_register(suite, test_list_ll_contains); + cx_test_register(suite, test_list_pll_contains); cx_test_register(suite, test_list_ll_clear); cx_test_register(suite, test_list_pll_clear); cx_test_register(suite, test_list_ll_at);