| 1 | #include <stdio.h> |
|---|
| 2 | #include <string.h> |
|---|
| 3 | #include <stdlib.h> |
|---|
| 4 | #include <kernel/OS.h> |
|---|
| 5 | |
|---|
| 6 | char testbuffer[] = "thisisatestthisisatestthisisatestthisisatestthisisatest" |
|---|
| 7 | "thisisatestthisisatestthisisatestthisisatestthisisatestthisisatest" |
|---|
| 8 | "thisisatestthisisatestthisisatestthisisatestthisisatestthisisatest" |
|---|
| 9 | "thisisatestthisisatestthisisatestthisisatestthisisatestthisisatest" |
|---|
| 10 | "thisisatestthisisatestthisisatestthisisatestthisisatestthisisatest" |
|---|
| 11 | "thisisatestthisisatestthisisatestthisisatestthisisatestthisisatest"; |
|---|
| 12 | |
|---|
| 13 | static status_t |
|---|
| 14 | rw_thread(void *arg) |
|---|
| 15 | { |
|---|
| 16 | int threadnum = (int)arg; |
|---|
| 17 | size_t result = 0; |
|---|
| 18 | char filename[64]; |
|---|
| 19 | char readbuffer[sizeof(testbuffer)]; |
|---|
| 20 | |
|---|
| 21 | snprintf(filename, sizeof(filename), "/boot/home/rw_test_%d", threadnum); |
|---|
| 22 | for (int i = 0; i < 100; i++) { |
|---|
| 23 | FILE *testfile = fopen(filename, "w"); |
|---|
| 24 | if (testfile != NULL) { |
|---|
| 25 | for (int j = 0; j < 100; j++) { |
|---|
| 26 | result = fwrite(testbuffer, 1, sizeof(testbuffer), testfile); |
|---|
| 27 | if (result < sizeof(testbuffer)) { |
|---|
| 28 | fprintf(stderr, "Thread %ld: write error - wrote fewer bytes than expected (actual: %ld, expected: %ld\n", threadnum, result, sizeof(testbuffer)); |
|---|
| 29 | exit(8); |
|---|
| 30 | } |
|---|
| 31 | } |
|---|
| 32 | fclose(testfile); |
|---|
| 33 | |
|---|
| 34 | testfile = fopen(filename, "r"); |
|---|
| 35 | if (testfile != NULL) { |
|---|
| 36 | for (int k = 0; k < 100; k++) { |
|---|
| 37 | memset(readbuffer, 0, sizeof(readbuffer)); |
|---|
| 38 | result = fread(readbuffer, 1, sizeof(readbuffer), testfile); |
|---|
| 39 | if (feof(testfile)) |
|---|
| 40 | break; |
|---|
| 41 | else if (result < sizeof(readbuffer)) { |
|---|
| 42 | fprintf(stderr, "Thread %ld: read: size less than expected (actual: %ld, expected: %ld)\n", threadnum, result, sizeof(readbuffer)); |
|---|
| 43 | exit(8); |
|---|
| 44 | } else |
|---|
| 45 | if (memcmp(readbuffer, testbuffer, sizeof(testbuffer))) |
|---|
| 46 | fprintf(stderr, "Thread %ld: Data returned from read mismatched\n", threadnum); |
|---|
| 47 | } |
|---|
| 48 | fclose(testfile); |
|---|
| 49 | } |
|---|
| 50 | } |
|---|
| 51 | } |
|---|
| 52 | |
|---|
| 53 | return B_OK; |
|---|
| 54 | } |
|---|
| 55 | |
|---|
| 56 | int main(int argc, char **argv) |
|---|
| 57 | { |
|---|
| 58 | int thread_count = 1; |
|---|
| 59 | status_t result; |
|---|
| 60 | |
|---|
| 61 | if (argc == 2) |
|---|
| 62 | thread_count = atoi(argv[1]); |
|---|
| 63 | |
|---|
| 64 | thread_id threads[thread_count]; |
|---|
| 65 | |
|---|
| 66 | for (int i = 0; i < thread_count; i++) |
|---|
| 67 | threads[i] = spawn_thread(rw_thread, "Reader/Writer test", B_NORMAL_PRIORITY, (void *)i); |
|---|
| 68 | |
|---|
| 69 | for (int j = 0; j < thread_count; j++) |
|---|
| 70 | wait_for_thread(threads[j], &result); |
|---|
| 71 | |
|---|
| 72 | printf("Test complete!\n"); |
|---|
| 73 | |
|---|
| 74 | return 0; |
|---|
| 75 | } |
|---|