Explorar o código

Migrated the bulk of the feature-specific tests

This involved some minor tweaks for the various types of tests, added
predicates to the test framework (necessary for test_entries and
test_alloc), and cleaned up some of the testing semantics such as
reporting how many tests are filtered, showing permutation config on
the result screen, and properly inheriting suite config in cases.
Christopher Haster %!s(int64=6) %!d(string=hai) anos
pai
achega
b06ce54279

+ 1 - 0
.gitignore

@@ -7,3 +7,4 @@
 blocks/
 lfs
 test.c
+tests_/*.toml.*

+ 88 - 38
scripts/test_.py

@@ -4,6 +4,20 @@
 # .toml files stored in the tests directory.
 #
 
+# TODO
+# - nargs > 1?
+# x show perm config on failure
+# x filtering
+# n show perm config on verbose?
+# - better lineno tracking for cases?
+# n non-int perms?
+# - different path format?
+# - suite.prologue, suite.epilogue
+# - in
+# x change BLOCK_CYCLES to -1 by default
+# x change persist behaviour
+# x config chaining correct
+
 import toml
 import glob
 import re
@@ -20,7 +34,7 @@ import shlex
 TESTDIR = 'tests_'
 RULES = """
 define FLATTEN
-%$(subst /,.,$(target:.c=.t.c)): $(target)
+%$(subst /,.,$(target:.c=.tc)): $(target)
     cat <(echo '#line 1 "$$<"') $$< > $$@
 endef
 $(foreach target,$(SRC),$(eval $(FLATTEN)))
@@ -28,12 +42,12 @@ $(foreach target,$(SRC),$(eval $(FLATTEN)))
 -include tests_/*.d
 
 .SECONDARY:
-%.c: %.t.c
+%.c: %.tc
     ./scripts/explode_asserts.py $< -o $@
 
 %.test: override CFLAGS += -fdiagnostics-color=always
 %.test: override CFLAGS += -ggdb
-%.test: %.test.o $(foreach f,$(subst /,.,$(SRC:.c=.o)),%.test.$f)
+%.test: %.test.o $(foreach f,$(subst /,.,$(SRC:.c=.o)),%.$f)
     $(CC) $(CFLAGS) $^ $(LFLAGS) -o $@
 """
 GLOBALS = """
@@ -49,7 +63,7 @@ DEFINES = {
     "LFS_PROG_SIZE": "LFS_READ_SIZE",
     "LFS_BLOCK_SIZE": 512,
     "LFS_BLOCK_COUNT": 1024,
-    "LFS_BLOCK_CYCLES": 1024,
+    "LFS_BLOCK_CYCLES": -1,
     "LFS_CACHE_SIZE": "(64 % LFS_PROG_SIZE == 0 ? 64 : LFS_PROG_SIZE)",
     "LFS_LOOKAHEAD_SIZE": 16,
     "LFS_ERASE_VALUE": 0xff,
@@ -122,12 +136,19 @@ class TestCase:
 
         self.code = config['code']
         self.defines = config.get('define', {})
+        self.if_ = config.get('if', None)
         self.leaky = config.get('leaky', False)
 
     def __str__(self):
         if hasattr(self, 'permno'):
-            return '%s[%d,%d]' % (
-                self.suite.name, self.caseno, self.permno)
+            if any(k not in self.case.defines for k in self.defines):
+                return '%s[%d,%d] (%s)' % (
+                    self.suite.name, self.caseno, self.permno, ', '.join(
+                        '%s=%s' % (k, v) for k, v in self.defines.items()
+                        if k not in self.case.defines))
+            else:
+                return '%s[%d,%d]' % (
+                    self.suite.name, self.caseno, self.permno)
         else:
             return '%s[%d]' % (
                 self.suite.name, self.caseno)
@@ -176,12 +197,26 @@ class TestCase:
 
         f.write('}\n')
 
+    def shouldtest(self, **args):
+        if self.if_ is not None:
+            return eval(self.if_, None, self.defines.copy())
+        else:
+            return True
+
     def test(self, exec=[], persist=False, gdb=False, failure=None, **args):
         # build command
         cmd = exec + ['./%s.test' % self.suite.path,
             repr(self.caseno), repr(self.permno)]
+
+        # persist disk or keep in RAM for speed?
         if persist:
-            cmd.append(self.suite.path + '.test.disk')
+            if persist != 'noerase':
+                try:
+                    os.remove(self.suite.path + '.disk')
+                except FileNotFoundError:
+                    pass
+
+            cmd.append(self.suite.path + '.disk')
 
         # failed? drop into debugger?
         if gdb and failure:
@@ -244,10 +279,10 @@ class ValgrindTestCase(TestCase):
         self.leaky = config.get('leaky', False)
         super().__init__(config, **args)
 
-    def test(self, exec=[], **args):
-        if self.leaky:
-            return
+    def shouldtest(self, **args):
+        return not self.leaky and super().shouldtest(**args)
 
+    def test(self, exec=[], **args):
         exec = exec + [
             'valgrind',
             '--leak-check=full',
@@ -260,14 +295,14 @@ class ReentrantTestCase(TestCase):
         self.reentrant = config.get('reentrant', False)
         super().__init__(config, **args)
 
-    def test(self, exec=[], persist=False, gdb=False, failure=None, **args):
-        if not self.reentrant:
-            return
+    def shouldtest(self, **args):
+        return self.reentrant and super().shouldtest(**args)
 
+    def test(self, exec=[], persist=False, gdb=False, failure=None, **args):
         # clear disk first?
-        if not persist:
+        if persist != 'noerase':
             try:
-                os.remove(self.suite.path + '.test.disk')
+                os.remove(self.suite.path + '.disk')
             except FileNotFoundError:
                 pass
 
@@ -293,7 +328,7 @@ class ReentrantTestCase(TestCase):
                     '33',
                 '--args']
             try:
-                return super().test(exec=nexec, persist=True, **args)
+                return super().test(exec=nexec, persist='noerase', **args)
             except TestFailure as nfailure:
                 if nfailure.returncode == 33:
                     continue
@@ -326,6 +361,11 @@ class TestSuite:
         # create initial test cases
         self.cases = []
         for i, (case, lineno) in enumerate(zip(config['case'], linenos)):
+            # give our case's config a copy of our "global" config
+            for k, v in config.items():
+                if k not in case:
+                    case[k] = v
+            # initialize test case
             self.cases.append(self.TestCase(case,
                 suite=self, caseno=i, lineno=lineno, **args))
 
@@ -430,7 +470,7 @@ class TestSuite:
         # add test-related rules
         rules = RULES.replace(4*' ', '\t')
 
-        with open(self.path + '.test.mk', 'w') as mk:
+        with open(self.path + '.mk', 'w') as mk:
             mk.write(rules)
             mk.write('\n')
 
@@ -440,13 +480,13 @@ class TestSuite:
                     self.path+'.test', k, v))
 
             # write test.c in base64 so make can decide when to rebuild
-            mk.write('%s: %s\n' % (self.path+'.test.t.c', self.path))
+            mk.write('%s: %s\n' % (self.path+'.test.tc', self.path))
             mk.write('\t@base64 -d <<< ')
             mk.write(base64.b64encode(
                 f.getvalue().encode('utf8')).decode('utf8'))
             mk.write(' > $@\n')
 
-        self.makefile = self.path + '.test.mk'
+        self.makefile = self.path + '.mk'
         self.target = self.path + '.test'
         return self.makefile, self.target
 
@@ -460,6 +500,8 @@ class TestSuite:
                 continue
             if permno is not None and perm.permno != permno:
                 continue
+            if not perm.shouldtest(**args):
+                continue
 
             try:
                 result = perm.test(**args)
@@ -473,11 +515,10 @@ class TestSuite:
                         sys.stdout.write('\n')
                     raise
             else:
-                if result == PASS:
-                    perm.result = PASS
-                    if not args.get('verbose', True):
-                        sys.stdout.write(PASS)
-                        sys.stdout.flush()
+                perm.result = PASS
+                if not args.get('verbose', True):
+                    sys.stdout.write(PASS)
+                    sys.stdout.flush()
 
         if not args.get('verbose', True):
             sys.stdout.write('\n')
@@ -581,6 +622,13 @@ def main(**args):
         sum(len(suite.cases) for suite in suites),
         sum(len(suite.perms) for suite in suites)))
 
+    filtered = 0
+    for suite in suites:
+        for perm in suite.perms:
+            filtered += perm.shouldtest(**args)
+    if filtered != sum(len(suite.perms) for suite in suites):
+        print('filtered down to %d permutations' % filtered)
+
     print('====== testing ======')
     try:
         for suite in suites:
@@ -588,18 +636,6 @@ def main(**args):
     except TestFailure:
         pass
 
-    if args.get('gdb', False):
-        failure = None
-        for suite in suites:
-            for perm in suite.perms:
-                if getattr(perm, 'result', PASS) != PASS:
-                    failure = perm.result
-        if failure is not None:
-            print('======= gdb ======')
-            # drop into gdb
-            failure.case.test(failure=failure, **args)
-            sys.exit(0)
-
     print('====== results ======')
     passed = 0
     failed = 0
@@ -633,6 +669,19 @@ def main(**args):
                 sys.stdout.write('\n')
                 failed += 1
 
+    if args.get('gdb', False):
+        failure = None
+        for suite in suites:
+            for perm in suite.perms:
+                if getattr(perm, 'result', PASS) != PASS:
+                    failure = perm.result
+        if failure is not None:
+            print('======= gdb ======')
+            # drop into gdb
+            failure.case.test(failure=failure, **args)
+            sys.exit(0)
+
+
     print('tests passed: %d' % passed)
     print('tests failed: %d' % failed)
 
@@ -652,8 +701,9 @@ if __name__ == "__main__":
         help="Output everything that is happening.")
     parser.add_argument('-k', '--keep-going', action='store_true',
         help="Run all tests instead of stopping on first error. Useful for CI.")
-    parser.add_argument('-p', '--persist', action='store_true',
-        help="Don't reset the tests disk before each test.")
+    parser.add_argument('-p', '--persist', choices=['erase', 'noerase'],
+        nargs='?', const='erase',
+        help="Store disk image in a file.")
     parser.add_argument('-g', '--gdb', choices=['init', 'start', 'assert'],
         nargs='?', const='assert',
         help="Drop into gdb on test failure.")

+ 566 - 0
tests_/test_alloc.toml

@@ -0,0 +1,566 @@
+# allocator tests
+# note for these to work there are many constraints on the device geometry
+
+[[case]] # parallel allocation test
+code = '''
+    const char *names[FILES] = {"bacon", "eggs", "pancakes"};
+    lfs_file_t files[FILES];
+
+    lfs_format(&lfs, &cfg) => 0;
+    lfs_mount(&lfs, &cfg) => 0;
+    lfs_mkdir(&lfs, "breakfast") => 0;
+    lfs_unmount(&lfs) => 0;
+
+    lfs_mount(&lfs, &cfg) => 0;
+    for (int n = 0; n < FILES; n++) {
+        sprintf(path, "breakfast/%s", names[n]);
+        lfs_file_open(&lfs, &files[n], path,
+                LFS_O_WRONLY | LFS_O_CREAT | LFS_O_APPEND) => 0;
+    }
+    for (int n = 0; n < FILES; n++) {
+        size = strlen(names[n]);
+        for (lfs_size_t i = 0; i < SIZE; i += size) {
+            lfs_file_write(&lfs, &files[n], names[n], size) => size;
+        }
+    }
+    for (int n = 0; n < FILES; n++) {
+        lfs_file_close(&lfs, &files[n]) => 0;
+    }
+    lfs_unmount(&lfs) => 0;
+
+    lfs_mount(&lfs, &cfg) => 0;
+    for (int n = 0; n < FILES; n++) {
+        sprintf(path, "breakfast/%s", names[n]);
+        lfs_file_open(&lfs, &file, path, LFS_O_RDONLY) => 0;
+        size = strlen(names[n]);
+        for (lfs_size_t i = 0; i < SIZE; i += size) {
+            lfs_file_read(&lfs, &file, buffer, size) => size;
+            assert(memcmp(buffer, names[n], size) == 0);
+        }
+        lfs_file_close(&lfs, &file) => 0;
+    }
+    lfs_unmount(&lfs) => 0;
+'''
+define.FILES = 3
+define.SIZE = '(((LFS_BLOCK_SIZE-8)*(LFS_BLOCK_COUNT-4)) / FILES)'
+
+[[case]] # serial allocation test
+code = '''
+    const char *names[FILES] = {"bacon", "eggs", "pancakes"};
+
+    lfs_format(&lfs, &cfg) => 0;
+    lfs_mount(&lfs, &cfg) => 0;
+    lfs_mkdir(&lfs, "breakfast") => 0;
+    lfs_unmount(&lfs) => 0;
+
+    for (int n = 0; n < FILES; n++) {
+        lfs_mount(&lfs, &cfg) => 0;
+        sprintf(path, "breakfast/%s", names[n]);
+        lfs_file_open(&lfs, &file, path,
+                LFS_O_WRONLY | LFS_O_CREAT | LFS_O_APPEND) => 0;
+        size = strlen(names[n]);
+        memcpy(buffer, names[n], size);
+        for (int i = 0; i < SIZE; i += size) {
+            lfs_file_write(&lfs, &file, buffer, size) => size;
+        }
+        lfs_file_close(&lfs, &file) => 0;
+        lfs_unmount(&lfs) => 0;
+    }
+
+    lfs_mount(&lfs, &cfg) => 0;
+    for (int n = 0; n < FILES; n++) {
+        sprintf(path, "breakfast/%s", names[n]);
+        lfs_file_open(&lfs, &file, path, LFS_O_RDONLY) => 0;
+        size = strlen(names[n]);
+        for (int i = 0; i < SIZE; i += size) {
+            lfs_file_read(&lfs, &file, buffer, size) => size;
+            assert(memcmp(buffer, names[n], size) == 0);
+        }
+        lfs_file_close(&lfs, &file) => 0;
+    }
+    lfs_unmount(&lfs) => 0;
+'''
+define.FILES = 3
+define.SIZE = '(((LFS_BLOCK_SIZE-8)*(LFS_BLOCK_COUNT-4)) / FILES)'
+
+[[case]] # parallel allocation reuse test
+code = '''
+    const char *names[FILES] = {"bacon", "eggs", "pancakes"};
+    lfs_file_t files[FILES];
+
+    lfs_format(&lfs, &cfg) => 0;
+
+    for (int c = 0; c < CYCLES; c++) {
+        lfs_mount(&lfs, &cfg) => 0;
+        lfs_mkdir(&lfs, "breakfast") => 0;
+        lfs_unmount(&lfs) => 0;
+
+        lfs_mount(&lfs, &cfg) => 0;
+        for (int n = 0; n < FILES; n++) {
+            sprintf(path, "breakfast/%s", names[n]);
+            lfs_file_open(&lfs, &files[n], path,
+                    LFS_O_WRONLY | LFS_O_CREAT | LFS_O_APPEND) => 0;
+        }
+        for (int n = 0; n < FILES; n++) {
+            size = strlen(names[n]);
+            for (int i = 0; i < SIZE; i += size) {
+                lfs_file_write(&lfs, &files[n], names[n], size) => size;
+            }
+        }
+        for (int n = 0; n < FILES; n++) {
+            lfs_file_close(&lfs, &files[n]) => 0;
+        }
+        lfs_unmount(&lfs) => 0;
+
+        lfs_mount(&lfs, &cfg) => 0;
+        for (int n = 0; n < FILES; n++) {
+            sprintf(path, "breakfast/%s", names[n]);
+            lfs_file_open(&lfs, &file, path, LFS_O_RDONLY) => 0;
+            size = strlen(names[n]);
+            for (int i = 0; i < SIZE; i += size) {
+                lfs_file_read(&lfs, &file, buffer, size) => size;
+                assert(memcmp(buffer, names[n], size) == 0);
+            }
+            lfs_file_close(&lfs, &file) => 0;
+        }
+        lfs_unmount(&lfs) => 0;
+
+        lfs_mount(&lfs, &cfg) => 0;
+        for (int n = 0; n < FILES; n++) {
+            sprintf(path, "breakfast/%s", names[n]);
+            lfs_remove(&lfs, path) => 0;
+        }
+        lfs_remove(&lfs, "breakfast") => 0;
+        lfs_unmount(&lfs) => 0;
+    }
+'''
+define.FILES = 3
+define.SIZE = '(((LFS_BLOCK_SIZE-8)*(LFS_BLOCK_COUNT-4)) / FILES)'
+define.CYCLES = [1, 10]
+
+[[case]] # serial allocation reuse test
+code = '''
+    const char *names[FILES] = {"bacon", "eggs", "pancakes"};
+
+    lfs_format(&lfs, &cfg) => 0;
+
+    for (int c = 0; c < CYCLES; c++) {
+        lfs_mount(&lfs, &cfg) => 0;
+        lfs_mkdir(&lfs, "breakfast") => 0;
+        lfs_unmount(&lfs) => 0;
+
+        for (int n = 0; n < FILES; n++) {
+            lfs_mount(&lfs, &cfg) => 0;
+            sprintf(path, "breakfast/%s", names[n]);
+            lfs_file_open(&lfs, &file, path,
+                    LFS_O_WRONLY | LFS_O_CREAT | LFS_O_APPEND) => 0;
+            size = strlen(names[n]);
+            memcpy(buffer, names[n], size);
+            for (int i = 0; i < SIZE; i += size) {
+                lfs_file_write(&lfs, &file, buffer, size) => size;
+            }
+            lfs_file_close(&lfs, &file) => 0;
+            lfs_unmount(&lfs) => 0;
+        }
+
+        lfs_mount(&lfs, &cfg) => 0;
+        for (int n = 0; n < FILES; n++) {
+            sprintf(path, "breakfast/%s", names[n]);
+            lfs_file_open(&lfs, &file, path, LFS_O_RDONLY) => 0;
+            size = strlen(names[n]);
+            for (int i = 0; i < SIZE; i += size) {
+                lfs_file_read(&lfs, &file, buffer, size) => size;
+                assert(memcmp(buffer, names[n], size) == 0);
+            }
+            lfs_file_close(&lfs, &file) => 0;
+        }
+        lfs_unmount(&lfs) => 0;
+
+        lfs_mount(&lfs, &cfg) => 0;
+        for (int n = 0; n < FILES; n++) {
+            sprintf(path, "breakfast/%s", names[n]);
+            lfs_remove(&lfs, path) => 0;
+        }
+        lfs_remove(&lfs, "breakfast") => 0;
+        lfs_unmount(&lfs) => 0;
+    }
+'''
+define.FILES = 3
+define.SIZE = '(((LFS_BLOCK_SIZE-8)*(LFS_BLOCK_COUNT-4)) / FILES)'
+define.CYCLES = [1, 10]
+
+[[case]] # exhaustion test
+code = '''
+    lfs_format(&lfs, &cfg) => 0;
+    lfs_mount(&lfs, &cfg) => 0;
+    lfs_file_open(&lfs, &file, "exhaustion", LFS_O_WRONLY | LFS_O_CREAT);
+    size = strlen("exhaustion");
+    memcpy(buffer, "exhaustion", size);
+    lfs_file_write(&lfs, &file, buffer, size) => size;
+    lfs_file_sync(&lfs, &file) => 0;
+
+    size = strlen("blahblahblahblah");
+    memcpy(buffer, "blahblahblahblah", size);
+    lfs_ssize_t res;
+    while (true) {
+        res = lfs_file_write(&lfs, &file, buffer, size);
+        if (res < 0) {
+            break;
+        }
+
+        res => size;
+    }
+    res => LFS_ERR_NOSPC;
+
+    lfs_file_close(&lfs, &file) => 0;
+    lfs_unmount(&lfs) => 0;
+
+    lfs_mount(&lfs, &cfg) => 0;
+    lfs_file_open(&lfs, &file, "exhaustion", LFS_O_RDONLY);
+    size = strlen("exhaustion");
+    lfs_file_size(&lfs, &file) => size;
+    lfs_file_read(&lfs, &file, buffer, size) => size;
+    memcmp(buffer, "exhaustion", size) => 0;
+    lfs_file_close(&lfs, &file) => 0;
+    lfs_unmount(&lfs) => 0;
+'''
+
+[[case]] # exhaustion wraparound test
+code = '''
+    lfs_format(&lfs, &cfg) => 0;
+    lfs_mount(&lfs, &cfg) => 0;
+
+    lfs_file_open(&lfs, &file, "padding", LFS_O_WRONLY | LFS_O_CREAT);
+    size = strlen("buffering");
+    memcpy(buffer, "buffering", size);
+    for (int i = 0; i < SIZE; i += size) {
+        lfs_file_write(&lfs, &file, buffer, size) => size;
+    }
+    lfs_file_close(&lfs, &file) => 0;
+    lfs_remove(&lfs, "padding") => 0;
+
+    lfs_file_open(&lfs, &file, "exhaustion", LFS_O_WRONLY | LFS_O_CREAT);
+    size = strlen("exhaustion");
+    memcpy(buffer, "exhaustion", size);
+    lfs_file_write(&lfs, &file, buffer, size) => size;
+    lfs_file_sync(&lfs, &file) => 0;
+
+    size = strlen("blahblahblahblah");
+    memcpy(buffer, "blahblahblahblah", size);
+    lfs_ssize_t res;
+    while (true) {
+        res = lfs_file_write(&lfs, &file, buffer, size);
+        if (res < 0) {
+            break;
+        }
+
+        res => size;
+    }
+    res => LFS_ERR_NOSPC;
+
+    lfs_file_close(&lfs, &file) => 0;
+    lfs_unmount(&lfs) => 0;
+
+    lfs_mount(&lfs, &cfg) => 0;
+    lfs_file_open(&lfs, &file, "exhaustion", LFS_O_RDONLY);
+    size = strlen("exhaustion");
+    lfs_file_size(&lfs, &file) => size;
+    lfs_file_read(&lfs, &file, buffer, size) => size;
+    memcmp(buffer, "exhaustion", size) => 0;
+    lfs_file_close(&lfs, &file) => 0;
+    lfs_remove(&lfs, "exhaustion") => 0;
+    lfs_unmount(&lfs) => 0;
+'''
+define.SIZE = '(((LFS_BLOCK_SIZE-8)*(LFS_BLOCK_COUNT-4)) / 3)'
+
+[[case]] # dir exhaustion test
+code = '''
+    lfs_format(&lfs, &cfg) => 0;
+    lfs_mount(&lfs, &cfg) => 0;
+
+    // find out max file size
+    lfs_mkdir(&lfs, "exhaustiondir") => 0;
+    size = strlen("blahblahblahblah");
+    memcpy(buffer, "blahblahblahblah", size);
+    lfs_file_open(&lfs, &file, "exhaustion", LFS_O_WRONLY | LFS_O_CREAT);
+    int count = 0;
+    while (true) {
+        err = lfs_file_write(&lfs, &file, buffer, size);
+        if (err < 0) {
+            break;
+        }
+
+        count += 1;
+    }
+    err => LFS_ERR_NOSPC;
+    lfs_file_close(&lfs, &file) => 0;
+
+    lfs_remove(&lfs, "exhaustion") => 0;
+    lfs_remove(&lfs, "exhaustiondir") => 0;
+
+    // see if dir fits with max file size
+    lfs_file_open(&lfs, &file, "exhaustion", LFS_O_WRONLY | LFS_O_CREAT);
+    for (int i = 0; i < count; i++) {
+        lfs_file_write(&lfs, &file, buffer, size) => size;
+    }
+    lfs_file_close(&lfs, &file) => 0;
+
+    lfs_mkdir(&lfs, "exhaustiondir") => 0;
+    lfs_remove(&lfs, "exhaustiondir") => 0;
+    lfs_remove(&lfs, "exhaustion") => 0;
+
+    // see if dir fits with > max file size
+    lfs_file_open(&lfs, &file, "exhaustion", LFS_O_WRONLY | LFS_O_CREAT);
+    for (int i = 0; i < count+1; i++) {
+        lfs_file_write(&lfs, &file, buffer, size) => size;
+    }
+    lfs_file_close(&lfs, &file) => 0;
+
+    lfs_mkdir(&lfs, "exhaustiondir") => LFS_ERR_NOSPC;
+
+    lfs_remove(&lfs, "exhaustion") => 0;
+    lfs_unmount(&lfs) => 0;
+'''
+
+# Below, I don't like these tests. They're fragile and depend _heavily_
+# on the geometry of the block device. But they are valuable. Eventually they
+# should be removed and replaced with generalized tests.
+
+[[case]] # chained dir exhaustion test
+code = '''
+    lfs_format(&lfs, &cfg) => 0;
+    lfs_mount(&lfs, &cfg) => 0;
+
+    // find out max file size
+    lfs_mkdir(&lfs, "exhaustiondir") => 0;
+    for (int i = 0; i < 10; i++) {
+        sprintf(path, "dirwithanexhaustivelylongnameforpadding%d", i);
+        lfs_mkdir(&lfs, path) => 0;
+    }
+    size = strlen("blahblahblahblah");
+    memcpy(buffer, "blahblahblahblah", size);
+    lfs_file_open(&lfs, &file, "exhaustion", LFS_O_WRONLY | LFS_O_CREAT);
+    int count = 0;
+    while (true) {
+        err = lfs_file_write(&lfs, &file, buffer, size);
+        if (err < 0) {
+            break;
+        }
+
+        count += 1;
+    }
+    err => LFS_ERR_NOSPC;
+    lfs_file_close(&lfs, &file) => 0;
+
+    lfs_remove(&lfs, "exhaustion") => 0;
+    lfs_remove(&lfs, "exhaustiondir") => 0;
+    for (int i = 0; i < 10; i++) {
+        sprintf(path, "dirwithanexhaustivelylongnameforpadding%d", i);
+        lfs_remove(&lfs, path) => 0;
+    }
+
+    // see that chained dir fails
+    lfs_file_open(&lfs, &file, "exhaustion", LFS_O_WRONLY | LFS_O_CREAT);
+    for (int i = 0; i < count+1; i++) {
+        lfs_file_write(&lfs, &file, buffer, size) => size;
+    }
+    lfs_file_sync(&lfs, &file) => 0;
+
+    for (int i = 0; i < 10; i++) {
+        sprintf(path, "dirwithanexhaustivelylongnameforpadding%d", i);
+        lfs_mkdir(&lfs, path) => 0;
+    }
+
+    lfs_mkdir(&lfs, "exhaustiondir") => LFS_ERR_NOSPC;
+
+    // shorten file to try a second chained dir
+    while (true) {
+        err = lfs_mkdir(&lfs, "exhaustiondir");
+        if (err != LFS_ERR_NOSPC) {
+            break;
+        }
+
+        lfs_ssize_t filesize = lfs_file_size(&lfs, &file);
+        filesize > 0 => true;
+
+        lfs_file_truncate(&lfs, &file, filesize - size) => 0;
+        lfs_file_sync(&lfs, &file) => 0;
+    }
+    err => 0;
+
+    lfs_mkdir(&lfs, "exhaustiondir2") => LFS_ERR_NOSPC;
+
+    lfs_file_close(&lfs, &file) => 0;
+    lfs_unmount(&lfs) => 0;
+'''
+define.LFS_BLOCK_SIZE = 512
+define.LFS_BLOCK_COUNT = 1024
+if = 'LFS_BLOCK_SIZE == 512 and LFS_BLOCK_COUNT == 1024'
+
+[[case]] # split dir test
+code = '''
+    lfs_format(&lfs, &cfg) => 0;
+    lfs_mount(&lfs, &cfg) => 0;
+
+    // create one block hole for half a directory
+    lfs_file_open(&lfs, &file, "bump", LFS_O_WRONLY | LFS_O_CREAT) => 0;
+    for (lfs_size_t i = 0; i < cfg.block_size; i += 2) {
+        memcpy(&buffer[i], "hi", 2);
+    }
+    lfs_file_write(&lfs, &file, buffer, cfg.block_size) => cfg.block_size;
+    lfs_file_close(&lfs, &file) => 0;
+
+    lfs_file_open(&lfs, &file, "exhaustion", LFS_O_WRONLY | LFS_O_CREAT);
+    size = strlen("blahblahblahblah");
+    memcpy(buffer, "blahblahblahblah", size);
+    for (lfs_size_t i = 0;
+            i < (cfg.block_count-4)*(cfg.block_size-8);
+            i += size) {
+        lfs_file_write(&lfs, &file, buffer, size) => size;
+    }
+    lfs_file_close(&lfs, &file) => 0;
+
+    // remount to force reset of lookahead
+    lfs_unmount(&lfs) => 0;
+    lfs_mount(&lfs, &cfg) => 0;
+
+    // open hole
+    lfs_remove(&lfs, "bump") => 0;
+
+    lfs_mkdir(&lfs, "splitdir") => 0;
+    lfs_file_open(&lfs, &file, "splitdir/bump",
+            LFS_O_WRONLY | LFS_O_CREAT) => 0;
+    for (lfs_size_t i = 0; i < cfg.block_size; i += 2) {
+        memcpy(&buffer[i], "hi", 2);
+    }
+    lfs_file_write(&lfs, &file, buffer, 2*cfg.block_size) => LFS_ERR_NOSPC;
+    lfs_file_close(&lfs, &file) => 0;
+
+    lfs_unmount(&lfs) => 0;
+'''
+define.LFS_BLOCK_SIZE = 512
+define.LFS_BLOCK_COUNT = 1024
+if = 'LFS_BLOCK_SIZE == 512 and LFS_BLOCK_COUNT == 1024'
+
+[[case]] # outdated lookahead test
+code = '''
+    lfs_format(&lfs, &cfg) => 0;
+    lfs_mount(&lfs, &cfg) => 0;
+
+    // fill completely with two files
+    lfs_file_open(&lfs, &file, "exhaustion1",
+            LFS_O_WRONLY | LFS_O_CREAT) => 0;
+    size = strlen("blahblahblahblah");
+    memcpy(buffer, "blahblahblahblah", size);
+    for (lfs_size_t i = 0;
+            i < ((cfg.block_count-2)/2)*(cfg.block_size-8);
+            i += size) {
+        lfs_file_write(&lfs, &file, buffer, size) => size;
+    }
+    lfs_file_close(&lfs, &file) => 0;
+
+    lfs_file_open(&lfs, &file, "exhaustion2",
+            LFS_O_WRONLY | LFS_O_CREAT) => 0;
+    size = strlen("blahblahblahblah");
+    memcpy(buffer, "blahblahblahblah", size);
+    for (lfs_size_t i = 0;
+            i < ((cfg.block_count-2+1)/2)*(cfg.block_size-8);
+            i += size) {
+        lfs_file_write(&lfs, &file, buffer, size) => size;
+    }
+    lfs_file_close(&lfs, &file) => 0;
+
+    // remount to force reset of lookahead
+    lfs_unmount(&lfs) => 0;
+    lfs_mount(&lfs, &cfg) => 0;
+
+    // rewrite one file
+    lfs_file_open(&lfs, &file, "exhaustion1",
+            LFS_O_WRONLY | LFS_O_TRUNC) => 0;
+    lfs_file_sync(&lfs, &file) => 0;
+    size = strlen("blahblahblahblah");
+    memcpy(buffer, "blahblahblahblah", size);
+    for (lfs_size_t i = 0;
+            i < ((cfg.block_count-2)/2)*(cfg.block_size-8);
+            i += size) {
+        lfs_file_write(&lfs, &file, buffer, size) => size;
+    }
+    lfs_file_close(&lfs, &file) => 0;
+
+    // rewrite second file, this requires lookahead does not
+    // use old population
+    lfs_file_open(&lfs, &file, "exhaustion2",
+            LFS_O_WRONLY | LFS_O_TRUNC) => 0;
+    lfs_file_sync(&lfs, &file) => 0;
+    size = strlen("blahblahblahblah");
+    memcpy(buffer, "blahblahblahblah", size);
+    for (lfs_size_t i = 0;
+            i < ((cfg.block_count-2+1)/2)*(cfg.block_size-8);
+            i += size) {
+        lfs_file_write(&lfs, &file, buffer, size) => size;
+    }
+    lfs_file_close(&lfs, &file) => 0;
+'''
+define.LFS_BLOCK_SIZE = 512
+define.LFS_BLOCK_COUNT = 1024
+if = 'LFS_BLOCK_SIZE == 512 and LFS_BLOCK_COUNT == 1024'
+
+[[case]] # outdated lookahead and split dir test
+code = '''
+    lfs_format(&lfs, &cfg) => 0;
+    lfs_mount(&lfs, &cfg) => 0;
+
+    // fill completely with two files
+    lfs_file_open(&lfs, &file, "exhaustion1",
+            LFS_O_WRONLY | LFS_O_CREAT) => 0;
+    size = strlen("blahblahblahblah");
+    memcpy(buffer, "blahblahblahblah", size);
+    for (lfs_size_t i = 0;
+            i < ((cfg.block_count-2)/2)*(cfg.block_size-8);
+            i += size) {
+        lfs_file_write(&lfs, &file, buffer, size) => size;
+    }
+    lfs_file_close(&lfs, &file) => 0;
+
+    lfs_file_open(&lfs, &file, "exhaustion2",
+            LFS_O_WRONLY | LFS_O_CREAT) => 0;
+    size = strlen("blahblahblahblah");
+    memcpy(buffer, "blahblahblahblah", size);
+    for (lfs_size_t i = 0;
+            i < ((cfg.block_count-2+1)/2)*(cfg.block_size-8);
+            i += size) {
+        lfs_file_write(&lfs, &file, buffer, size) => size;
+    }
+    lfs_file_close(&lfs, &file) => 0;
+
+    // remount to force reset of lookahead
+    lfs_unmount(&lfs) => 0;
+    lfs_mount(&lfs, &cfg) => 0;
+
+    // rewrite one file with a hole of one block
+    lfs_file_open(&lfs, &file, "exhaustion1",
+            LFS_O_WRONLY | LFS_O_TRUNC) => 0;
+    lfs_file_sync(&lfs, &file) => 0;
+    size = strlen("blahblahblahblah");
+    memcpy(buffer, "blahblahblahblah", size);
+    for (lfs_size_t i = 0;
+            i < ((cfg.block_count-2)/2 - 1)*(cfg.block_size-8);
+            i += size) {
+        lfs_file_write(&lfs, &file, buffer, size) => size;
+    }
+    lfs_file_close(&lfs, &file) => 0;
+
+    // try to allocate a directory, should fail!
+    lfs_mkdir(&lfs, "split") => LFS_ERR_NOSPC;
+
+    // file should not fail
+    lfs_file_open(&lfs, &file, "notasplit",
+            LFS_O_WRONLY | LFS_O_CREAT) => 0;
+    lfs_file_write(&lfs, &file, "hi", 2) => 2;
+    lfs_file_close(&lfs, &file) => 0;
+
+    lfs_unmount(&lfs) => 0;
+'''
+define.LFS_BLOCK_SIZE = 512
+define.LFS_BLOCK_COUNT = 1024
+if = 'LFS_BLOCK_SIZE == 512 and LFS_BLOCK_COUNT == 1024'

+ 305 - 0
tests_/test_attrs.toml

@@ -0,0 +1,305 @@
+[[case]] # set/get attribute
+code = '''
+    lfs_format(&lfs, &cfg) => 0;
+    lfs_mount(&lfs, &cfg) => 0;
+    lfs_mkdir(&lfs, "hello") => 0;
+    lfs_file_open(&lfs, &file, "hello/hello", LFS_O_WRONLY | LFS_O_CREAT) => 0;
+    lfs_file_write(&lfs, &file, "hello", strlen("hello")) => strlen("hello");
+    lfs_file_close(&lfs, &file);
+    lfs_unmount(&lfs) => 0;
+
+    lfs_mount(&lfs, &cfg) => 0;
+    memset(buffer, 0, sizeof(buffer));
+    lfs_setattr(&lfs, "hello", 'A', "aaaa",   4) => 0;
+    lfs_setattr(&lfs, "hello", 'B', "bbbbbb", 6) => 0;
+    lfs_setattr(&lfs, "hello", 'C', "ccccc",  5) => 0;
+    lfs_getattr(&lfs, "hello", 'A', buffer,    4) => 4;
+    lfs_getattr(&lfs, "hello", 'B', buffer+4,  6) => 6;
+    lfs_getattr(&lfs, "hello", 'C', buffer+10, 5) => 5;
+    memcmp(buffer,    "aaaa",   4) => 0;
+    memcmp(buffer+4,  "bbbbbb", 6) => 0;
+    memcmp(buffer+10, "ccccc",  5) => 0;
+
+    lfs_setattr(&lfs, "hello", 'B', "", 0) => 0;
+    lfs_getattr(&lfs, "hello", 'A', buffer,    4) => 4;
+    lfs_getattr(&lfs, "hello", 'B', buffer+4,  6) => 0;
+    lfs_getattr(&lfs, "hello", 'C', buffer+10, 5) => 5;
+    memcmp(buffer,    "aaaa",         4) => 0;
+    memcmp(buffer+4,  "\0\0\0\0\0\0", 6) => 0;
+    memcmp(buffer+10, "ccccc",        5) => 0;
+
+    lfs_removeattr(&lfs, "hello", 'B') => 0;
+    lfs_getattr(&lfs, "hello", 'A', buffer,    4) => 4;
+    lfs_getattr(&lfs, "hello", 'B', buffer+4,  6) => LFS_ERR_NOATTR;
+    lfs_getattr(&lfs, "hello", 'C', buffer+10, 5) => 5;
+    memcmp(buffer,    "aaaa",         4) => 0;
+    memcmp(buffer+4,  "\0\0\0\0\0\0", 6) => 0;
+    memcmp(buffer+10, "ccccc",        5) => 0;
+
+    lfs_setattr(&lfs, "hello", 'B', "dddddd", 6) => 0;
+    lfs_getattr(&lfs, "hello", 'A', buffer,    4) => 4;
+    lfs_getattr(&lfs, "hello", 'B', buffer+4,  6) => 6;
+    lfs_getattr(&lfs, "hello", 'C', buffer+10, 5) => 5;
+    memcmp(buffer,    "aaaa",   4) => 0;
+    memcmp(buffer+4,  "dddddd", 6) => 0;
+    memcmp(buffer+10, "ccccc",  5) => 0;
+
+    lfs_setattr(&lfs, "hello", 'B', "eee", 3) => 0;
+    lfs_getattr(&lfs, "hello", 'A', buffer,    4) => 4;
+    lfs_getattr(&lfs, "hello", 'B', buffer+4,  6) => 3;
+    lfs_getattr(&lfs, "hello", 'C', buffer+10, 5) => 5;
+    memcmp(buffer,    "aaaa",      4) => 0;
+    memcmp(buffer+4,  "eee\0\0\0", 6) => 0;
+    memcmp(buffer+10, "ccccc",     5) => 0;
+
+    lfs_setattr(&lfs, "hello", 'A', buffer, LFS_ATTR_MAX+1) => LFS_ERR_NOSPC;
+    lfs_setattr(&lfs, "hello", 'B', "fffffffff", 9) => 0;
+    lfs_getattr(&lfs, "hello", 'A', buffer,    4) => 4;
+    lfs_getattr(&lfs, "hello", 'B', buffer+4,  6) => 9;
+    lfs_getattr(&lfs, "hello", 'C', buffer+10, 5) => 5;
+
+    lfs_unmount(&lfs) => 0;
+
+    lfs_mount(&lfs, &cfg) => 0;
+    memset(buffer, 0, sizeof(buffer));
+    lfs_getattr(&lfs, "hello", 'A', buffer,    4) => 4;
+    lfs_getattr(&lfs, "hello", 'B', buffer+4,  9) => 9;
+    lfs_getattr(&lfs, "hello", 'C', buffer+13, 5) => 5;
+    memcmp(buffer,    "aaaa",      4) => 0;
+    memcmp(buffer+4,  "fffffffff", 9) => 0;
+    memcmp(buffer+13, "ccccc",     5) => 0;
+
+    lfs_file_open(&lfs, &file, "hello/hello", LFS_O_RDONLY) => 0;
+    lfs_file_read(&lfs, &file, buffer, sizeof(buffer)) => strlen("hello");
+    memcmp(buffer, "hello", strlen("hello")) => 0;
+    lfs_file_close(&lfs, &file);
+    lfs_unmount(&lfs) => 0;
+'''
+
+[[case]] # set/get root attribute
+code = '''
+    lfs_format(&lfs, &cfg) => 0;
+    lfs_mount(&lfs, &cfg) => 0;
+    lfs_mkdir(&lfs, "hello") => 0;
+    lfs_file_open(&lfs, &file, "hello/hello", LFS_O_WRONLY | LFS_O_CREAT) => 0;
+    lfs_file_write(&lfs, &file, "hello", strlen("hello")) => strlen("hello");
+    lfs_file_close(&lfs, &file);
+    lfs_unmount(&lfs) => 0;
+    lfs_mount(&lfs, &cfg) => 0;
+
+    memset(buffer, 0, sizeof(buffer));
+    lfs_setattr(&lfs, "/", 'A', "aaaa",   4) => 0;
+    lfs_setattr(&lfs, "/", 'B', "bbbbbb", 6) => 0;
+    lfs_setattr(&lfs, "/", 'C', "ccccc",  5) => 0;
+    lfs_getattr(&lfs, "/", 'A', buffer,    4) => 4;
+    lfs_getattr(&lfs, "/", 'B', buffer+4,  6) => 6;
+    lfs_getattr(&lfs, "/", 'C', buffer+10, 5) => 5;
+    memcmp(buffer,    "aaaa",   4) => 0;
+    memcmp(buffer+4,  "bbbbbb", 6) => 0;
+    memcmp(buffer+10, "ccccc",  5) => 0;
+
+    lfs_setattr(&lfs, "/", 'B', "", 0) => 0;
+    lfs_getattr(&lfs, "/", 'A', buffer,    4) => 4;
+    lfs_getattr(&lfs, "/", 'B', buffer+4,  6) => 0;
+    lfs_getattr(&lfs, "/", 'C', buffer+10, 5) => 5;
+    memcmp(buffer,    "aaaa",         4) => 0;
+    memcmp(buffer+4,  "\0\0\0\0\0\0", 6) => 0;
+    memcmp(buffer+10, "ccccc",        5) => 0;
+
+    lfs_removeattr(&lfs, "/", 'B') => 0;
+    lfs_getattr(&lfs, "/", 'A', buffer,    4) => 4;
+    lfs_getattr(&lfs, "/", 'B', buffer+4,  6) => LFS_ERR_NOATTR;
+    lfs_getattr(&lfs, "/", 'C', buffer+10, 5) => 5;
+    memcmp(buffer,    "aaaa",         4) => 0;
+    memcmp(buffer+4,  "\0\0\0\0\0\0", 6) => 0;
+    memcmp(buffer+10, "ccccc",        5) => 0;
+
+    lfs_setattr(&lfs, "/", 'B', "dddddd", 6) => 0;
+    lfs_getattr(&lfs, "/", 'A', buffer,    4) => 4;
+    lfs_getattr(&lfs, "/", 'B', buffer+4,  6) => 6;
+    lfs_getattr(&lfs, "/", 'C', buffer+10, 5) => 5;
+    memcmp(buffer,    "aaaa",   4) => 0;
+    memcmp(buffer+4,  "dddddd", 6) => 0;
+    memcmp(buffer+10, "ccccc",  5) => 0;
+
+    lfs_setattr(&lfs, "/", 'B', "eee", 3) => 0;
+    lfs_getattr(&lfs, "/", 'A', buffer,    4) => 4;
+    lfs_getattr(&lfs, "/", 'B', buffer+4,  6) => 3;
+    lfs_getattr(&lfs, "/", 'C', buffer+10, 5) => 5;
+    memcmp(buffer,    "aaaa",      4) => 0;
+    memcmp(buffer+4,  "eee\0\0\0", 6) => 0;
+    memcmp(buffer+10, "ccccc",     5) => 0;
+
+    lfs_setattr(&lfs, "/", 'A', buffer, LFS_ATTR_MAX+1) => LFS_ERR_NOSPC;
+    lfs_setattr(&lfs, "/", 'B', "fffffffff", 9) => 0;
+    lfs_getattr(&lfs, "/", 'A', buffer,    4) => 4;
+    lfs_getattr(&lfs, "/", 'B', buffer+4,  6) => 9;
+    lfs_getattr(&lfs, "/", 'C', buffer+10, 5) => 5;
+    lfs_unmount(&lfs) => 0;
+
+    lfs_mount(&lfs, &cfg) => 0;
+    memset(buffer, 0, sizeof(buffer));
+    lfs_getattr(&lfs, "/", 'A', buffer,    4) => 4;
+    lfs_getattr(&lfs, "/", 'B', buffer+4,  9) => 9;
+    lfs_getattr(&lfs, "/", 'C', buffer+13, 5) => 5;
+    memcmp(buffer,    "aaaa",      4) => 0;
+    memcmp(buffer+4,  "fffffffff", 9) => 0;
+    memcmp(buffer+13, "ccccc",     5) => 0;
+
+    lfs_file_open(&lfs, &file, "hello/hello", LFS_O_RDONLY) => 0;
+    lfs_file_read(&lfs, &file, buffer, sizeof(buffer)) => strlen("hello");
+    memcmp(buffer, "hello", strlen("hello")) => 0;
+    lfs_file_close(&lfs, &file);
+    lfs_unmount(&lfs) => 0;
+'''
+
+[[case]] # set/get file attribute
+code = '''
+    lfs_format(&lfs, &cfg) => 0;
+    lfs_mount(&lfs, &cfg) => 0;
+    lfs_mkdir(&lfs, "hello") => 0;
+    lfs_file_open(&lfs, &file, "hello/hello", LFS_O_WRONLY | LFS_O_CREAT) => 0;
+    lfs_file_write(&lfs, &file, "hello", strlen("hello")) => strlen("hello");
+    lfs_file_close(&lfs, &file);
+    lfs_unmount(&lfs) => 0;
+    lfs_mount(&lfs, &cfg) => 0;
+
+    lfs_mount(&lfs, &cfg) => 0;
+    memset(buffer, 0, sizeof(buffer));
+    struct lfs_attr attrs1[] = {
+        {'A', buffer,    4},
+        {'B', buffer+4,  6},
+        {'C', buffer+10, 5},
+    };
+    struct lfs_file_config cfg1 = {.attrs=attrs1, .attr_count=3};
+
+    lfs_file_opencfg(&lfs, &file, "hello/hello", LFS_O_WRONLY, &cfg1) => 0;
+    memcpy(buffer,    "aaaa",   4);
+    memcpy(buffer+4,  "bbbbbb", 6);
+    memcpy(buffer+10, "ccccc",  5);
+    lfs_file_close(&lfs, &file) => 0;
+    memset(buffer, 0, 15);
+    lfs_file_opencfg(&lfs, &file, "hello/hello", LFS_O_RDONLY, &cfg1) => 0;
+    lfs_file_close(&lfs, &file) => 0;
+    memcmp(buffer,    "aaaa",   4) => 0;
+    memcmp(buffer+4,  "bbbbbb", 6) => 0;
+    memcmp(buffer+10, "ccccc",  5) => 0;
+
+    attrs1[1].size = 0;
+    lfs_file_opencfg(&lfs, &file, "hello/hello", LFS_O_WRONLY, &cfg1) => 0;
+    lfs_file_close(&lfs, &file) => 0;
+    memset(buffer, 0, 15);
+    attrs1[1].size = 6;
+    lfs_file_opencfg(&lfs, &file, "hello/hello", LFS_O_RDONLY, &cfg1) => 0;
+    lfs_file_close(&lfs, &file) => 0;
+    memcmp(buffer,    "aaaa",         4) => 0;
+    memcmp(buffer+4,  "\0\0\0\0\0\0", 6) => 0;
+    memcmp(buffer+10, "ccccc",        5) => 0;
+
+    attrs1[1].size = 6;
+    lfs_file_opencfg(&lfs, &file, "hello/hello", LFS_O_WRONLY, &cfg1) => 0;
+    memcpy(buffer+4,  "dddddd", 6);
+    lfs_file_close(&lfs, &file) => 0;
+    memset(buffer, 0, 15);
+    attrs1[1].size = 6;
+    lfs_file_opencfg(&lfs, &file, "hello/hello", LFS_O_RDONLY, &cfg1) => 0;
+    lfs_file_close(&lfs, &file) => 0;
+    memcmp(buffer,    "aaaa",   4) => 0;
+    memcmp(buffer+4,  "dddddd", 6) => 0;
+    memcmp(buffer+10, "ccccc",  5) => 0;
+
+    attrs1[1].size = 3;
+    lfs_file_opencfg(&lfs, &file, "hello/hello", LFS_O_WRONLY, &cfg1) => 0;
+    memcpy(buffer+4,  "eee", 3);
+    lfs_file_close(&lfs, &file) => 0;
+    memset(buffer, 0, 15);
+    attrs1[1].size = 6;
+    lfs_file_opencfg(&lfs, &file, "hello/hello", LFS_O_RDONLY, &cfg1) => 0;
+    lfs_file_close(&lfs, &file) => 0;
+    memcmp(buffer,    "aaaa",      4) => 0;
+    memcmp(buffer+4,  "eee\0\0\0", 6) => 0;
+    memcmp(buffer+10, "ccccc",     5) => 0;
+
+    attrs1[0].size = LFS_ATTR_MAX+1;
+    lfs_file_opencfg(&lfs, &file, "hello/hello", LFS_O_WRONLY, &cfg1)
+        => LFS_ERR_NOSPC;
+
+    struct lfs_attr attrs2[] = {
+        {'A', buffer,    4},
+        {'B', buffer+4,  9},
+        {'C', buffer+13, 5},
+    };
+    struct lfs_file_config cfg2 = {.attrs=attrs2, .attr_count=3};
+    lfs_file_opencfg(&lfs, &file, "hello/hello", LFS_O_RDWR, &cfg2) => 0;
+    memcpy(buffer+4,  "fffffffff", 9);
+    lfs_file_close(&lfs, &file) => 0;
+    attrs1[0].size = 4;
+    lfs_file_opencfg(&lfs, &file, "hello/hello", LFS_O_RDONLY, &cfg1) => 0;
+    lfs_file_close(&lfs, &file) => 0;
+
+    lfs_unmount(&lfs) => 0;
+
+    lfs_mount(&lfs, &cfg) => 0;
+    memset(buffer, 0, sizeof(buffer));
+    struct lfs_attr attrs3[] = {
+        {'A', buffer,    4},
+        {'B', buffer+4,  9},
+        {'C', buffer+13, 5},
+    };
+    struct lfs_file_config cfg3 = {.attrs=attrs3, .attr_count=3};
+
+    lfs_file_opencfg(&lfs, &file, "hello/hello", LFS_O_RDONLY, &cfg3) => 0;
+    lfs_file_close(&lfs, &file) => 0;
+    memcmp(buffer,    "aaaa",      4) => 0;
+    memcmp(buffer+4,  "fffffffff", 9) => 0;
+    memcmp(buffer+13, "ccccc",     5) => 0;
+
+    lfs_file_open(&lfs, &file, "hello/hello", LFS_O_RDONLY) => 0;
+    lfs_file_read(&lfs, &file, buffer, sizeof(buffer)) => strlen("hello");
+    memcmp(buffer, "hello", strlen("hello")) => 0;
+    lfs_file_close(&lfs, &file);
+    lfs_unmount(&lfs) => 0;
+'''
+
+[[case]] # deferred file attributes
+code = '''
+    lfs_format(&lfs, &cfg) => 0;
+    lfs_mount(&lfs, &cfg) => 0;
+    lfs_mkdir(&lfs, "hello") => 0;
+    lfs_file_open(&lfs, &file, "hello/hello", LFS_O_WRONLY | LFS_O_CREAT) => 0;
+    lfs_file_write(&lfs, &file, "hello", strlen("hello")) => strlen("hello");
+    lfs_file_close(&lfs, &file);
+    lfs_unmount(&lfs) => 0;
+
+    lfs_mount(&lfs, &cfg) => 0;
+    lfs_setattr(&lfs, "hello/hello", 'B', "fffffffff",  9) => 0;
+    lfs_setattr(&lfs, "hello/hello", 'C', "ccccc",      5) => 0;
+
+    memset(buffer, 0, sizeof(buffer));
+    struct lfs_attr attrs1[] = {
+        {'B', "gggg", 4},
+        {'C', "",     0},
+        {'D', "hhhh", 4},
+    };
+    struct lfs_file_config cfg1 = {.attrs=attrs1, .attr_count=3};
+
+    lfs_file_opencfg(&lfs, &file, "hello/hello", LFS_O_WRONLY, &cfg1) => 0;
+
+    lfs_getattr(&lfs, "hello/hello", 'B', buffer,    9) => 9;
+    lfs_getattr(&lfs, "hello/hello", 'C', buffer+9,  9) => 5;
+    lfs_getattr(&lfs, "hello/hello", 'D', buffer+18, 9) => LFS_ERR_NOATTR;
+    memcmp(buffer,    "fffffffff",          9) => 0;
+    memcmp(buffer+9,  "ccccc\0\0\0\0",      9) => 0;
+    memcmp(buffer+18, "\0\0\0\0\0\0\0\0\0", 9) => 0;
+
+    lfs_file_sync(&lfs, &file) => 0;
+    lfs_getattr(&lfs, "hello/hello", 'B', buffer,    9) => 4;
+    lfs_getattr(&lfs, "hello/hello", 'C', buffer+9,  9) => 0;
+    lfs_getattr(&lfs, "hello/hello", 'D', buffer+18, 9) => 4;
+    memcmp(buffer,    "gggg\0\0\0\0\0",     9) => 0;
+    memcmp(buffer+9,  "\0\0\0\0\0\0\0\0\0", 9) => 0;
+    memcmp(buffer+18, "hhhh\0\0\0\0\0",     9) => 0;
+
+    lfs_file_close(&lfs, &file) => 0;
+    lfs_unmount(&lfs) => 0;
+'''

+ 122 - 0
tests_/test_dirs.toml

@@ -714,3 +714,125 @@ code = '''
     lfs_dir_close(&lfs, &dir) => 0;
     lfs_unmount(&lfs) => 0;
 '''
+
+[[case]] # directory seek
+code = '''
+    lfs_format(&lfs, &cfg) => 0;
+    lfs_mount(&lfs, &cfg) => 0;
+    lfs_mkdir(&lfs, "hello") => 0;
+    for (int i = 0; i < COUNT; i++) {
+        sprintf(path, "hello/kitty%03d", i);
+        lfs_mkdir(&lfs, path) => 0;
+    }
+    lfs_unmount(&lfs) => 0;
+
+    for (int j = 2; j < COUNT; j++) {
+        lfs_mount(&lfs, &cfg) => 0;
+        lfs_dir_open(&lfs, &dir, "hello") => 0;
+        lfs_dir_read(&lfs, &dir, &info) => 1;
+        assert(strcmp(info.name, ".") == 0);
+        assert(info.type == LFS_TYPE_DIR);
+        lfs_dir_read(&lfs, &dir, &info) => 1;
+        assert(strcmp(info.name, "..") == 0);
+        assert(info.type == LFS_TYPE_DIR);
+
+        lfs_soff_t pos;
+        for (int i = 0; i < j; i++) {
+            sprintf(path, "kitty%03d", i);
+            lfs_dir_read(&lfs, &dir, &info) => 1;
+            assert(strcmp(info.name, path) == 0);
+            assert(info.type == LFS_TYPE_DIR);
+            pos = lfs_dir_tell(&lfs, &dir);
+            assert(pos >= 0);
+        }
+
+        lfs_dir_seek(&lfs, &dir, pos) => 0;
+        sprintf(path, "kitty%03d", j);
+        lfs_dir_read(&lfs, &dir, &info) => 1;
+        assert(strcmp(info.name, path) == 0);
+        assert(info.type == LFS_TYPE_DIR);
+
+        lfs_dir_rewind(&lfs, &dir) => 0;
+        sprintf(path, "kitty%03d", 0);
+        lfs_dir_read(&lfs, &dir, &info) => 1;
+        assert(strcmp(info.name, ".") == 0);
+        assert(info.type == LFS_TYPE_DIR);
+        lfs_dir_read(&lfs, &dir, &info) => 1;
+        assert(strcmp(info.name, "..") == 0);
+        assert(info.type == LFS_TYPE_DIR);
+        lfs_dir_read(&lfs, &dir, &info) => 1;
+        assert(strcmp(info.name, path) == 0);
+        assert(info.type == LFS_TYPE_DIR);
+
+        lfs_dir_seek(&lfs, &dir, pos) => 0;
+        sprintf(path, "kitty%03d", j);
+        lfs_dir_read(&lfs, &dir, &info) => 1;
+        assert(strcmp(info.name, path) == 0);
+        assert(info.type == LFS_TYPE_DIR);
+
+        lfs_dir_close(&lfs, &dir) => 0;
+        lfs_unmount(&lfs) => 0;
+    }
+'''
+define.COUNT = [4, 128, 132]
+
+[[case]] # root seek
+code = '''
+    lfs_format(&lfs, &cfg) => 0;
+    lfs_mount(&lfs, &cfg) => 0;
+    for (int i = 0; i < COUNT; i++) {
+        sprintf(path, "hi%03d", i);
+        lfs_mkdir(&lfs, path) => 0;
+    }
+    lfs_unmount(&lfs) => 0;
+
+    for (int j = 2; j < COUNT; j++) {
+        lfs_mount(&lfs, &cfg) => 0;
+        lfs_dir_open(&lfs, &dir, "/") => 0;
+        lfs_dir_read(&lfs, &dir, &info) => 1;
+        assert(strcmp(info.name, ".") == 0);
+        assert(info.type == LFS_TYPE_DIR);
+        lfs_dir_read(&lfs, &dir, &info) => 1;
+        assert(strcmp(info.name, "..") == 0);
+        assert(info.type == LFS_TYPE_DIR);
+
+        lfs_soff_t pos;
+        for (int i = 0; i < j; i++) {
+            sprintf(path, "hi%03d", i);
+            lfs_dir_read(&lfs, &dir, &info) => 1;
+            assert(strcmp(info.name, path) == 0);
+            assert(info.type == LFS_TYPE_DIR);
+            pos = lfs_dir_tell(&lfs, &dir);
+            assert(pos >= 0);
+        }
+
+        lfs_dir_seek(&lfs, &dir, pos) => 0;
+        sprintf(path, "hi%03d", j);
+        lfs_dir_read(&lfs, &dir, &info) => 1;
+        assert(strcmp(info.name, path) == 0);
+        assert(info.type == LFS_TYPE_DIR);
+
+        lfs_dir_rewind(&lfs, &dir) => 0;
+        sprintf(path, "hi%03d", 0);
+        lfs_dir_read(&lfs, &dir, &info) => 1;
+        assert(strcmp(info.name, ".") == 0);
+        assert(info.type == LFS_TYPE_DIR);
+        lfs_dir_read(&lfs, &dir, &info) => 1;
+        assert(strcmp(info.name, "..") == 0);
+        assert(info.type == LFS_TYPE_DIR);
+        lfs_dir_read(&lfs, &dir, &info) => 1;
+        assert(strcmp(info.name, path) == 0);
+        assert(info.type == LFS_TYPE_DIR);
+
+        lfs_dir_seek(&lfs, &dir, pos) => 0;
+        sprintf(path, "hi%03d", j);
+        lfs_dir_read(&lfs, &dir, &info) => 1;
+        assert(strcmp(info.name, path) == 0);
+        assert(info.type == LFS_TYPE_DIR);
+
+        lfs_dir_close(&lfs, &dir) => 0;
+        lfs_unmount(&lfs) => 0;
+    }
+'''
+define.COUNT = [4, 128, 132]
+

+ 611 - 0
tests_/test_entries.toml

@@ -0,0 +1,611 @@
+# These tests are for some specific corner cases with neighboring inline files.
+# Note that these tests are intended for 512 byte inline sizes. They should
+# still pass with other inline sizes but wouldn't be testing anything.
+
+define.LFS_CACHE_SIZE = 512
+if = 'LFS_CACHE_SIZE == 512'
+
+[[case]] # entry grow test
+code = '''
+    uint8_t wbuffer[1024];
+    uint8_t rbuffer[1024];
+
+    lfs_format(&lfs, &cfg) => 0;
+    lfs_mount(&lfs, &cfg) => 0;
+
+    // write hi0 20
+    sprintf(path, "hi0"); size = 20;
+    lfs_file_open(&lfs, &file, path,
+            LFS_O_WRONLY | LFS_O_CREAT | LFS_O_TRUNC) => 0;
+    memset(wbuffer, 'c', size);
+    lfs_file_write(&lfs, &file, wbuffer, size) => size;
+    lfs_file_size(&lfs, &file) => size;
+    lfs_file_close(&lfs, &file) => 0;
+    // write hi1 20
+    sprintf(path, "hi1"); size = 20;
+    lfs_file_open(&lfs, &file, path,
+            LFS_O_WRONLY | LFS_O_CREAT | LFS_O_TRUNC) => 0;
+    memset(wbuffer, 'c', size);
+    lfs_file_write(&lfs, &file, wbuffer, size) => size;
+    lfs_file_size(&lfs, &file) => size;
+    lfs_file_close(&lfs, &file) => 0;
+    // write hi2 20
+    sprintf(path, "hi2"); size = 20;
+    lfs_file_open(&lfs, &file, path,
+            LFS_O_WRONLY | LFS_O_CREAT | LFS_O_TRUNC) => 0;
+    memset(wbuffer, 'c', size);
+    lfs_file_write(&lfs, &file, wbuffer, size) => size;
+    lfs_file_size(&lfs, &file) => size;
+    lfs_file_close(&lfs, &file) => 0;
+    // write hi3 20
+    sprintf(path, "hi3"); size = 20;
+    lfs_file_open(&lfs, &file, path,
+            LFS_O_WRONLY | LFS_O_CREAT | LFS_O_TRUNC) => 0;
+    memset(wbuffer, 'c', size);
+    lfs_file_write(&lfs, &file, wbuffer, size) => size;
+    lfs_file_size(&lfs, &file) => size;
+    lfs_file_close(&lfs, &file) => 0;
+
+    // read hi1 20
+    sprintf(path, "hi1"); size = 20;
+    lfs_file_open(&lfs, &file, path, LFS_O_RDONLY) => 0;
+    lfs_file_size(&lfs, &file) => size;
+    lfs_file_read(&lfs, &file, rbuffer, size) => size;
+    memcmp(rbuffer, wbuffer, size) => 0;
+    lfs_file_close(&lfs, &file) => 0;
+    // write hi1 200
+    sprintf(path, "hi1"); size = 200;
+    lfs_file_open(&lfs, &file, path,
+            LFS_O_WRONLY | LFS_O_CREAT | LFS_O_TRUNC) => 0;
+    memset(wbuffer, 'c', size);
+    lfs_file_write(&lfs, &file, wbuffer, size) => size;
+    lfs_file_size(&lfs, &file) => size;
+    lfs_file_close(&lfs, &file) => 0;
+
+    // read hi0 20
+    sprintf(path, "hi0"); size = 20;
+    lfs_file_open(&lfs, &file, path, LFS_O_RDONLY) => 0;
+    lfs_file_size(&lfs, &file) => size;
+    lfs_file_read(&lfs, &file, rbuffer, size) => size;
+    memcmp(rbuffer, wbuffer, size) => 0;
+    lfs_file_close(&lfs, &file) => 0;
+    // read hi1 200
+    sprintf(path, "hi1"); size = 200;
+    lfs_file_open(&lfs, &file, path, LFS_O_RDONLY) => 0;
+    lfs_file_size(&lfs, &file) => size;
+    lfs_file_read(&lfs, &file, rbuffer, size) => size;
+    memcmp(rbuffer, wbuffer, size) => 0;
+    lfs_file_close(&lfs, &file) => 0;
+    // read hi2 20
+    sprintf(path, "hi2"); size = 20;
+    lfs_file_open(&lfs, &file, path, LFS_O_RDONLY) => 0;
+    lfs_file_size(&lfs, &file) => size;
+    lfs_file_read(&lfs, &file, rbuffer, size) => size;
+    memcmp(rbuffer, wbuffer, size) => 0;
+    lfs_file_close(&lfs, &file) => 0;
+    // read hi3 20
+    sprintf(path, "hi3"); size = 20;
+    lfs_file_open(&lfs, &file, path, LFS_O_RDONLY) => 0;
+    lfs_file_size(&lfs, &file) => size;
+    lfs_file_read(&lfs, &file, rbuffer, size) => size;
+    memcmp(rbuffer, wbuffer, size) => 0;
+    lfs_file_close(&lfs, &file) => 0;
+
+    lfs_unmount(&lfs) => 0;
+'''
+
+[[case]] # entry shrink test
+code = '''
+    uint8_t wbuffer[1024];
+    uint8_t rbuffer[1024];
+
+    lfs_format(&lfs, &cfg) => 0;
+    lfs_mount(&lfs, &cfg) => 0;
+
+    // write hi0 20
+    sprintf(path, "hi0"); size = 20;
+    lfs_file_open(&lfs, &file, path,
+            LFS_O_WRONLY | LFS_O_CREAT | LFS_O_TRUNC) => 0;
+    memset(wbuffer, 'c', size);
+    lfs_file_write(&lfs, &file, wbuffer, size) => size;
+    lfs_file_size(&lfs, &file) => size;
+    lfs_file_close(&lfs, &file) => 0;
+    // write hi1 200
+    sprintf(path, "hi1"); size = 200;
+    lfs_file_open(&lfs, &file, path,
+            LFS_O_WRONLY | LFS_O_CREAT | LFS_O_TRUNC) => 0;
+    memset(wbuffer, 'c', size);
+    lfs_file_write(&lfs, &file, wbuffer, size) => size;
+    lfs_file_size(&lfs, &file) => size;
+    lfs_file_close(&lfs, &file) => 0;
+    // write hi2 20
+    sprintf(path, "hi2"); size = 20;
+    lfs_file_open(&lfs, &file, path,
+            LFS_O_WRONLY | LFS_O_CREAT | LFS_O_TRUNC) => 0;
+    memset(wbuffer, 'c', size);
+    lfs_file_write(&lfs, &file, wbuffer, size) => size;
+    lfs_file_size(&lfs, &file) => size;
+    lfs_file_close(&lfs, &file) => 0;
+    // write hi3 20
+    sprintf(path, "hi3"); size = 20;
+    lfs_file_open(&lfs, &file, path,
+            LFS_O_WRONLY | LFS_O_CREAT | LFS_O_TRUNC) => 0;
+    memset(wbuffer, 'c', size);
+    lfs_file_write(&lfs, &file, wbuffer, size) => size;
+    lfs_file_size(&lfs, &file) => size;
+    lfs_file_close(&lfs, &file) => 0;
+
+    // read hi1 200
+    sprintf(path, "hi1"); size = 200;
+    lfs_file_open(&lfs, &file, path, LFS_O_RDONLY) => 0;
+    lfs_file_size(&lfs, &file) => size;
+    lfs_file_read(&lfs, &file, rbuffer, size) => size;
+    memcmp(rbuffer, wbuffer, size) => 0;
+    lfs_file_close(&lfs, &file) => 0;
+    // write hi1 20
+    sprintf(path, "hi1"); size = 20;
+    lfs_file_open(&lfs, &file, path,
+            LFS_O_WRONLY | LFS_O_CREAT | LFS_O_TRUNC) => 0;
+    memset(wbuffer, 'c', size);
+    lfs_file_write(&lfs, &file, wbuffer, size) => size;
+    lfs_file_size(&lfs, &file) => size;
+    lfs_file_close(&lfs, &file) => 0;
+
+    // read hi0 20
+    sprintf(path, "hi0"); size = 20;
+    lfs_file_open(&lfs, &file, path, LFS_O_RDONLY) => 0;
+    lfs_file_size(&lfs, &file) => size;
+    lfs_file_read(&lfs, &file, rbuffer, size) => size;
+    memcmp(rbuffer, wbuffer, size) => 0;
+    lfs_file_close(&lfs, &file) => 0;
+    // read hi1 20
+    sprintf(path, "hi1"); size = 20;
+    lfs_file_open(&lfs, &file, path, LFS_O_RDONLY) => 0;
+    lfs_file_size(&lfs, &file) => size;
+    lfs_file_read(&lfs, &file, rbuffer, size) => size;
+    memcmp(rbuffer, wbuffer, size) => 0;
+    lfs_file_close(&lfs, &file) => 0;
+    // read hi2 20
+    sprintf(path, "hi2"); size = 20;
+    lfs_file_open(&lfs, &file, path, LFS_O_RDONLY) => 0;
+    lfs_file_size(&lfs, &file) => size;
+    lfs_file_read(&lfs, &file, rbuffer, size) => size;
+    memcmp(rbuffer, wbuffer, size) => 0;
+    lfs_file_close(&lfs, &file) => 0;
+    // read hi3 20
+    sprintf(path, "hi3"); size = 20;
+    lfs_file_open(&lfs, &file, path, LFS_O_RDONLY) => 0;
+    lfs_file_size(&lfs, &file) => size;
+    lfs_file_read(&lfs, &file, rbuffer, size) => size;
+    memcmp(rbuffer, wbuffer, size) => 0;
+    lfs_file_close(&lfs, &file) => 0;
+
+    lfs_unmount(&lfs) => 0;
+'''
+
+[[case]] # entry spill test
+code = '''
+    uint8_t wbuffer[1024];
+    uint8_t rbuffer[1024];
+
+    lfs_format(&lfs, &cfg) => 0;
+    lfs_mount(&lfs, &cfg) => 0;
+
+    // write hi0 200
+    sprintf(path, "hi0"); size = 200;
+    lfs_file_open(&lfs, &file, path,
+            LFS_O_WRONLY | LFS_O_CREAT | LFS_O_TRUNC) => 0;
+    memset(wbuffer, 'c', size);
+    lfs_file_write(&lfs, &file, wbuffer, size) => size;
+    lfs_file_size(&lfs, &file) => size;
+    lfs_file_close(&lfs, &file) => 0;
+    // write hi1 200
+    sprintf(path, "hi1"); size = 200;
+    lfs_file_open(&lfs, &file, path,
+            LFS_O_WRONLY | LFS_O_CREAT | LFS_O_TRUNC) => 0;
+    memset(wbuffer, 'c', size);
+    lfs_file_write(&lfs, &file, wbuffer, size) => size;
+    lfs_file_size(&lfs, &file) => size;
+    lfs_file_close(&lfs, &file) => 0;
+    // write hi2 200
+    sprintf(path, "hi2"); size = 200;
+    lfs_file_open(&lfs, &file, path,
+            LFS_O_WRONLY | LFS_O_CREAT | LFS_O_TRUNC) => 0;
+    memset(wbuffer, 'c', size);
+    lfs_file_write(&lfs, &file, wbuffer, size) => size;
+    lfs_file_size(&lfs, &file) => size;
+    lfs_file_close(&lfs, &file) => 0;
+    // write hi3 200
+    sprintf(path, "hi3"); size = 200;
+    lfs_file_open(&lfs, &file, path,
+            LFS_O_WRONLY | LFS_O_CREAT | LFS_O_TRUNC) => 0;
+    memset(wbuffer, 'c', size);
+    lfs_file_write(&lfs, &file, wbuffer, size) => size;
+    lfs_file_size(&lfs, &file) => size;
+    lfs_file_close(&lfs, &file) => 0;
+
+    // read hi0 200
+    sprintf(path, "hi0"); size = 200;
+    lfs_file_open(&lfs, &file, path, LFS_O_RDONLY) => 0;
+    lfs_file_size(&lfs, &file) => size;
+    lfs_file_read(&lfs, &file, rbuffer, size) => size;
+    memcmp(rbuffer, wbuffer, size) => 0;
+    lfs_file_close(&lfs, &file) => 0;
+    // read hi1 200
+    sprintf(path, "hi1"); size = 200;
+    lfs_file_open(&lfs, &file, path, LFS_O_RDONLY) => 0;
+    lfs_file_size(&lfs, &file) => size;
+    lfs_file_read(&lfs, &file, rbuffer, size) => size;
+    memcmp(rbuffer, wbuffer, size) => 0;
+    lfs_file_close(&lfs, &file) => 0;
+    // read hi2 200
+    sprintf(path, "hi2"); size = 200;
+    lfs_file_open(&lfs, &file, path, LFS_O_RDONLY) => 0;
+    lfs_file_size(&lfs, &file) => size;
+    lfs_file_read(&lfs, &file, rbuffer, size) => size;
+    memcmp(rbuffer, wbuffer, size) => 0;
+    lfs_file_close(&lfs, &file) => 0;
+    // read hi3 200
+    sprintf(path, "hi3"); size = 200;
+    lfs_file_open(&lfs, &file, path, LFS_O_RDONLY) => 0;
+    lfs_file_size(&lfs, &file) => size;
+    lfs_file_read(&lfs, &file, rbuffer, size) => size;
+    memcmp(rbuffer, wbuffer, size) => 0;
+    lfs_file_close(&lfs, &file) => 0;
+
+    lfs_unmount(&lfs) => 0;
+'''
+
+[[case]] # entry push spill test
+code = '''
+    uint8_t wbuffer[1024];
+    uint8_t rbuffer[1024];
+
+    lfs_format(&lfs, &cfg) => 0;
+    lfs_mount(&lfs, &cfg) => 0;
+
+    // write hi0 200
+    sprintf(path, "hi0"); size = 200;
+    lfs_file_open(&lfs, &file, path,
+            LFS_O_WRONLY | LFS_O_CREAT | LFS_O_TRUNC) => 0;
+    memset(wbuffer, 'c', size);
+    lfs_file_write(&lfs, &file, wbuffer, size) => size;
+    lfs_file_size(&lfs, &file) => size;
+    lfs_file_close(&lfs, &file) => 0;
+    // write hi1 20
+    sprintf(path, "hi1"); size = 20;
+    lfs_file_open(&lfs, &file, path,
+            LFS_O_WRONLY | LFS_O_CREAT | LFS_O_TRUNC) => 0;
+    memset(wbuffer, 'c', size);
+    lfs_file_write(&lfs, &file, wbuffer, size) => size;
+    lfs_file_size(&lfs, &file) => size;
+    lfs_file_close(&lfs, &file) => 0;
+    // write hi2 200
+    sprintf(path, "hi2"); size = 200;
+    lfs_file_open(&lfs, &file, path,
+            LFS_O_WRONLY | LFS_O_CREAT | LFS_O_TRUNC) => 0;
+    memset(wbuffer, 'c', size);
+    lfs_file_write(&lfs, &file, wbuffer, size) => size;
+    lfs_file_size(&lfs, &file) => size;
+    lfs_file_close(&lfs, &file) => 0;
+    // write hi3 200
+    sprintf(path, "hi3"); size = 200;
+    lfs_file_open(&lfs, &file, path,
+            LFS_O_WRONLY | LFS_O_CREAT | LFS_O_TRUNC) => 0;
+    memset(wbuffer, 'c', size);
+    lfs_file_write(&lfs, &file, wbuffer, size) => size;
+    lfs_file_size(&lfs, &file) => size;
+    lfs_file_close(&lfs, &file) => 0;
+
+    // read hi1 20
+    sprintf(path, "hi1"); size = 20;
+    lfs_file_open(&lfs, &file, path, LFS_O_RDONLY) => 0;
+    lfs_file_size(&lfs, &file) => size;
+    lfs_file_read(&lfs, &file, rbuffer, size) => size;
+    memcmp(rbuffer, wbuffer, size) => 0;
+    lfs_file_close(&lfs, &file) => 0;
+    // write hi1 200
+    sprintf(path, "hi1"); size = 200;
+    lfs_file_open(&lfs, &file, path,
+            LFS_O_WRONLY | LFS_O_CREAT | LFS_O_TRUNC) => 0;
+    memset(wbuffer, 'c', size);
+    lfs_file_write(&lfs, &file, wbuffer, size) => size;
+    lfs_file_size(&lfs, &file) => size;
+    lfs_file_close(&lfs, &file) => 0;
+
+    // read hi0 200
+    sprintf(path, "hi0"); size = 200;
+    lfs_file_open(&lfs, &file, path, LFS_O_RDONLY) => 0;
+    lfs_file_size(&lfs, &file) => size;
+    lfs_file_read(&lfs, &file, rbuffer, size) => size;
+    memcmp(rbuffer, wbuffer, size) => 0;
+    lfs_file_close(&lfs, &file) => 0;
+    // read hi1 200
+    sprintf(path, "hi1"); size = 200;
+    lfs_file_open(&lfs, &file, path, LFS_O_RDONLY) => 0;
+    lfs_file_size(&lfs, &file) => size;
+    lfs_file_read(&lfs, &file, rbuffer, size) => size;
+    memcmp(rbuffer, wbuffer, size) => 0;
+    lfs_file_close(&lfs, &file) => 0;
+    // read hi2 200
+    sprintf(path, "hi2"); size = 200;
+    lfs_file_open(&lfs, &file, path, LFS_O_RDONLY) => 0;
+    lfs_file_size(&lfs, &file) => size;
+    lfs_file_read(&lfs, &file, rbuffer, size) => size;
+    memcmp(rbuffer, wbuffer, size) => 0;
+    lfs_file_close(&lfs, &file) => 0;
+    // read hi3 200
+    sprintf(path, "hi3"); size = 200;
+    lfs_file_open(&lfs, &file, path, LFS_O_RDONLY) => 0;
+    lfs_file_size(&lfs, &file) => size;
+    lfs_file_read(&lfs, &file, rbuffer, size) => size;
+    memcmp(rbuffer, wbuffer, size) => 0;
+    lfs_file_close(&lfs, &file) => 0;
+
+    lfs_unmount(&lfs) => 0;
+'''
+
+[[case]] # entry push spill two test
+code = '''
+    uint8_t wbuffer[1024];
+    uint8_t rbuffer[1024];
+
+    lfs_format(&lfs, &cfg) => 0;
+    lfs_mount(&lfs, &cfg) => 0;
+
+    // write hi0 200
+    sprintf(path, "hi0"); size = 200;
+    lfs_file_open(&lfs, &file, path,
+            LFS_O_WRONLY | LFS_O_CREAT | LFS_O_TRUNC) => 0;
+    memset(wbuffer, 'c', size);
+    lfs_file_write(&lfs, &file, wbuffer, size) => size;
+    lfs_file_size(&lfs, &file) => size;
+    lfs_file_close(&lfs, &file) => 0;
+    // write hi1 20
+    sprintf(path, "hi1"); size = 20;
+    lfs_file_open(&lfs, &file, path,
+            LFS_O_WRONLY | LFS_O_CREAT | LFS_O_TRUNC) => 0;
+    memset(wbuffer, 'c', size);
+    lfs_file_write(&lfs, &file, wbuffer, size) => size;
+    lfs_file_size(&lfs, &file) => size;
+    lfs_file_close(&lfs, &file) => 0;
+    // write hi2 200
+    sprintf(path, "hi2"); size = 200;
+    lfs_file_open(&lfs, &file, path,
+            LFS_O_WRONLY | LFS_O_CREAT | LFS_O_TRUNC) => 0;
+    memset(wbuffer, 'c', size);
+    lfs_file_write(&lfs, &file, wbuffer, size) => size;
+    lfs_file_size(&lfs, &file) => size;
+    lfs_file_close(&lfs, &file) => 0;
+    // write hi3 200
+    sprintf(path, "hi3"); size = 200;
+    lfs_file_open(&lfs, &file, path,
+            LFS_O_WRONLY | LFS_O_CREAT | LFS_O_TRUNC) => 0;
+    memset(wbuffer, 'c', size);
+    lfs_file_write(&lfs, &file, wbuffer, size) => size;
+    lfs_file_size(&lfs, &file) => size;
+    lfs_file_close(&lfs, &file) => 0;
+    // write hi4 200
+    sprintf(path, "hi4"); size = 200;
+    lfs_file_open(&lfs, &file, path,
+            LFS_O_WRONLY | LFS_O_CREAT | LFS_O_TRUNC) => 0;
+    memset(wbuffer, 'c', size);
+    lfs_file_write(&lfs, &file, wbuffer, size) => size;
+    lfs_file_size(&lfs, &file) => size;
+    lfs_file_close(&lfs, &file) => 0;
+
+    // read hi1 20
+    sprintf(path, "hi1"); size = 20;
+    lfs_file_open(&lfs, &file, path, LFS_O_RDONLY) => 0;
+    lfs_file_size(&lfs, &file) => size;
+    lfs_file_read(&lfs, &file, rbuffer, size) => size;
+    memcmp(rbuffer, wbuffer, size) => 0;
+    lfs_file_close(&lfs, &file) => 0;
+    // write hi1 200
+    sprintf(path, "hi1"); size = 200;
+    lfs_file_open(&lfs, &file, path,
+            LFS_O_WRONLY | LFS_O_CREAT | LFS_O_TRUNC) => 0;
+    memset(wbuffer, 'c', size);
+    lfs_file_write(&lfs, &file, wbuffer, size) => size;
+    lfs_file_size(&lfs, &file) => size;
+    lfs_file_close(&lfs, &file) => 0;
+
+    // read hi0 200
+    sprintf(path, "hi0"); size = 200;
+    lfs_file_open(&lfs, &file, path, LFS_O_RDONLY) => 0;
+    lfs_file_size(&lfs, &file) => size;
+    lfs_file_read(&lfs, &file, rbuffer, size) => size;
+    memcmp(rbuffer, wbuffer, size) => 0;
+    lfs_file_close(&lfs, &file) => 0;
+    // read hi1 200
+    sprintf(path, "hi1"); size = 200;
+    lfs_file_open(&lfs, &file, path, LFS_O_RDONLY) => 0;
+    lfs_file_size(&lfs, &file) => size;
+    lfs_file_read(&lfs, &file, rbuffer, size) => size;
+    memcmp(rbuffer, wbuffer, size) => 0;
+    lfs_file_close(&lfs, &file) => 0;
+    // read hi2 200
+    sprintf(path, "hi2"); size = 200;
+    lfs_file_open(&lfs, &file, path, LFS_O_RDONLY) => 0;
+    lfs_file_size(&lfs, &file) => size;
+    lfs_file_read(&lfs, &file, rbuffer, size) => size;
+    memcmp(rbuffer, wbuffer, size) => 0;
+    lfs_file_close(&lfs, &file) => 0;
+    // read hi3 200
+    sprintf(path, "hi3"); size = 200;
+    lfs_file_open(&lfs, &file, path, LFS_O_RDONLY) => 0;
+    lfs_file_size(&lfs, &file) => size;
+    lfs_file_read(&lfs, &file, rbuffer, size) => size;
+    memcmp(rbuffer, wbuffer, size) => 0;
+    lfs_file_close(&lfs, &file) => 0;
+    // read hi4 200
+    sprintf(path, "hi4"); size = 200;
+    lfs_file_open(&lfs, &file, path, LFS_O_RDONLY) => 0;
+    lfs_file_size(&lfs, &file) => size;
+    lfs_file_read(&lfs, &file, rbuffer, size) => size;
+    memcmp(rbuffer, wbuffer, size) => 0;
+    lfs_file_close(&lfs, &file) => 0;
+
+    lfs_unmount(&lfs) => 0;
+'''
+
+[[case]] # entry drop test
+code = '''
+    uint8_t wbuffer[1024];
+    uint8_t rbuffer[1024];
+
+    lfs_format(&lfs, &cfg) => 0;
+    lfs_mount(&lfs, &cfg) => 0;
+
+    // write hi0 200
+    sprintf(path, "hi0"); size = 200;
+    lfs_file_open(&lfs, &file, path,
+            LFS_O_WRONLY | LFS_O_CREAT | LFS_O_TRUNC) => 0;
+    memset(wbuffer, 'c', size);
+    lfs_file_write(&lfs, &file, wbuffer, size) => size;
+    lfs_file_size(&lfs, &file) => size;
+    lfs_file_close(&lfs, &file) => 0;
+    // write hi1 200
+    sprintf(path, "hi1"); size = 200;
+    lfs_file_open(&lfs, &file, path,
+            LFS_O_WRONLY | LFS_O_CREAT | LFS_O_TRUNC) => 0;
+    memset(wbuffer, 'c', size);
+    lfs_file_write(&lfs, &file, wbuffer, size) => size;
+    lfs_file_size(&lfs, &file) => size;
+    lfs_file_close(&lfs, &file) => 0;
+    // write hi2 200
+    sprintf(path, "hi2"); size = 200;
+    lfs_file_open(&lfs, &file, path,
+            LFS_O_WRONLY | LFS_O_CREAT | LFS_O_TRUNC) => 0;
+    memset(wbuffer, 'c', size);
+    lfs_file_write(&lfs, &file, wbuffer, size) => size;
+    lfs_file_size(&lfs, &file) => size;
+    lfs_file_close(&lfs, &file) => 0;
+    // write hi3 200
+    sprintf(path, "hi3"); size = 200;
+    lfs_file_open(&lfs, &file, path,
+            LFS_O_WRONLY | LFS_O_CREAT | LFS_O_TRUNC) => 0;
+    memset(wbuffer, 'c', size);
+    lfs_file_write(&lfs, &file, wbuffer, size) => size;
+    lfs_file_size(&lfs, &file) => size;
+    lfs_file_close(&lfs, &file) => 0;
+
+    lfs_remove(&lfs, "hi1") => 0;
+    lfs_stat(&lfs, "hi1", &info) => LFS_ERR_NOENT;
+    // read hi0 200
+    sprintf(path, "hi0"); size = 200;
+    lfs_file_open(&lfs, &file, path, LFS_O_RDONLY) => 0;
+    lfs_file_size(&lfs, &file) => size;
+    lfs_file_read(&lfs, &file, rbuffer, size) => size;
+    memcmp(rbuffer, wbuffer, size) => 0;
+    lfs_file_close(&lfs, &file) => 0;
+    // read hi2 200
+    sprintf(path, "hi2"); size = 200;
+    lfs_file_open(&lfs, &file, path, LFS_O_RDONLY) => 0;
+    lfs_file_size(&lfs, &file) => size;
+    lfs_file_read(&lfs, &file, rbuffer, size) => size;
+    memcmp(rbuffer, wbuffer, size) => 0;
+    lfs_file_close(&lfs, &file) => 0;
+    // read hi3 200
+    sprintf(path, "hi3"); size = 200;
+    lfs_file_open(&lfs, &file, path, LFS_O_RDONLY) => 0;
+    lfs_file_size(&lfs, &file) => size;
+    lfs_file_read(&lfs, &file, rbuffer, size) => size;
+    memcmp(rbuffer, wbuffer, size) => 0;
+    lfs_file_close(&lfs, &file) => 0;
+
+    lfs_remove(&lfs, "hi2") => 0;
+    lfs_stat(&lfs, "hi2", &info) => LFS_ERR_NOENT;
+    // read hi0 200
+    sprintf(path, "hi0"); size = 200;
+    lfs_file_open(&lfs, &file, path, LFS_O_RDONLY) => 0;
+    lfs_file_size(&lfs, &file) => size;
+    lfs_file_read(&lfs, &file, rbuffer, size) => size;
+    memcmp(rbuffer, wbuffer, size) => 0;
+    lfs_file_close(&lfs, &file) => 0;
+    // read hi3 200
+    sprintf(path, "hi3"); size = 200;
+    lfs_file_open(&lfs, &file, path, LFS_O_RDONLY) => 0;
+    lfs_file_size(&lfs, &file) => size;
+    lfs_file_read(&lfs, &file, rbuffer, size) => size;
+    memcmp(rbuffer, wbuffer, size) => 0;
+    lfs_file_close(&lfs, &file) => 0;
+
+    lfs_remove(&lfs, "hi3") => 0;
+    lfs_stat(&lfs, "hi3", &info) => LFS_ERR_NOENT;
+    // read hi0 200
+    sprintf(path, "hi0"); size = 200;
+    lfs_file_open(&lfs, &file, path, LFS_O_RDONLY) => 0;
+    lfs_file_size(&lfs, &file) => size;
+    lfs_file_read(&lfs, &file, rbuffer, size) => size;
+    memcmp(rbuffer, wbuffer, size) => 0;
+    lfs_file_close(&lfs, &file) => 0;
+
+    lfs_remove(&lfs, "hi0") => 0;
+    lfs_stat(&lfs, "hi0", &info) => LFS_ERR_NOENT;
+
+    lfs_unmount(&lfs) => 0;
+'''
+
+[[case]] # create too big
+code = '''
+    lfs_format(&lfs, &cfg) => 0;
+
+    lfs_mount(&lfs, &cfg) => 0;
+    memset(path, 'm', 200);
+    path[200] = '\0';
+
+    size = 400;
+    lfs_file_open(&lfs, &file, path,
+            LFS_O_WRONLY | LFS_O_CREAT | LFS_O_TRUNC) => 0;
+    uint8_t wbuffer[1024];
+    memset(wbuffer, 'c', size);
+    lfs_file_write(&lfs, &file, wbuffer, size) => size;
+    lfs_file_close(&lfs, &file) => 0;
+
+    size = 400;
+    lfs_file_open(&lfs, &file, path, LFS_O_RDONLY) => 0;
+    uint8_t rbuffer[1024];
+    lfs_file_read(&lfs, &file, rbuffer, size) => size;
+    memcmp(rbuffer, wbuffer, size) => 0;
+    lfs_file_close(&lfs, &file) => 0;
+    lfs_unmount(&lfs) => 0;
+'''
+
+[[case]] # resize too big
+code = '''
+    lfs_format(&lfs, &cfg) => 0;
+
+    lfs_mount(&lfs, &cfg) => 0;
+    memset(path, 'm', 200);
+    path[200] = '\0';
+
+    size = 40;
+    lfs_file_open(&lfs, &file, path,
+            LFS_O_WRONLY | LFS_O_CREAT | LFS_O_TRUNC) => 0;
+    uint8_t wbuffer[1024];
+    memset(wbuffer, 'c', size);
+    lfs_file_write(&lfs, &file, wbuffer, size) => size;
+    lfs_file_close(&lfs, &file) => 0;
+
+    size = 40;
+    lfs_file_open(&lfs, &file, path, LFS_O_RDONLY) => 0;
+    uint8_t rbuffer[1024];
+    lfs_file_read(&lfs, &file, rbuffer, size) => size;
+    memcmp(rbuffer, wbuffer, size) => 0;
+    lfs_file_close(&lfs, &file) => 0;
+
+    size = 400;
+    lfs_file_open(&lfs, &file, path,
+            LFS_O_WRONLY | LFS_O_CREAT | LFS_O_TRUNC) => 0;
+    memset(wbuffer, 'c', size);
+    lfs_file_write(&lfs, &file, wbuffer, size) => size;
+    lfs_file_close(&lfs, &file) => 0;
+
+    size = 400;
+    lfs_file_open(&lfs, &file, path, LFS_O_RDONLY) => 0;
+    lfs_file_read(&lfs, &file, rbuffer, size) => size;
+    memcmp(rbuffer, wbuffer, size) => 0;
+    lfs_file_close(&lfs, &file) => 0;
+    lfs_unmount(&lfs) => 0;
+'''

+ 262 - 0
tests_/test_interspersed.toml

@@ -0,0 +1,262 @@
+
+[[case]] # interspersed file test
+code = '''
+    lfs_file_t files[FILES];
+    const char alphas[] = "abcdefghijklmnopqrstuvwxyz";
+    lfs_format(&lfs, &cfg) => 0;
+    lfs_mount(&lfs, &cfg) => 0;
+    for (int j = 0; j < FILES; j++) {
+        sprintf(path, "%c", alphas[j]);
+        lfs_file_open(&lfs, &files[j], path,
+                LFS_O_WRONLY | LFS_O_CREAT | LFS_O_EXCL) => 0;
+    }
+
+    for (int i = 0; i < SIZE; i++) {
+        for (int j = 0; j < FILES; j++) {
+            lfs_file_write(&lfs, &files[j], &alphas[j], 1) => 1;
+        }
+    }
+
+    for (int j = 0; j < FILES; j++) {
+        lfs_file_close(&lfs, &files[j]);
+    }
+
+    lfs_dir_open(&lfs, &dir, "/") => 0;
+    lfs_dir_read(&lfs, &dir, &info) => 1;
+    assert(strcmp(info.name, ".") == 0);
+    assert(info.type == LFS_TYPE_DIR);
+    lfs_dir_read(&lfs, &dir, &info) => 1;
+    assert(strcmp(info.name, "..") == 0);
+    assert(info.type == LFS_TYPE_DIR);
+    for (int j = 0; j < FILES; j++) {
+        sprintf(path, "%c", alphas[j]);
+        lfs_dir_read(&lfs, &dir, &info) => 1;
+        assert(strcmp(info.name, path) == 0);
+        assert(info.type == LFS_TYPE_REG);
+        assert(info.size == SIZE);
+    }
+    lfs_dir_read(&lfs, &dir, &info) => 0;
+    lfs_dir_close(&lfs, &dir) => 0;
+
+    for (int j = 0; j < FILES; j++) {
+        sprintf(path, "%c", alphas[j]);
+        lfs_file_open(&lfs, &files[j], path, LFS_O_RDONLY) => 0;
+    }
+
+    for (int i = 0; i < 10; i++) {
+        for (int j = 0; j < FILES; j++) {
+            lfs_file_read(&lfs, &files[j], buffer, 1) => 1;
+            assert(buffer[0] == alphas[j]);
+        }
+    }
+
+    for (int j = 0; j < FILES; j++) {
+        lfs_file_close(&lfs, &files[j]);
+    }
+    
+    lfs_unmount(&lfs) => 0;
+'''
+# TODO FILES=26 found bug
+#define.SIZE = [10, 100]
+#define.FILES = [4, 10, 26] 
+define = [
+    {SIZE=10, FILES=4},
+    {SIZE=10, FILES=10},
+    #{SIZE=10, FILES=26},
+    {SIZE=100, FILES=4},
+    {SIZE=100, FILES=10},
+    #{SIZE=100, FILES=26},
+]
+
+[[case]] # interspersed remove file test
+code = '''
+    const char alphas[] = "abcdefghijklmnopqrstuvwxyz";
+    lfs_format(&lfs, &cfg) => 0;
+    lfs_mount(&lfs, &cfg) => 0;
+    for (int j = 0; j < FILES; j++) {
+        sprintf(path, "%c", alphas[j]);
+        lfs_file_open(&lfs, &file, path,
+                LFS_O_WRONLY | LFS_O_CREAT | LFS_O_EXCL) => 0;
+        for (int i = 0; i < SIZE; i++) {
+            lfs_file_write(&lfs, &file, &alphas[j], 1) => 1;
+        }
+        lfs_file_close(&lfs, &file);
+    }
+    lfs_unmount(&lfs) => 0;
+
+    lfs_mount(&lfs, &cfg) => 0;
+    lfs_file_open(&lfs, &file, "zzz", LFS_O_WRONLY | LFS_O_CREAT) => 0;
+    for (int j = 0; j < FILES; j++) {
+        lfs_file_write(&lfs, &file, (const void*)"~", 1) => 1;
+        lfs_file_sync(&lfs, &file) => 0;
+
+        sprintf(path, "%c", alphas[j]);
+        lfs_remove(&lfs, path) => 0;
+    }
+    lfs_file_close(&lfs, &file);
+
+    lfs_dir_open(&lfs, &dir, "/") => 0;
+    lfs_dir_read(&lfs, &dir, &info) => 1;
+    assert(strcmp(info.name, ".") == 0);
+    assert(info.type == LFS_TYPE_DIR);
+    lfs_dir_read(&lfs, &dir, &info) => 1;
+    assert(strcmp(info.name, "..") == 0);
+    assert(info.type == LFS_TYPE_DIR);
+    lfs_dir_read(&lfs, &dir, &info) => 1;
+    assert(strcmp(info.name, "zzz") == 0);
+    assert(info.type == LFS_TYPE_REG);
+    assert(info.size == FILES);
+    lfs_dir_read(&lfs, &dir, &info) => 0;
+    lfs_dir_close(&lfs, &dir) => 0;
+
+    lfs_file_open(&lfs, &file, "zzz", LFS_O_RDONLY) => 0;
+    for (int i = 0; i < FILES; i++) {
+        lfs_file_read(&lfs, &file, buffer, 1) => 1;
+        assert(buffer[0] == '~');
+    }
+    lfs_file_close(&lfs, &file);
+    
+    lfs_unmount(&lfs) => 0;
+'''
+define.SIZE = [10, 100]
+define.FILES = [4, 10, 26]
+
+[[case]] # remove inconveniently test
+code = '''
+    lfs_format(&lfs, &cfg) => 0;
+    lfs_mount(&lfs, &cfg) => 0;
+    lfs_file_t files[3];
+    lfs_file_open(&lfs, &files[0], "e", LFS_O_WRONLY | LFS_O_CREAT) => 0;
+    lfs_file_open(&lfs, &files[1], "f", LFS_O_WRONLY | LFS_O_CREAT) => 0;
+    lfs_file_open(&lfs, &files[2], "g", LFS_O_WRONLY | LFS_O_CREAT) => 0;
+
+    for (int i = 0; i < SIZE/2; i++) {
+        lfs_file_write(&lfs, &files[0], (const void*)"e", 1) => 1;
+        lfs_file_write(&lfs, &files[1], (const void*)"f", 1) => 1;
+        lfs_file_write(&lfs, &files[2], (const void*)"g", 1) => 1;
+    }
+
+    lfs_remove(&lfs, "f") => 0;
+
+    for (int i = 0; i < SIZE/2; i++) {
+        lfs_file_write(&lfs, &files[0], (const void*)"e", 1) => 1;
+        lfs_file_write(&lfs, &files[1], (const void*)"f", 1) => 1;
+        lfs_file_write(&lfs, &files[2], (const void*)"g", 1) => 1;
+    }
+
+    lfs_file_close(&lfs, &files[0]);
+    lfs_file_close(&lfs, &files[1]);
+    lfs_file_close(&lfs, &files[2]);
+
+    lfs_dir_open(&lfs, &dir, "/") => 0;
+    lfs_dir_read(&lfs, &dir, &info) => 1;
+    assert(strcmp(info.name, ".") == 0);
+    assert(info.type == LFS_TYPE_DIR);
+    lfs_dir_read(&lfs, &dir, &info) => 1;
+    assert(strcmp(info.name, "..") == 0);
+    assert(info.type == LFS_TYPE_DIR);
+    lfs_dir_read(&lfs, &dir, &info) => 1;
+    assert(strcmp(info.name, "e") == 0);
+    assert(info.type == LFS_TYPE_REG);
+    assert(info.size == SIZE);
+    lfs_dir_read(&lfs, &dir, &info) => 1;
+    assert(strcmp(info.name, "g") == 0);
+    assert(info.type == LFS_TYPE_REG);
+    assert(info.size == SIZE);
+    lfs_dir_read(&lfs, &dir, &info) => 0;
+    lfs_dir_close(&lfs, &dir) => 0;
+
+    lfs_file_open(&lfs, &files[0], "e", LFS_O_RDONLY) => 0;
+    lfs_file_open(&lfs, &files[1], "g", LFS_O_RDONLY) => 0;
+    for (int i = 0; i < SIZE; i++) {
+        lfs_file_read(&lfs, &files[0], buffer, 1) => 1;
+        assert(buffer[0] == 'e');
+        lfs_file_read(&lfs, &files[1], buffer, 1) => 1;
+        assert(buffer[0] == 'g');
+    }
+    lfs_file_close(&lfs, &files[0]);
+    lfs_file_close(&lfs, &files[1]);
+    
+    lfs_unmount(&lfs) => 0;
+'''
+define.SIZE = [10, 100]
+
+[[case]] # reentrant interspersed file test
+code = '''
+    lfs_file_t files[FILES];
+    const char alphas[] = "abcdefghijklmnopqrstuvwxyz";
+
+    err = lfs_mount(&lfs, &cfg);
+    if (err) {
+        lfs_format(&lfs, &cfg) => 0;
+        lfs_mount(&lfs, &cfg) => 0;
+    }
+
+    for (int j = 0; j < FILES; j++) {
+        sprintf(path, "%c", alphas[j]);
+        lfs_file_open(&lfs, &files[j], path,
+                LFS_O_WRONLY | LFS_O_CREAT | LFS_O_APPEND) => 0;
+    }
+
+    for (int i = 0; i < SIZE; i++) {
+        for (int j = 0; j < FILES; j++) {
+            size = lfs_file_size(&lfs, &files[j]);
+            assert((int)size >= 0);
+            if ((int)size <= i) {
+                lfs_file_write(&lfs, &files[j], &alphas[j], 1) => 1;
+                lfs_file_sync(&lfs, &files[j]) => 0;
+            }
+        }
+    }
+
+    for (int j = 0; j < FILES; j++) {
+        lfs_file_close(&lfs, &files[j]);
+    }
+
+    lfs_dir_open(&lfs, &dir, "/") => 0;
+    lfs_dir_read(&lfs, &dir, &info) => 1;
+    assert(strcmp(info.name, ".") == 0);
+    assert(info.type == LFS_TYPE_DIR);
+    lfs_dir_read(&lfs, &dir, &info) => 1;
+    assert(strcmp(info.name, "..") == 0);
+    assert(info.type == LFS_TYPE_DIR);
+    for (int j = 0; j < FILES; j++) {
+        sprintf(path, "%c", alphas[j]);
+        lfs_dir_read(&lfs, &dir, &info) => 1;
+        assert(strcmp(info.name, path) == 0);
+        assert(info.type == LFS_TYPE_REG);
+        assert(info.size == SIZE);
+    }
+    lfs_dir_read(&lfs, &dir, &info) => 0;
+    lfs_dir_close(&lfs, &dir) => 0;
+
+    for (int j = 0; j < FILES; j++) {
+        sprintf(path, "%c", alphas[j]);
+        lfs_file_open(&lfs, &files[j], path, LFS_O_RDONLY) => 0;
+    }
+
+    for (int i = 0; i < 10; i++) {
+        for (int j = 0; j < FILES; j++) {
+            lfs_file_read(&lfs, &files[j], buffer, 1) => 1;
+            assert(buffer[0] == alphas[j]);
+        }
+    }
+
+    for (int j = 0; j < FILES; j++) {
+        lfs_file_close(&lfs, &files[j]);
+    }
+    
+    lfs_unmount(&lfs) => 0;
+'''
+# TODO FILES=26 found bug
+#define.SIZE = [10, 100]
+#define.FILES = [4, 10, 26] 
+define = [
+    {SIZE=10, FILES=4},
+    {SIZE=10, FILES=10},
+    #{SIZE=10, FILES=26},
+    {SIZE=100, FILES=4},
+    #{SIZE=100, FILES=10},
+    #{SIZE=100, FILES=26},
+]
+reentrant = true

+ 294 - 0
tests_/test_paths.toml

@@ -0,0 +1,294 @@
+
+[[case]] # simple path test
+code = '''
+    lfs_format(&lfs, &cfg) => 0;
+    lfs_mount(&lfs, &cfg) => 0;
+    lfs_mkdir(&lfs, "tea") => 0;
+    lfs_mkdir(&lfs, "tea/hottea") => 0;
+    lfs_mkdir(&lfs, "tea/warmtea") => 0;
+    lfs_mkdir(&lfs, "tea/coldtea") => 0;
+
+    lfs_stat(&lfs, "tea/hottea", &info) => 0;
+    assert(strcmp(info.name, "hottea") == 0);
+    lfs_stat(&lfs, "/tea/hottea", &info) => 0;
+    assert(strcmp(info.name, "hottea") == 0);
+
+    lfs_mkdir(&lfs, "/milk") => 0;
+    lfs_stat(&lfs, "/milk", &info) => 0;
+    assert(strcmp(info.name, "milk") == 0);
+    lfs_stat(&lfs, "milk", &info) => 0;
+    assert(strcmp(info.name, "milk") == 0);
+    lfs_unmount(&lfs) => 0;
+'''
+
+[[case]] # redundant slashes
+code = '''
+    lfs_format(&lfs, &cfg) => 0;
+    lfs_mount(&lfs, &cfg) => 0;
+    lfs_mkdir(&lfs, "tea") => 0;
+    lfs_mkdir(&lfs, "tea/hottea") => 0;
+    lfs_mkdir(&lfs, "tea/warmtea") => 0;
+    lfs_mkdir(&lfs, "tea/coldtea") => 0;
+
+    lfs_stat(&lfs, "/tea/hottea", &info) => 0;
+    assert(strcmp(info.name, "hottea") == 0);
+    lfs_stat(&lfs, "//tea//hottea", &info) => 0;
+    assert(strcmp(info.name, "hottea") == 0);
+    lfs_stat(&lfs, "///tea///hottea", &info) => 0;
+    assert(strcmp(info.name, "hottea") == 0);
+
+    lfs_mkdir(&lfs, "////milk") => 0;
+    lfs_stat(&lfs, "////milk", &info) => 0;
+    assert(strcmp(info.name, "milk") == 0);
+    lfs_stat(&lfs, "milk", &info) => 0;
+    assert(strcmp(info.name, "milk") == 0);
+    lfs_unmount(&lfs) => 0;
+'''
+
+[[case]] # dot path test
+code = '''
+    lfs_format(&lfs, &cfg) => 0;
+    lfs_mount(&lfs, &cfg) => 0;
+    lfs_mkdir(&lfs, "tea") => 0;
+    lfs_mkdir(&lfs, "tea/hottea") => 0;
+    lfs_mkdir(&lfs, "tea/warmtea") => 0;
+    lfs_mkdir(&lfs, "tea/coldtea") => 0;
+
+    lfs_stat(&lfs, "./tea/hottea", &info) => 0;
+    assert(strcmp(info.name, "hottea") == 0);
+    lfs_stat(&lfs, "/./tea/hottea", &info) => 0;
+    assert(strcmp(info.name, "hottea") == 0);
+    lfs_stat(&lfs, "/././tea/hottea", &info) => 0;
+    assert(strcmp(info.name, "hottea") == 0);
+    lfs_stat(&lfs, "/./tea/./hottea", &info) => 0;
+    assert(strcmp(info.name, "hottea") == 0);
+
+    lfs_mkdir(&lfs, "/./milk") => 0;
+    lfs_stat(&lfs, "/./milk", &info) => 0;
+    assert(strcmp(info.name, "milk") == 0);
+    lfs_stat(&lfs, "milk", &info) => 0;
+    assert(strcmp(info.name, "milk") == 0);
+    lfs_unmount(&lfs) => 0;
+'''
+
+[[case]] # dot dot path test
+code = '''
+    lfs_format(&lfs, &cfg) => 0;
+    lfs_mount(&lfs, &cfg) => 0;
+    lfs_mkdir(&lfs, "tea") => 0;
+    lfs_mkdir(&lfs, "tea/hottea") => 0;
+    lfs_mkdir(&lfs, "tea/warmtea") => 0;
+    lfs_mkdir(&lfs, "tea/coldtea") => 0;
+    lfs_mkdir(&lfs, "coffee") => 0;
+    lfs_mkdir(&lfs, "coffee/hotcoffee") => 0;
+    lfs_mkdir(&lfs, "coffee/warmcoffee") => 0;
+    lfs_mkdir(&lfs, "coffee/coldcoffee") => 0;
+
+    lfs_stat(&lfs, "coffee/../tea/hottea", &info) => 0;
+    assert(strcmp(info.name, "hottea") == 0);
+    lfs_stat(&lfs, "tea/coldtea/../hottea", &info) => 0;
+    assert(strcmp(info.name, "hottea") == 0);
+    lfs_stat(&lfs, "coffee/coldcoffee/../../tea/hottea", &info) => 0;
+    assert(strcmp(info.name, "hottea") == 0);
+    lfs_stat(&lfs, "coffee/../coffee/../tea/hottea", &info) => 0;
+    assert(strcmp(info.name, "hottea") == 0);
+
+    lfs_mkdir(&lfs, "coffee/../milk") => 0;
+    lfs_stat(&lfs, "coffee/../milk", &info) => 0;
+    strcmp(info.name, "milk") => 0;
+    lfs_stat(&lfs, "milk", &info) => 0;
+    strcmp(info.name, "milk") => 0;
+    lfs_unmount(&lfs) => 0;
+'''
+
+[[case]] # trailing dot path test
+code = '''
+    lfs_format(&lfs, &cfg) => 0;
+    lfs_mount(&lfs, &cfg) => 0;
+    lfs_mkdir(&lfs, "tea") => 0;
+    lfs_mkdir(&lfs, "tea/hottea") => 0;
+    lfs_mkdir(&lfs, "tea/warmtea") => 0;
+    lfs_mkdir(&lfs, "tea/coldtea") => 0;
+
+    lfs_stat(&lfs, "tea/hottea/", &info) => 0;
+    assert(strcmp(info.name, "hottea") == 0);
+    lfs_stat(&lfs, "tea/hottea/.", &info) => 0;
+    assert(strcmp(info.name, "hottea") == 0);
+    lfs_stat(&lfs, "tea/hottea/./.", &info) => 0;
+    assert(strcmp(info.name, "hottea") == 0);
+    lfs_stat(&lfs, "tea/hottea/..", &info) => 0;
+    assert(strcmp(info.name, "tea") == 0);
+    lfs_stat(&lfs, "tea/hottea/../.", &info) => 0;
+    assert(strcmp(info.name, "tea") == 0);
+    lfs_unmount(&lfs) => 0;
+'''
+
+[[case]] # leading dot path test
+code = '''
+    lfs_format(&lfs, &cfg) => 0;
+    lfs_mount(&lfs, &cfg) => 0;
+    lfs_mkdir(&lfs, ".milk") => 0;
+    lfs_stat(&lfs, ".milk", &info) => 0;
+    strcmp(info.name, ".milk") => 0;
+    lfs_stat(&lfs, "tea/.././.milk", &info) => 0;
+    strcmp(info.name, ".milk") => 0;
+    lfs_unmount(&lfs) => 0;
+'''
+
+[[case]] # root dot dot path test
+code = '''
+    lfs_format(&lfs, &cfg) => 0;
+    lfs_mount(&lfs, &cfg) => 0;
+    lfs_mkdir(&lfs, "tea") => 0;
+    lfs_mkdir(&lfs, "tea/hottea") => 0;
+    lfs_mkdir(&lfs, "tea/warmtea") => 0;
+    lfs_mkdir(&lfs, "tea/coldtea") => 0;
+    lfs_mkdir(&lfs, "coffee") => 0;
+    lfs_mkdir(&lfs, "coffee/hotcoffee") => 0;
+    lfs_mkdir(&lfs, "coffee/warmcoffee") => 0;
+    lfs_mkdir(&lfs, "coffee/coldcoffee") => 0;
+
+    lfs_stat(&lfs, "coffee/../../../../../../tea/hottea", &info) => 0;
+    strcmp(info.name, "hottea") => 0;
+
+    lfs_mkdir(&lfs, "coffee/../../../../../../milk") => 0;
+    lfs_stat(&lfs, "coffee/../../../../../../milk", &info) => 0;
+    strcmp(info.name, "milk") => 0;
+    lfs_stat(&lfs, "milk", &info) => 0;
+    strcmp(info.name, "milk") => 0;
+    lfs_unmount(&lfs) => 0;
+'''
+
+[[case]] # invalid path tests
+code = '''
+    lfs_format(&lfs, &cfg);
+    lfs_mount(&lfs, &cfg) => 0;
+    lfs_stat(&lfs, "dirt", &info) => LFS_ERR_NOENT;
+    lfs_stat(&lfs, "dirt/ground", &info) => LFS_ERR_NOENT;
+    lfs_stat(&lfs, "dirt/ground/earth", &info) => LFS_ERR_NOENT;
+
+    lfs_remove(&lfs, "dirt") => LFS_ERR_NOENT;
+    lfs_remove(&lfs, "dirt/ground") => LFS_ERR_NOENT;
+    lfs_remove(&lfs, "dirt/ground/earth") => LFS_ERR_NOENT;
+
+    lfs_mkdir(&lfs, "dirt/ground") => LFS_ERR_NOENT;
+    lfs_file_open(&lfs, &file, "dirt/ground", LFS_O_WRONLY | LFS_O_CREAT)
+            => LFS_ERR_NOENT;
+    lfs_mkdir(&lfs, "dirt/ground/earth") => LFS_ERR_NOENT;
+    lfs_file_open(&lfs, &file, "dirt/ground/earth", LFS_O_WRONLY | LFS_O_CREAT)
+            => LFS_ERR_NOENT;
+    lfs_unmount(&lfs) => 0;
+'''
+
+[[case]] # root operations
+code = '''
+    lfs_format(&lfs, &cfg) => 0;
+    lfs_mount(&lfs, &cfg) => 0;
+    lfs_stat(&lfs, "/", &info) => 0;
+    assert(strcmp(info.name, "/") == 0);
+    assert(info.type == LFS_TYPE_DIR);
+
+    lfs_mkdir(&lfs, "/") => LFS_ERR_EXIST;
+    lfs_file_open(&lfs, &file, "/", LFS_O_WRONLY | LFS_O_CREAT)
+            => LFS_ERR_ISDIR;
+
+    lfs_remove(&lfs, "/") => LFS_ERR_INVAL;
+    lfs_unmount(&lfs) => 0;
+'''
+
+[[case]] # root representations
+code = '''
+    lfs_format(&lfs, &cfg) => 0;
+    lfs_mount(&lfs, &cfg) => 0;
+    lfs_stat(&lfs, "/", &info) => 0;
+    assert(strcmp(info.name, "/") == 0);
+    assert(info.type == LFS_TYPE_DIR);
+    lfs_stat(&lfs, "", &info) => 0;
+    assert(strcmp(info.name, "/") == 0);
+    assert(info.type == LFS_TYPE_DIR);
+    lfs_stat(&lfs, ".", &info) => 0;
+    assert(strcmp(info.name, "/") == 0);
+    assert(info.type == LFS_TYPE_DIR);
+    lfs_stat(&lfs, "..", &info) => 0;
+    assert(strcmp(info.name, "/") == 0);
+    assert(info.type == LFS_TYPE_DIR);
+    lfs_stat(&lfs, "//", &info) => 0;
+    assert(strcmp(info.name, "/") == 0);
+    assert(info.type == LFS_TYPE_DIR);
+    lfs_stat(&lfs, "./", &info) => 0;
+    assert(strcmp(info.name, "/") == 0);
+    assert(info.type == LFS_TYPE_DIR);
+    lfs_unmount(&lfs) => 0;
+'''
+
+[[case]] # superblock conflict test
+code = '''
+    lfs_format(&lfs, &cfg) => 0;
+    lfs_mount(&lfs, &cfg) => 0;
+    lfs_stat(&lfs, "littlefs", &info) => LFS_ERR_NOENT;
+    lfs_remove(&lfs, "littlefs") => LFS_ERR_NOENT;
+
+    lfs_mkdir(&lfs, "littlefs") => 0;
+    lfs_stat(&lfs, "littlefs", &info) => 0;
+    assert(strcmp(info.name, "littlefs") == 0);
+    assert(info.type == LFS_TYPE_DIR);
+    lfs_remove(&lfs, "littlefs") => 0;
+    lfs_stat(&lfs, "littlefs", &info) => LFS_ERR_NOENT;
+    lfs_unmount(&lfs) => 0;
+'''
+
+[[case]] # max path test
+code = '''
+    lfs_format(&lfs, &cfg) => 0;
+    lfs_mount(&lfs, &cfg) => 0;
+    lfs_mkdir(&lfs, "coffee") => 0;
+    lfs_mkdir(&lfs, "coffee/hotcoffee") => 0;
+    lfs_mkdir(&lfs, "coffee/warmcoffee") => 0;
+    lfs_mkdir(&lfs, "coffee/coldcoffee") => 0;
+
+    memset(path, 'w', LFS_NAME_MAX+1);
+    path[LFS_NAME_MAX+2] = '\0';
+    lfs_mkdir(&lfs, path) => LFS_ERR_NAMETOOLONG;
+    lfs_file_open(&lfs, &file, path, LFS_O_WRONLY | LFS_O_CREAT)
+            => LFS_ERR_NAMETOOLONG;
+
+    memcpy(path, "coffee/", strlen("coffee/"));
+    memset(path+strlen("coffee/"), 'w', LFS_NAME_MAX+1);
+    path[strlen("coffee/")+LFS_NAME_MAX+2] = '\0';
+    lfs_mkdir(&lfs, path) => LFS_ERR_NAMETOOLONG;
+    lfs_file_open(&lfs, &file, path, LFS_O_WRONLY | LFS_O_CREAT)
+            => LFS_ERR_NAMETOOLONG;
+    lfs_unmount(&lfs) => 0;
+'''
+
+[[case]] # really big path test
+code = '''
+    lfs_format(&lfs, &cfg) => 0;
+    lfs_mount(&lfs, &cfg) => 0;
+    lfs_mkdir(&lfs, "coffee") => 0;
+    lfs_mkdir(&lfs, "coffee/hotcoffee") => 0;
+    lfs_mkdir(&lfs, "coffee/warmcoffee") => 0;
+    lfs_mkdir(&lfs, "coffee/coldcoffee") => 0;
+
+    lfs_mount(&lfs, &cfg) => 0;
+    memset(path, 'w', LFS_NAME_MAX);
+    path[LFS_NAME_MAX] = '\0';
+    lfs_mkdir(&lfs, path) => 0;
+    lfs_remove(&lfs, path) => 0;
+    lfs_file_open(&lfs, &file, path,
+            LFS_O_WRONLY | LFS_O_CREAT) => 0;
+    lfs_file_close(&lfs, &file) => 0;
+    lfs_remove(&lfs, path) => 0;
+
+    memcpy(path, "coffee/", strlen("coffee/"));
+    memset(path+strlen("coffee/"), 'w', LFS_NAME_MAX);
+    path[strlen("coffee/")+LFS_NAME_MAX] = '\0';
+    lfs_mkdir(&lfs, path) => 0;
+    lfs_remove(&lfs, path) => 0;
+    lfs_file_open(&lfs, &file, path,
+            LFS_O_WRONLY | LFS_O_CREAT) => 0;
+    lfs_file_close(&lfs, &file) => 0;
+    lfs_remove(&lfs, path) => 0;
+    lfs_unmount(&lfs) => 0;
+'''
+

+ 380 - 0
tests_/test_seek.toml

@@ -0,0 +1,380 @@
+
+[[case]] # simple file seek
+code = '''
+    lfs_format(&lfs, &cfg) => 0;
+    lfs_mount(&lfs, &cfg) => 0;
+    lfs_file_open(&lfs, &file, "kitty",
+            LFS_O_WRONLY | LFS_O_CREAT | LFS_O_APPEND) => 0;
+    size = strlen("kittycatcat");
+    memcpy(buffer, "kittycatcat", size);
+    for (int j = 0; j < COUNT; j++) {
+        lfs_file_write(&lfs, &file, buffer, size);
+    }
+    lfs_file_close(&lfs, &file) => 0;
+    lfs_unmount(&lfs) => 0;
+
+    lfs_mount(&lfs, &cfg) => 0;
+    lfs_file_open(&lfs, &file, "kitty", LFS_O_RDONLY) => 0;
+
+    lfs_soff_t pos;
+    size = strlen("kittycatcat");
+    for (int i = 0; i < SKIP; i++) {
+        lfs_file_read(&lfs, &file, buffer, size) => size;
+        memcmp(buffer, "kittycatcat", size) => 0;
+        pos = lfs_file_tell(&lfs, &file);
+    }
+    pos >= 0 => 1;
+
+    lfs_file_seek(&lfs, &file, pos, LFS_SEEK_SET) => pos;
+    lfs_file_read(&lfs, &file, buffer, size) => size;
+    memcmp(buffer, "kittycatcat", size) => 0;
+
+    lfs_file_rewind(&lfs, &file) => 0;
+    lfs_file_read(&lfs, &file, buffer, size) => size;
+    memcmp(buffer, "kittycatcat", size) => 0;
+
+    lfs_file_seek(&lfs, &file, 0, LFS_SEEK_CUR) => size;
+    lfs_file_read(&lfs, &file, buffer, size) => size;
+    memcmp(buffer, "kittycatcat", size) => 0;
+
+    lfs_file_seek(&lfs, &file, size, LFS_SEEK_CUR) => 3*size;
+    lfs_file_read(&lfs, &file, buffer, size) => size;
+    memcmp(buffer, "kittycatcat", size) => 0;
+
+    lfs_file_seek(&lfs, &file, pos, LFS_SEEK_SET) => pos;
+    lfs_file_read(&lfs, &file, buffer, size) => size;
+    memcmp(buffer, "kittycatcat", size) => 0;
+
+    lfs_file_seek(&lfs, &file, -size, LFS_SEEK_CUR) => pos;
+    lfs_file_read(&lfs, &file, buffer, size) => size;
+    memcmp(buffer, "kittycatcat", size) => 0;
+
+    lfs_file_seek(&lfs, &file, -size, LFS_SEEK_END) >= 0 => 1;
+    lfs_file_read(&lfs, &file, buffer, size) => size;
+    memcmp(buffer, "kittycatcat", size) => 0;
+
+    size = lfs_file_size(&lfs, &file);
+    lfs_file_seek(&lfs, &file, 0, LFS_SEEK_CUR) => size;
+
+    lfs_file_close(&lfs, &file) => 0;
+    lfs_unmount(&lfs) => 0;
+'''
+define = [
+    {COUNT=132, SKIP=4},
+    {COUNT=132, SKIP=128},
+    {COUNT=200, SKIP=10},
+    {COUNT=200, SKIP=100},
+    {COUNT=4,   SKIP=1},
+    {COUNT=4,   SKIP=2},
+]
+
+[[case]] # simple file seek and write
+code = '''
+    lfs_format(&lfs, &cfg) => 0;
+    lfs_mount(&lfs, &cfg) => 0;
+    lfs_file_open(&lfs, &file, "kitty",
+            LFS_O_WRONLY | LFS_O_CREAT | LFS_O_APPEND) => 0;
+    size = strlen("kittycatcat");
+    memcpy(buffer, "kittycatcat", size);
+    for (int j = 0; j < COUNT; j++) {
+        lfs_file_write(&lfs, &file, buffer, size);
+    }
+    lfs_file_close(&lfs, &file) => 0;
+    lfs_unmount(&lfs) => 0;
+
+    lfs_mount(&lfs, &cfg) => 0;
+    lfs_file_open(&lfs, &file, "kitty", LFS_O_RDWR) => 0;
+
+    lfs_soff_t pos;
+    size = strlen("kittycatcat");
+    for (int i = 0; i < SKIP; i++) {
+        lfs_file_read(&lfs, &file, buffer, size) => size;
+        memcmp(buffer, "kittycatcat", size) => 0;
+        pos = lfs_file_tell(&lfs, &file);
+    }
+    pos >= 0 => 1;
+
+    memcpy(buffer, "doggodogdog", size);
+    lfs_file_seek(&lfs, &file, pos, LFS_SEEK_SET) => pos;
+    lfs_file_write(&lfs, &file, buffer, size) => size;
+
+    lfs_file_seek(&lfs, &file, pos, LFS_SEEK_SET) => pos;
+    lfs_file_read(&lfs, &file, buffer, size) => size;
+    memcmp(buffer, "doggodogdog", size) => 0;
+
+    lfs_file_rewind(&lfs, &file) => 0;
+    lfs_file_read(&lfs, &file, buffer, size) => size;
+    memcmp(buffer, "kittycatcat", size) => 0;
+
+    lfs_file_seek(&lfs, &file, pos, LFS_SEEK_SET) => pos;
+    lfs_file_read(&lfs, &file, buffer, size) => size;
+    memcmp(buffer, "doggodogdog", size) => 0;
+
+    lfs_file_seek(&lfs, &file, -size, LFS_SEEK_END) >= 0 => 1;
+    lfs_file_read(&lfs, &file, buffer, size) => size;
+    memcmp(buffer, "kittycatcat", size) => 0;
+
+    size = lfs_file_size(&lfs, &file);
+    lfs_file_seek(&lfs, &file, 0, LFS_SEEK_CUR) => size;
+
+    lfs_file_close(&lfs, &file) => 0;
+    lfs_unmount(&lfs) => 0;
+'''
+define = [
+    {COUNT=132, SKIP=4},
+    {COUNT=132, SKIP=128},
+    {COUNT=200, SKIP=10},
+    {COUNT=200, SKIP=100},
+    {COUNT=4,   SKIP=1},
+    {COUNT=4,   SKIP=2},
+]
+
+[[case]] # boundary seek and writes
+code = '''
+    lfs_format(&lfs, &cfg) => 0;
+    lfs_mount(&lfs, &cfg) => 0;
+    lfs_file_open(&lfs, &file, "kitty",
+            LFS_O_WRONLY | LFS_O_CREAT | LFS_O_APPEND) => 0;
+    size = strlen("kittycatcat");
+    memcpy(buffer, "kittycatcat", size);
+    for (int j = 0; j < COUNT; j++) {
+        lfs_file_write(&lfs, &file, buffer, size);
+    }
+    lfs_file_close(&lfs, &file) => 0;
+    lfs_unmount(&lfs) => 0;
+
+    lfs_mount(&lfs, &cfg) => 0;
+    lfs_file_open(&lfs, &file, "kitty", LFS_O_RDWR) => 0;
+
+    size = strlen("hedgehoghog");
+    const lfs_soff_t offsets[] = OFFSETS;
+
+    for (unsigned i = 0; i < sizeof(offsets) / sizeof(offsets[0]); i++) {
+        lfs_soff_t off = offsets[i];
+        memcpy(buffer, "hedgehoghog", size);
+        lfs_file_seek(&lfs, &file, off, LFS_SEEK_SET) => off;
+        lfs_file_write(&lfs, &file, buffer, size) => size;
+        lfs_file_seek(&lfs, &file, off, LFS_SEEK_SET) => off;
+        lfs_file_read(&lfs, &file, buffer, size) => size;
+        memcmp(buffer, "hedgehoghog", size) => 0;
+
+        lfs_file_seek(&lfs, &file, 0, LFS_SEEK_SET) => 0;
+        lfs_file_read(&lfs, &file, buffer, size) => size;
+        memcmp(buffer, "kittycatcat", size) => 0;
+
+        lfs_file_seek(&lfs, &file, off, LFS_SEEK_SET) => off;
+        lfs_file_read(&lfs, &file, buffer, size) => size;
+        memcmp(buffer, "hedgehoghog", size) => 0;
+
+        lfs_file_sync(&lfs, &file) => 0;
+
+        lfs_file_seek(&lfs, &file, 0, LFS_SEEK_SET) => 0;
+        lfs_file_read(&lfs, &file, buffer, size) => size;
+        memcmp(buffer, "kittycatcat", size) => 0;
+
+        lfs_file_seek(&lfs, &file, off, LFS_SEEK_SET) => off;
+        lfs_file_read(&lfs, &file, buffer, size) => size;
+        memcmp(buffer, "hedgehoghog", size) => 0;
+    }
+
+    lfs_file_close(&lfs, &file) => 0;
+    lfs_unmount(&lfs) => 0;
+'''
+define.COUNT = 132
+define.OFFSETS = '"{512, 1020, 513, 1021, 511, 1019, 1441}"'
+
+[[case]] # out of bounds seek
+code = '''
+    lfs_format(&lfs, &cfg) => 0;
+    lfs_mount(&lfs, &cfg) => 0;
+    lfs_file_open(&lfs, &file, "kitty",
+            LFS_O_WRONLY | LFS_O_CREAT | LFS_O_APPEND) => 0;
+    size = strlen("kittycatcat");
+    memcpy(buffer, "kittycatcat", size);
+    for (int j = 0; j < COUNT; j++) {
+        lfs_file_write(&lfs, &file, buffer, size);
+    }
+    lfs_file_close(&lfs, &file) => 0;
+    lfs_unmount(&lfs) => 0;
+    lfs_mount(&lfs, &cfg) => 0;
+    lfs_file_open(&lfs, &file, "kitty", LFS_O_RDWR) => 0;
+
+    size = strlen("kittycatcat");
+    lfs_file_size(&lfs, &file) => COUNT*size;
+    lfs_file_seek(&lfs, &file, (COUNT+SKIP)*size,
+            LFS_SEEK_SET) => (COUNT+SKIP)*size;
+    lfs_file_read(&lfs, &file, buffer, size) => 0;
+
+    memcpy(buffer, "porcupineee", size);
+    lfs_file_write(&lfs, &file, buffer, size) => size;
+
+    lfs_file_seek(&lfs, &file, (COUNT+SKIP)*size,
+            LFS_SEEK_SET) => (COUNT+SKIP)*size;
+    lfs_file_read(&lfs, &file, buffer, size) => size;
+    memcmp(buffer, "porcupineee", size) => 0;
+
+    lfs_file_seek(&lfs, &file, COUNT*size,
+            LFS_SEEK_SET) => COUNT*size;
+    lfs_file_read(&lfs, &file, buffer, size) => size;
+    memcmp(buffer, "\0\0\0\0\0\0\0\0\0\0\0", size) => 0;
+
+    lfs_file_seek(&lfs, &file, -((COUNT+SKIP)*size),
+            LFS_SEEK_CUR) => LFS_ERR_INVAL;
+    lfs_file_tell(&lfs, &file) => (COUNT+1)*size;
+
+    lfs_file_seek(&lfs, &file, -((COUNT+2*SKIP)*size),
+            LFS_SEEK_END) => LFS_ERR_INVAL;
+    lfs_file_tell(&lfs, &file) => (COUNT+1)*size;
+
+    lfs_file_close(&lfs, &file) => 0;
+    lfs_unmount(&lfs) => 0;
+'''
+define = [
+    {COUNT=132, SKIP=4},
+    {COUNT=132, SKIP=128},
+    {COUNT=200, SKIP=10},
+    {COUNT=200, SKIP=100},
+    {COUNT=4,   SKIP=2},
+    {COUNT=4,   SKIP=3},
+]
+
+[[case]] # inline write and seek
+code = '''
+    lfs_format(&lfs, &cfg) => 0;
+    lfs_mount(&lfs, &cfg) => 0;
+    lfs_file_open(&lfs, &file, "tinykitty",
+            LFS_O_RDWR | LFS_O_CREAT) => 0;
+    int j = 0;
+    int k = 0;
+
+    memcpy(buffer, "abcdefghijklmnopqrstuvwxyz", 26);
+    for (unsigned i = 0; i < SIZE; i++) {
+        lfs_file_write(&lfs, &file, &buffer[j++ % 26], 1) => 1;
+        lfs_file_tell(&lfs, &file) => i+1;
+        lfs_file_size(&lfs, &file) => i+1;
+    }
+
+    lfs_file_seek(&lfs, &file, 0, LFS_SEEK_SET) => 0;
+    lfs_file_tell(&lfs, &file) => 0;
+    lfs_file_size(&lfs, &file) => SIZE;
+    for (unsigned i = 0; i < SIZE; i++) {
+        uint8_t c;
+        lfs_file_read(&lfs, &file, &c, 1) => 1;
+        c => buffer[k++ % 26];
+    }
+
+    lfs_file_sync(&lfs, &file) => 0;
+    lfs_file_tell(&lfs, &file) => SIZE;
+    lfs_file_size(&lfs, &file) => SIZE;
+
+    lfs_file_seek(&lfs, &file, 0, LFS_SEEK_SET) => 0;
+    for (unsigned i = 0; i < SIZE; i++) {
+        lfs_file_write(&lfs, &file, &buffer[j++ % 26], 1) => 1;
+        lfs_file_tell(&lfs, &file) => i+1;
+        lfs_file_size(&lfs, &file) => SIZE;
+        lfs_file_sync(&lfs, &file) => 0;
+        lfs_file_tell(&lfs, &file) => i+1;
+        lfs_file_size(&lfs, &file) => SIZE;
+        if (i < SIZE-2) {
+            uint8_t c[3];
+            lfs_file_seek(&lfs, &file, -1, LFS_SEEK_CUR) => i;
+            lfs_file_read(&lfs, &file, &c, 3) => 3;
+            lfs_file_tell(&lfs, &file) => i+3;
+            lfs_file_size(&lfs, &file) => SIZE;
+            lfs_file_seek(&lfs, &file, i+1, LFS_SEEK_SET) => i+1;
+            lfs_file_tell(&lfs, &file) => i+1;
+            lfs_file_size(&lfs, &file) => SIZE;
+        }
+    }
+
+    lfs_file_seek(&lfs, &file, 0, LFS_SEEK_SET) => 0;
+    lfs_file_tell(&lfs, &file) => 0;
+    lfs_file_size(&lfs, &file) => SIZE;
+    for (unsigned i = 0; i < SIZE; i++) {
+        uint8_t c;
+        lfs_file_read(&lfs, &file, &c, 1) => 1;
+        c => buffer[k++ % 26];
+    }
+
+    lfs_file_sync(&lfs, &file) => 0;
+    lfs_file_tell(&lfs, &file) => SIZE;
+    lfs_file_size(&lfs, &file) => SIZE;
+
+    lfs_file_close(&lfs, &file) => 0;
+    lfs_unmount(&lfs) => 0;
+'''
+define.SIZE = [2, 4, 128, 132]
+
+[[case]] # file seek and write with power-loss
+code = '''
+    err = lfs_mount(&lfs, &cfg);
+    if (err) {
+        lfs_format(&lfs, &cfg) => 0;
+        lfs_mount(&lfs, &cfg) => 0;
+    }
+    err = lfs_file_open(&lfs, &file, "kitty", LFS_O_RDONLY);
+    assert(!err || err == LFS_ERR_NOENT);
+    if (!err) {
+        if (lfs_file_size(&lfs, &file) != 0) {
+            lfs_file_size(&lfs, &file) => 11*COUNT;
+            for (int j = 0; j < COUNT; j++) {
+                memset(buffer, 0, 11+1);
+                lfs_file_read(&lfs, &file, buffer, 11) => 11;
+                assert(memcmp(buffer, "kittycatcat", 11) == 0 ||
+                       memcmp(buffer, "doggodogdog", 11) == 0);
+            }
+        }
+        lfs_file_close(&lfs, &file) => 0;
+    }
+
+    lfs_file_open(&lfs, &file, "kitty", LFS_O_WRONLY | LFS_O_CREAT) => 0;
+    if (lfs_file_size(&lfs, &file) == 0) {
+        for (int j = 0; j < COUNT; j++) {
+            strcpy((char*)buffer, "kittycatcat");
+            size = strlen((char*)buffer);
+            lfs_file_write(&lfs, &file, buffer, size) => size;
+        }
+    }
+    lfs_file_close(&lfs, &file) => 0;
+
+    strcpy((char*)buffer, "doggodogdog");
+    size = strlen((char*)buffer);
+
+    lfs_file_open(&lfs, &file, "kitty", LFS_O_RDWR) => 0;
+    lfs_file_size(&lfs, &file) => COUNT*size;
+    // seek and write using quadratic probing to touch all
+    // 11-byte words in the file
+    lfs_off_t off = 0;
+    for (int j = 0; j < COUNT; j++) {
+        off = (5*off + 1) % COUNT;
+        lfs_file_seek(&lfs, &file, off*size, LFS_SEEK_SET) => off*size;
+        lfs_file_read(&lfs, &file, buffer, size) => size;
+        assert(memcmp(buffer, "kittycatcat", size) == 0 ||
+               memcmp(buffer, "doggodogdog", size) == 0);
+        if (memcmp(buffer, "doggodogdog", size) != 0) {
+            lfs_file_seek(&lfs, &file, off*size, LFS_SEEK_SET) => off*size;
+            strcpy((char*)buffer, "doggodogdog");
+            lfs_file_write(&lfs, &file, buffer, size) => size;
+            lfs_file_seek(&lfs, &file, off*size, LFS_SEEK_SET) => off*size;
+            lfs_file_read(&lfs, &file, buffer, size) => size;
+            assert(memcmp(buffer, "doggodogdog", size) == 0);
+            lfs_file_sync(&lfs, &file) => 0;
+            lfs_file_seek(&lfs, &file, off*size, LFS_SEEK_SET) => off*size;
+            lfs_file_read(&lfs, &file, buffer, size) => size;
+            assert(memcmp(buffer, "doggodogdog", size) == 0);
+        }
+    }
+    lfs_file_close(&lfs, &file) => 0;
+
+    lfs_file_open(&lfs, &file, "kitty", LFS_O_RDWR) => 0;
+    lfs_file_size(&lfs, &file) => COUNT*size;
+    for (int j = 0; j < COUNT; j++) {
+        lfs_file_read(&lfs, &file, buffer, size) => size;
+        assert(memcmp(buffer, "doggodogdog", size) == 0);
+    }
+    lfs_file_close(&lfs, &file) => 0;
+    lfs_unmount(&lfs) => 0;
+'''
+# must be power-of-2 for quadratic probing to be exhaustive
+define.COUNT = [4, 64, 128]
+reentrant = true

+ 395 - 0
tests_/test_truncate.toml

@@ -0,0 +1,395 @@
+[[case]] # simple truncate
+code = '''
+    lfs_format(&lfs, &cfg) => 0;
+    lfs_mount(&lfs, &cfg) => 0;
+    lfs_file_open(&lfs, &file, "baldynoop",
+            LFS_O_WRONLY | LFS_O_CREAT) => 0;
+
+    strcpy((char*)buffer, "hair");
+    size = strlen((char*)buffer);
+    for (lfs_off_t j = 0; j < LARGESIZE; j += size) {
+        lfs_file_write(&lfs, &file, buffer, size) => size;
+    }
+    lfs_file_size(&lfs, &file) => LARGESIZE;
+
+    lfs_file_close(&lfs, &file) => 0;
+    lfs_unmount(&lfs) => 0;
+    
+    lfs_mount(&lfs, &cfg) => 0;
+    lfs_file_open(&lfs, &file, "baldynoop", LFS_O_RDWR) => 0;
+    lfs_file_size(&lfs, &file) => LARGESIZE;
+
+    lfs_file_truncate(&lfs, &file, MEDIUMSIZE) => 0;
+    lfs_file_size(&lfs, &file) => MEDIUMSIZE;
+
+    lfs_file_close(&lfs, &file) => 0;
+    lfs_unmount(&lfs) => 0;
+
+    lfs_mount(&lfs, &cfg) => 0;
+    lfs_file_open(&lfs, &file, "baldynoop", LFS_O_RDONLY) => 0;
+    lfs_file_size(&lfs, &file) => MEDIUMSIZE;
+
+    size = strlen("hair");
+    for (lfs_off_t j = 0; j < MEDIUMSIZE; j += size) {
+        lfs_file_read(&lfs, &file, buffer, size) => size;
+        memcmp(buffer, "hair", size) => 0;
+    }
+    lfs_file_read(&lfs, &file, buffer, size) => 0;
+
+    lfs_file_close(&lfs, &file) => 0;
+    lfs_unmount(&lfs) => 0;
+'''
+define.MEDIUMSIZE = [32, 2048]
+define.LARGESIZE = 8192
+
+[[case]] # truncate and read
+code = '''
+    lfs_format(&lfs, &cfg) => 0;
+    lfs_mount(&lfs, &cfg) => 0;
+    lfs_file_open(&lfs, &file, "baldyread",
+            LFS_O_WRONLY | LFS_O_CREAT) => 0;
+
+    strcpy((char*)buffer, "hair");
+    size = strlen((char*)buffer);
+    for (lfs_off_t j = 0; j < LARGESIZE; j += size) {
+        lfs_file_write(&lfs, &file, buffer, size) => size;
+    }
+    lfs_file_size(&lfs, &file) => LARGESIZE;
+
+    lfs_file_close(&lfs, &file) => 0;
+    lfs_unmount(&lfs) => 0;
+
+    lfs_mount(&lfs, &cfg) => 0;
+    lfs_file_open(&lfs, &file, "baldyread", LFS_O_RDWR) => 0;
+    lfs_file_size(&lfs, &file) => LARGESIZE;
+
+    lfs_file_truncate(&lfs, &file, MEDIUMSIZE) => 0;
+    lfs_file_size(&lfs, &file) => MEDIUMSIZE;
+
+    size = strlen("hair");
+    for (lfs_off_t j = 0; j < MEDIUMSIZE; j += size) {
+        lfs_file_read(&lfs, &file, buffer, size) => size;
+        memcmp(buffer, "hair", size) => 0;
+    }
+    lfs_file_read(&lfs, &file, buffer, size) => 0;
+
+    lfs_file_close(&lfs, &file) => 0;
+    lfs_unmount(&lfs) => 0;
+
+    lfs_mount(&lfs, &cfg) => 0;
+    lfs_file_open(&lfs, &file, "baldyread", LFS_O_RDONLY) => 0;
+    lfs_file_size(&lfs, &file) => MEDIUMSIZE;
+
+    size = strlen("hair");
+    for (lfs_off_t j = 0; j < MEDIUMSIZE; j += size) {
+        lfs_file_read(&lfs, &file, buffer, size) => size;
+        memcmp(buffer, "hair", size) => 0;
+    }
+    lfs_file_read(&lfs, &file, buffer, size) => 0;
+
+    lfs_file_close(&lfs, &file) => 0;
+    lfs_unmount(&lfs) => 0;
+'''
+define.MEDIUMSIZE = [32, 2048]
+define.LARGESIZE = 8192
+
+[[case]] # write, truncate, and read
+code = '''
+    lfs_format(&lfs, &cfg) => 0;
+    lfs_mount(&lfs, &cfg) => 0;
+    lfs_file_open(&lfs, &file, "sequence",
+            LFS_O_RDWR | LFS_O_CREAT | LFS_O_TRUNC) => 0;
+
+    size = lfs.cfg->cache_size;
+    lfs_size_t qsize = size / 4;
+    uint8_t *wb = buffer;
+    uint8_t *rb = buffer + size;
+    for (lfs_off_t j = 0; j < size; ++j) {
+        wb[j] = j;
+    }
+
+    /* Spread sequence over size */
+    lfs_file_write(&lfs, &file, wb, size) => size;
+    lfs_file_size(&lfs, &file) => size;
+    lfs_file_tell(&lfs, &file) => size;
+
+    lfs_file_seek(&lfs, &file, 0, LFS_SEEK_SET) => 0;
+    lfs_file_tell(&lfs, &file) => 0;
+
+    /* Chop off the last quarter */
+    lfs_size_t trunc = size - qsize;
+    lfs_file_truncate(&lfs, &file, trunc) => 0;
+    lfs_file_tell(&lfs, &file) => 0;
+    lfs_file_size(&lfs, &file) => trunc;
+
+    /* Read should produce first 3/4 */
+    lfs_file_read(&lfs, &file, rb, size) => trunc;
+    memcmp(rb, wb, trunc) => 0;
+
+    /* Move to 1/4 */
+    lfs_file_size(&lfs, &file) => trunc;
+    lfs_file_seek(&lfs, &file, qsize, LFS_SEEK_SET) => qsize;
+    lfs_file_tell(&lfs, &file) => qsize;
+
+    /* Chop to 1/2 */
+    trunc -= qsize;
+    lfs_file_truncate(&lfs, &file, trunc) => 0;
+    lfs_file_tell(&lfs, &file) => qsize;
+    lfs_file_size(&lfs, &file) => trunc;
+    
+    /* Read should produce second quarter */
+    lfs_file_read(&lfs, &file, rb, size) => trunc - qsize;
+    memcmp(rb, wb + qsize, trunc - qsize) => 0;
+
+    lfs_file_close(&lfs, &file) => 0;
+    lfs_unmount(&lfs) => 0;
+'''
+
+[[case]] # truncate and write
+code = '''
+    lfs_format(&lfs, &cfg) => 0;
+    lfs_mount(&lfs, &cfg) => 0;
+    lfs_file_open(&lfs, &file, "baldywrite",
+            LFS_O_WRONLY | LFS_O_CREAT) => 0;
+
+    strcpy((char*)buffer, "hair");
+    size = strlen((char*)buffer);
+    for (lfs_off_t j = 0; j < LARGESIZE; j += size) {
+        lfs_file_write(&lfs, &file, buffer, size) => size;
+    }
+    lfs_file_size(&lfs, &file) => LARGESIZE;
+
+    lfs_file_close(&lfs, &file) => 0;
+    lfs_unmount(&lfs) => 0;
+
+    lfs_mount(&lfs, &cfg) => 0;
+    lfs_file_open(&lfs, &file, "baldywrite", LFS_O_RDWR) => 0;
+    lfs_file_size(&lfs, &file) => LARGESIZE;
+
+    lfs_file_truncate(&lfs, &file, MEDIUMSIZE) => 0;
+    lfs_file_size(&lfs, &file) => MEDIUMSIZE;
+
+    strcpy((char*)buffer, "bald");
+    size = strlen((char*)buffer);
+    for (lfs_off_t j = 0; j < MEDIUMSIZE; j += size) {
+        lfs_file_write(&lfs, &file, buffer, size) => size;
+    }
+    lfs_file_size(&lfs, &file) => MEDIUMSIZE;
+
+    lfs_file_close(&lfs, &file) => 0;
+    lfs_unmount(&lfs) => 0;
+
+    lfs_mount(&lfs, &cfg) => 0;
+    lfs_file_open(&lfs, &file, "baldywrite", LFS_O_RDONLY) => 0;
+    lfs_file_size(&lfs, &file) => MEDIUMSIZE;
+
+    size = strlen("bald");
+    for (lfs_off_t j = 0; j < MEDIUMSIZE; j += size) {
+        lfs_file_read(&lfs, &file, buffer, size) => size;
+        memcmp(buffer, "bald", size) => 0;
+    }
+    lfs_file_read(&lfs, &file, buffer, size) => 0;
+
+    lfs_file_close(&lfs, &file) => 0;
+    lfs_unmount(&lfs) => 0;
+'''
+define.MEDIUMSIZE = [32, 2048]
+define.LARGESIZE = 8192
+
+[[case]] # truncate write under powerloss
+code = '''
+    err = lfs_mount(&lfs, &cfg);
+    if (err) {
+        lfs_format(&lfs, &cfg) => 0;
+        lfs_mount(&lfs, &cfg) => 0;
+    }
+    err = lfs_file_open(&lfs, &file, "baldy", LFS_O_RDONLY);
+    assert(!err || err == LFS_ERR_NOENT);
+    if (!err) {
+        size = lfs_file_size(&lfs, &file);
+        assert(size == 0 ||
+                size == LARGESIZE ||
+                size == MEDIUMSIZE ||
+                size == SMALLSIZE);
+        for (lfs_off_t j = 0; j < size; j += 4) {
+            lfs_file_read(&lfs, &file, buffer, 4) => 4;
+            assert(memcmp(buffer, "hair", 4) == 0 ||
+                   memcmp(buffer, "bald", 4) == 0 ||
+                   memcmp(buffer, "comb", 4) == 0);
+        }
+        lfs_file_close(&lfs, &file) => 0;
+    }
+
+    lfs_file_open(&lfs, &file, "baldy",
+            LFS_O_WRONLY | LFS_O_CREAT | LFS_O_TRUNC) => 0;
+    lfs_file_size(&lfs, &file) => 0;
+    strcpy((char*)buffer, "hair");
+    size = strlen((char*)buffer);
+    for (lfs_off_t j = 0; j < LARGESIZE; j += size) {
+        lfs_file_write(&lfs, &file, buffer, size) => size;
+    }
+    lfs_file_size(&lfs, &file) => LARGESIZE;
+    lfs_file_close(&lfs, &file) => 0;
+
+    lfs_file_open(&lfs, &file, "baldy", LFS_O_RDWR) => 0;
+    lfs_file_size(&lfs, &file) => LARGESIZE;
+    lfs_file_truncate(&lfs, &file, MEDIUMSIZE) => 0;
+    lfs_file_size(&lfs, &file) => MEDIUMSIZE;
+    strcpy((char*)buffer, "bald");
+    size = strlen((char*)buffer);
+    for (lfs_off_t j = 0; j < MEDIUMSIZE; j += size) {
+        lfs_file_write(&lfs, &file, buffer, size) => size;
+    }
+    lfs_file_size(&lfs, &file) => MEDIUMSIZE;
+    lfs_file_close(&lfs, &file) => 0;
+
+    lfs_file_open(&lfs, &file, "baldy", LFS_O_RDWR) => 0;
+    lfs_file_size(&lfs, &file) => MEDIUMSIZE;
+    lfs_file_truncate(&lfs, &file, SMALLSIZE) => 0;
+    lfs_file_size(&lfs, &file) => SMALLSIZE;
+    strcpy((char*)buffer, "comb");
+    size = strlen((char*)buffer);
+    for (lfs_off_t j = 0; j < SMALLSIZE; j += size) {
+        lfs_file_write(&lfs, &file, buffer, size) => size;
+    }
+    lfs_file_size(&lfs, &file) => SMALLSIZE;
+    lfs_file_close(&lfs, &file) => 0;
+
+    lfs_unmount(&lfs) => 0;
+'''
+define.SMALLSIZE = [4, 512]
+define.MEDIUMSIZE = [32, 1024]
+define.LARGESIZE = 2048
+reentrant = true
+
+[[case]] # more aggressive general truncation tests
+code = '''
+    #define COUNT 5
+    const struct {
+        lfs_off_t startsizes[COUNT];
+        lfs_off_t startseeks[COUNT];
+        lfs_off_t hotsizes[COUNT];
+        lfs_off_t coldsizes[COUNT];
+    } configs[] = {
+        // cold shrinking
+        {{2*LARGESIZE, 2*LARGESIZE, 2*LARGESIZE, 2*LARGESIZE, 2*LARGESIZE},
+         {2*LARGESIZE, 2*LARGESIZE, 2*LARGESIZE, 2*LARGESIZE, 2*LARGESIZE},
+         {2*LARGESIZE, 2*LARGESIZE, 2*LARGESIZE, 2*LARGESIZE, 2*LARGESIZE},
+         {          0,   SMALLSIZE,  MEDIUMSIZE,   LARGESIZE, 2*LARGESIZE}},
+        // cold expanding
+        {{2*LARGESIZE, 2*LARGESIZE, 2*LARGESIZE, 2*LARGESIZE, 2*LARGESIZE},
+         {2*LARGESIZE, 2*LARGESIZE, 2*LARGESIZE, 2*LARGESIZE, 2*LARGESIZE},
+         {2*LARGESIZE, 2*LARGESIZE, 2*LARGESIZE, 2*LARGESIZE, 2*LARGESIZE},
+         {          0,   SMALLSIZE,  MEDIUMSIZE,   LARGESIZE, 2*LARGESIZE}},
+        // warm shrinking truncate
+        {{2*LARGESIZE, 2*LARGESIZE, 2*LARGESIZE, 2*LARGESIZE, 2*LARGESIZE},
+         {2*LARGESIZE, 2*LARGESIZE, 2*LARGESIZE, 2*LARGESIZE, 2*LARGESIZE},
+         {          0,   SMALLSIZE,  MEDIUMSIZE,   LARGESIZE, 2*LARGESIZE},
+         {          0,           0,           0,           0,           0}},
+        // warm expanding truncate
+        {{          0,   SMALLSIZE,  MEDIUMSIZE,   LARGESIZE, 2*LARGESIZE},
+         {          0,   SMALLSIZE,  MEDIUMSIZE,   LARGESIZE, 2*LARGESIZE},
+         {2*LARGESIZE, 2*LARGESIZE, 2*LARGESIZE, 2*LARGESIZE, 2*LARGESIZE},
+         {2*LARGESIZE, 2*LARGESIZE, 2*LARGESIZE, 2*LARGESIZE, 2*LARGESIZE}},
+        // mid-file shrinking truncate
+        {{2*LARGESIZE, 2*LARGESIZE, 2*LARGESIZE, 2*LARGESIZE, 2*LARGESIZE},
+         {  LARGESIZE,   LARGESIZE,   LARGESIZE,   LARGESIZE,   LARGESIZE},
+         {          0,   SMALLSIZE,  MEDIUMSIZE,   LARGESIZE, 2*LARGESIZE},
+         {          0,           0,           0,           0,           0}},
+        // mid-file expanding truncate
+        {{          0,   SMALLSIZE,   MEDIUMSIZE,  LARGESIZE, 2*LARGESIZE},
+         {          0,           0,   SMALLSIZE,  MEDIUMSIZE,   LARGESIZE},
+         {2*LARGESIZE, 2*LARGESIZE, 2*LARGESIZE, 2*LARGESIZE, 2*LARGESIZE},
+         {2*LARGESIZE, 2*LARGESIZE, 2*LARGESIZE, 2*LARGESIZE, 2*LARGESIZE}},
+    };
+
+    const lfs_off_t *startsizes = configs[CONFIG].startsizes;
+    const lfs_off_t *startseeks = configs[CONFIG].startseeks;
+    const lfs_off_t *hotsizes   = configs[CONFIG].hotsizes;
+    const lfs_off_t *coldsizes  = configs[CONFIG].coldsizes;
+
+    lfs_format(&lfs, &cfg) => 0;
+    lfs_mount(&lfs, &cfg) => 0;
+
+    for (unsigned i = 0; i < COUNT; i++) {
+        sprintf(path, "hairyhead%d", i);
+        lfs_file_open(&lfs, &file, path,
+                LFS_O_WRONLY | LFS_O_CREAT | LFS_O_TRUNC) => 0;
+
+        strcpy((char*)buffer, "hair");
+        size = strlen((char*)buffer);
+        for (lfs_off_t j = 0; j < startsizes[i]; j += size) {
+            lfs_file_write(&lfs, &file, buffer, size) => size;
+        }
+        lfs_file_size(&lfs, &file) => startsizes[i];
+
+        if (startseeks[i] != startsizes[i]) {
+            lfs_file_seek(&lfs, &file,
+                    startseeks[i], LFS_SEEK_SET) => startseeks[i];
+        }
+
+        lfs_file_truncate(&lfs, &file, hotsizes[i]) => 0;
+        lfs_file_size(&lfs, &file) => hotsizes[i];
+
+        lfs_file_close(&lfs, &file) => 0;
+    }
+
+    lfs_unmount(&lfs) => 0;
+
+    lfs_mount(&lfs, &cfg) => 0;
+
+    for (unsigned i = 0; i < COUNT; i++) {
+        sprintf(path, "hairyhead%d", i);
+        lfs_file_open(&lfs, &file, path, LFS_O_RDWR) => 0;
+        lfs_file_size(&lfs, &file) => hotsizes[i];
+
+        size = strlen("hair");
+        lfs_off_t j = 0;
+        for (; j < startsizes[i] && j < hotsizes[i]; j += size) {
+            lfs_file_read(&lfs, &file, buffer, size) => size;
+            memcmp(buffer, "hair", size) => 0;
+        }
+
+        for (; j < hotsizes[i]; j += size) {
+            lfs_file_read(&lfs, &file, buffer, size) => size;
+            memcmp(buffer, "\0\0\0\0", size) => 0;
+        }
+
+        lfs_file_truncate(&lfs, &file, coldsizes[i]) => 0;
+        lfs_file_size(&lfs, &file) => coldsizes[i];
+
+        lfs_file_close(&lfs, &file) => 0;
+    }
+
+    lfs_unmount(&lfs) => 0;
+
+    lfs_mount(&lfs, &cfg) => 0;
+
+    for (unsigned i = 0; i < COUNT; i++) {
+        sprintf(path, "hairyhead%d", i);
+        lfs_file_open(&lfs, &file, path, LFS_O_RDONLY) => 0;
+        lfs_file_size(&lfs, &file) => coldsizes[i];
+
+        size = strlen("hair");
+        lfs_off_t j = 0;
+        for (; j < startsizes[i] && j < hotsizes[i] && j < coldsizes[i];
+                j += size) {
+            lfs_file_read(&lfs, &file, buffer, size) => size;
+            memcmp(buffer, "hair", size) => 0;
+        }
+
+        for (; j < coldsizes[i]; j += size) {
+            lfs_file_read(&lfs, &file, buffer, size) => size;
+            memcmp(buffer, "\0\0\0\0", size) => 0;
+        }
+
+        lfs_file_close(&lfs, &file) => 0;
+    }
+
+    lfs_unmount(&lfs) => 0;
+'''
+define.CONFIG = 'range(6)'
+define.SMALLSIZE = 32
+define.MEDIUMSIZE = 2048
+define.LARGESIZE = 8192
+