1# These tests don't really test littlefs at all, they are here only to make
2# sure the underlying block device is working.
3#
4# Note we use 251, a prime, in places to avoid aliasing powers of 2.
5#
6
7[cases.test_bd_one_block]
8defines.READ = ['READ_SIZE', 'BLOCK_SIZE']
9defines.PROG = ['PROG_SIZE', 'BLOCK_SIZE']
10code = '''
11    uint8_t buffer[lfs_max(READ, PROG)];
12
13    // write data
14    cfg->erase(cfg, 0) => 0;
15    for (lfs_off_t i = 0; i < cfg->block_size; i += PROG) {
16        for (lfs_off_t j = 0; j < PROG; j++) {
17            buffer[j] = (i+j) % 251;
18        }
19        cfg->prog(cfg, 0, i, buffer, PROG) => 0;
20    }
21
22    // read data
23    for (lfs_off_t i = 0; i < cfg->block_size; i += READ) {
24        cfg->read(cfg, 0, i, buffer, READ) => 0;
25
26        for (lfs_off_t j = 0; j < READ; j++) {
27            LFS_ASSERT(buffer[j] == (i+j) % 251);
28        }
29    }
30'''
31
32[cases.test_bd_two_block]
33defines.READ = ['READ_SIZE', 'BLOCK_SIZE']
34defines.PROG = ['PROG_SIZE', 'BLOCK_SIZE']
35code = '''
36    uint8_t buffer[lfs_max(READ, PROG)];
37    lfs_block_t block;
38
39    // write block 0
40    block = 0;
41    cfg->erase(cfg, block) => 0;
42    for (lfs_off_t i = 0; i < cfg->block_size; i += PROG) {
43        for (lfs_off_t j = 0; j < PROG; j++) {
44            buffer[j] = (block+i+j) % 251;
45        }
46        cfg->prog(cfg, block, i, buffer, PROG) => 0;
47    }
48
49    // read block 0
50    block = 0;
51    for (lfs_off_t i = 0; i < cfg->block_size; i += READ) {
52        cfg->read(cfg, block, i, buffer, READ) => 0;
53
54        for (lfs_off_t j = 0; j < READ; j++) {
55            LFS_ASSERT(buffer[j] == (block+i+j) % 251);
56        }
57    }
58
59    // write block 1
60    block = 1;
61    cfg->erase(cfg, block) => 0;
62    for (lfs_off_t i = 0; i < cfg->block_size; i += PROG) {
63        for (lfs_off_t j = 0; j < PROG; j++) {
64            buffer[j] = (block+i+j) % 251;
65        }
66        cfg->prog(cfg, block, i, buffer, PROG) => 0;
67    }
68
69    // read block 1
70    block = 1;
71    for (lfs_off_t i = 0; i < cfg->block_size; i += READ) {
72        cfg->read(cfg, block, i, buffer, READ) => 0;
73
74        for (lfs_off_t j = 0; j < READ; j++) {
75            LFS_ASSERT(buffer[j] == (block+i+j) % 251);
76        }
77    }
78
79    // read block 0 again
80    block = 0;
81    for (lfs_off_t i = 0; i < cfg->block_size; i += READ) {
82        cfg->read(cfg, block, i, buffer, READ) => 0;
83
84        for (lfs_off_t j = 0; j < READ; j++) {
85            LFS_ASSERT(buffer[j] == (block+i+j) % 251);
86        }
87    }
88'''
89
90[cases.test_bd_last_block]
91defines.READ = ['READ_SIZE', 'BLOCK_SIZE']
92defines.PROG = ['PROG_SIZE', 'BLOCK_SIZE']
93code = '''
94    uint8_t buffer[lfs_max(READ, PROG)];
95    lfs_block_t block;
96
97    // write block 0
98    block = 0;
99    cfg->erase(cfg, block) => 0;
100    for (lfs_off_t i = 0; i < cfg->block_size; i += PROG) {
101        for (lfs_off_t j = 0; j < PROG; j++) {
102            buffer[j] = (block+i+j) % 251;
103        }
104        cfg->prog(cfg, block, i, buffer, PROG) => 0;
105    }
106
107    // read block 0
108    block = 0;
109    for (lfs_off_t i = 0; i < cfg->block_size; i += READ) {
110        cfg->read(cfg, block, i, buffer, READ) => 0;
111
112        for (lfs_off_t j = 0; j < READ; j++) {
113            LFS_ASSERT(buffer[j] == (block+i+j) % 251);
114        }
115    }
116
117    // write block n-1
118    block = cfg->block_count-1;
119    cfg->erase(cfg, block) => 0;
120    for (lfs_off_t i = 0; i < cfg->block_size; i += PROG) {
121        for (lfs_off_t j = 0; j < PROG; j++) {
122            buffer[j] = (block+i+j) % 251;
123        }
124        cfg->prog(cfg, block, i, buffer, PROG) => 0;
125    }
126
127    // read block n-1
128    block = cfg->block_count-1;
129    for (lfs_off_t i = 0; i < cfg->block_size; i += READ) {
130        cfg->read(cfg, block, i, buffer, READ) => 0;
131
132        for (lfs_off_t j = 0; j < READ; j++) {
133            LFS_ASSERT(buffer[j] == (block+i+j) % 251);
134        }
135    }
136
137    // read block 0 again
138    block = 0;
139    for (lfs_off_t i = 0; i < cfg->block_size; i += READ) {
140        cfg->read(cfg, block, i, buffer, READ) => 0;
141
142        for (lfs_off_t j = 0; j < READ; j++) {
143            LFS_ASSERT(buffer[j] == (block+i+j) % 251);
144        }
145    }
146'''
147
148[cases.test_bd_powers_of_two]
149defines.READ = ['READ_SIZE', 'BLOCK_SIZE']
150defines.PROG = ['PROG_SIZE', 'BLOCK_SIZE']
151code = '''
152    uint8_t buffer[lfs_max(READ, PROG)];
153
154    // write/read every power of 2
155    lfs_block_t block = 1;
156    while (block < cfg->block_count) {
157        // write
158        cfg->erase(cfg, block) => 0;
159        for (lfs_off_t i = 0; i < cfg->block_size; i += PROG) {
160            for (lfs_off_t j = 0; j < PROG; j++) {
161                buffer[j] = (block+i+j) % 251;
162            }
163            cfg->prog(cfg, block, i, buffer, PROG) => 0;
164        }
165
166        // read
167        for (lfs_off_t i = 0; i < cfg->block_size; i += READ) {
168            cfg->read(cfg, block, i, buffer, READ) => 0;
169
170            for (lfs_off_t j = 0; j < READ; j++) {
171                LFS_ASSERT(buffer[j] == (block+i+j) % 251);
172            }
173        }
174
175        block *= 2;
176    }
177
178    // read every power of 2 again
179    block = 1;
180    while (block < cfg->block_count) {
181        // read
182        for (lfs_off_t i = 0; i < cfg->block_size; i += READ) {
183            cfg->read(cfg, block, i, buffer, READ) => 0;
184
185            for (lfs_off_t j = 0; j < READ; j++) {
186                LFS_ASSERT(buffer[j] == (block+i+j) % 251);
187            }
188        }
189
190        block *= 2;
191    }
192'''
193
194[cases.test_bd_fibonacci]
195defines.READ = ['READ_SIZE', 'BLOCK_SIZE']
196defines.PROG = ['PROG_SIZE', 'BLOCK_SIZE']
197code = '''
198    uint8_t buffer[lfs_max(READ, PROG)];
199
200    // write/read every fibonacci number on our device
201    lfs_block_t block = 1;
202    lfs_block_t block_ = 1;
203    while (block < cfg->block_count) {
204        // write
205        cfg->erase(cfg, block) => 0;
206        for (lfs_off_t i = 0; i < cfg->block_size; i += PROG) {
207            for (lfs_off_t j = 0; j < PROG; j++) {
208                buffer[j] = (block+i+j) % 251;
209            }
210            cfg->prog(cfg, block, i, buffer, PROG) => 0;
211        }
212
213        // read
214        for (lfs_off_t i = 0; i < cfg->block_size; i += READ) {
215            cfg->read(cfg, block, i, buffer, READ) => 0;
216
217            for (lfs_off_t j = 0; j < READ; j++) {
218                LFS_ASSERT(buffer[j] == (block+i+j) % 251);
219            }
220        }
221
222        lfs_block_t nblock = block + block_;
223        block_ = block;
224        block = nblock;
225    }
226
227    // read every fibonacci number again
228    block = 1;
229    block_ = 1;
230    while (block < cfg->block_count) {
231        // read
232        for (lfs_off_t i = 0; i < cfg->block_size; i += READ) {
233            cfg->read(cfg, block, i, buffer, READ) => 0;
234
235            for (lfs_off_t j = 0; j < READ; j++) {
236                LFS_ASSERT(buffer[j] == (block+i+j) % 251);
237            }
238        }
239
240        lfs_block_t nblock = block + block_;
241        block_ = block;
242        block = nblock;
243    }
244'''
245
246
247
248
249