00001
00002
00003 #include <random.h>
00004 #include <stdio.h>
00005 #include <string.h>
00006 #include <syscall.h>
00007 #include "tests/lib.h"
00008 #include "tests/main.h"
00009
00010 #if TEST_SIZE % BLOCK_SIZE != 0
00011 #error TEST_SIZE must be a multiple of BLOCK_SIZE
00012 #endif
00013
00014 #define BLOCK_CNT (TEST_SIZE / BLOCK_SIZE)
00015
00016 char buf[TEST_SIZE];
00017 int order[BLOCK_CNT];
00018
00019 void
00020 test_main (void)
00021 {
00022 const char *file_name = "bazzle";
00023 int fd;
00024 size_t i;
00025
00026 random_init (57);
00027 random_bytes (buf, sizeof buf);
00028
00029 for (i = 0; i < BLOCK_CNT; i++)
00030 order[i] = i;
00031
00032 CHECK (create (file_name, TEST_SIZE), "create \"%s\"", file_name);
00033 CHECK ((fd = open (file_name)) > 1, "open \"%s\"", file_name);
00034
00035 msg ("write \"%s\" in random order", file_name);
00036 shuffle (order, BLOCK_CNT, sizeof *order);
00037 for (i = 0; i < BLOCK_CNT; i++)
00038 {
00039 size_t ofs = BLOCK_SIZE * order[i];
00040 seek (fd, ofs);
00041 if (write (fd, buf + ofs, BLOCK_SIZE) != BLOCK_SIZE)
00042 fail ("write %d bytes at offset %zu failed", (int) BLOCK_SIZE, ofs);
00043 }
00044
00045 msg ("read \"%s\" in random order", file_name);
00046 shuffle (order, BLOCK_CNT, sizeof *order);
00047 for (i = 0; i < BLOCK_CNT; i++)
00048 {
00049 char block[BLOCK_SIZE];
00050 size_t ofs = BLOCK_SIZE * order[i];
00051 seek (fd, ofs);
00052 if (read (fd, block, BLOCK_SIZE) != BLOCK_SIZE)
00053 fail ("read %d bytes at offset %zu failed", (int) BLOCK_SIZE, ofs);
00054 compare_bytes (block, buf + ofs, BLOCK_SIZE, ofs, file_name);
00055 }
00056
00057 msg ("close \"%s\"", file_name);
00058 close (fd);
00059 }