11 vmsg (const char *format, va_list args)
13 printf ("(%s) ", test_name);
14 vprintf (format, args);
18 msg (const char *format, ...)
24 va_start (args, format);
31 fail (const char *format, ...)
35 va_start (args, format);
37 printf (": FAILED\n");
44 check (bool success, const char *format, ...)
48 va_start (args, format);
60 printf (": FAILED\n");
67 seq_test (const char *filename, void *buf, size_t size, size_t initial_size,
68 size_t (*block_size_func) (void),
69 void (*check_func) (int fd, long ofs))
74 random_bytes (buf, size);
75 check (create (filename, initial_size), "create \"%s\"", filename);
76 check ((fd = open (filename)) > 1, "open \"%s\"", filename);
79 msg ("writing \"%s\"", filename);
82 size_t block_size = block_size_func ();
83 if (block_size > size - ofs)
84 block_size = size - ofs;
86 if (write (fd, buf + ofs, block_size) <= 0)
87 fail ("write %zu bytes at offset %zu in \"%s\" failed",
88 block_size, ofs, filename);
91 if (check_func != NULL)
94 msg ("close \"%s\"", filename);
96 check_file (filename, buf, size);
100 swap (void *a_, void *b_, size_t size)
106 for (i = 0; i < size; i++)
115 shuffle (void *buf_, size_t cnt, size_t size)
120 for (i = 0; i < cnt; i++)
122 size_t j = i + random_ulong () % (cnt - i);
123 swap (buf + i * size, buf + j * size, size);
128 check_file (const char *filename, const void *buf_, size_t size)
130 const char *buf = buf_;
135 check ((fd = open (filename)) > 1, "open \"%s\" for verification", filename);
140 size_t block_size = size - ofs;
141 if (block_size > sizeof block)
142 block_size = sizeof block;
144 if (read (fd, block, block_size) <= 0)
145 fail ("read %zu bytes at offset %zu in \"%s\" failed",
146 block_size, ofs, filename);
148 if (memcmp (buf + ofs, block, block_size))
150 if (block_size <= 512)
152 printf ("Expected data:\n");
153 hex_dump (ofs, buf + ofs, block_size, false);
154 printf ("Actually read data:\n");
155 hex_dump (ofs, block, block_size, false);
157 fail ("%zu bytes at offset %zu differed from expected",
164 msg ("close \"%s\"", filename);