Add test for mmap_file and simplify makefile.

Also fix typo in mmap_file.h.
This commit is contained in:
Érico Rolim 2020-09-14 00:43:59 -03:00
parent 06eb9d4222
commit e01615adff
3 changed files with 25 additions and 4 deletions

View File

@ -15,15 +15,17 @@ HEADERS = purr.h mmap_file.h
OBJS = purr.o socket.o urls.o files.o comm.o formats.o encrypt.o mmap_file.o
TEST = tests
TOBJS = tests.o formats.o urls.o
TOBJS = tests.o formats.o urls.o mmap_file.o
all: $(FINAL)
check: $(TEST)
./tests
$(OBJS): $(HEADERS)
$(OBJS): CFLAGS += $(WARN) $(INC)
$(OBJS) $(TOBJS): $(HEADERS)
$(OBJS) $(TOBJS): CFLAGS += $(WARN)
encrypt.o: CFLAGS += $(INC)
purr: $(OBJS) $(LIBS)
tests: $(TOBJS) $(LIBS)

View File

@ -4,7 +4,7 @@
#include <stdint.h>
#include <sys/mman.h>
#define RESET_MMAP(file) do{(file).offset = 0; (file).cursor = 0}while(0);
#define RESET_MMAP(file) do{(file).offset = 0; (file).cursor = 0;}while(0);
#define ERROR_MMAP(file) ((file).data == MAP_FAILED || (file).data == NULL)
#define CLOSE_MMAP(file) do{if((file).data != MAP_FAILED && (file).data != NULL) munmap((file).data, (file).size);}while(0);

19
tests.c
View File

@ -1,8 +1,11 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <sys/random.h>
#include "purr.h"
#include "mmap_file.h"
static int compare_strings(const char *expected, const char *result, const char *function)
{
@ -81,5 +84,21 @@ int main()
assert(err == 0);
}
{
/* mmap_file.c */
int write_size = 1024 * 1024;
struct mmap_file f = {.size = 2 * write_size, .prot = PROT_MEM, .flags = MAP_MEM};
f.data = mmap(NULL, f.size, f.prot, f.flags, -1, 0);
assert(!ERROR_MMAP(f));
uint8_t *data = malloc(write_size);
getrandom(data, write_size, 0);
assert(data);
int written = write_into_mmap(&f, data, write_size);
assert(f.offset == written);
RESET_MMAP(f);
read_from_mmap(&f, write_size);
rv = compare_arrays(data, f.data, write_size, "{write_into,read_from}_mmap") ? 1 : rv;
}
return rv;
}