Fix negative modulo for C89 (#45)

In C89, negative integer division and modulo is implementation-defined.

Case 1: division rounds to 0 (C89/90, C99, C11, C18)
-9 / 7 -> -1
-9 % 7 -> -2

Case 2: division rounds to negative infinity (C89/90)
-9 / 7 -> -2
-9 % 7 -> 5

This change fixes the deque data structure from having negative division and modulo to prevent this implementation-defined behavior.
This commit is contained in:
Bailey Thompson
2019-05-21 17:46:53 -04:00
committed by GitHub
parent b07e06d62e
commit 411af131c2
3 changed files with 33 additions and 6 deletions

View File

@@ -253,6 +253,25 @@ static void test_clear_out_of_memory(void)
assert(!deque_destroy(me));
}
void test_single_full_block(void)
{
int i;
int num = 5;
deque me = deque_init(sizeof(int));
for (i = 0; i < 5; i++) {
deque_push_front(me, &num);
}
for (i = 0; i < 3; i++) {
deque_push_back(me, &num);
}
for (i = 0; i < 8; i++) {
deque_pop_back(&num, me);
}
deque_trim(me);
assert(deque_size(me) == 0);
deque_destroy(me);
}
void test_deque(void)
{
test_invalid_init();
@@ -263,4 +282,5 @@ void test_deque(void)
test_push_front_out_of_memory();
test_push_back_out_of_memory();
test_clear_out_of_memory();
test_single_full_block();
}