~jkaivo/mapalloc

d5ecd8fe69115c8587e16a2110bbb92790050b52 — Jakob Kaivo 2 years ago 6e97e2f
add some simple tests
4 files changed, 53 insertions(+), 1 deletions(-)

M Makefile
A test/overflow.c
A test/underflow.c
A test/zero.c
M Makefile => Makefile +14 -1
@@ 1,15 1,20 @@
.POSIX:

BINDIR=bin
LIBDIR=lib
OBJDIR=obj
SRCDIR=src
INCDIR=include
TESTDIR=test
CFLAGS=-I$(INCDIR) -Wall -Wextra -Wpedantic -g

OBJECTS=$(OBJDIR)/mapalloc.o
TESTS=$(BINDIR)/overflow $(BINDIR)/underflow $(BINDIR)/zero

all: $(LIBDIR)/libmapalloc.a $(LIBDIR)/libmapalloc.so

tests: $(TESTS)

$(OBJDIR)/mapalloc.o: $(SRCDIR)/mapalloc.c $(INCDIR)/mapalloc.h
	@mkdir -p $(@D)
	$(CC) -c -o $@ $(CFLAGS) $(SRCDIR)/$(*F).c


@@ 22,5 27,13 @@ $(LIBDIR)/libmapalloc.so: $(OBJECTS)
	@mkdir -p $(@D)
	$(CC) -o $@ -shared $(OBJECTS)

$(BINDIR)/overflow: $(TESTDIR)/overflow.c $(LIBDIR)/libmapalloc.a
$(BINDIR)/underflow: $(TESTDIR)/underflow.c $(LIBDIR)/libmapalloc.a
$(BINDIR)/zero: $(TESTDIR)/zero.c $(LIBDIR)/libmapalloc.a

$(TESTS):
	@mkdir -p $(@D)
	$(CC) -o $@ $(CFLAGS) $(TESTDIR)/$(@F).c -L$(LIBDIR) -lmapalloc

clean:
	$(RM) -rf $(LIBDIR) $(OBJDIR)
	$(RM) -rf $(LIBDIR) $(OBJDIR) $(BINDIR)

A test/overflow.c => test/overflow.c +13 -0
@@ 0,0 1,13 @@
#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
#include <unistd.h>
#include "mapalloc.h"

int main(void)
{
	char *ptr = map_malloc(1);
	long pagesize = sysconf(_SC_PAGESIZE);
	printf("ptr: %p, pagesize %ld\n", ptr, pagesize);
	ptr[pagesize] = '\0';
	printf("shouldn't get here\n");
}

A test/underflow.c => test/underflow.c +13 -0
@@ 0,0 1,13 @@
#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
#include <unistd.h>
#include "mapalloc.h"

int main(void)
{
	char *ptr = map_malloc(1);
	long pagesize = sysconf(_SC_PAGESIZE);
	printf("ptr: %p, pagesize %ld\n", ptr, pagesize);
	ptr[-1] = '\0';
	printf("shouldn't get here\n");
}

A test/zero.c => test/zero.c +13 -0
@@ 0,0 1,13 @@
#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
#include <unistd.h>
#include "mapalloc.h"

int main(void)
{
	char *ptr = map_malloc(0);
	long pagesize = sysconf(_SC_PAGESIZE);
	printf("ptr: %p, pagesize %ld\n", ptr, pagesize);
	ptr[0] = '\0';
	printf("shouldn't get here\n");
}