diff --git a/Makefile b/Makefile index 91ccd67..ef5d3e2 100644 --- a/Makefile +++ b/Makefile @@ -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) diff --git a/tests/threads_read_same_file.c b/tests/threads_read_same_file.c index a66d3d8..3eef91d 100644 --- a/tests/threads_read_same_file.c +++ b/tests/threads_read_same_file.c @@ -3,11 +3,15 @@ #include #include +#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; @@ -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"; @@ -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"); diff --git a/tests/threads_truncate_and_append.c b/tests/threads_truncate_and_append.c index c3cb3b8..b5305c7 100644 --- a/tests/threads_truncate_and_append.c +++ b/tests/threads_truncate_and_append.c @@ -4,11 +4,12 @@ #include #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; @@ -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; @@ -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; diff --git a/tests/threads_write_different_files.c b/tests/threads_write_different_files.c index c77cf76..9e65796 100644 --- a/tests/threads_write_different_files.c +++ b/tests/threads_write_different_files.c @@ -24,6 +24,8 @@ void *function(void *ipath) { assert(tfs_close(f) != -1); + free(str); + pthread_exit(NULL); } @@ -49,6 +51,8 @@ void *function2(void *ipath) { assert(tfs_close(f) != -1); + free(str); + pthread_exit(NULL); } diff --git a/tests/threads_write_same_file.c b/tests/threads_write_same_file.c index 3471810..705e5f2 100644 --- a/tests/threads_write_same_file.c +++ b/tests/threads_write_same_file.c @@ -20,6 +20,8 @@ void *function(void *ipath) { assert(tfs_close(f) != -1); + free(str); + pthread_exit(NULL); } @@ -58,6 +60,8 @@ int main() { assert(tfs_close(f) != -1); + free(str); + printf("Successful test.\n"); return 0; } \ No newline at end of file