summaryrefslogtreecommitdiffstats
path: root/tests/filesys/base/random.inc
diff options
context:
space:
mode:
Diffstat (limited to 'tests/filesys/base/random.inc')
-rw-r--r--tests/filesys/base/random.inc59
1 files changed, 59 insertions, 0 deletions
diff --git a/tests/filesys/base/random.inc b/tests/filesys/base/random.inc
new file mode 100644
index 0000000..eeeea68
--- /dev/null
+++ b/tests/filesys/base/random.inc
@@ -0,0 +1,59 @@
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
16char buf[TEST_SIZE];
17int order[BLOCK_CNT];
18
19void
20test_main (void)
21{
22 const char *file_name = "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 (file_name, TEST_SIZE), "create \"%s\"", file_name);
33 CHECK ((fd = open (file_name)) > 1, "open \"%s\"", file_name);
34
35 msg ("write \"%s\" in random order", file_name);
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", file_name);
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, file_name);
55 }
56
57 msg ("close \"%s\"", file_name);
58 close (fd);
59}