Make tests public. Rewrite most tests. Add tests.
[pintos-anon] / src / tests / filesys / extended / grow-two-files.c
1 #include <random.h>
2 #include <syscall.h>
3 #include "tests/lib.h"
4 #include "tests/main.h"
5
6 #define FILE_SIZE 8143
7 static char buf_a[FILE_SIZE];
8 static char buf_b[FILE_SIZE];
9
10 static void
11 write_some_bytes (const char *filename, int fd, const char *buf, size_t *ofs) 
12 {
13   if (*ofs < FILE_SIZE) 
14     {
15       size_t block_size = random_ulong () % (FILE_SIZE / 8) + 1;
16       size_t ret_val;
17       if (block_size > FILE_SIZE - *ofs)
18         block_size = FILE_SIZE - *ofs;
19
20       ret_val = write (fd, buf + *ofs, block_size);
21       if (ret_val != block_size)
22         fail ("write %zu bytes at offset %zu in \"%s\" returned %zu",
23               block_size, *ofs, filename, ret_val);
24       *ofs += block_size;
25     }
26 }
27
28 void
29 test_main (void) 
30 {
31   int fd_a, fd_b;
32   size_t ofs_a = 0, ofs_b = 0;
33
34   random_init (0);
35   random_bytes (buf_a, sizeof buf_a);
36   random_bytes (buf_b, sizeof buf_b);
37
38   CHECK (create ("a", 0), "create \"a\"");
39   CHECK (create ("b", 0), "create \"b\"");
40
41   CHECK ((fd_a = open ("a")) > 1, "open \"a\"");
42   CHECK ((fd_b = open ("b")) > 1, "open \"b\"");
43
44   msg ("write \"a\" and \"b\" alternately");
45   while (ofs_a < FILE_SIZE || ofs_b < FILE_SIZE) 
46     {
47       write_some_bytes ("a", fd_a, buf_a, &ofs_a);
48       write_some_bytes ("b", fd_b, buf_b, &ofs_b);
49     }
50
51   msg ("close \"a\"");
52   close (fd_a);
53
54   msg ("close \"b\"");
55   close (fd_b);
56
57   check_file ("a", buf_a, FILE_SIZE);
58   check_file ("b", buf_b, FILE_SIZE);
59 }