diff --git a/lfs.c b/lfs.c index f7a1bb03..50653295 100644 --- a/lfs.c +++ b/lfs.c @@ -4507,104 +4507,6 @@ static int lfsr_btree_commit(lfs_t *lfs, lfsr_btree_t *btree, return 0; } -// TODO move to tests -static int lfsr_btree_push(lfs_t *lfs, lfsr_btree_t *btree, - lfs_size_t bid, lfsr_tag_t tag, lfs_size_t weight, lfsr_data_t data) { - LFS_ASSERT(bid <= lfsr_btree_weight(btree)); - return lfsr_btree_commit(lfs, btree, LFSR_ATTRS( - LFSR_ATTR(bid, TAG(tag), +weight, DATA(data)))); -} - -// TODO move to tests -static int lfsr_btree_set(lfs_t *lfs, lfsr_btree_t *btree, - lfs_size_t bid, lfsr_tag_t tag, lfs_size_t weight, lfsr_data_t data) { - LFS_ASSERT(bid < lfsr_btree_weight(btree)); - LFS_ASSERT(lfsr_btree_weight(btree) > 0); - - // TODO yes this is completely redundant and eventually should be removed - // - // We're looking up the bid here to find it's weight so we can compute the - // correct delta. We could move this into lfsr_btree_commit__ hackily, but - // the currrent theory is we don't need this at all and upper layers can - // calculate the delta instead of the absolute weight when needed. - lfs_size_t weight_; - int err = lfsr_btree_lookupnext(lfs, btree, bid, - NULL, NULL, &weight_, NULL); - if (err) { - return err; - } - - // note we need a second tag here in case our entry has a - // name attributes, the name attribute holds the weight not - // the struct tag - return lfsr_btree_commit(lfs, btree, LFSR_ATTRS( - LFSR_ATTR(bid, WIDE(TAG(tag)), 0, DATA(data)), - LFSR_ATTR(bid, GROW(RM), weight - weight_, NULL))); -} - -// TODO move to tests -static int lfsr_btree_pop(lfs_t *lfs, lfsr_btree_t *btree, lfs_size_t bid) { - LFS_ASSERT(bid < lfsr_btree_weight(btree)); - LFS_ASSERT(lfsr_btree_weight(btree) > 0); - - // TODO yes this is completely redundant and eventually should be removed - // - // We're looking up the bid here to find it's weight so we can compute the - // correct delta. We could move this into lfsr_btree_commit__ hackily, but - // the currrent theory is we don't need this at all and upper layers can - // calculate the delta instead of the absolute weight when needed. - lfs_size_t weight_; - int err = lfsr_btree_lookupnext(lfs, btree, bid, - NULL, NULL, &weight_, NULL); - if (err) { - return err; - } - - return lfsr_btree_commit(lfs, btree, LFSR_ATTRS( - LFSR_ATTR(bid, RM, -weight_, NULL))); -} - -// TODO move to tests -// lfsr_btree_split can be done with a update+push, but this function -// does all this in one commit, which is much more efficient -// -// this is also the only btree function that creates name entries, in theory -// push could as well, we just don't need the functionality for littlefs -// -static int lfsr_btree_split(lfs_t *lfs, lfsr_btree_t *btree, - lfs_size_t bid, lfsr_data_t name, - lfsr_tag_t tag1, lfs_size_t weight1, lfsr_data_t data1, - lfsr_tag_t tag2, lfs_size_t weight2, lfsr_data_t data2) { - LFS_ASSERT(bid < lfsr_btree_weight(btree)); - LFS_ASSERT(lfsr_btree_weight(btree) > 0); - - // TODO yes this is completely redundant and eventually should be removed - // - // We're looking up the bid here to find it's weight so we can compute the - // correct delta. We could move this into lfsr_btree_commit__ hackily, but - // the currrent theory is we don't need this at all and upper layers can - // calculate the delta instead of the absolute weight when needed. - lfs_size_t weight_; - int err = lfsr_btree_lookupnext(lfs, btree, bid, - NULL, NULL, &weight_, NULL); - if (err) { - return err; - } - - return lfsr_btree_commit(lfs, btree, LFSR_ATTRS( - LFSR_ATTR(bid, GROW(RM), +weight1-weight_, NULL), - LFSR_ATTR(bid-(weight_-1)+weight1-1, TAG(tag1), 0, DATA(data1)), - (lfsr_data_size(&name) > 0 - ? LFSR_ATTR(bid-(weight_-1)+weight1, - BNAME, +weight2, DATA(name)) - : LFSR_ATTR_NOOP), - (lfsr_data_size(&name) > 0 - ? LFSR_ATTR(bid-(weight_-1)+weight1+weight2-1, - TAG(tag2), 0, DATA(data2)) - : LFSR_ATTR(bid-(weight_-1)+weight1, - TAG(tag2), +weight2, DATA(data2))))); -} - // lookup in a btree by name static int lfsr_btree_namelookup(lfs_t *lfs, const lfsr_btree_t *btree, lfs_size_t did, const char *name, lfs_size_t name_size, diff --git a/tests/test_btree.toml b/tests/test_btree.toml index c4284b7e..e4a32cac 100644 --- a/tests/test_btree.toml +++ b/tests/test_btree.toml @@ -5,6 +5,85 @@ after = 'test_rbyd' # of the disk for these tests defines.LOOKAHEAD_SIZE = 'lfs_alignup(BLOCK_COUNT / 8, 8)' +# helper functions +in = 'lfs.c' +code = ''' + static int lfsr_btree_push(lfs_t *lfs, lfsr_btree_t *btree, + lfs_size_t bid, lfsr_tag_t tag, lfs_size_t weight, + lfsr_data_t data) { + LFS_ASSERT(bid <= lfsr_btree_weight(btree)); + return lfsr_btree_commit(lfs, btree, LFSR_ATTRS( + LFSR_ATTR(bid, TAG(tag), +weight, DATA(data)))); + } + + static int lfsr_btree_set(lfs_t *lfs, lfsr_btree_t *btree, + lfs_size_t bid, lfsr_tag_t tag, lfs_size_t weight, + lfsr_data_t data) { + LFS_ASSERT(bid < lfsr_btree_weight(btree)); + LFS_ASSERT(lfsr_btree_weight(btree) > 0); + + // lookup weight to compute deltas + lfs_size_t weight_; + int err = lfsr_btree_lookupnext(lfs, btree, bid, + NULL, NULL, &weight_, NULL); + if (err) { + return err; + } + + // note we need a second tag here in case our entry has a + // name attributes, the name attribute holds the weight not + // the struct tag + return lfsr_btree_commit(lfs, btree, LFSR_ATTRS( + LFSR_ATTR(bid, WIDE(TAG(tag)), 0, DATA(data)), + LFSR_ATTR(bid, GROW(RM), weight - weight_, NULL))); + } + + static int lfsr_btree_pop(lfs_t *lfs, lfsr_btree_t *btree, lfs_size_t bid) { + LFS_ASSERT(bid < lfsr_btree_weight(btree)); + LFS_ASSERT(lfsr_btree_weight(btree) > 0); + + // lookup weight to compute deltas + lfs_size_t weight_; + int err = lfsr_btree_lookupnext(lfs, btree, bid, + NULL, NULL, &weight_, NULL); + if (err) { + return err; + } + + return lfsr_btree_commit(lfs, btree, LFSR_ATTRS( + LFSR_ATTR(bid, RM, -weight_, NULL))); + } + + static int lfsr_btree_split(lfs_t *lfs, lfsr_btree_t *btree, + lfs_size_t bid, lfsr_data_t name, + lfsr_tag_t tag1, lfs_size_t weight1, lfsr_data_t data1, + lfsr_tag_t tag2, lfs_size_t weight2, lfsr_data_t data2) { + LFS_ASSERT(bid < lfsr_btree_weight(btree)); + LFS_ASSERT(lfsr_btree_weight(btree) > 0); + + // lookup weight to compute deltas + lfs_size_t weight_; + int err = lfsr_btree_lookupnext(lfs, btree, bid, + NULL, NULL, &weight_, NULL); + if (err) { + return err; + } + + return lfsr_btree_commit(lfs, btree, LFSR_ATTRS( + LFSR_ATTR(bid, GROW(RM), +weight1-weight_, NULL), + LFSR_ATTR(bid-(weight_-1)+weight1-1, TAG(tag1), 0, DATA(data1)), + (lfsr_data_size(&name) > 0 + ? LFSR_ATTR(bid-(weight_-1)+weight1, + BNAME, +weight2, DATA(name)) + : LFSR_ATTR_NOOP), + (lfsr_data_size(&name) > 0 + ? LFSR_ATTR(bid-(weight_-1)+weight1+weight2-1, + TAG(tag2), 0, DATA(data2)) + : LFSR_ATTR(bid-(weight_-1)+weight1, + TAG(tag2), +weight2, DATA(data2))))); + } +''' + # test an empty tree [cases.test_btree_zero]