Skip to content

Commit

Permalink
list.h: fix return codes E_EMPTY
Browse files Browse the repository at this point in the history
For:

* cdada_list_first()
* cdada_list_last()
* cdada_list_pop_front()
* cdada_list_pop_back()

Make sure that E_EMPTY is return instead of E_NOT_FOUND, according
to API contract.
  • Loading branch information
msune committed Apr 19, 2021
1 parent 79a5282 commit 1f5dc20
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 17 deletions.
8 changes: 5 additions & 3 deletions include/cdada/__list_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ int cdada_list_first_last_u(const __cdada_list_int_t* m, std::list<T>* m_u,
typename std::list<T>::const_iterator it;
it = m_u->begin();
if(it == m_u->end())
return CDADA_E_NOT_FOUND;
return CDADA_E_EMPTY;

if(m->val_len == m->user_val_len)
*aux = *it;
Expand All @@ -179,7 +179,7 @@ int cdada_list_first_last_u(const __cdada_list_int_t* m, std::list<T>* m_u,
typename std::list<T>::const_reverse_iterator rit;
rit = m_u->rbegin();
if(rit == m_u->rend())
return CDADA_E_NOT_FOUND;
return CDADA_E_EMPTY;

if(m->val_len == m->user_val_len)
*aux = *rit;
Expand Down Expand Up @@ -262,13 +262,15 @@ int cdada_list_push_u(__cdada_list_int_t* m, std::list<T>* m_u,
else
m_u->push_back(aux);


return CDADA_SUCCESS;
}

template<typename T>
int cdada_list_pop_u(__cdada_list_int_t* m, std::list<T>* m_u, bool front){

if(m_u->size() == 0)
return CDADA_E_EMPTY;

if(front)
m_u->pop_front();
else
Expand Down
4 changes: 2 additions & 2 deletions include/cdada/list.h
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ int cdada_list_get(const cdada_list_t* list, const uint32_t pos, void* val);
*
* @returns Return codes:
* CDADA_SUCCESS: first element was retrieved
* CDADA_E_NOT_FOUND: list has no elements
* CDADA_E_EMPTY: list has no elements
* CDADA_E_UNKNOWN: corrupted list or internal error (bug)
* CDADA_E_INVALID: list is NULL or corrupted
*/
Expand All @@ -207,7 +207,7 @@ int cdada_list_first(const cdada_list_t* list, void* val);
*
* @returns Return codes:
* CDADA_SUCCESS: last element was retrieved
* CDADA_E_NOT_FOUND: list has no elements
* CDADA_E_EMPTY: list has no elements
* CDADA_E_UNKNOWN: corrupted list or internal error (bug)
* CDADA_E_INVALID: list is NULL or corrupted
*/
Expand Down
47 changes: 35 additions & 12 deletions test/list_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,12 @@ int test_u8_insert_removal(){

TEST_ASSERT(cdada_list_size(list) == 0);
TEST_ASSERT(cdada_list_empty(list) == true);
TEST_ASSERT(cdada_list_first(list, &key) == CDADA_E_NOT_FOUND);
TEST_ASSERT(cdada_list_last(list, &key) == CDADA_E_NOT_FOUND);

//Test E_EMPTY
TEST_ASSERT(cdada_list_first(list, &key) == CDADA_E_EMPTY);
TEST_ASSERT(cdada_list_last(list, &key) == CDADA_E_EMPTY);
TEST_ASSERT(cdada_list_pop_front(list) == CDADA_E_EMPTY);
TEST_ASSERT(cdada_list_pop_back(list) == CDADA_E_EMPTY);

//Add one key & get
key = 0;
Expand Down Expand Up @@ -347,8 +351,12 @@ int test_u16_insert_removal(){

TEST_ASSERT(cdada_list_size(list) == 0);
TEST_ASSERT(cdada_list_empty(list) == true);
TEST_ASSERT(cdada_list_first(list, &key) == CDADA_E_NOT_FOUND);
TEST_ASSERT(cdada_list_last(list, &key) == CDADA_E_NOT_FOUND);

//Test E_EMPTY
TEST_ASSERT(cdada_list_first(list, &key) == CDADA_E_EMPTY);
TEST_ASSERT(cdada_list_last(list, &key) == CDADA_E_EMPTY);
TEST_ASSERT(cdada_list_pop_front(list) == CDADA_E_EMPTY);
TEST_ASSERT(cdada_list_pop_back(list) == CDADA_E_EMPTY);

//Add one key & get
key = 0;
Expand Down Expand Up @@ -618,8 +626,12 @@ int test_u32_insert_removal(){

TEST_ASSERT(cdada_list_size(list) == 0);
TEST_ASSERT(cdada_list_empty(list) == true);
TEST_ASSERT(cdada_list_first(list, &key) == CDADA_E_NOT_FOUND);
TEST_ASSERT(cdada_list_last(list, &key) == CDADA_E_NOT_FOUND);

//Test E_EMPTY
TEST_ASSERT(cdada_list_first(list, &key) == CDADA_E_EMPTY);
TEST_ASSERT(cdada_list_last(list, &key) == CDADA_E_EMPTY);
TEST_ASSERT(cdada_list_pop_front(list) == CDADA_E_EMPTY);
TEST_ASSERT(cdada_list_pop_back(list) == CDADA_E_EMPTY);

//Add one key & get
key = 0;
Expand Down Expand Up @@ -888,9 +900,12 @@ int test_u64_insert_removal_traverse(){

TEST_ASSERT(cdada_list_size(list) == 0);
TEST_ASSERT(cdada_list_empty(list) == true);
TEST_ASSERT(cdada_list_first(list, &key) == CDADA_E_NOT_FOUND);
TEST_ASSERT(cdada_list_last(list, &key) == CDADA_E_NOT_FOUND);

//Test E_EMPTY
TEST_ASSERT(cdada_list_first(list, &key) == CDADA_E_EMPTY);
TEST_ASSERT(cdada_list_last(list, &key) == CDADA_E_EMPTY);
TEST_ASSERT(cdada_list_pop_front(list) == CDADA_E_EMPTY);
TEST_ASSERT(cdada_list_pop_back(list) == CDADA_E_EMPTY);

//Add one key & get
key = 0ULL;
Expand Down Expand Up @@ -1299,8 +1314,12 @@ int _test_u552_insert_removal_traverse(){

TEST_ASSERT(cdada_list_size(list) == 0);
TEST_ASSERT(cdada_list_empty(list) == true);
TEST_ASSERT(cdada_list_first(list, &key) == CDADA_E_NOT_FOUND);
TEST_ASSERT(cdada_list_last(list, &key) == CDADA_E_NOT_FOUND);

//Test E_EMPTY
TEST_ASSERT(cdada_list_first(list, &key) == CDADA_E_EMPTY);
TEST_ASSERT(cdada_list_last(list, &key) == CDADA_E_EMPTY);
TEST_ASSERT(cdada_list_pop_front(list) == CDADA_E_EMPTY);
TEST_ASSERT(cdada_list_pop_back(list) == CDADA_E_EMPTY);

//Add one key & get
key.mid = 0;
Expand Down Expand Up @@ -1590,8 +1609,12 @@ int test_u3552_insert_removal_traverse_custom(){

TEST_ASSERT(cdada_list_size(list) == 0);
TEST_ASSERT(cdada_list_empty(list) == true);
TEST_ASSERT(cdada_list_first(list, &key) == CDADA_E_NOT_FOUND);
TEST_ASSERT(cdada_list_last(list, &key) == CDADA_E_NOT_FOUND);

//Test E_EMPTY
TEST_ASSERT(cdada_list_first(list, &key) == CDADA_E_EMPTY);
TEST_ASSERT(cdada_list_last(list, &key) == CDADA_E_EMPTY);
TEST_ASSERT(cdada_list_pop_front(list) == CDADA_E_EMPTY);
TEST_ASSERT(cdada_list_pop_back(list) == CDADA_E_EMPTY);

//Add one key & get
key.mid = 0;
Expand Down

0 comments on commit 1f5dc20

Please sign in to comment.