Skip to content

Commit

Permalink
make sll methods with unused return values return void
Browse files Browse the repository at this point in the history
The singly linked list methods sll_push(), sll_move_append(), and
sll_move_append_first() return a reference to the list that they operate
on. However, this return value is not needed as it does not convey any
information to the caller.

These functions are called only for their side effects, and in practice
all callers (outside of the test suite) ignored the return value.

Making these functions return void simplifies the API and makes it clear
that they are only used for side effects and no information is conveyed
by the return value.
  • Loading branch information
bertschinger committed Sep 12, 2024
1 parent 20f5728 commit 453f65e
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 41 deletions.
8 changes: 4 additions & 4 deletions include/SinglyLinkedList.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,10 @@ struct SinglyLinkedList {
typedef struct SinglyLinkedList sll_t;

sll_t *sll_init(sll_t *sll);
sll_t *sll_push(sll_t *sll, void *data); /* back */
void *sll_pop(sll_t *sll); /* front */
sll_t *sll_move_append_first(sll_t *dst, sll_t *src, const uint64_t n); /* move first n from src to dst, appending to dst */
sll_t *sll_move_append(sll_t *dst, sll_t *src); /* move all from src to dst, appending to dst */
void sll_push(sll_t *sll, void *data); /* back */
void *sll_pop(sll_t *sll); /* front */
void sll_move_append_first(sll_t *dst, sll_t *src, const uint64_t n); /* move first n from src to dst, appending to dst */
void sll_move_append(sll_t *dst, sll_t *src); /* move all from src to dst, appending to dst */
uint64_t sll_get_size(sll_t *sll);

/* functions for looping over a sll */
Expand Down
16 changes: 8 additions & 8 deletions src/SinglyLinkedList.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ sll_t *sll_init(sll_t *sll) {
return sll_clear(sll);
}

sll_t *sll_push(sll_t *sll, void *data) {
void sll_push(sll_t *sll, void *data) {
/* Not checking arguments */

sll_node_t *node = calloc(1, sizeof(sll_node_t));
Expand All @@ -100,7 +100,7 @@ sll_t *sll_push(sll_t *sll, void *data) {

sll->size++;

return sll;
return;
}

void *sll_pop(sll_t *sll) {
Expand All @@ -125,11 +125,11 @@ void *sll_pop(sll_t *sll) {
return data;
}

sll_t *sll_move_append_first(sll_t *dst, sll_t *src, const uint64_t n) {
void sll_move_append_first(sll_t *dst, sll_t *src, const uint64_t n) {
/* Not checking arguments */

if (!src->size) {
return dst;
return;
}

/* Connect src->head to dst->tail first */
Expand All @@ -146,7 +146,7 @@ sll_t *sll_move_append_first(sll_t *dst, sll_t *src, const uint64_t n) {
dst->tail = src->tail;
sll_clear(src);

return dst;
return;
}

/* find up to n nodes from src */
Expand All @@ -168,12 +168,12 @@ sll_t *sll_move_append_first(sll_t *dst, sll_t *src, const uint64_t n) {
dst->size += i;
src->size -= i;

return dst;
return;
}

sll_t *sll_move_append(sll_t *dst, sll_t *src) {
void sll_move_append(sll_t *dst, sll_t *src) {
/* Not checking arguments */
return sll_move_append_first(dst, src, src->size);
sll_move_append_first(dst, src, src->size);
}

uint64_t sll_get_size(sll_t *sll) {
Expand Down
58 changes: 29 additions & 29 deletions test/unit/googletest/SinglyLinkedList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ TEST(SinglyLinkedList, push) {
EXPECT_EQ(sll.tail, nullptr);

// push first item onto sll
EXPECT_EQ(&sll, sll_push(&sll, nullptr));
sll_push(&sll, nullptr);

// head and tail are no longer NULL
EXPECT_NE(sll.head, nullptr);
Expand All @@ -92,7 +92,7 @@ TEST(SinglyLinkedList, push) {
EXPECT_EQ(sll.head, sll.tail);

// push second item onto sll
EXPECT_EQ(&sll, sll_push(&sll, nullptr));
sll_push(&sll, nullptr);

// head and tail are still not NULL
EXPECT_NE(sll.head, nullptr);
Expand All @@ -117,7 +117,7 @@ TEST(SinglyLinkedList, pop) {
for(size_t items = 1; items <= count; items++) {
// push items
for(size_t i = 1; i <= items; i++) {
EXPECT_EQ(&sll, sll_push(&sll, data));
sll_push(&sll, data);
}
EXPECT_EQ(sll.size, items);

Expand Down Expand Up @@ -165,7 +165,7 @@ TEST(SinglyLinkedList, move_append_first) {
sll_init(&src);

// empty src
EXPECT_EQ(sll_move_append_first(&dst, &src, src_count), &dst);
sll_move_append_first(&dst, &src, src_count);
EXPECT_EQ(dst.head, nullptr);
EXPECT_EQ(dst.tail, nullptr);
EXPECT_EQ(dst.size, (uint64_t) 0);
Expand All @@ -176,11 +176,11 @@ TEST(SinglyLinkedList, move_append_first) {
// src with items
{
for(std::size_t i = 0; i < src_count; i++) {
EXPECT_EQ(sll_push(&src, (void *) (uintptr_t) (i + src_start)), &src);
sll_push(&src, (void *) (uintptr_t) (i + src_start));
}

// move some of src
EXPECT_EQ(sll_move_append_first(&dst, &src, src_half), &dst);
sll_move_append_first(&dst, &src, src_half);
EXPECT_NE(dst.head, nullptr);
EXPECT_EQ((uint64_t) (uintptr_t) sll_node_data(dst.head), (uint64_t) src_start);
EXPECT_NE(dst.tail, nullptr);
Expand All @@ -193,7 +193,7 @@ TEST(SinglyLinkedList, move_append_first) {
EXPECT_EQ(src.size, src_count - src_half);

// move all of src
EXPECT_EQ(sll_move_append_first(&dst, &src, src_count), &dst);
sll_move_append_first(&dst, &src, src_count);
EXPECT_NE(dst.head, nullptr);
EXPECT_EQ((uint64_t) (uintptr_t) sll_node_data(dst.head), (uint64_t) src_start);
EXPECT_NE(dst.tail, nullptr);
Expand All @@ -218,12 +218,12 @@ TEST(SinglyLinkedList, move_append_first) {
static const std::size_t dst_count = dst_stop - dst_start;

for(std::size_t i = 0; i < dst_count; i++) {
EXPECT_EQ(sll_push(&dst, (void *) (uintptr_t) (i + dst_start)), &dst);
sll_push(&dst, (void *) (uintptr_t) (i + dst_start));
}

// empty src
{
EXPECT_EQ(sll_move_append_first(&dst, &src, src_count), &dst);
sll_move_append_first(&dst, &src, src_count);
EXPECT_NE(dst.head, nullptr);
EXPECT_EQ((uint64_t) (uintptr_t) sll_node_data(dst.head), (uint64_t) dst_start);
EXPECT_NE(dst.tail, nullptr);
Expand All @@ -237,11 +237,11 @@ TEST(SinglyLinkedList, move_append_first) {
// src with items
{
for(std::size_t i = 0; i < src_count; i++) {
EXPECT_EQ(sll_push(&src, (void *) (uintptr_t) (i + src_start)), &src);
sll_push(&src, (void *) (uintptr_t) (i + src_start));
}

// move some of src
EXPECT_EQ(sll_move_append_first(&dst, &src, src_half), &dst);
sll_move_append_first(&dst, &src, src_half);
EXPECT_NE(dst.head, nullptr);
EXPECT_EQ((uint64_t) (uintptr_t) sll_node_data(dst.head), (uint64_t) dst_start);
EXPECT_NE(dst.tail, nullptr);
Expand All @@ -254,7 +254,7 @@ TEST(SinglyLinkedList, move_append_first) {
EXPECT_EQ(src.size, src_count - src_half);

// move all of src
EXPECT_EQ(sll_move_append_first(&dst, &src, src_count), &dst);
sll_move_append_first(&dst, &src, src_count);
EXPECT_NE(dst.head, nullptr);
EXPECT_EQ((uint64_t) (uintptr_t) sll_node_data(dst.head), (uint64_t) dst_start);
EXPECT_NE(dst.tail, nullptr);
Expand Down Expand Up @@ -284,7 +284,7 @@ TEST(SinglyLinkedList, move_append) {
sll_init(&src);

// empty src
EXPECT_EQ(sll_move_append(&dst, &src), &dst);
sll_move_append(&dst, &src);
EXPECT_EQ(dst.head, nullptr);
EXPECT_EQ(dst.tail, nullptr);
EXPECT_EQ(dst.size, (uint64_t) 0);
Expand All @@ -295,11 +295,11 @@ TEST(SinglyLinkedList, move_append) {
// src with items
{
for(std::size_t i = 0; i < src_count; i++) {
EXPECT_EQ(sll_push(&src, (void *) (uintptr_t) (i + src_start)), &src);
sll_push(&src, (void *) (uintptr_t) (i + src_start));
}

// move all of src
EXPECT_EQ(sll_move_append(&dst, &src), &dst);
sll_move_append(&dst, &src);
EXPECT_NE(dst.head, nullptr);
EXPECT_EQ((uint64_t) (uintptr_t) sll_node_data(dst.head), (uint64_t) src_start);
EXPECT_NE(dst.tail, nullptr);
Expand All @@ -324,12 +324,12 @@ TEST(SinglyLinkedList, move_append) {
static const std::size_t dst_count = dst_stop - dst_start;

for(std::size_t i = 0; i < dst_count; i++) {
EXPECT_EQ(sll_push(&dst, (void *) (uintptr_t) (i + dst_start)), &dst);
sll_push(&dst, (void *) (uintptr_t) (i + dst_start));
}

// empty src
{
EXPECT_EQ(sll_move_append(&dst, &src), &dst);
sll_move_append(&dst, &src);
EXPECT_NE(dst.head, nullptr);
EXPECT_EQ((uint64_t) (uintptr_t) sll_node_data(dst.head), (uint64_t) dst_start);
EXPECT_NE(dst.tail, nullptr);
Expand All @@ -343,11 +343,11 @@ TEST(SinglyLinkedList, move_append) {
// src with items
{
for(std::size_t i = 0; i < src_count; i++) {
EXPECT_EQ(sll_push(&src, (void *) (uintptr_t) (i + src_start)), &src);
sll_push(&src, (void *) (uintptr_t) (i + src_start));
}

// move all of src
EXPECT_EQ(sll_move_append(&dst, &src), &dst);
sll_move_append(&dst, &src);
EXPECT_NE(dst.head, nullptr);
EXPECT_EQ((uint64_t) (uintptr_t) sll_node_data(dst.head), (uint64_t) dst_start);
EXPECT_NE(dst.tail, nullptr);
Expand All @@ -371,8 +371,8 @@ TEST(SinglyLinkedList, head_node) {
EXPECT_EQ(sll.head, nullptr);
EXPECT_EQ(sll.head, sll_head_node(&sll));

EXPECT_EQ(&sll, sll_push(&sll, nullptr));
EXPECT_EQ(&sll, sll_push(&sll, nullptr));
sll_push(&sll, nullptr);
sll_push(&sll, nullptr);

// non-null head node
EXPECT_NE(sll.head, nullptr);
Expand All @@ -388,18 +388,18 @@ TEST(SinglyLinkedList, next_node) {
EXPECT_EQ(&sll, sll_init(&sll));

// push first node
EXPECT_EQ(&sll, sll_push(&sll, nullptr));
sll_push(&sll, nullptr);
sll_node_t *head = sll_head_node(&sll);
EXPECT_NE(head, nullptr);
EXPECT_EQ(sll_next_node(head), nullptr);

// push second node
EXPECT_EQ(&sll, sll_push(&sll, nullptr));
sll_push(&sll, nullptr);
sll_node_t *second = sll.tail;
EXPECT_EQ(second, sll_next_node(head));

// push third node
EXPECT_EQ(&sll, sll_push(&sll, nullptr));
sll_push(&sll, nullptr);
sll_node_t *third = sll.tail;
EXPECT_EQ(third, sll_next_node(second));

Expand All @@ -414,8 +414,8 @@ TEST(SinglyLinkedList, tail_node) {
EXPECT_EQ(sll.tail, nullptr);
EXPECT_EQ(sll.tail, sll_tail_node(&sll));

EXPECT_EQ(&sll, sll_push(&sll, nullptr));
EXPECT_EQ(&sll, sll_push(&sll, nullptr));
sll_push(&sll, nullptr);
sll_push(&sll, nullptr);

// non-null tail node
EXPECT_NE(sll.tail, nullptr);
Expand All @@ -429,7 +429,7 @@ TEST(SinglyLinkedList, node_data) {
int value = 1234;
sll_t sll;
EXPECT_EQ(&sll, sll_init(&sll));
EXPECT_EQ(&sll, sll_push(&sll, &value));
sll_push(&sll, &value);

void *data = sll_node_data(sll_head_node(&sll));
EXPECT_EQ(&value, data);
Expand All @@ -447,7 +447,7 @@ TEST(SinglyLinkedList, loop) {
EXPECT_EQ(&sll, sll_init(&sll));
for(uint64_t i = 0; i < count; i++) {
values[i] = i;
EXPECT_EQ(&sll, sll_push(&sll, &values[i]));
sll_push(&sll, &values[i]);
}

// check values
Expand All @@ -467,7 +467,7 @@ TEST(SinglyLinkedList, destroy_func) {

void *ptr = malloc(10);
ASSERT_NE(ptr, nullptr);
ASSERT_EQ(&sll, sll_push(&sll, ptr));
sll_push(&sll, ptr);

// use valgrind to check for leaks
sll_destroy(&sll, free);
Expand Down

0 comments on commit 453f65e

Please sign in to comment.