Skip to content
Snippets Groups Projects
Commit 3ae46d86 authored by jD91mZM2's avatar jD91mZM2
Browse files

Merge branch 'expected_alloc_test' into 'master'

Move alloc tests to expected-output tests

See merge request !226
parents bf13674e 70857f99
No related branches found
No related tags found
No related merge requests found
......@@ -37,6 +37,7 @@ EXPECT_NAMES=\
stdio/setvbuf \
stdio/sprintf \
stdlib/a64l \
stdlib/alloc \
stdlib/atof \
stdlib/atoi \
stdlib/div \
......@@ -104,7 +105,6 @@ NAMES=\
pwd \
stdio/tempnam \
stdio/tmpnam \
stdlib/alloc \
stdlib/bsearch \
stdlib/mktemp \
stdlib/realpath \
......
malloc (size 0): (OK)
malloc: pointer: (not NULL), error value: 0 = Success
malloc (SIZE_MAX): pointer: (nil), error value: 12 = Out of memory
calloc (size 0): (OK)
calloc: pointer: (not NULL), error value: 0 = Success
calloc (overflowing): pointer: (nil), error value: 12 = Out of memory
realloc (size 0): (OK)
realloc: pointer: (not NULL), error value: 0 = Success
realloc (SIZE_MAX): pointer: (nil), error value: 12 = Out of memory
memalign (size 0): (OK)
memalign: pointer: (alignment OK), error value: 0 = Success
memalign (SIZE_MAX): pointer: (nil), error value: 12 = Out of memory
memalign (alignment 0): pointer: (nil), error value: 22 = Invalid argument
memalign (alignment 3): pointer: (nil), error value: 22 = Invalid argument
aligned_alloc (size % alignment == 0): pointer: (alignment OK), error value: 0 = Success
aligned_alloc (size % alignment != 0): pointer: (nil), error value: 22 = Invalid argument
valloc (size 0): (OK)
valloc: pointer: (alignment OK), error value: 0 = Success
valloc (SIZE_MAX): pointer: (nil), error value: 12 = Out of memory
posix_memalign: pointer: (alignment OK), error value: 0 = Success
posix_memalign (alignment 0): pointer: (nil), error value: 22 = Invalid argument
posix_memalign (non-power-of-two multiple of sizeof(void *)): pointer: (nil), error value: 22 = Invalid argument
posix_memalign (size 0): (OK)
posix_memalign (SIZE_MAX): pointer: (nil), error value: 12 = Out of memory
......@@ -4,9 +4,77 @@
#include <stdlib.h>
#include <stddef.h> /* for size_t */
#include <stdint.h> /* for SIZE_MAX */
#include <string.h> /* for strerror() */
#include <unistd.h> /* for sysconf() */
#include "test_helpers.h"
/* For regular allocations that should succeed without particular
* alignment requirements. */
void test_non_null(void *ptr, int error_val) {
if (ptr != NULL) {
// Constant output for successful case
printf("pointer: (not NULL), ");
}
else {
printf("pointer: %p, ", ptr);
}
printf("error value: %d = %s\n",
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) {
/* Cast to uintptr_t to allow taking modulo of address. The
* uintptr_t type is guaranteed to be able to hold any valid object
* address. */
uintptr_t ptr_alignment_rem = (uintptr_t)ptr % (uintptr_t)alignment;
if (ptr_alignment_rem == 0) {
// Constant output for successful case
printf("pointer: (alignment OK), ");
}
else {
printf("pointer: %p, ", ptr);
}
printf("error value: %d = %s\n",
error_val, strerror(error_val));
}
/* For testing functions that should return pointers with a particular
* alignment. With invalid alignment, we expect constant output (a NULL
* pointer and EINVAL). */
void test_invalid_aligned(void *ptr, int error_val) {
printf("pointer: %p, error value: %d = %s\n",
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_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) {
printf("pointer: %p, error value: %d = %s\n",
ptr, error_val, strerror(error_val));
}
int main(void) {
size_t sample_alloc_size = 256;
size_t sample_realloc_size = sample_alloc_size + 1;
......@@ -14,6 +82,7 @@ int main(void) {
/* ensure values are mapped to variables */
size_t zero_size = 0;
size_t max_size = SIZE_MAX;
size_t page_size = (size_t)sysconf(_SC_PAGESIZE);
size_t aligned_alloc_alignment = 128;
size_t aligned_alloc_goodsize = 256;
size_t aligned_alloc_badsize = 257;
......@@ -25,16 +94,15 @@ int main(void) {
errno = 0;
char * ptr_zerosize_malloc = (char *)malloc(zero_size);
int malloc_zerosize_errno = errno;
printf("malloc (size 0) : %p, errno: %d = %s\n",
ptr_zerosize_malloc, malloc_zerosize_errno,
strerror(malloc_zerosize_errno));
printf("malloc (size 0): ");
test_size_zero(ptr_zerosize_malloc, 1, malloc_zerosize_errno);
free(ptr_zerosize_malloc);
errno = 0;
char * ptr_malloc = (char *)malloc(sample_alloc_size);
int malloc_errno = errno;
printf("malloc : %p, errno: %d = %s\n",
ptr_malloc, malloc_errno, strerror(malloc_errno));
printf("malloc: ");
test_non_null(ptr_malloc, malloc_errno);
for(i = 0; i < sample_alloc_size; i++) {
ptr_malloc[i] = (char)i;
}
......@@ -43,24 +111,22 @@ int main(void) {
errno = 0;
char * ptr_malloc_maxsize = (char *)malloc(max_size);
int malloc_maxsize_errno = errno;
printf("malloc (SIZE_MAX) : %p, errno: %d = %s\n",
ptr_malloc_maxsize, malloc_maxsize_errno,
strerror(malloc_maxsize_errno));
printf("malloc (SIZE_MAX): ");
test_cannot_alloc(ptr_malloc_maxsize, malloc_maxsize_errno);
free(ptr_malloc_maxsize);
errno = 0;
char * ptr_zerosize_calloc = (char *)calloc(zero_size, 1);
int calloc_zerosize_errno = errno;
printf("calloc (size 0) : %p, errno: %d = %s\n",
ptr_zerosize_calloc,
calloc_zerosize_errno, strerror(calloc_zerosize_errno));
printf("calloc (size 0): ");
test_size_zero(ptr_zerosize_calloc, 1, calloc_zerosize_errno);
free(ptr_zerosize_calloc);
errno = 0;
char * ptr_calloc = (char *)calloc(sample_alloc_size, 1);
int calloc_errno = errno;
printf("calloc : %p, errno: %d = %s\n", ptr_calloc,
calloc_errno, strerror(calloc_errno));
printf("calloc: ");
test_non_null(ptr_calloc, calloc_errno);
for(i = 0; i < sample_alloc_size; i++) {
ptr_calloc[i] = (char)i;
}
......@@ -69,26 +135,24 @@ int main(void) {
errno = 0;
char * ptr_calloc_overflow = (char *)calloc(max_size, max_size);
int calloc_overflow_errno = errno;
printf("calloc (overflowing) : %p, errno: %d = %s\n",
ptr_calloc_overflow, calloc_overflow_errno,
strerror(calloc_overflow_errno));
free(ptr_calloc_overflow); /* clean up correctly even if overflow is not handled */
printf("calloc (overflowing): ");
test_cannot_alloc(ptr_calloc_overflow, calloc_overflow_errno);
free(ptr_calloc_overflow);
char * ptr_realloc_size0 = (char *)malloc(sample_alloc_size);
errno = 0;
ptr_realloc_size0 = (char *)realloc(ptr_realloc_size0, zero_size);
int realloc_size0_errno = errno;
printf("realloc (size 0) : %p, errno: %d = %s\n",
ptr_realloc_size0, realloc_size0_errno,
strerror(realloc_size0_errno));
printf("realloc (size 0): ");
test_size_zero(ptr_realloc_size0, 1, realloc_size0_errno);
free(ptr_realloc_size0);
char * ptr_realloc = (char *)malloc(sample_alloc_size);
errno = 0;
ptr_realloc = (char *)realloc(ptr_realloc, sample_realloc_size);
int realloc_errno = errno;
printf("realloc : %p, errno: %d = %s\n",
ptr_realloc, realloc_errno, strerror(realloc_errno));
printf("realloc: ");
test_non_null(ptr_realloc, realloc_errno);
for(i = 0; i < sample_realloc_size; i++) {
ptr_realloc[i] = (char)i;
}
......@@ -98,162 +162,115 @@ int main(void) {
errno = 0;
ptr_realloc_maxsize = (char *)realloc(ptr_realloc_maxsize, max_size);
int realloc_maxsize_errno = errno;
printf("realloc (SIZE_MAX) : %p, errno: %d = %s\n",
ptr_realloc_maxsize, realloc_maxsize_errno,
strerror(realloc_maxsize_errno));
printf("realloc (SIZE_MAX): ");
test_cannot_alloc(ptr_realloc_maxsize, realloc_maxsize_errno);
free(ptr_realloc_maxsize);
errno = 0;
char * ptr_memalign_size0 = (char *)memalign(256, zero_size);
char * ptr_memalign_size0 = (char *)memalign(aligned_alloc_alignment, zero_size);
int memalign_size0_errno = errno;
printf("memalign (size 0) : %p, errno: %d = %s\n",
ptr_memalign_size0, memalign_size0_errno,
strerror(memalign_size0_errno));
printf("memalign (size 0): ");
test_size_zero(ptr_memalign_size0, aligned_alloc_alignment, memalign_size0_errno);
free(ptr_memalign_size0);
errno = 0;
char * ptr_memalign = (char *)memalign(256, sample_alloc_size);
char * ptr_memalign = (char *)memalign(aligned_alloc_alignment, sample_alloc_size);
int memalign_errno = errno;
printf("memalign : %p, errno: %d = %s\n", ptr_memalign,
memalign_errno, strerror(memalign_errno));
printf("memalign: ");
test_valid_aligned(ptr_memalign, aligned_alloc_alignment, memalign_errno);
for(i = 0; i < sample_alloc_size; i++) {
ptr_memalign[i] = (char)i;
}
free(ptr_memalign);
errno = 0;
char * ptr_memalign_maxsize = (char *)memalign(256, max_size);
char * ptr_memalign_maxsize = (char *)memalign(aligned_alloc_alignment, max_size);
int memalign_maxsize_errno = errno;
printf("memalign (SIZE_MAX) : %p, errno: %d = %s\n",
ptr_memalign_maxsize, memalign_maxsize_errno,
strerror(memalign_maxsize_errno));
printf("memalign (SIZE_MAX): ");
test_cannot_alloc(ptr_memalign_maxsize, memalign_maxsize_errno);
free(ptr_memalign_maxsize);
errno = 0;
char * ptr_memalign_align0 = (char *)memalign(0, sample_alloc_size);
int memalign_align0_errno = errno;
printf("memalign (alignment 0): %p, errno: %d = %s\n",
ptr_memalign_align0, memalign_align0_errno,
strerror(memalign_align0_errno));
printf("memalign (alignment 0): ");
test_invalid_aligned(ptr_memalign_align0, memalign_align0_errno);
free(ptr_memalign_align0);
errno = 0;
char * ptr_memalign_align3 = (char *)memalign(3, sample_alloc_size);
int memalign_align3_errno = errno;
printf("memalign (alignment 3): %p, errno: %d = %s\n",
ptr_memalign_align3, memalign_align3_errno,
strerror(memalign_align3_errno));
printf("memalign (alignment 3): ");
test_invalid_aligned(ptr_memalign_align3, memalign_align3_errno);
free(ptr_memalign_align3);
errno = 0;
char * ptr_aligned_alloc_goodsize = (char *)aligned_alloc(aligned_alloc_alignment, aligned_alloc_goodsize);
int aligned_alloc_goodsize_errno = errno;
printf("aligned_alloc (size %% alignment == 0):\n");
printf(" %p, errno: %d = %s\n",
ptr_aligned_alloc_goodsize, aligned_alloc_goodsize_errno,
strerror(aligned_alloc_goodsize_errno));
printf("aligned_alloc (size %% alignment == 0): ");
test_valid_aligned(ptr_aligned_alloc_goodsize, aligned_alloc_alignment, aligned_alloc_goodsize_errno);
free(ptr_aligned_alloc_goodsize);
errno = 0;
char * ptr_aligned_alloc_badsize = (char *)aligned_alloc(aligned_alloc_alignment, aligned_alloc_badsize);
int aligned_alloc_badsize_errno = errno;
printf("aligned_alloc (size %% alignment != 0):\n");
printf(" %p, errno: %d = %s\n",
ptr_aligned_alloc_badsize, aligned_alloc_badsize_errno,
strerror(aligned_alloc_badsize_errno));
printf("aligned_alloc (size %% alignment != 0): ");
test_invalid_aligned(ptr_aligned_alloc_badsize, aligned_alloc_badsize_errno);
free(ptr_aligned_alloc_badsize);
errno = 0;
char * ptr_valloc_size0 = (char *)valloc(zero_size);
int valloc_size0_errno = errno;
printf("valloc (size 0) : %p, errno: %d = %s\n",
ptr_valloc_size0, valloc_size0_errno,
strerror(valloc_size0_errno));
printf("valloc (size 0): ");
test_size_zero(ptr_valloc_size0, page_size, valloc_size0_errno);
free(ptr_valloc_size0);
errno = 0;
char * ptr_valloc = (char *)valloc(sample_alloc_size);
int valloc_errno = errno;
printf("valloc : %p, errno: %d = %s\n",
ptr_valloc, valloc_errno, strerror(valloc_errno));
printf("valloc: ");
test_valid_aligned(ptr_valloc, page_size, valloc_errno);
free(ptr_valloc);
errno = 0;
char * ptr_valloc_maxsize = (char *)valloc(max_size);
int valloc_maxsize_errno = errno;
printf("valloc (SIZE_MAX) : %p, errno: %d = %s\n",
ptr_valloc_maxsize, valloc_maxsize_errno,
strerror(valloc_maxsize_errno));
printf("valloc (SIZE_MAX): ");
test_cannot_alloc(ptr_valloc_maxsize, valloc_maxsize_errno);
free(ptr_valloc_maxsize);
errno = 0;
void * ptr_posix_memalign = NULL;
int posix_memalign_return = posix_memalign(&ptr_posix_memalign, pow2_mul_voidptr_size, sample_alloc_size);
int posix_memalign_errno = errno;
printf("posix_memalign:\n");
printf(" %p, return value: %d = %s,\n",
ptr_posix_memalign,
posix_memalign_return, strerror(posix_memalign_return));
/* strerror() can only be called once in a printf call for correct
* results */
printf(" errno: %d = %s\n",
posix_memalign_errno,
strerror(posix_memalign_errno));
printf("posix_memalign: ");
test_valid_aligned(ptr_posix_memalign, pow2_mul_voidptr_size, posix_memalign_return);
free(ptr_posix_memalign);
errno = 0;
void * ptr_posix_memalign_align0 = NULL;
int posix_memalign_align0_return = posix_memalign(&ptr_posix_memalign_align0, zero_size, sample_alloc_size);
int posix_memalign_align0_errno = errno;
printf("posix_memalign (alignment 0):\n");
printf(" %p, return value: %d = %s,\n",
ptr_posix_memalign_align0,
posix_memalign_align0_return,
strerror(posix_memalign_align0_return));
printf(" errno: %d = %s\n",
posix_memalign_align0_errno,
strerror(posix_memalign_align0_errno));
printf("posix_memalign (alignment 0): ");
test_invalid_aligned(ptr_posix_memalign_align0, posix_memalign_align0_return);
free(ptr_posix_memalign_align0);
errno = 0;
void * ptr_posix_memalign_nonpow2mul = NULL;
int posix_memalign_nonpow2mul_return = posix_memalign(&ptr_posix_memalign_nonpow2mul, nonpow2_mul_voidptr_size, sample_alloc_size);
int posix_memalign_nonpow2mul_errno = errno;
printf("posix_memalign (non-power-of-two multiple of sizeof(void *)):\n");
printf(" %p, return value: %d = %s,\n",
ptr_posix_memalign_nonpow2mul,
posix_memalign_nonpow2mul_return,
strerror(posix_memalign_nonpow2mul_return));
printf(" errno: %d = %s\n",
posix_memalign_nonpow2mul_errno,
strerror(posix_memalign_nonpow2mul_errno));
printf("posix_memalign (non-power-of-two multiple of sizeof(void *)): ");
test_invalid_aligned(ptr_posix_memalign_nonpow2mul, posix_memalign_nonpow2mul_return);
free(ptr_posix_memalign_nonpow2mul);
errno = 0;
void * ptr_posix_memalign_size0 = NULL;
int posix_memalign_size0_return = posix_memalign(&ptr_posix_memalign_size0, pow2_mul_voidptr_size, zero_size);
int posix_memalign_size0_errno = errno;
printf("posix_memalign (size 0):\n");
printf(" %p, return value: %d = %s,\n",
ptr_posix_memalign_size0,
posix_memalign_size0_return,
strerror(posix_memalign_size0_return));
printf(" errno: %d = %s\n",
posix_memalign_size0_errno,
strerror(posix_memalign_size0_errno));
printf("posix_memalign (size 0): ");
test_size_zero(ptr_posix_memalign_size0, pow2_mul_voidptr_size, posix_memalign_size0_return);
free(ptr_posix_memalign_size0);
errno = 0;
void * ptr_posix_memalign_maxsize = NULL;
int posix_memalign_maxsize_return = posix_memalign(&ptr_posix_memalign_maxsize, pow2_mul_voidptr_size, max_size);
int posix_memalign_maxsize_errno = errno;
printf("posix_memalign (SIZE_MAX):\n");
printf(" %p, return value: %d = %s,\n",
ptr_posix_memalign_maxsize,
posix_memalign_maxsize_return,
strerror(posix_memalign_maxsize_return));
printf(" errno: %d = %s\n",
posix_memalign_maxsize_errno,
strerror(posix_memalign_maxsize_errno));
printf("posix_memalign (SIZE_MAX): ");
test_cannot_alloc(ptr_posix_memalign_maxsize, posix_memalign_maxsize_return);
free(ptr_posix_memalign_maxsize);
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment