Skip to content

Commit 3c93678

Browse files
kartbendkalowsk
authored andcommitted
tests: lib: min_heap: add test coverage for edge cases
Add missing tests for edge cases (ex. peek on empty heap, push to full heap, ...) to get to 100% coverage (lines & branches) Signed-off-by: Benjamin Cabé <benjamin@zephyrproject.org>
1 parent 37919af commit 3c93678

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

tests/lib/min_heap/src/test_min_heap.c

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,11 @@ ZTEST(min_heap_api, test_insert)
125125
ret = min_heap_push(&my_heap_gt, &elements[i]);
126126
zassert_ok(ret, "min_heap_push failed");
127127
}
128+
129+
/* Try to push one more element to the now full heap */
130+
ret = min_heap_push(&my_heap_gt, &elements[0]);
131+
zassert_equal(ret, -ENOMEM, "push on full heap should return -ENOMEM");
132+
128133
validate_heap_order_gt(&my_heap_gt);
129134
}
130135

@@ -152,6 +157,7 @@ ZTEST(min_heap_api, test_peek_and_pop)
152157
zassert_equal(peek_key, pop.key, "Peek/pop error");
153158
zassert_equal(pop.key, LOWEST_PRIORITY_LS, "heap error %d", pop.key);
154159
validate_heap_order_ls(&runtime_heap);
160+
zassert_is_null(min_heap_peek(&runtime_heap), "peek on empty heap should return NULL");
155161
}
156162

157163
ZTEST(min_heap_api, test_find_and_remove)
@@ -165,13 +171,23 @@ ZTEST(min_heap_api, test_find_and_remove)
165171
}
166172

167173
int target_key = 5;
174+
int wrong_key = 100;
168175
size_t index;
169-
struct data removed, *found;
176+
struct data removed, *found, *found_ignore_index, *not_found;
170177

171178
found = min_heap_find(&my_heap, match_key, &target_key, &index);
179+
found_ignore_index = min_heap_find(&my_heap, match_key, &target_key, NULL);
180+
not_found = min_heap_find(&my_heap, match_key, &wrong_key, &index);
172181

173182
zassert_not_null(found, "min_heap_find failure");
183+
zassert_not_null(found_ignore_index, "min_heap_find failure");
184+
zassert_is_null(not_found, "min_heap_find failure");
185+
zassert_equal(found->key, target_key, "Found wrong element");
174186
min_heap_remove(&my_heap, index, &removed);
187+
zassert_false(min_heap_is_empty(&my_heap), "Heap should not be empty");
188+
min_heap_remove(&my_heap, HEAP_CAPACITY, &removed);
189+
zassert_false(min_heap_remove(&my_heap, HEAP_CAPACITY, &removed),
190+
"remove with invalid index should return false");
175191
validate_heap_order_ls(&my_heap);
176192
zassert_true(min_heap_is_empty(&my_heap), "Empty check fail");
177193
}

0 commit comments

Comments
 (0)