Skip to content
This repository has been archived by the owner on Jul 21, 2023. It is now read-only.

Commit

Permalink
Fix tests (#16)
Browse files Browse the repository at this point in the history
Co-authored-by: Diogo Cardoso <[email protected]>
  • Loading branch information
RafDevX and d-card authored Jan 14, 2022
1 parent 107ffe1 commit 235519e
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 36 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ TARGET_EXECS := tests/test1 tests/copy_to_external_simple tests/copy_to_external
vpath # clears VPATH
vpath %.h $(INCLUDE_DIRS)

LDFLAGS += -pthread -fsanitize=thread
LDFLAGS += -pthread

CFLAGS = -std=c11 -D_POSIX_C_SOURCE=200809L
CFLAGS += $(INCLUDES)
Expand Down
14 changes: 9 additions & 5 deletions tests/threads_read_same_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@
#include <pthread.h>
#include <string.h>

#define LEN 1555
#define BUFFER_SIZE 4000
#define THREADS 20

void *function(void *istr) {

char *str = (char *)istr;
char *path = "/f1";
char buffer[4000];
char buffer[BUFFER_SIZE];

int f;
ssize_t r;
Expand All @@ -32,7 +36,7 @@ void *function(void *istr) {
}

int main() {
char *str = (char *)malloc(sizeof(char) * 1555);
char str[LEN];
memset(str, 'A', 1555);
char *path = "/f1";

Expand All @@ -45,11 +49,11 @@ int main() {

r = tfs_write(f, str, strlen(str));
assert(r == strlen(str));
pthread_t tid[20];
for (int i = 0; i < 20; i++) {
pthread_t tid[THREADS];
for (int i = 0; i < THREADS; i++) {
pthread_create(&tid[i], NULL, &function, str);
}
for (int i = 0; i < 20; i++) {
for (int i = 0; i < THREADS; i++) {
pthread_join(tid[i], NULL);
}
printf("Successful test.\n");
Expand Down
75 changes: 45 additions & 30 deletions tests/threads_truncate_and_append.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
#include <string.h>

#define LEN 1555
#define THREADS 19

void *truncate(void *ipath) {

char str[LEN];
memset(str, 'A', LEN);
memset(str, 'C', LEN);
char *path = (char *)ipath;

int f;
Expand All @@ -28,7 +29,7 @@ void *truncate(void *ipath) {
void *append(void *ipath) {

char str[LEN];
memset(str, 'A', LEN);
memset(str, 'B', LEN);
char *path = (char *)ipath;

int f;
Expand All @@ -48,44 +49,58 @@ void *append(void *ipath) {
void write_to_file(char *path, char *contents) {
int f = tfs_open(path, TFS_O_CREAT);
assert(f != -1);
ssize_t r = tfs_write(f, contents, strlen(contents));
assert(r == strlen(contents));
ssize_t r = tfs_write(f, contents, LEN);
assert(r == LEN);
assert(tfs_close(f) != -1);
}

int main() {
char *path_append = "/append.txt";
char *path_trunc = "/trunc.txt";
char str[LEN];
memset(str, 'A', LEN);
char path[THREADS][10];
char strA[LEN];
memset(strA, 'A', LEN);
char strB[LEN];
memset(strB, 'B', LEN);
char strC[LEN];
memset(strC, 'C', LEN);
assert(tfs_init() != -1);

write_to_file(path_append, str);
write_to_file(path_trunc, str);

pthread_t tid[19];
for (int i = 0; i < 19; i++) {
if (i % 2 == 0)
pthread_create(&tid[i], NULL, &append, (void *)path_append);
else
pthread_create(&tid[i], NULL, &truncate, (void *)path_trunc);
for (int i = 0; i < THREADS; i++) {
sprintf(path[i], "/%d.txt", i);
write_to_file(path[i], strA);
}
for (int i = 0; i < 19; i++) {
pthread_join(tid[i], NULL);
pthread_t tid[THREADS];
for (int i = 0; i < THREADS; i++) {
if (i % 2 == 0) {
pthread_create(&tid[i], NULL, &append, (void *)path[i]);
} else {
pthread_create(&tid[i], NULL, &truncate, (void *)path[i]);
}
}

// TODO: check

/*f = tfs_open(path, 0);
assert(f != -1);
r = tfs_read(f, buffer, sizeof(buffer) - 1);
assert(r == strlen(str));
for (int i = 0; i < THREADS; i++) {
pthread_join(tid[i], NULL);

buffer[r] = '\0';
assert(strcmp(buffer, str) == 0);
int f = tfs_open(path[i], 0);
assert(f != -1);

char tmp[LEN];
if (i % 2 == 0) {
// append
assert(tfs_read(f, tmp, LEN) == LEN);
assert(memcmp(strA, tmp, LEN) == 0);
assert(tfs_read(f, tmp, LEN) == LEN);
assert(memcmp(strB, tmp, LEN) == 0);
assert(tfs_read(f, tmp, LEN) == 0);
} else {
// truncate
assert(tfs_read(f, tmp, LEN) == LEN);
assert(memcmp(strC, tmp, LEN) == 0);

assert(tfs_read(f, tmp, LEN) == 0);
}
assert(tfs_close(f) == 0);
}

assert(tfs_close(f) != -1);*/
assert(tfs_destroy() == 0);

printf("Successful test.\n");
return 0;
Expand Down
4 changes: 4 additions & 0 deletions tests/threads_write_different_files.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ void *function(void *ipath) {

assert(tfs_close(f) != -1);

free(str);

pthread_exit(NULL);
}

Expand All @@ -49,6 +51,8 @@ void *function2(void *ipath) {

assert(tfs_close(f) != -1);

free(str);

pthread_exit(NULL);
}

Expand Down
4 changes: 4 additions & 0 deletions tests/threads_write_same_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ void *function(void *ipath) {

assert(tfs_close(f) != -1);

free(str);

pthread_exit(NULL);
}

Expand Down Expand Up @@ -58,6 +60,8 @@ int main() {

assert(tfs_close(f) != -1);

free(str);

printf("Successful test.\n");
return 0;
}

0 comments on commit 235519e

Please sign in to comment.