00001
00002
00003
00004 #include <random.h>
00005 #include <syscall.h>
00006 #include "tests/lib.h"
00007 #include "tests/main.h"
00008
00009 #define FILE_SIZE 8143
00010 static char buf_a[FILE_SIZE];
00011 static char buf_b[FILE_SIZE];
00012
00013 static void
00014 write_some_bytes (const char *file_name, int fd, const char *buf, size_t *ofs)
00015 {
00016 if (*ofs < FILE_SIZE)
00017 {
00018 size_t block_size = random_ulong () % (FILE_SIZE / 8) + 1;
00019 size_t ret_val;
00020 if (block_size > FILE_SIZE - *ofs)
00021 block_size = FILE_SIZE - *ofs;
00022
00023 ret_val = write (fd, buf + *ofs, block_size);
00024 if (ret_val != block_size)
00025 fail ("write %zu bytes at offset %zu in \"%s\" returned %zu",
00026 block_size, *ofs, file_name, ret_val);
00027 *ofs += block_size;
00028 }
00029 }
00030
00031 void
00032 test_main (void)
00033 {
00034 int fd_a, fd_b;
00035 size_t ofs_a = 0, ofs_b = 0;
00036
00037 random_init (0);
00038 random_bytes (buf_a, sizeof buf_a);
00039 random_bytes (buf_b, sizeof buf_b);
00040
00041 CHECK (create ("a", 0), "create \"a\"");
00042 CHECK (create ("b", 0), "create \"b\"");
00043
00044 CHECK ((fd_a = open ("a")) > 1, "open \"a\"");
00045 CHECK ((fd_b = open ("b")) > 1, "open \"b\"");
00046
00047 msg ("write \"a\" and \"b\" alternately");
00048 while (ofs_a < FILE_SIZE || ofs_b < FILE_SIZE)
00049 {
00050 write_some_bytes ("a", fd_a, buf_a, &ofs_a);
00051 write_some_bytes ("b", fd_b, buf_b, &ofs_b);
00052 }
00053
00054 msg ("close \"a\"");
00055 close (fd_a);
00056
00057 msg ("close \"b\"");
00058 close (fd_b);
00059
00060 check_file ("a", buf_a, FILE_SIZE);
00061 check_file ("b", buf_b, FILE_SIZE);
00062 }