mirror of https://github.com/ericonr/purr-c.git
Add test for mmap_file and simplify makefile.
Also fix typo in mmap_file.h.
This commit is contained in:
parent
06eb9d4222
commit
e01615adff
8
makefile
8
makefile
|
@ -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
|
OBJS = purr.o socket.o urls.o files.o comm.o formats.o encrypt.o mmap_file.o
|
||||||
|
|
||||||
TEST = tests
|
TEST = tests
|
||||||
TOBJS = tests.o formats.o urls.o
|
TOBJS = tests.o formats.o urls.o mmap_file.o
|
||||||
|
|
||||||
all: $(FINAL)
|
all: $(FINAL)
|
||||||
|
|
||||||
check: $(TEST)
|
check: $(TEST)
|
||||||
./tests
|
./tests
|
||||||
|
|
||||||
$(OBJS): $(HEADERS)
|
$(OBJS) $(TOBJS): $(HEADERS)
|
||||||
$(OBJS): CFLAGS += $(WARN) $(INC)
|
$(OBJS) $(TOBJS): CFLAGS += $(WARN)
|
||||||
|
encrypt.o: CFLAGS += $(INC)
|
||||||
|
|
||||||
purr: $(OBJS) $(LIBS)
|
purr: $(OBJS) $(LIBS)
|
||||||
tests: $(TOBJS) $(LIBS)
|
tests: $(TOBJS) $(LIBS)
|
||||||
|
|
||||||
|
|
|
@ -4,7 +4,7 @@
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <sys/mman.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 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);
|
#define CLOSE_MMAP(file) do{if((file).data != MAP_FAILED && (file).data != NULL) munmap((file).data, (file).size);}while(0);
|
||||||
|
|
||||||
|
|
19
tests.c
19
tests.c
|
@ -1,8 +1,11 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
|
#include <sys/random.h>
|
||||||
|
|
||||||
#include "purr.h"
|
#include "purr.h"
|
||||||
|
#include "mmap_file.h"
|
||||||
|
|
||||||
static int compare_strings(const char *expected, const char *result, const char *function)
|
static int compare_strings(const char *expected, const char *result, const char *function)
|
||||||
{
|
{
|
||||||
|
@ -81,5 +84,21 @@ int main()
|
||||||
assert(err == 0);
|
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;
|
return rv;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue