std_allocator and testing
This commit is contained in:
+1
-24
@@ -31,8 +31,6 @@ extern const allocator_t allocator_stdlib;
|
|||||||
|
|
||||||
#ifdef ALLOCATOR_IMPLEMENTATION
|
#ifdef ALLOCATOR_IMPLEMENTATION
|
||||||
|
|
||||||
// convenience functions ///////////////////////////////////////////////////////
|
|
||||||
|
|
||||||
void *allocator_alloc_func(allocator_t this, size_t bytes, const char *file, int line) {
|
void *allocator_alloc_func(allocator_t this, size_t bytes, const char *file, int line) {
|
||||||
return this.vtbl->alloc(this.this, bytes, file, line);
|
return this.vtbl->alloc(this.this, bytes, file, line);
|
||||||
}
|
}
|
||||||
@@ -43,28 +41,7 @@ void allocator_free_func(allocator_t this, void *ptr, const char *file, int line
|
|||||||
this.vtbl->free(this.this, ptr, file, line);
|
this.vtbl->free(this.this, ptr, file, line);
|
||||||
}
|
}
|
||||||
|
|
||||||
// allocator_stdlib ////////////////////////////////////////////////////////////
|
#include "std_allocator.c"
|
||||||
|
|
||||||
static void *stdlib_alloc(void *_, size_t bytes, const char *_, int _) {
|
|
||||||
return malloc(bytes);
|
|
||||||
}
|
|
||||||
static void *stdlib_resize(void *_, void *old_ptr, size_t bytes, const char *_, int _) {
|
|
||||||
return realloc(old_ptr, bytes);
|
|
||||||
}
|
|
||||||
static void stdlib_free(void *_, void *ptr, const char *_, int _) {
|
|
||||||
free(ptr);
|
|
||||||
}
|
|
||||||
|
|
||||||
static const allocator_vtbl_t stdlib_vtbl = {
|
|
||||||
.alloc = stdlib_alloc,
|
|
||||||
.resize = stdlib_resize,
|
|
||||||
.free = stdlib_free,
|
|
||||||
};
|
|
||||||
|
|
||||||
const allocator_t allocator_stdlib = (allocator_t) {
|
|
||||||
.this=NULL,
|
|
||||||
.vtbl=&stdlib_vtbl,
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif // ALLOCATOR_IMPLEMENTATION
|
#endif // ALLOCATOR_IMPLEMENTATION
|
||||||
#endif // ALLOCATOR_H
|
#endif // ALLOCATOR_H
|
||||||
|
|||||||
@@ -0,0 +1,24 @@
|
|||||||
|
# Makefile
|
||||||
|
|
||||||
|
# Compiler and flags
|
||||||
|
CC := gcc
|
||||||
|
CFLAGS := -Wall -Wextra -O2
|
||||||
|
LDFLAGS := -lcriterion
|
||||||
|
|
||||||
|
# Output binary
|
||||||
|
TESTBIN := /tmp/all_tests
|
||||||
|
|
||||||
|
# Phony target
|
||||||
|
.PHONY: test
|
||||||
|
|
||||||
|
test:
|
||||||
|
@echo "Discovering test files..."
|
||||||
|
@files=$$(find . -type f -name 'test*.c'); \
|
||||||
|
if [ -z "$$files" ]; then \
|
||||||
|
echo "No test files found!"; \
|
||||||
|
else \
|
||||||
|
echo "Compiling all test files into $(TESTBIN)..."; \
|
||||||
|
$(CC) $(CFLAGS) $$files -o $(TESTBIN) $(LDFLAGS); \
|
||||||
|
echo "Running tests..."; \
|
||||||
|
$(TESTBIN); \
|
||||||
|
fi
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
#include "allocator.h"
|
||||||
|
|
||||||
|
static void *stdlib_alloc(void *this, size_t bytes, const char *file, int line) {
|
||||||
|
(void)this;
|
||||||
|
(void)file;
|
||||||
|
(void)line;
|
||||||
|
return malloc(bytes);
|
||||||
|
}
|
||||||
|
static void *stdlib_resize(void *this, void *old_ptr, size_t bytes, const char *file, int line) {
|
||||||
|
(void)this;
|
||||||
|
(void)file;
|
||||||
|
(void)line;
|
||||||
|
return realloc(old_ptr, bytes);
|
||||||
|
}
|
||||||
|
static void stdlib_free(void *this, void *ptr, const char *file, int line) {
|
||||||
|
(void)this;
|
||||||
|
(void)file;
|
||||||
|
(void)line;
|
||||||
|
free(ptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
static const allocator_vtbl_t stdlib_vtbl = {
|
||||||
|
.alloc = stdlib_alloc,
|
||||||
|
.resize = stdlib_resize,
|
||||||
|
.free = stdlib_free,
|
||||||
|
};
|
||||||
|
|
||||||
|
const allocator_t allocator_stdlib = (allocator_t) {
|
||||||
|
.this=NULL,
|
||||||
|
.vtbl=&stdlib_vtbl,
|
||||||
|
};
|
||||||
|
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
#include <criterion/criterion.h>
|
||||||
|
#define ALLOCATOR_IMPLEMENTATION
|
||||||
|
#include "allocator.h"
|
||||||
|
|
||||||
|
Test(std_allocator, test) {
|
||||||
|
allocator_t this = allocator_stdlib;
|
||||||
|
void *ptr = allocator_alloc(this, 10);
|
||||||
|
cr_assert(ptr != ((void *)0), "non null from malloc");
|
||||||
|
void *new_ptr = allocator_resize(this, ptr, 1024*1024*500);
|
||||||
|
cr_assert(new_ptr != ((void *)0), "non null from realloc");
|
||||||
|
cr_assert(new_ptr != ptr, "realloc is not the same ptr as malloc");
|
||||||
|
|
||||||
|
allocator_free(this, new_ptr);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user