1
2[cases.test_interspersed_files]
3defines.SIZE = [10, 100]
4defines.FILES = [4, 10, 26]
5code = '''
6    lfs_t lfs;
7    lfs_file_t files[FILES];
8    const char alphas[] = "abcdefghijklmnopqrstuvwxyz";
9    lfs_format(&lfs, cfg) => 0;
10    lfs_mount(&lfs, cfg) => 0;
11    for (int j = 0; j < FILES; j++) {
12        char path[1024];
13        sprintf(path, "%c", alphas[j]);
14        lfs_file_open(&lfs, &files[j], path,
15                LFS_O_WRONLY | LFS_O_CREAT | LFS_O_EXCL) => 0;
16    }
17
18    for (int i = 0; i < SIZE; i++) {
19        for (int j = 0; j < FILES; j++) {
20            lfs_file_write(&lfs, &files[j], &alphas[j], 1) => 1;
21        }
22    }
23
24    for (int j = 0; j < FILES; j++) {
25        lfs_file_close(&lfs, &files[j]);
26    }
27
28    lfs_dir_t dir;
29    lfs_dir_open(&lfs, &dir, "/") => 0;
30    struct lfs_info info;
31    lfs_dir_read(&lfs, &dir, &info) => 1;
32    assert(strcmp(info.name, ".") == 0);
33    assert(info.type == LFS_TYPE_DIR);
34    lfs_dir_read(&lfs, &dir, &info) => 1;
35    assert(strcmp(info.name, "..") == 0);
36    assert(info.type == LFS_TYPE_DIR);
37    for (int j = 0; j < FILES; j++) {
38        char path[1024];
39        sprintf(path, "%c", alphas[j]);
40        lfs_dir_read(&lfs, &dir, &info) => 1;
41        assert(strcmp(info.name, path) == 0);
42        assert(info.type == LFS_TYPE_REG);
43        assert(info.size == SIZE);
44    }
45    lfs_dir_read(&lfs, &dir, &info) => 0;
46    lfs_dir_close(&lfs, &dir) => 0;
47
48    for (int j = 0; j < FILES; j++) {
49        char path[1024];
50        sprintf(path, "%c", alphas[j]);
51        lfs_file_open(&lfs, &files[j], path, LFS_O_RDONLY) => 0;
52    }
53
54    for (int i = 0; i < 10; i++) {
55        for (int j = 0; j < FILES; j++) {
56            uint8_t buffer[1024];
57            lfs_file_read(&lfs, &files[j], buffer, 1) => 1;
58            assert(buffer[0] == alphas[j]);
59        }
60    }
61
62    for (int j = 0; j < FILES; j++) {
63        lfs_file_close(&lfs, &files[j]);
64    }
65
66    lfs_unmount(&lfs) => 0;
67'''
68
69[cases.test_interspersed_remove_files]
70defines.SIZE = [10, 100]
71defines.FILES = [4, 10, 26]
72code = '''
73    lfs_t lfs;
74    const char alphas[] = "abcdefghijklmnopqrstuvwxyz";
75    lfs_format(&lfs, cfg) => 0;
76    lfs_mount(&lfs, cfg) => 0;
77    for (int j = 0; j < FILES; j++) {
78        char path[1024];
79        sprintf(path, "%c", alphas[j]);
80        lfs_file_t file;
81        lfs_file_open(&lfs, &file, path,
82                LFS_O_WRONLY | LFS_O_CREAT | LFS_O_EXCL) => 0;
83        for (int i = 0; i < SIZE; i++) {
84            lfs_file_write(&lfs, &file, &alphas[j], 1) => 1;
85        }
86        lfs_file_close(&lfs, &file);
87    }
88    lfs_unmount(&lfs) => 0;
89
90    lfs_mount(&lfs, cfg) => 0;
91    lfs_file_t file;
92    lfs_file_open(&lfs, &file, "zzz", LFS_O_WRONLY | LFS_O_CREAT) => 0;
93    for (int j = 0; j < FILES; j++) {
94        lfs_file_write(&lfs, &file, (const void*)"~", 1) => 1;
95        lfs_file_sync(&lfs, &file) => 0;
96
97        char path[1024];
98        sprintf(path, "%c", alphas[j]);
99        lfs_remove(&lfs, path) => 0;
100    }
101    lfs_file_close(&lfs, &file);
102
103    lfs_dir_t dir;
104    lfs_dir_open(&lfs, &dir, "/") => 0;
105    struct lfs_info info;
106    lfs_dir_read(&lfs, &dir, &info) => 1;
107    assert(strcmp(info.name, ".") == 0);
108    assert(info.type == LFS_TYPE_DIR);
109    lfs_dir_read(&lfs, &dir, &info) => 1;
110    assert(strcmp(info.name, "..") == 0);
111    assert(info.type == LFS_TYPE_DIR);
112    lfs_dir_read(&lfs, &dir, &info) => 1;
113    assert(strcmp(info.name, "zzz") == 0);
114    assert(info.type == LFS_TYPE_REG);
115    assert(info.size == FILES);
116    lfs_dir_read(&lfs, &dir, &info) => 0;
117    lfs_dir_close(&lfs, &dir) => 0;
118
119    lfs_file_open(&lfs, &file, "zzz", LFS_O_RDONLY) => 0;
120    for (int i = 0; i < FILES; i++) {
121        uint8_t buffer[1024];
122        lfs_file_read(&lfs, &file, buffer, 1) => 1;
123        assert(buffer[0] == '~');
124    }
125    lfs_file_close(&lfs, &file);
126
127    lfs_unmount(&lfs) => 0;
128'''
129
130[cases.test_interspersed_remove_inconveniently]
131defines.SIZE = [10, 100]
132code = '''
133    lfs_t lfs;
134    lfs_format(&lfs, cfg) => 0;
135    lfs_mount(&lfs, cfg) => 0;
136    lfs_file_t files[3];
137    lfs_file_open(&lfs, &files[0], "e", LFS_O_WRONLY | LFS_O_CREAT) => 0;
138    lfs_file_open(&lfs, &files[1], "f", LFS_O_WRONLY | LFS_O_CREAT) => 0;
139    lfs_file_open(&lfs, &files[2], "g", LFS_O_WRONLY | LFS_O_CREAT) => 0;
140
141    for (int i = 0; i < SIZE/2; i++) {
142        lfs_file_write(&lfs, &files[0], (const void*)"e", 1) => 1;
143        lfs_file_write(&lfs, &files[1], (const void*)"f", 1) => 1;
144        lfs_file_write(&lfs, &files[2], (const void*)"g", 1) => 1;
145    }
146
147    lfs_remove(&lfs, "f") => 0;
148
149    for (int i = 0; i < SIZE/2; i++) {
150        lfs_file_write(&lfs, &files[0], (const void*)"e", 1) => 1;
151        lfs_file_write(&lfs, &files[1], (const void*)"f", 1) => 1;
152        lfs_file_write(&lfs, &files[2], (const void*)"g", 1) => 1;
153    }
154
155    lfs_file_close(&lfs, &files[0]);
156    lfs_file_close(&lfs, &files[1]);
157    lfs_file_close(&lfs, &files[2]);
158
159    lfs_dir_t dir;
160    lfs_dir_open(&lfs, &dir, "/") => 0;
161    struct lfs_info info;
162    lfs_dir_read(&lfs, &dir, &info) => 1;
163    assert(strcmp(info.name, ".") == 0);
164    assert(info.type == LFS_TYPE_DIR);
165    lfs_dir_read(&lfs, &dir, &info) => 1;
166    assert(strcmp(info.name, "..") == 0);
167    assert(info.type == LFS_TYPE_DIR);
168    lfs_dir_read(&lfs, &dir, &info) => 1;
169    assert(strcmp(info.name, "e") == 0);
170    assert(info.type == LFS_TYPE_REG);
171    assert(info.size == SIZE);
172    lfs_dir_read(&lfs, &dir, &info) => 1;
173    assert(strcmp(info.name, "g") == 0);
174    assert(info.type == LFS_TYPE_REG);
175    assert(info.size == SIZE);
176    lfs_dir_read(&lfs, &dir, &info) => 0;
177    lfs_dir_close(&lfs, &dir) => 0;
178
179    lfs_file_open(&lfs, &files[0], "e", LFS_O_RDONLY) => 0;
180    lfs_file_open(&lfs, &files[1], "g", LFS_O_RDONLY) => 0;
181    for (int i = 0; i < SIZE; i++) {
182        uint8_t buffer[1024];
183        lfs_file_read(&lfs, &files[0], buffer, 1) => 1;
184        assert(buffer[0] == 'e');
185        lfs_file_read(&lfs, &files[1], buffer, 1) => 1;
186        assert(buffer[0] == 'g');
187    }
188    lfs_file_close(&lfs, &files[0]);
189    lfs_file_close(&lfs, &files[1]);
190
191    lfs_unmount(&lfs) => 0;
192'''
193
194[cases.test_interspersed_reentrant_files]
195defines.SIZE = [10, 100]
196defines.FILES = [4, 10, 26]
197reentrant = true
198code = '''
199    lfs_t lfs;
200    lfs_file_t files[FILES];
201    const char alphas[] = "abcdefghijklmnopqrstuvwxyz";
202
203    int err = lfs_mount(&lfs, cfg);
204    if (err) {
205        lfs_format(&lfs, cfg) => 0;
206        lfs_mount(&lfs, cfg) => 0;
207    }
208
209    for (int j = 0; j < FILES; j++) {
210        char path[1024];
211        sprintf(path, "%c", alphas[j]);
212        lfs_file_open(&lfs, &files[j], path,
213                LFS_O_WRONLY | LFS_O_CREAT | LFS_O_APPEND) => 0;
214    }
215
216    for (int i = 0; i < SIZE; i++) {
217        for (int j = 0; j < FILES; j++) {
218            lfs_ssize_t size = lfs_file_size(&lfs, &files[j]);
219            assert(size >= 0);
220            if ((int)size <= i) {
221                lfs_file_write(&lfs, &files[j], &alphas[j], 1) => 1;
222                lfs_file_sync(&lfs, &files[j]) => 0;
223            }
224        }
225    }
226
227    for (int j = 0; j < FILES; j++) {
228        lfs_file_close(&lfs, &files[j]);
229    }
230
231    lfs_dir_t dir;
232    lfs_dir_open(&lfs, &dir, "/") => 0;
233    struct lfs_info info;
234    lfs_dir_read(&lfs, &dir, &info) => 1;
235    assert(strcmp(info.name, ".") == 0);
236    assert(info.type == LFS_TYPE_DIR);
237    lfs_dir_read(&lfs, &dir, &info) => 1;
238    assert(strcmp(info.name, "..") == 0);
239    assert(info.type == LFS_TYPE_DIR);
240    for (int j = 0; j < FILES; j++) {
241        char path[1024];
242        sprintf(path, "%c", alphas[j]);
243        lfs_dir_read(&lfs, &dir, &info) => 1;
244        assert(strcmp(info.name, path) == 0);
245        assert(info.type == LFS_TYPE_REG);
246        assert(info.size == SIZE);
247    }
248    lfs_dir_read(&lfs, &dir, &info) => 0;
249    lfs_dir_close(&lfs, &dir) => 0;
250
251    for (int j = 0; j < FILES; j++) {
252        char path[1024];
253        sprintf(path, "%c", alphas[j]);
254        lfs_file_open(&lfs, &files[j], path, LFS_O_RDONLY) => 0;
255    }
256
257    for (int i = 0; i < 10; i++) {
258        for (int j = 0; j < FILES; j++) {
259            uint8_t buffer[1024];
260            lfs_file_read(&lfs, &files[j], buffer, 1) => 1;
261            assert(buffer[0] == alphas[j]);
262        }
263    }
264
265    for (int j = 0; j < FILES; j++) {
266        lfs_file_close(&lfs, &files[j]);
267    }
268
269    lfs_unmount(&lfs) => 0;
270'''
271