Add tests
This commit is contained in:
parent
df2ed3acda
commit
dd8ecf50db
@ -1,10 +1,20 @@
|
|||||||
SRC = $(wildcard *.c)
|
SRC = $(wildcard *.c)
|
||||||
OBJ = $(filter-out build/main.o build/test.o, $(SRC:%.c=build/%.o))
|
OBJ = $(filter-out build/main.o build/test.o, $(SRC:%.c=build/%.o))
|
||||||
FLAGS = -Wall -Wextra -g -O3
|
FLAGS = -Wall -Wextra -g -O3
|
||||||
|
TEST_SRCDIR = tests
|
||||||
|
|
||||||
all: build/test
|
FAIL_TESTS_SRC += $(wildcard $(TEST_SRCDIR)/fail/*.c)
|
||||||
|
SUCC_TESTS_SRC += $(wildcard $(TEST_SRCDIR)/success/*.c)
|
||||||
|
TESTS_OBJ = $(FAIL_TESTS_SRC:$(TEST_SRCDIR)/fail/%.c=build/test-fail-%) $(SUCC_TESTS_SRC:$(TEST_SRCDIR)/success/%.c=build/test-success-%)
|
||||||
|
|
||||||
build/test: test.c $(OBJ)
|
all: build-tests
|
||||||
|
|
||||||
|
build-tests: $(TESTS_OBJ)
|
||||||
|
|
||||||
|
build/test-fail-%: tests/fail/%.c $(OBJ)
|
||||||
|
gcc $^ -o $@ $(FLAGS)
|
||||||
|
|
||||||
|
build/test-success-%: tests/success/%.c $(OBJ)
|
||||||
gcc $^ -o $@ $(FLAGS)
|
gcc $^ -o $@ $(FLAGS)
|
||||||
|
|
||||||
build/%.o: %.c %.h
|
build/%.o: %.c %.h
|
||||||
|
25
TP/TP2/rendu/make.sh
Executable file
25
TP/TP2/rendu/make.sh
Executable file
@ -0,0 +1,25 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
test_failed () {
|
||||||
|
exit 1
|
||||||
|
}
|
||||||
|
|
||||||
|
make build-tests
|
||||||
|
|
||||||
|
for file in build/test-fail-*; do
|
||||||
|
echo "===== $file =====";
|
||||||
|
$file &>/dev/null
|
||||||
|
if [ $? -eq 0 ]; then
|
||||||
|
echo "$file should have failed"
|
||||||
|
test_failed
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
|
||||||
|
for file in build/test-success-*; do
|
||||||
|
echo "===== $file =====";
|
||||||
|
$file &>/dev/null
|
||||||
|
if [ $? -ne 0 ]; then
|
||||||
|
echo "$file failed"
|
||||||
|
test_failed
|
||||||
|
fi
|
||||||
|
done
|
48
TP/TP2/rendu/tests/fail/bad_copy.c
Normal file
48
TP/TP2/rendu/tests/fail/bad_copy.c
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
#include <string.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#include "../../vmap.h"
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
Memory* mem = memory_init(-1);
|
||||||
|
int pid1 = 12;
|
||||||
|
int pid2 = 24;
|
||||||
|
|
||||||
|
char* real1 = malloc(sizeof(char)*32);
|
||||||
|
real1[0] = 'h';
|
||||||
|
real1[1] = 'e';
|
||||||
|
real1[2] = 'l';
|
||||||
|
real1[3] = 'l';
|
||||||
|
real1[4] = 'o';
|
||||||
|
real1[5] = '!';
|
||||||
|
real1[6] = '\0';
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
char* data1 = my_malloc(mem, 32*sizeof(char), pid1);
|
||||||
|
char* data2 = my_malloc(mem, 32*sizeof(char), pid2);
|
||||||
|
|
||||||
|
vmap_copy_from_memory(mem, real1, data1, 8, pid1);
|
||||||
|
|
||||||
|
|
||||||
|
my_copy(mem, data1, data2, 32*sizeof(char), pid1); //! src and dst have different PIDs
|
||||||
|
|
||||||
|
char* real = malloc(sizeof(char)*32);
|
||||||
|
|
||||||
|
vmap_copy_to_memory(mem, data2, real, 7, pid2);
|
||||||
|
|
||||||
|
if (strcmp(real, real1)) {
|
||||||
|
printf("%d\n", strcmp(real, real1));
|
||||||
|
fprintf(stderr, "copy test failed\n");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
free(real);
|
||||||
|
free(real1);
|
||||||
|
|
||||||
|
my_free(mem, data1, pid1);
|
||||||
|
my_free(mem, data2, pid2);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
27
TP/TP2/rendu/tests/fail/bad_free.c
Normal file
27
TP/TP2/rendu/tests/fail/bad_free.c
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
#include <string.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#include "../../vmap.h"
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
Memory* mem = memory_init(-1);
|
||||||
|
int pid1 = 12;
|
||||||
|
int pid2 = 24;
|
||||||
|
|
||||||
|
char* real1 = malloc(sizeof(char)*32);
|
||||||
|
real1[0] = 'h';
|
||||||
|
real1[1] = 'e';
|
||||||
|
real1[2] = 'l';
|
||||||
|
real1[3] = 'l';
|
||||||
|
real1[4] = 'o';
|
||||||
|
real1[5] = '!';
|
||||||
|
real1[6] = '\0';
|
||||||
|
|
||||||
|
char* data1 = my_malloc(mem, 32*sizeof(char), pid1);
|
||||||
|
char* data2 = my_malloc(mem, 32*sizeof(char), pid2);
|
||||||
|
|
||||||
|
my_free(mem, data1, pid1);
|
||||||
|
my_free(mem, data2, pid1); //! bad PID
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
@ -1,11 +1,11 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
#include "vmap.h"
|
#include "../../vmap.h"
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
// Add more tests here
|
Memory* mem = memory_init(-1);
|
||||||
VMap* vmap = vmap_init();
|
int pid = 12;
|
||||||
|
|
||||||
char* real1 = malloc(sizeof(char)*32);
|
char* real1 = malloc(sizeof(char)*32);
|
||||||
real1[0] = 'h';
|
real1[0] = 'h';
|
||||||
@ -16,18 +16,18 @@ int main() {
|
|||||||
real1[5] = '!';
|
real1[5] = '!';
|
||||||
real1[6] = '\0';
|
real1[6] = '\0';
|
||||||
|
|
||||||
char* data1 = my_malloc(vmap, 32*sizeof(char));
|
char* data1 = my_malloc(mem, 32*sizeof(char), pid);
|
||||||
char* data2 = my_malloc(vmap, 32*sizeof(char));
|
char* data2 = my_malloc(mem, 32*sizeof(char), pid);
|
||||||
|
|
||||||
vmap_copy_from_memory(vmap, real1, data1, 8);
|
vmap_copy_from_memory(mem, real1, data1, 8, pid);
|
||||||
|
|
||||||
my_copy(vmap, data1, data2, 32*sizeof(char));
|
my_copy(mem, data1, data2, 32*sizeof(char), pid);
|
||||||
|
|
||||||
char* real = malloc(sizeof(char)*32);
|
char* real = malloc(sizeof(char)*32);
|
||||||
|
|
||||||
vmap_copy_to_memory(vmap, data2, real, 7);
|
vmap_copy_to_memory(mem, data2, real, 7, pid);
|
||||||
|
|
||||||
if (strcmp(real1, real1)) {
|
if (strcmp(real, real1)) {
|
||||||
printf("%d\n", strcmp(real, real1));
|
printf("%d\n", strcmp(real, real1));
|
||||||
fprintf(stderr, "copy test failed\n");
|
fprintf(stderr, "copy test failed\n");
|
||||||
return 1;
|
return 1;
|
||||||
@ -36,8 +36,8 @@ int main() {
|
|||||||
free(real);
|
free(real);
|
||||||
free(real1);
|
free(real1);
|
||||||
|
|
||||||
my_free(vmap, data1);
|
my_free(mem, data1, pid);
|
||||||
my_free(vmap, data2);
|
my_free(mem, data2, pid);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user