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