char *test_expand_contract() { int old_max = array->max; DArray_expand(array); mu_assert((unsigned int)array->max == old_max + array->expand_rate, "Wrong size after expand"); DArray_contract(array); mu_assert((unsigned int)array->max == array->expand_rate + 1, "Should stay at the expand_rate at least."); DArray_contract(array); mu_assert((unsigned int)array->max == array->expand_rate + 1, "Should stay at the expand_rate at least."); return NULL; }
void *DArray_pop(DArray *array) { check(array->end > 0, "can't shrink array beyond zero"); void *el = DArray_remove(array, array->end); array->end--; if (array->end > (int)array->expand_rate && array->end % array->expand_rate) { DArray_contract(array); } return el; error: return NULL; }
void *DArray_pop(DArray *array) { check(array->end - 1 >= 0, "Attempt to pop from empty array."); void *el = DArray_remove(array, array->end - 1); array->end--; if(DArray_end(array) > (int)array->expand_rate && DArray_end(array) % array->expand_rate) { DArray_contract(array); } return el; error: return NULL; }
char *test_expand_contract() { int old_max = array->max; DArray_expand(array); mu_assert((unsigned int)array->max == old_max + array->expand_rate, "Wrong size after expand."); old_max = array->max; DArray_contract(array); if ((unsigned int)(old_max - array->end - 1) > array->expand_rate) { mu_assert((unsigned int)array->max == old_max - array->expand_rate, "Should contract."); } else { mu_assert(array->max == old_max, "Should not contract."); } old_max = array->max; DArray_contract(array); if ((unsigned int)(old_max - array->end - 1) > array->expand_rate) { mu_assert((unsigned int)array->max == old_max - array->expand_rate, "Should contract."); } else { mu_assert(array->max == old_max, "Should not contract."); } return NULL; }