895b65884694b41061b79996093add1fa49d1550
[pintos-anon] / grading / filesys / random.inc
1 /* -*- c -*- */
2
3 #include <random.h>
4 #include <stdio.h>
5 #include <string.h>
6 #include <syscall.h>
7 #include "fslib.h"
8
9 #if TEST_SIZE % BLOCK_SIZE != 0
10 #error TEST_SIZE must be a multiple of BLOCK_SIZE
11 #endif
12
13 #define BLOCK_CNT (TEST_SIZE / BLOCK_SIZE)
14
15 char buf[TEST_SIZE];
16 int order[BLOCK_CNT];
17
18 void
19 test_main (void) 
20 {
21   const char *filename = "bazzle";
22   int fd;
23   size_t i;
24
25   random_init (57);
26   random_bytes (buf, sizeof buf);
27
28   for (i = 0; i < BLOCK_CNT; i++)
29     order[i] = i;
30
31   CHECK (create (filename, TEST_SIZE), "create \"%s\"", filename);
32   CHECK ((fd = open (filename)) > 1, "open \"%s\"", filename);
33
34   msg ("write \"%s\" in random order", filename);
35   shuffle (order, BLOCK_CNT, sizeof *order);
36   for (i = 0; i < BLOCK_CNT; i++) 
37     {
38       size_t ofs = BLOCK_SIZE * order[i];
39       seek (fd, ofs);
40       if (write (fd, buf + ofs, BLOCK_SIZE) <= 0)
41         fail ("write %d bytes at offset %zu failed", (int) BLOCK_SIZE, ofs);
42     }
43
44   msg ("read \"%s\" in random order", filename);
45   shuffle (order, BLOCK_CNT, sizeof *order);
46   for (i = 0; i < BLOCK_CNT; i++) 
47     {
48       char block[BLOCK_SIZE];
49       size_t ofs = BLOCK_SIZE * order[i];
50       seek (fd, ofs);
51       if (read (fd, block, BLOCK_SIZE) <= 0)
52         fail ("read %d bytes at offset %zu failed", (int) BLOCK_SIZE, ofs);
53       compare_bytes (block, buf + ofs, BLOCK_SIZE, ofs, filename);
54     }
55
56   msg ("close \"%s\"", filename);
57   close (fd);
58 }