Skip to content

Commit

Permalink
test zero initialized element comparison
Browse files Browse the repository at this point in the history
  • Loading branch information
TheGAzed committed Dec 29, 2024
1 parent 04025b3 commit 4303091
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
10 changes: 7 additions & 3 deletions headers/binary_set.h
Original file line number Diff line number Diff line change
Expand Up @@ -165,11 +165,16 @@ static inline void destroy_binary_set(binary_set_s * set, const destroy_binary_s
}

static inline bool contains_binary_set(const binary_set_s set, const BINARY_SET_DATA_TYPE element) {
if (set.compare) { // uses buildin binary search implmentation if compare function is specified
return bsearch(&element, set.elements, set.size, sizeof(BINARY_SET_DATA_TYPE), set.compare) != NULL;
}
// else custom binary search is used as memcmp takes three arguments as input
// (bsearch requires a 2 argument compare function). IF ONLY NESTED FUNCTION WERE PART OF THE C STANDARD.

BINARY_SET_DATA_TYPE * base = set.elements;
for (size_t limit = set.size; limit != 0; limit >>= 1) {
BINARY_SET_DATA_TYPE * current_element = base + (limit >> 1);
printf("%d ", current_element->sub_one);
const int cmp = set.compare ? set.compare(&element, current_element) : memcmp(&element, current_element, sizeof(BINARY_SET_DATA_TYPE));
const int cmp = memcmp(&element, current_element, sizeof(BINARY_SET_DATA_TYPE));
if (cmp == 0) {
return true;
}
Expand All @@ -178,7 +183,6 @@ static inline bool contains_binary_set(const binary_set_s set, const BINARY_SET_
limit--;
}
}
puts("\n");

return false;
}
Expand Down
4 changes: 3 additions & 1 deletion test/binary_set/infinite_realloc_binary_set.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ TEST IRBS_02(void) {

binary_set_s united = union_binary_set(set_one, set_two, NULL);
for (int i = 0; i < (REALLOC_BINARY_SET_CHUNK - 1) * 3; ++i) {
ASSERTm("[IRBS-ERROR] expected set to contain i", contains_binary_set(united, (BINARY_SET_DATA_TYPE) { .sub_one = i, }));
BINARY_SET_DATA_TYPE element = { 0 };
element.sub_one = i;
ASSERTm("[IRBS-ERROR] expected set to contain i", contains_binary_set(united, element));
}

destroy_binary_set(&united, NULL);
Expand Down

0 comments on commit 4303091

Please sign in to comment.