Initial commit
This commit is contained in:
11
tests/01_malloc.c
Normal file
11
tests/01_malloc.c
Normal file
@@ -0,0 +1,11 @@
|
||||
#define DEBUG
|
||||
#include <stdlib.h>
|
||||
#include "../include/mem.h"
|
||||
|
||||
int main(void){
|
||||
size_t nbytes = 20;
|
||||
void *ptr = ALLOC(0, nbytes);
|
||||
ASSERTED(ptr != NULL);
|
||||
ASSERTED( mem_is_zero(ptr, nbytes));
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
11
tests/02_calloc.c
Normal file
11
tests/02_calloc.c
Normal file
@@ -0,0 +1,11 @@
|
||||
#define DEBUG
|
||||
#include <stdlib.h>
|
||||
#include "../include/mem.h"
|
||||
|
||||
int main(void){
|
||||
size_t nbytes = 20;
|
||||
void *ptr = ALLOC(NOZERO, nbytes);
|
||||
ASSERTED(ptr != NULL);
|
||||
ASSERTED( !mem_is_zero(ptr, nbytes));
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
11
tests/03_malloc_softfail.c
Normal file
11
tests/03_malloc_softfail.c
Normal file
@@ -0,0 +1,11 @@
|
||||
#define DEBUG
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include "../include/mem.h"
|
||||
|
||||
int main(void){
|
||||
size_t nbytes = MEM_GB(1024);
|
||||
void *ptr = ALLOC(SOFT_FAIL, nbytes);
|
||||
ASSERTED(ptr == NULL);
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
28
tests/04_malloc_exception.c
Normal file
28
tests/04_malloc_exception.c
Normal file
@@ -0,0 +1,28 @@
|
||||
#define DEBUG
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include "../include/mem.h"
|
||||
|
||||
int main(void){
|
||||
size_t nbytes = MEM_GB(1024);
|
||||
void *buf;
|
||||
TRY {
|
||||
buf = ALLOC(0,nbytes); /* try 1GB */
|
||||
if (!buf)
|
||||
RAISE(OOM);
|
||||
free(buf);
|
||||
}
|
||||
EXCEPT(OOM) {
|
||||
/* handle memory failure gracefully */
|
||||
fprintf(stderr, "Caught: %s\n", OOM.reason);
|
||||
return EXIT_SUCCESS; /* requested behavior */
|
||||
}
|
||||
FINALLY {
|
||||
/* cleanup if needed, runs always */
|
||||
if (buf) free(buf);
|
||||
}
|
||||
END_TRY;
|
||||
|
||||
return EXIT_FAILURE; /* shouldn't reach here for this example */
|
||||
}
|
||||
Reference in New Issue
Block a user