Skip to content
Snippets Groups Projects

Move alloc tests to expected-output tests

Merged Peter Limkilde Svendsen requested to merge plimkilde/relibc:expected_alloc_test into master
1 file
+ 24
21
Compare changes
  • Side-by-side
  • Inline
+ 24
21
@@ -23,21 +23,6 @@ void test_non_null(void *ptr, int error_val) {
error_val, strerror(error_val));
}
/* For testing size-0 allocation requests. */
void test_size_zero(void *ptr, int error_val) {
/* For allocation functions, POSIX permits returning either a NULL
* pointer and optionally an implementation-defined error value, or
* succeeding with a non-NULL pointer. */
if (ptr == NULL || (ptr != NULL && error_val == 0)) {
// Constant output for successful case
printf("(OK)\n");
}
else {
printf("pointer: %p, error value: %d = %s\n",
ptr, error_val, strerror(error_val));
}
}
/* For testing functions that should return pointers with a particular
* alignment (successful case). */
void test_valid_aligned(void *ptr, size_t alignment, int error_val) {
@@ -65,6 +50,24 @@ void test_invalid_aligned(void *ptr, int error_val) {
ptr, error_val, strerror(error_val));
}
/* For testing size-0 allocation requests. */
void test_size_zero(void *ptr, size_t alignment, int error_val) {
/* Facilitates checking alignment upon non-NULL return */
uintptr_t ptr_alignment_rem = (uintptr_t)ptr % (uintptr_t)alignment;
/* For allocation functions, POSIX permits returning either a NULL
* pointer and optionally an implementation-defined error value, or
* succeeding with a non-NULL pointer. */
if (ptr == NULL || (ptr != NULL && ptr_alignment_rem == 0 && error_val == 0)) {
// Constant output for successful case
printf("(OK)\n");
}
else {
printf("pointer: %p, error value: %d = %s\n",
ptr, error_val, strerror(error_val));
}
}
/* For cases where we expect allocation to fail, returning a NULL
* pointer and indicating ENOMEM. */
void test_cannot_alloc(void *ptr, int error_val) {
@@ -92,7 +95,7 @@ int main(void) {
char * ptr_zerosize_malloc = (char *)malloc(zero_size);
int malloc_zerosize_errno = errno;
printf("malloc (size 0): ");
test_size_zero(ptr_zerosize_malloc, malloc_zerosize_errno);
test_size_zero(ptr_zerosize_malloc, 1, malloc_zerosize_errno);
free(ptr_zerosize_malloc);
errno = 0;
@@ -116,7 +119,7 @@ int main(void) {
char * ptr_zerosize_calloc = (char *)calloc(zero_size, 1);
int calloc_zerosize_errno = errno;
printf("calloc (size 0): ");
test_size_zero(ptr_zerosize_calloc, calloc_zerosize_errno);
test_size_zero(ptr_zerosize_calloc, 1, calloc_zerosize_errno);
free(ptr_zerosize_calloc);
errno = 0;
@@ -141,7 +144,7 @@ int main(void) {
ptr_realloc_size0 = (char *)realloc(ptr_realloc_size0, zero_size);
int realloc_size0_errno = errno;
printf("realloc (size 0): ");
test_size_zero(ptr_realloc_size0, realloc_size0_errno);
test_size_zero(ptr_realloc_size0, 1, realloc_size0_errno);
free(ptr_realloc_size0);
char * ptr_realloc = (char *)malloc(sample_alloc_size);
@@ -167,7 +170,7 @@ int main(void) {
char * ptr_memalign_size0 = (char *)memalign(256, zero_size);
int memalign_size0_errno = errno;
printf("memalign (size 0): ");
test_size_zero(ptr_memalign_size0, memalign_size0_errno);
test_size_zero(ptr_memalign_size0, 256, memalign_size0_errno);
free(ptr_memalign_size0);
errno = 0;
@@ -219,7 +222,7 @@ int main(void) {
char * ptr_valloc_size0 = (char *)valloc(zero_size);
int valloc_size0_errno = errno;
printf("valloc (size 0): ");
test_size_zero(ptr_valloc_size0, valloc_size0_errno);
test_size_zero(ptr_valloc_size0, page_size, valloc_size0_errno);
free(ptr_valloc_size0);
errno = 0;
@@ -261,7 +264,7 @@ int main(void) {
void * ptr_posix_memalign_size0 = NULL;
int posix_memalign_size0_return = posix_memalign(&ptr_posix_memalign_size0, pow2_mul_voidptr_size, zero_size);
printf("posix_memalign (size 0): ");
test_size_zero(ptr_posix_memalign_size0, posix_memalign_size0_return);
test_size_zero(ptr_posix_memalign_size0, pow2_mul_voidptr_size, posix_memalign_size0_return);
free(ptr_posix_memalign_size0);
errno = 0;
Loading