Skip to content

Commit 8a95fdf

Browse files
committed
Added file read/write tests and some framework updates
1 parent a711675 commit 8a95fdf

File tree

5 files changed

+147
-17
lines changed

5 files changed

+147
-17
lines changed

emubd/lfs_emubd.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ int lfs_emubd_read(lfs_emubd_t *emu, lfs_block_t block,
8888

8989
// Iterate over blocks until enough data is read
9090
while (size > 0) {
91-
snprintf(emu->child, LFS_NAME_MAX, "%d", block);
91+
snprintf(emu->child, LFS_NAME_MAX, "%x", block);
9292
size_t count = lfs_min(emu->info.erase_size - off, size);
9393

9494
FILE *f = fopen(emu->path, "rb");
@@ -137,7 +137,7 @@ int lfs_emubd_prog(lfs_emubd_t *emu, lfs_block_t block,
137137

138138
// Iterate over blocks until enough data is read
139139
while (size > 0) {
140-
snprintf(emu->child, LFS_NAME_MAX, "%d", block);
140+
snprintf(emu->child, LFS_NAME_MAX, "%x", block);
141141
size_t count = lfs_min(emu->info.erase_size - off, size);
142142

143143
FILE *f = fopen(emu->path, "r+b");
@@ -186,7 +186,7 @@ int lfs_emubd_erase(lfs_emubd_t *emu, lfs_block_t block,
186186

187187
// Iterate and erase blocks
188188
while (size > 0) {
189-
snprintf(emu->child, LFS_NAME_MAX, "%d", block);
189+
snprintf(emu->child, LFS_NAME_MAX, "%x", block);
190190
struct stat st;
191191
int err = stat(emu->path, &st);
192192
if (err && errno != ENOENT) {

tests/template.fmt

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,31 @@
55
#include <string.h>
66
#include <stdlib.h>
77

8+
89
// test stuff
910
void test_log(const char *s, uintmax_t v) {{
1011
printf("%s: %jd\n", s, v);
1112
}}
1213

13-
void test_assert(const char *s, uintmax_t v, uintmax_t e) {{
14-
test_log(s, v);
14+
void test_assert(const char *file, unsigned line,
15+
const char *s, uintmax_t v, uintmax_t e) {{
16+
static const char *last[2] = {{0, 0}};
17+
if (v != e || !(last[0] == s || last[1] == s)) {{
18+
test_log(s, v);
19+
last[0] = last[1];
20+
last[1] = s;
21+
}}
22+
1523
if (v != e) {{
16-
printf("\033[31massert %s failed, expected %jd\033[0m\n", s, e);
24+
printf("\033[31m%s:%u: assert %s failed, expected %jd\033[0m\n",
25+
file, line, s, e);
1726
exit(-2);
1827
}}
1928
}}
2029

30+
#define test_assert(s, v, e) test_assert(__FILE__, __LINE__, s, v, e)
31+
32+
2133
// lfs declarations
2234
lfs_t lfs;
2335
lfs_emubd_t bd;
@@ -42,6 +54,7 @@ const struct lfs_config config = {{
4254
}};
4355

4456

57+
// Entry point
4558
int main() {{
4659
lfs_emubd_create(&bd, "blocks");
4760

tests/test.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,22 @@ def generate(test):
1111

1212
lines = []
1313

14-
for line in test:
15-
if '=>' in line:
16-
test, expect = line.strip().strip(';').split('=>')
17-
lines.append('res = {test};'.format(test=test.strip()))
18-
lines.append('test_assert("{name}", res, {expect});'.format(
14+
for line in re.split('(?<=[;{}])\n', test.read()):
15+
match = re.match('( *)(.*)=>(.*);', line, re.MULTILINE)
16+
if match:
17+
tab, test, expect = match.groups()
18+
lines.append(tab+'res = {test};'.format(test=test.strip()))
19+
lines.append(tab+'test_assert("{name}", res, {expect});'.format(
1920
name = re.match('\w*', test.strip()).group(),
2021
expect = expect.strip()))
2122
else:
22-
lines.append(line.strip())
23+
lines.append(line)
2324

2425
with open('test.c', 'w') as file:
25-
file.write(template.format(tests='\n'.join(4*' ' + l for l in lines)))
26+
file.write(template.format(tests='\n'.join(lines)))
2627

2728
def compile():
28-
os.environ['DEBUG'] = '1'
29-
os.environ['CFLAGS'] = '-Werror'
29+
os.environ['CFLAGS'] = os.environ.get('CFLAGS', '') + ' -Werror'
3030
subprocess.check_call(['make', '--no-print-directory', '-s'], env=os.environ)
3131

3232
def execute():

tests/test_dirs.sh

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@ set -eu
33

44
echo "=== Directory tests ==="
55
rm -rf blocks
6-
7-
echo "--- Root directory ---"
86
tests/test.py << TEST
97
lfs_format(&lfs, &config) => 0;
8+
TEST
109

10+
echo "--- Root directory ---"
11+
tests/test.py << TEST
12+
lfs_mount(&lfs, &config) => 0;
1113
lfs_dir_open(&lfs, &dir[0], "/") => 0;
1214
lfs_dir_close(&lfs, &dir[0]) => 0;
15+
lfs_unmount(&lfs) => 0;
1316
TEST
1417

1518
echo "--- Directory creation ---"
@@ -33,12 +36,16 @@ tests/test.py << TEST
3336
lfs_dir_open(&lfs, &dir[0], "/") => 0;
3437
lfs_dir_read(&lfs, &dir[0], &info) => 1;
3538
strcmp(info.name, ".") => 0;
39+
info.type => LFS_TYPE_DIR;
3640
lfs_dir_read(&lfs, &dir[0], &info) => 1;
3741
strcmp(info.name, "..") => 0;
42+
info.type => LFS_TYPE_DIR;
3843
lfs_dir_read(&lfs, &dir[0], &info) => 1;
3944
strcmp(info.name, "potato") => 0;
45+
info.type => LFS_TYPE_DIR;
4046
lfs_dir_read(&lfs, &dir[0], &info) => 1;
4147
strcmp(info.name, "burito") => 0;
48+
info.type => LFS_TYPE_REG;
4249
lfs_dir_read(&lfs, &dir[0], &info) => 0;
4350
lfs_dir_close(&lfs, &dir[0]) => 0;
4451
lfs_unmount(&lfs) => 0;

tests/test_files.sh

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
#!/bin/bash
2+
set -eu
3+
4+
SMALLSIZE=32
5+
MEDIUMSIZE=8192
6+
LARGESIZE=262144
7+
8+
echo "=== File tests ==="
9+
rm -rf blocks
10+
tests/test.py << TEST
11+
lfs_format(&lfs, &config) => 0;
12+
TEST
13+
14+
echo "--- Simple file test ---"
15+
tests/test.py << TEST
16+
lfs_mount(&lfs, &config) => 0;
17+
lfs_file_open(&lfs, &file[0], "hello", LFS_O_RDWR | LFS_O_CREAT | LFS_O_APPEND) => 0;
18+
size = strlen("Hello World!\n");
19+
memcpy(wbuffer, "Hello World!\n", size);
20+
lfs_file_write(&lfs, &file[0], wbuffer, size) => size;
21+
lfs_file_read(&lfs, &file[0], rbuffer, size) => size;
22+
memcmp(rbuffer, wbuffer, size) => 0;
23+
lfs_file_close(&lfs, &file[0]) => 0;
24+
lfs_unmount(&lfs) => 0;
25+
TEST
26+
27+
w_test() {
28+
tests/test.py << TEST
29+
lfs_size_t size = $1;
30+
lfs_size_t chunk = 31;
31+
srand(0);
32+
lfs_mount(&lfs, &config) => 0;
33+
lfs_file_open(&lfs, &file[0], "$2", LFS_O_WRONLY | LFS_O_CREAT) => 0;
34+
for (lfs_size_t i = 0; i < size; i += chunk) {
35+
chunk = (chunk < size - i) ? chunk : size - i;
36+
for (lfs_size_t b = 0; b < chunk; b++) {
37+
buffer[b] = rand() & 0xff;
38+
}
39+
lfs_file_write(&lfs, &file[0], buffer, chunk) => chunk;
40+
}
41+
lfs_file_close(&lfs, &file[0]) => 0;
42+
lfs_unmount(&lfs) => 0;
43+
TEST
44+
}
45+
46+
r_test() {
47+
tests/test.py << TEST
48+
lfs_size_t size = $1;
49+
lfs_size_t chunk = 29;
50+
srand(0);
51+
lfs_mount(&lfs, &config) => 0;
52+
lfs_file_open(&lfs, &file[0], "$2", LFS_O_RDONLY) => 0;
53+
for (lfs_size_t i = 0; i < size; i += chunk) {
54+
chunk = (chunk < size - i) ? chunk : size - i;
55+
lfs_file_read(&lfs, &file[0], buffer, chunk) => chunk;
56+
for (lfs_size_t b = 0; b < chunk && i+b < size; b++) {
57+
buffer[b] => rand() & 0xff;
58+
}
59+
}
60+
lfs_file_close(&lfs, &file[0]) => 0;
61+
lfs_unmount(&lfs) => 0;
62+
TEST
63+
}
64+
65+
echo "--- Small file test ---"
66+
w_test $SMALLSIZE smallavacado
67+
r_test $SMALLSIZE smallavacado
68+
69+
echo "--- Medium file test ---"
70+
w_test $MEDIUMSIZE mediumavacado
71+
r_test $MEDIUMSIZE mediumavacado
72+
73+
echo "--- Large file test ---"
74+
w_test $LARGESIZE largeavacado
75+
r_test $LARGESIZE largeavacado
76+
77+
echo "--- Non-overlap check ---"
78+
r_test $SMALLSIZE smallavacado
79+
r_test $MEDIUMSIZE mediumavacado
80+
r_test $LARGESIZE largeavacado
81+
82+
echo "--- Dir check ---"
83+
tests/test.py << TEST
84+
lfs_mount(&lfs, &config) => 0;
85+
lfs_dir_open(&lfs, &dir[0], "/") => 0;
86+
lfs_dir_read(&lfs, &dir[0], &info) => 1;
87+
lfs_dir_read(&lfs, &dir[0], &info) => 1;
88+
lfs_dir_read(&lfs, &dir[0], &info) => 1;
89+
strcmp(info.name, "hello") => 0;
90+
info.type => LFS_TYPE_REG;
91+
info.size => strlen("Hello World!\n");
92+
lfs_dir_read(&lfs, &dir[0], &info) => 1;
93+
strcmp(info.name, "smallavacado") => 0;
94+
info.type => LFS_TYPE_REG;
95+
info.size => $SMALLSIZE;
96+
lfs_dir_read(&lfs, &dir[0], &info) => 1;
97+
strcmp(info.name, "mediumavacado") => 0;
98+
info.type => LFS_TYPE_REG;
99+
info.size => $MEDIUMSIZE;
100+
lfs_dir_read(&lfs, &dir[0], &info) => 1;
101+
strcmp(info.name, "largeavacado") => 0;
102+
info.type => LFS_TYPE_REG;
103+
info.size => $LARGESIZE;
104+
lfs_dir_read(&lfs, &dir[0], &info) => 0;
105+
lfs_dir_close(&lfs, &dir[0]) => 0;
106+
lfs_unmount(&lfs) => 0;
107+
TEST
108+
109+
echo "--- Results ---"
110+
tests/stats.py

0 commit comments

Comments
 (0)