119ea8026Sopenharmony_ci
219ea8026Sopenharmony_ci# simple file seek
319ea8026Sopenharmony_ci[cases.test_seek_read]
419ea8026Sopenharmony_cidefines = [
519ea8026Sopenharmony_ci    {COUNT=132, SKIP=4},
619ea8026Sopenharmony_ci    {COUNT=132, SKIP=128},
719ea8026Sopenharmony_ci    {COUNT=200, SKIP=10},
819ea8026Sopenharmony_ci    {COUNT=200, SKIP=100},
919ea8026Sopenharmony_ci    {COUNT=4,   SKIP=1},
1019ea8026Sopenharmony_ci    {COUNT=4,   SKIP=2},
1119ea8026Sopenharmony_ci]
1219ea8026Sopenharmony_cicode = '''
1319ea8026Sopenharmony_ci    lfs_t lfs;
1419ea8026Sopenharmony_ci    lfs_format(&lfs, cfg) => 0;
1519ea8026Sopenharmony_ci    lfs_mount(&lfs, cfg) => 0;
1619ea8026Sopenharmony_ci    lfs_file_t file;
1719ea8026Sopenharmony_ci    lfs_file_open(&lfs, &file, "kitty",
1819ea8026Sopenharmony_ci            LFS_O_WRONLY | LFS_O_CREAT | LFS_O_APPEND) => 0;
1919ea8026Sopenharmony_ci    size_t size = strlen("kittycatcat");
2019ea8026Sopenharmony_ci    uint8_t buffer[1024];
2119ea8026Sopenharmony_ci    memcpy(buffer, "kittycatcat", size);
2219ea8026Sopenharmony_ci    for (int j = 0; j < COUNT; j++) {
2319ea8026Sopenharmony_ci        lfs_file_write(&lfs, &file, buffer, size);
2419ea8026Sopenharmony_ci    }
2519ea8026Sopenharmony_ci    lfs_file_close(&lfs, &file) => 0;
2619ea8026Sopenharmony_ci    lfs_unmount(&lfs) => 0;
2719ea8026Sopenharmony_ci
2819ea8026Sopenharmony_ci    lfs_mount(&lfs, cfg) => 0;
2919ea8026Sopenharmony_ci    lfs_file_open(&lfs, &file, "kitty", LFS_O_RDONLY) => 0;
3019ea8026Sopenharmony_ci
3119ea8026Sopenharmony_ci    lfs_soff_t pos = -1;
3219ea8026Sopenharmony_ci    size = strlen("kittycatcat");
3319ea8026Sopenharmony_ci    for (int i = 0; i < SKIP; i++) {
3419ea8026Sopenharmony_ci        lfs_file_read(&lfs, &file, buffer, size) => size;
3519ea8026Sopenharmony_ci        memcmp(buffer, "kittycatcat", size) => 0;
3619ea8026Sopenharmony_ci        pos = lfs_file_tell(&lfs, &file);
3719ea8026Sopenharmony_ci    }
3819ea8026Sopenharmony_ci    assert(pos >= 0);
3919ea8026Sopenharmony_ci
4019ea8026Sopenharmony_ci    lfs_file_seek(&lfs, &file, pos, LFS_SEEK_SET) => pos;
4119ea8026Sopenharmony_ci    lfs_file_read(&lfs, &file, buffer, size) => size;
4219ea8026Sopenharmony_ci    memcmp(buffer, "kittycatcat", size) => 0;
4319ea8026Sopenharmony_ci
4419ea8026Sopenharmony_ci    lfs_file_rewind(&lfs, &file) => 0;
4519ea8026Sopenharmony_ci    lfs_file_read(&lfs, &file, buffer, size) => size;
4619ea8026Sopenharmony_ci    memcmp(buffer, "kittycatcat", size) => 0;
4719ea8026Sopenharmony_ci
4819ea8026Sopenharmony_ci    lfs_file_seek(&lfs, &file, 0, LFS_SEEK_CUR) => size;
4919ea8026Sopenharmony_ci    lfs_file_read(&lfs, &file, buffer, size) => size;
5019ea8026Sopenharmony_ci    memcmp(buffer, "kittycatcat", size) => 0;
5119ea8026Sopenharmony_ci
5219ea8026Sopenharmony_ci    lfs_file_seek(&lfs, &file, size, LFS_SEEK_CUR) => 3*size;
5319ea8026Sopenharmony_ci    lfs_file_read(&lfs, &file, buffer, size) => size;
5419ea8026Sopenharmony_ci    memcmp(buffer, "kittycatcat", size) => 0;
5519ea8026Sopenharmony_ci
5619ea8026Sopenharmony_ci    lfs_file_seek(&lfs, &file, pos, LFS_SEEK_SET) => pos;
5719ea8026Sopenharmony_ci    lfs_file_read(&lfs, &file, buffer, size) => size;
5819ea8026Sopenharmony_ci    memcmp(buffer, "kittycatcat", size) => 0;
5919ea8026Sopenharmony_ci
6019ea8026Sopenharmony_ci    lfs_file_seek(&lfs, &file, -size, LFS_SEEK_CUR) => pos;
6119ea8026Sopenharmony_ci    lfs_file_read(&lfs, &file, buffer, size) => size;
6219ea8026Sopenharmony_ci    memcmp(buffer, "kittycatcat", size) => 0;
6319ea8026Sopenharmony_ci
6419ea8026Sopenharmony_ci    lfs_file_seek(&lfs, &file, -size, LFS_SEEK_END) >= 0 => 1;
6519ea8026Sopenharmony_ci    lfs_file_read(&lfs, &file, buffer, size) => size;
6619ea8026Sopenharmony_ci    memcmp(buffer, "kittycatcat", size) => 0;
6719ea8026Sopenharmony_ci
6819ea8026Sopenharmony_ci    size = lfs_file_size(&lfs, &file);
6919ea8026Sopenharmony_ci    lfs_file_seek(&lfs, &file, 0, LFS_SEEK_CUR) => size;
7019ea8026Sopenharmony_ci
7119ea8026Sopenharmony_ci    lfs_file_close(&lfs, &file) => 0;
7219ea8026Sopenharmony_ci    lfs_unmount(&lfs) => 0;
7319ea8026Sopenharmony_ci'''
7419ea8026Sopenharmony_ci
7519ea8026Sopenharmony_ci# simple file seek and write
7619ea8026Sopenharmony_ci[cases.test_seek_write]
7719ea8026Sopenharmony_cidefines = [
7819ea8026Sopenharmony_ci    {COUNT=132, SKIP=4},
7919ea8026Sopenharmony_ci    {COUNT=132, SKIP=128},
8019ea8026Sopenharmony_ci    {COUNT=200, SKIP=10},
8119ea8026Sopenharmony_ci    {COUNT=200, SKIP=100},
8219ea8026Sopenharmony_ci    {COUNT=4,   SKIP=1},
8319ea8026Sopenharmony_ci    {COUNT=4,   SKIP=2},
8419ea8026Sopenharmony_ci]
8519ea8026Sopenharmony_cicode = '''
8619ea8026Sopenharmony_ci    lfs_t lfs;
8719ea8026Sopenharmony_ci    lfs_format(&lfs, cfg) => 0;
8819ea8026Sopenharmony_ci    lfs_mount(&lfs, cfg) => 0;
8919ea8026Sopenharmony_ci    lfs_file_t file;
9019ea8026Sopenharmony_ci    lfs_file_open(&lfs, &file, "kitty",
9119ea8026Sopenharmony_ci            LFS_O_WRONLY | LFS_O_CREAT | LFS_O_APPEND) => 0;
9219ea8026Sopenharmony_ci    size_t size = strlen("kittycatcat");
9319ea8026Sopenharmony_ci    uint8_t buffer[1024];
9419ea8026Sopenharmony_ci    memcpy(buffer, "kittycatcat", size);
9519ea8026Sopenharmony_ci    for (int j = 0; j < COUNT; j++) {
9619ea8026Sopenharmony_ci        lfs_file_write(&lfs, &file, buffer, size);
9719ea8026Sopenharmony_ci    }
9819ea8026Sopenharmony_ci    lfs_file_close(&lfs, &file) => 0;
9919ea8026Sopenharmony_ci    lfs_unmount(&lfs) => 0;
10019ea8026Sopenharmony_ci
10119ea8026Sopenharmony_ci    lfs_mount(&lfs, cfg) => 0;
10219ea8026Sopenharmony_ci    lfs_file_open(&lfs, &file, "kitty", LFS_O_RDWR) => 0;
10319ea8026Sopenharmony_ci
10419ea8026Sopenharmony_ci    lfs_soff_t pos = -1;
10519ea8026Sopenharmony_ci    size = strlen("kittycatcat");
10619ea8026Sopenharmony_ci    for (int i = 0; i < SKIP; i++) {
10719ea8026Sopenharmony_ci        lfs_file_read(&lfs, &file, buffer, size) => size;
10819ea8026Sopenharmony_ci        memcmp(buffer, "kittycatcat", size) => 0;
10919ea8026Sopenharmony_ci        pos = lfs_file_tell(&lfs, &file);
11019ea8026Sopenharmony_ci    }
11119ea8026Sopenharmony_ci    assert(pos >= 0);
11219ea8026Sopenharmony_ci
11319ea8026Sopenharmony_ci    memcpy(buffer, "doggodogdog", size);
11419ea8026Sopenharmony_ci    lfs_file_seek(&lfs, &file, pos, LFS_SEEK_SET) => pos;
11519ea8026Sopenharmony_ci    lfs_file_write(&lfs, &file, buffer, size) => size;
11619ea8026Sopenharmony_ci
11719ea8026Sopenharmony_ci    lfs_file_seek(&lfs, &file, pos, LFS_SEEK_SET) => pos;
11819ea8026Sopenharmony_ci    lfs_file_read(&lfs, &file, buffer, size) => size;
11919ea8026Sopenharmony_ci    memcmp(buffer, "doggodogdog", size) => 0;
12019ea8026Sopenharmony_ci
12119ea8026Sopenharmony_ci    lfs_file_rewind(&lfs, &file) => 0;
12219ea8026Sopenharmony_ci    lfs_file_read(&lfs, &file, buffer, size) => size;
12319ea8026Sopenharmony_ci    memcmp(buffer, "kittycatcat", size) => 0;
12419ea8026Sopenharmony_ci
12519ea8026Sopenharmony_ci    lfs_file_seek(&lfs, &file, pos, LFS_SEEK_SET) => pos;
12619ea8026Sopenharmony_ci    lfs_file_read(&lfs, &file, buffer, size) => size;
12719ea8026Sopenharmony_ci    memcmp(buffer, "doggodogdog", size) => 0;
12819ea8026Sopenharmony_ci
12919ea8026Sopenharmony_ci    lfs_file_seek(&lfs, &file, -size, LFS_SEEK_END) >= 0 => 1;
13019ea8026Sopenharmony_ci    lfs_file_read(&lfs, &file, buffer, size) => size;
13119ea8026Sopenharmony_ci    memcmp(buffer, "kittycatcat", size) => 0;
13219ea8026Sopenharmony_ci
13319ea8026Sopenharmony_ci    size = lfs_file_size(&lfs, &file);
13419ea8026Sopenharmony_ci    lfs_file_seek(&lfs, &file, 0, LFS_SEEK_CUR) => size;
13519ea8026Sopenharmony_ci
13619ea8026Sopenharmony_ci    lfs_file_close(&lfs, &file) => 0;
13719ea8026Sopenharmony_ci    lfs_unmount(&lfs) => 0;
13819ea8026Sopenharmony_ci'''
13919ea8026Sopenharmony_ci
14019ea8026Sopenharmony_ci# boundary seek and writes
14119ea8026Sopenharmony_ci[cases.test_seek_boundary_write]
14219ea8026Sopenharmony_cidefines.COUNT = 132
14319ea8026Sopenharmony_cicode = '''
14419ea8026Sopenharmony_ci    lfs_t lfs;
14519ea8026Sopenharmony_ci    lfs_format(&lfs, cfg) => 0;
14619ea8026Sopenharmony_ci    lfs_mount(&lfs, cfg) => 0;
14719ea8026Sopenharmony_ci    lfs_file_t file;
14819ea8026Sopenharmony_ci    lfs_file_open(&lfs, &file, "kitty",
14919ea8026Sopenharmony_ci            LFS_O_WRONLY | LFS_O_CREAT | LFS_O_APPEND) => 0;
15019ea8026Sopenharmony_ci    size_t size = strlen("kittycatcat");
15119ea8026Sopenharmony_ci    uint8_t buffer[1024];
15219ea8026Sopenharmony_ci    memcpy(buffer, "kittycatcat", size);
15319ea8026Sopenharmony_ci    for (int j = 0; j < COUNT; j++) {
15419ea8026Sopenharmony_ci        lfs_file_write(&lfs, &file, buffer, size);
15519ea8026Sopenharmony_ci    }
15619ea8026Sopenharmony_ci    lfs_file_close(&lfs, &file) => 0;
15719ea8026Sopenharmony_ci    lfs_unmount(&lfs) => 0;
15819ea8026Sopenharmony_ci
15919ea8026Sopenharmony_ci    lfs_mount(&lfs, cfg) => 0;
16019ea8026Sopenharmony_ci    lfs_file_open(&lfs, &file, "kitty", LFS_O_RDWR) => 0;
16119ea8026Sopenharmony_ci
16219ea8026Sopenharmony_ci    size = strlen("hedgehoghog");
16319ea8026Sopenharmony_ci    const lfs_soff_t offsets[] = {512, 1020, 513, 1021, 511, 1019, 1441};
16419ea8026Sopenharmony_ci
16519ea8026Sopenharmony_ci    for (unsigned i = 0; i < sizeof(offsets) / sizeof(offsets[0]); i++) {
16619ea8026Sopenharmony_ci        lfs_soff_t off = offsets[i];
16719ea8026Sopenharmony_ci        memcpy(buffer, "hedgehoghog", size);
16819ea8026Sopenharmony_ci        lfs_file_seek(&lfs, &file, off, LFS_SEEK_SET) => off;
16919ea8026Sopenharmony_ci        lfs_file_write(&lfs, &file, buffer, size) => size;
17019ea8026Sopenharmony_ci        lfs_file_seek(&lfs, &file, off, LFS_SEEK_SET) => off;
17119ea8026Sopenharmony_ci        lfs_file_read(&lfs, &file, buffer, size) => size;
17219ea8026Sopenharmony_ci        memcmp(buffer, "hedgehoghog", size) => 0;
17319ea8026Sopenharmony_ci
17419ea8026Sopenharmony_ci        lfs_file_seek(&lfs, &file, 0, LFS_SEEK_SET) => 0;
17519ea8026Sopenharmony_ci        lfs_file_read(&lfs, &file, buffer, size) => size;
17619ea8026Sopenharmony_ci        memcmp(buffer, "kittycatcat", size) => 0;
17719ea8026Sopenharmony_ci
17819ea8026Sopenharmony_ci        lfs_file_seek(&lfs, &file, off, LFS_SEEK_SET) => off;
17919ea8026Sopenharmony_ci        lfs_file_read(&lfs, &file, buffer, size) => size;
18019ea8026Sopenharmony_ci        memcmp(buffer, "hedgehoghog", size) => 0;
18119ea8026Sopenharmony_ci
18219ea8026Sopenharmony_ci        lfs_file_sync(&lfs, &file) => 0;
18319ea8026Sopenharmony_ci
18419ea8026Sopenharmony_ci        lfs_file_seek(&lfs, &file, 0, LFS_SEEK_SET) => 0;
18519ea8026Sopenharmony_ci        lfs_file_read(&lfs, &file, buffer, size) => size;
18619ea8026Sopenharmony_ci        memcmp(buffer, "kittycatcat", size) => 0;
18719ea8026Sopenharmony_ci
18819ea8026Sopenharmony_ci        lfs_file_seek(&lfs, &file, off, LFS_SEEK_SET) => off;
18919ea8026Sopenharmony_ci        lfs_file_read(&lfs, &file, buffer, size) => size;
19019ea8026Sopenharmony_ci        memcmp(buffer, "hedgehoghog", size) => 0;
19119ea8026Sopenharmony_ci    }
19219ea8026Sopenharmony_ci
19319ea8026Sopenharmony_ci    lfs_file_close(&lfs, &file) => 0;
19419ea8026Sopenharmony_ci    lfs_unmount(&lfs) => 0;
19519ea8026Sopenharmony_ci'''
19619ea8026Sopenharmony_ci
19719ea8026Sopenharmony_ci# out of bounds seek
19819ea8026Sopenharmony_ci[cases.test_seek_out_of_bounds]
19919ea8026Sopenharmony_cidefines = [
20019ea8026Sopenharmony_ci    {COUNT=132, SKIP=4},
20119ea8026Sopenharmony_ci    {COUNT=132, SKIP=128},
20219ea8026Sopenharmony_ci    {COUNT=200, SKIP=10},
20319ea8026Sopenharmony_ci    {COUNT=200, SKIP=100},
20419ea8026Sopenharmony_ci    {COUNT=4,   SKIP=2},
20519ea8026Sopenharmony_ci    {COUNT=4,   SKIP=3},
20619ea8026Sopenharmony_ci]
20719ea8026Sopenharmony_cicode = '''
20819ea8026Sopenharmony_ci    lfs_t lfs;
20919ea8026Sopenharmony_ci    lfs_format(&lfs, cfg) => 0;
21019ea8026Sopenharmony_ci    lfs_mount(&lfs, cfg) => 0;
21119ea8026Sopenharmony_ci    lfs_file_t file;
21219ea8026Sopenharmony_ci    lfs_file_open(&lfs, &file, "kitty",
21319ea8026Sopenharmony_ci            LFS_O_WRONLY | LFS_O_CREAT | LFS_O_APPEND) => 0;
21419ea8026Sopenharmony_ci    size_t size = strlen("kittycatcat");
21519ea8026Sopenharmony_ci    uint8_t buffer[1024];
21619ea8026Sopenharmony_ci    memcpy(buffer, "kittycatcat", size);
21719ea8026Sopenharmony_ci    for (int j = 0; j < COUNT; j++) {
21819ea8026Sopenharmony_ci        lfs_file_write(&lfs, &file, buffer, size);
21919ea8026Sopenharmony_ci    }
22019ea8026Sopenharmony_ci    lfs_file_close(&lfs, &file) => 0;
22119ea8026Sopenharmony_ci    lfs_unmount(&lfs) => 0;
22219ea8026Sopenharmony_ci    lfs_mount(&lfs, cfg) => 0;
22319ea8026Sopenharmony_ci    lfs_file_open(&lfs, &file, "kitty", LFS_O_RDWR) => 0;
22419ea8026Sopenharmony_ci
22519ea8026Sopenharmony_ci    size = strlen("kittycatcat");
22619ea8026Sopenharmony_ci    lfs_file_size(&lfs, &file) => COUNT*size;
22719ea8026Sopenharmony_ci    lfs_file_seek(&lfs, &file, (COUNT+SKIP)*size,
22819ea8026Sopenharmony_ci            LFS_SEEK_SET) => (COUNT+SKIP)*size;
22919ea8026Sopenharmony_ci    lfs_file_read(&lfs, &file, buffer, size) => 0;
23019ea8026Sopenharmony_ci
23119ea8026Sopenharmony_ci    memcpy(buffer, "porcupineee", size);
23219ea8026Sopenharmony_ci    lfs_file_write(&lfs, &file, buffer, size) => size;
23319ea8026Sopenharmony_ci
23419ea8026Sopenharmony_ci    lfs_file_seek(&lfs, &file, (COUNT+SKIP)*size,
23519ea8026Sopenharmony_ci            LFS_SEEK_SET) => (COUNT+SKIP)*size;
23619ea8026Sopenharmony_ci    lfs_file_read(&lfs, &file, buffer, size) => size;
23719ea8026Sopenharmony_ci    memcmp(buffer, "porcupineee", size) => 0;
23819ea8026Sopenharmony_ci
23919ea8026Sopenharmony_ci    lfs_file_seek(&lfs, &file, COUNT*size,
24019ea8026Sopenharmony_ci            LFS_SEEK_SET) => COUNT*size;
24119ea8026Sopenharmony_ci    lfs_file_read(&lfs, &file, buffer, size) => size;
24219ea8026Sopenharmony_ci    memcmp(buffer, "\0\0\0\0\0\0\0\0\0\0\0", size) => 0;
24319ea8026Sopenharmony_ci
24419ea8026Sopenharmony_ci    lfs_file_seek(&lfs, &file, -((COUNT+SKIP)*size),
24519ea8026Sopenharmony_ci            LFS_SEEK_CUR) => LFS_ERR_INVAL;
24619ea8026Sopenharmony_ci    lfs_file_tell(&lfs, &file) => (COUNT+1)*size;
24719ea8026Sopenharmony_ci
24819ea8026Sopenharmony_ci    lfs_file_seek(&lfs, &file, -((COUNT+2*SKIP)*size),
24919ea8026Sopenharmony_ci            LFS_SEEK_END) => LFS_ERR_INVAL;
25019ea8026Sopenharmony_ci    lfs_file_tell(&lfs, &file) => (COUNT+1)*size;
25119ea8026Sopenharmony_ci
25219ea8026Sopenharmony_ci    lfs_file_close(&lfs, &file) => 0;
25319ea8026Sopenharmony_ci    lfs_unmount(&lfs) => 0;
25419ea8026Sopenharmony_ci'''
25519ea8026Sopenharmony_ci
25619ea8026Sopenharmony_ci# inline write and seek
25719ea8026Sopenharmony_ci[cases.test_seek_inline_write]
25819ea8026Sopenharmony_cidefines.SIZE = [2, 4, 128, 132]
25919ea8026Sopenharmony_cicode = '''
26019ea8026Sopenharmony_ci    lfs_t lfs;
26119ea8026Sopenharmony_ci    lfs_format(&lfs, cfg) => 0;
26219ea8026Sopenharmony_ci    lfs_mount(&lfs, cfg) => 0;
26319ea8026Sopenharmony_ci    lfs_file_t file;
26419ea8026Sopenharmony_ci    lfs_file_open(&lfs, &file, "tinykitty",
26519ea8026Sopenharmony_ci            LFS_O_RDWR | LFS_O_CREAT) => 0;
26619ea8026Sopenharmony_ci    int j = 0;
26719ea8026Sopenharmony_ci    int k = 0;
26819ea8026Sopenharmony_ci
26919ea8026Sopenharmony_ci    uint8_t buffer[1024];
27019ea8026Sopenharmony_ci    memcpy(buffer, "abcdefghijklmnopqrstuvwxyz", 26);
27119ea8026Sopenharmony_ci    for (unsigned i = 0; i < SIZE; i++) {
27219ea8026Sopenharmony_ci        lfs_file_write(&lfs, &file, &buffer[j++ % 26], 1) => 1;
27319ea8026Sopenharmony_ci        lfs_file_tell(&lfs, &file) => i+1;
27419ea8026Sopenharmony_ci        lfs_file_size(&lfs, &file) => i+1;
27519ea8026Sopenharmony_ci    }
27619ea8026Sopenharmony_ci
27719ea8026Sopenharmony_ci    lfs_file_seek(&lfs, &file, 0, LFS_SEEK_SET) => 0;
27819ea8026Sopenharmony_ci    lfs_file_tell(&lfs, &file) => 0;
27919ea8026Sopenharmony_ci    lfs_file_size(&lfs, &file) => SIZE;
28019ea8026Sopenharmony_ci    for (unsigned i = 0; i < SIZE; i++) {
28119ea8026Sopenharmony_ci        uint8_t c;
28219ea8026Sopenharmony_ci        lfs_file_read(&lfs, &file, &c, 1) => 1;
28319ea8026Sopenharmony_ci        c => buffer[k++ % 26];
28419ea8026Sopenharmony_ci    }
28519ea8026Sopenharmony_ci
28619ea8026Sopenharmony_ci    lfs_file_sync(&lfs, &file) => 0;
28719ea8026Sopenharmony_ci    lfs_file_tell(&lfs, &file) => SIZE;
28819ea8026Sopenharmony_ci    lfs_file_size(&lfs, &file) => SIZE;
28919ea8026Sopenharmony_ci
29019ea8026Sopenharmony_ci    lfs_file_seek(&lfs, &file, 0, LFS_SEEK_SET) => 0;
29119ea8026Sopenharmony_ci    for (unsigned i = 0; i < SIZE; i++) {
29219ea8026Sopenharmony_ci        lfs_file_write(&lfs, &file, &buffer[j++ % 26], 1) => 1;
29319ea8026Sopenharmony_ci        lfs_file_tell(&lfs, &file) => i+1;
29419ea8026Sopenharmony_ci        lfs_file_size(&lfs, &file) => SIZE;
29519ea8026Sopenharmony_ci        lfs_file_sync(&lfs, &file) => 0;
29619ea8026Sopenharmony_ci        lfs_file_tell(&lfs, &file) => i+1;
29719ea8026Sopenharmony_ci        lfs_file_size(&lfs, &file) => SIZE;
29819ea8026Sopenharmony_ci        if (i < SIZE-2) {
29919ea8026Sopenharmony_ci            uint8_t c[3];
30019ea8026Sopenharmony_ci            lfs_file_seek(&lfs, &file, -1, LFS_SEEK_CUR) => i;
30119ea8026Sopenharmony_ci            lfs_file_read(&lfs, &file, &c, 3) => 3;
30219ea8026Sopenharmony_ci            lfs_file_tell(&lfs, &file) => i+3;
30319ea8026Sopenharmony_ci            lfs_file_size(&lfs, &file) => SIZE;
30419ea8026Sopenharmony_ci            lfs_file_seek(&lfs, &file, i+1, LFS_SEEK_SET) => i+1;
30519ea8026Sopenharmony_ci            lfs_file_tell(&lfs, &file) => i+1;
30619ea8026Sopenharmony_ci            lfs_file_size(&lfs, &file) => SIZE;
30719ea8026Sopenharmony_ci        }
30819ea8026Sopenharmony_ci    }
30919ea8026Sopenharmony_ci
31019ea8026Sopenharmony_ci    lfs_file_seek(&lfs, &file, 0, LFS_SEEK_SET) => 0;
31119ea8026Sopenharmony_ci    lfs_file_tell(&lfs, &file) => 0;
31219ea8026Sopenharmony_ci    lfs_file_size(&lfs, &file) => SIZE;
31319ea8026Sopenharmony_ci    for (unsigned i = 0; i < SIZE; i++) {
31419ea8026Sopenharmony_ci        uint8_t c;
31519ea8026Sopenharmony_ci        lfs_file_read(&lfs, &file, &c, 1) => 1;
31619ea8026Sopenharmony_ci        c => buffer[k++ % 26];
31719ea8026Sopenharmony_ci    }
31819ea8026Sopenharmony_ci
31919ea8026Sopenharmony_ci    lfs_file_sync(&lfs, &file) => 0;
32019ea8026Sopenharmony_ci    lfs_file_tell(&lfs, &file) => SIZE;
32119ea8026Sopenharmony_ci    lfs_file_size(&lfs, &file) => SIZE;
32219ea8026Sopenharmony_ci
32319ea8026Sopenharmony_ci    lfs_file_close(&lfs, &file) => 0;
32419ea8026Sopenharmony_ci    lfs_unmount(&lfs) => 0;
32519ea8026Sopenharmony_ci'''
32619ea8026Sopenharmony_ci
32719ea8026Sopenharmony_ci# file seek and write with power-loss
32819ea8026Sopenharmony_ci[cases.test_seek_reentrant_write]
32919ea8026Sopenharmony_ci# must be power-of-2 for quadratic probing to be exhaustive
33019ea8026Sopenharmony_cidefines.COUNT = [4, 64, 128]
33119ea8026Sopenharmony_cireentrant = true
33219ea8026Sopenharmony_cicode = '''
33319ea8026Sopenharmony_ci    lfs_t lfs;
33419ea8026Sopenharmony_ci    int err = lfs_mount(&lfs, cfg);
33519ea8026Sopenharmony_ci    if (err) {
33619ea8026Sopenharmony_ci        lfs_format(&lfs, cfg) => 0;
33719ea8026Sopenharmony_ci        lfs_mount(&lfs, cfg) => 0;
33819ea8026Sopenharmony_ci    }
33919ea8026Sopenharmony_ci    lfs_file_t file;
34019ea8026Sopenharmony_ci    uint8_t buffer[1024];
34119ea8026Sopenharmony_ci    err = lfs_file_open(&lfs, &file, "kitty", LFS_O_RDONLY);
34219ea8026Sopenharmony_ci    assert(!err || err == LFS_ERR_NOENT);
34319ea8026Sopenharmony_ci    if (!err) {
34419ea8026Sopenharmony_ci        if (lfs_file_size(&lfs, &file) != 0) {
34519ea8026Sopenharmony_ci            lfs_file_size(&lfs, &file) => 11*COUNT;
34619ea8026Sopenharmony_ci            for (int j = 0; j < COUNT; j++) {
34719ea8026Sopenharmony_ci                memset(buffer, 0, 11+1);
34819ea8026Sopenharmony_ci                lfs_file_read(&lfs, &file, buffer, 11) => 11;
34919ea8026Sopenharmony_ci                assert(memcmp(buffer, "kittycatcat", 11) == 0 ||
35019ea8026Sopenharmony_ci                       memcmp(buffer, "doggodogdog", 11) == 0);
35119ea8026Sopenharmony_ci            }
35219ea8026Sopenharmony_ci        }
35319ea8026Sopenharmony_ci        lfs_file_close(&lfs, &file) => 0;
35419ea8026Sopenharmony_ci    }
35519ea8026Sopenharmony_ci
35619ea8026Sopenharmony_ci    lfs_file_open(&lfs, &file, "kitty", LFS_O_WRONLY | LFS_O_CREAT) => 0;
35719ea8026Sopenharmony_ci    if (lfs_file_size(&lfs, &file) == 0) {
35819ea8026Sopenharmony_ci        for (int j = 0; j < COUNT; j++) {
35919ea8026Sopenharmony_ci            strcpy((char*)buffer, "kittycatcat");
36019ea8026Sopenharmony_ci            size_t size = strlen((char*)buffer);
36119ea8026Sopenharmony_ci            lfs_file_write(&lfs, &file, buffer, size) => size;
36219ea8026Sopenharmony_ci        }
36319ea8026Sopenharmony_ci    }
36419ea8026Sopenharmony_ci    lfs_file_close(&lfs, &file) => 0;
36519ea8026Sopenharmony_ci
36619ea8026Sopenharmony_ci    strcpy((char*)buffer, "doggodogdog");
36719ea8026Sopenharmony_ci    size_t size = strlen((char*)buffer);
36819ea8026Sopenharmony_ci
36919ea8026Sopenharmony_ci    lfs_file_open(&lfs, &file, "kitty", LFS_O_RDWR) => 0;
37019ea8026Sopenharmony_ci    lfs_file_size(&lfs, &file) => COUNT*size;
37119ea8026Sopenharmony_ci    // seek and write using quadratic probing to touch all
37219ea8026Sopenharmony_ci    // 11-byte words in the file
37319ea8026Sopenharmony_ci    lfs_off_t off = 0;
37419ea8026Sopenharmony_ci    for (int j = 0; j < COUNT; j++) {
37519ea8026Sopenharmony_ci        off = (5*off + 1) % COUNT;
37619ea8026Sopenharmony_ci        lfs_file_seek(&lfs, &file, off*size, LFS_SEEK_SET) => off*size;
37719ea8026Sopenharmony_ci        lfs_file_read(&lfs, &file, buffer, size) => size;
37819ea8026Sopenharmony_ci        assert(memcmp(buffer, "kittycatcat", size) == 0 ||
37919ea8026Sopenharmony_ci               memcmp(buffer, "doggodogdog", size) == 0);
38019ea8026Sopenharmony_ci        if (memcmp(buffer, "doggodogdog", size) != 0) {
38119ea8026Sopenharmony_ci            lfs_file_seek(&lfs, &file, off*size, LFS_SEEK_SET) => off*size;
38219ea8026Sopenharmony_ci            strcpy((char*)buffer, "doggodogdog");
38319ea8026Sopenharmony_ci            lfs_file_write(&lfs, &file, buffer, size) => size;
38419ea8026Sopenharmony_ci            lfs_file_seek(&lfs, &file, off*size, LFS_SEEK_SET) => off*size;
38519ea8026Sopenharmony_ci            lfs_file_read(&lfs, &file, buffer, size) => size;
38619ea8026Sopenharmony_ci            assert(memcmp(buffer, "doggodogdog", size) == 0);
38719ea8026Sopenharmony_ci            lfs_file_sync(&lfs, &file) => 0;
38819ea8026Sopenharmony_ci            lfs_file_seek(&lfs, &file, off*size, LFS_SEEK_SET) => off*size;
38919ea8026Sopenharmony_ci            lfs_file_read(&lfs, &file, buffer, size) => size;
39019ea8026Sopenharmony_ci            assert(memcmp(buffer, "doggodogdog", size) == 0);
39119ea8026Sopenharmony_ci        }
39219ea8026Sopenharmony_ci    }
39319ea8026Sopenharmony_ci    lfs_file_close(&lfs, &file) => 0;
39419ea8026Sopenharmony_ci
39519ea8026Sopenharmony_ci    lfs_file_open(&lfs, &file, "kitty", LFS_O_RDWR) => 0;
39619ea8026Sopenharmony_ci    lfs_file_size(&lfs, &file) => COUNT*size;
39719ea8026Sopenharmony_ci    for (int j = 0; j < COUNT; j++) {
39819ea8026Sopenharmony_ci        lfs_file_read(&lfs, &file, buffer, size) => size;
39919ea8026Sopenharmony_ci        assert(memcmp(buffer, "doggodogdog", size) == 0);
40019ea8026Sopenharmony_ci    }
40119ea8026Sopenharmony_ci    lfs_file_close(&lfs, &file) => 0;
40219ea8026Sopenharmony_ci    lfs_unmount(&lfs) => 0;
40319ea8026Sopenharmony_ci'''
404