From: Kirill Shcherbatov <kshcherbatov@tarantool.org> To: tarantool-patches@freelists.org, vdavydov.dev@gmail.com Cc: kostja@tarantool.org, Kirill Shcherbatov <kshcherbatov@tarantool.org> Subject: [PATCH v1 2/4] lib: introduce json_token_is_leaf helper Date: Thu, 27 Dec 2018 14:15:53 +0300 [thread overview] Message-ID: <599faa8e6df622cffa9795a0d99073d2e789bbd9.1545909237.git.kshcherbatov@tarantool.org> (raw) In-Reply-To: <cover.1545909236.git.kshcherbatov@tarantool.org> A new json_token_is_leaf routine tests if the passed JSON token is a JSON tree leaf(has no child record). Needed for #1012 --- src/lib/json/json.h | 11 +++++++++++ test/unit/json.c | 39 ++++++++++++++++++++++++++++++++++++++- test/unit/json.result | 10 +++++++++- 3 files changed, 58 insertions(+), 2 deletions(-) diff --git a/src/lib/json/json.h b/src/lib/json/json.h index c0adca450..0bc99474b 100644 --- a/src/lib/json/json.h +++ b/src/lib/json/json.h @@ -30,6 +30,7 @@ * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ +#include <stdbool.h> #include <stddef.h> #include <stdint.h> #include "trivia/util.h" @@ -253,6 +254,16 @@ json_path_cmp(const char *a, int a_len, const char *b, int b_len, int json_path_validate(const char *path, int path_len, int index_base); +/** + * Test if the passed JSON token is a JSON tree leaf + * (has no child records). + */ +static inline bool +json_token_is_leaf(struct json_token *token) +{ + return token->max_child_idx == -1; +} + /** * An snprint-style function to print path to a token in a JSON * tree. The root node doesn't necessarily have to be the tree diff --git a/test/unit/json.c b/test/unit/json.c index 26cc5088b..6dac688d1 100644 --- a/test/unit/json.c +++ b/test/unit/json.c @@ -508,17 +508,54 @@ test_path_snprintf() footer(); } +void +test_json_helpers() +{ + struct json_tree tree; + int rc = json_tree_create(&tree); + fail_if(rc != 0); + struct test_struct records[5]; + const char *path0 = "[1][20][\"file\"]"; + int path0_len = strlen(path0); + const char *path = "[1][20][\"file\"][8]"; + int path_len = strlen(path); + + header(); + plan(4); + + int records_idx = 0; + struct test_struct *node, *node_tmp; + node = test_add_path(&tree, path0, path0_len, records, &records_idx); + fail_if(&node->node != &records[2].node); + is(json_token_is_leaf(&records[1].node), false, "interm node is not leaf"); + is(json_token_is_leaf(&records[2].node), true, "last node is leaf"); + + node = test_add_path(&tree, path, path_len, records, &records_idx); + fail_if(&node->node != &records[3].node); + is(json_token_is_leaf(&records[2].node), false, + "last node became interm - it can't be leaf anymore"); + is(json_token_is_leaf(&records[3].node), true, "last node is leaf"); + + json_tree_foreach_entry_safe(node, &tree.root, struct test_struct, + node, node_tmp) + json_tree_del(&tree, &node->node); + json_tree_destroy(&tree); + check_plan(); + footer(); +} + int main() { header(); - plan(5); + plan(6); test_basic(); test_errors(); test_tree(); test_path_cmp(); test_path_snprintf(); + test_json_helpers(); int rc = check_plan(); footer(); diff --git a/test/unit/json.result b/test/unit/json.result index a73203154..ea2d1af8e 100644 --- a/test/unit/json.result +++ b/test/unit/json.result @@ -1,5 +1,5 @@ *** main *** -1..5 +1..6 *** test_basic *** 1..71 ok 1 - parse <[1]> @@ -191,4 +191,12 @@ ok 4 - subtests ok 18 - "1-byte size buffer" - correct rc: have 18 expected 18 ok 5 - subtests *** test_path_snprintf: done *** + *** test_json_helpers *** + 1..4 + ok 1 - interm node is not leaf + ok 2 - last node is leaf + ok 3 - last node became interm - it can't be leaf anymore + ok 4 - last node is leaf +ok 6 - subtests + *** test_json_helpers: done *** *** main: done *** -- 2.19.2
next prev parent reply other threads:[~2018-12-27 11:15 UTC|newest] Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top 2018-12-27 11:15 [PATCH v1 0/4] box: JSON preparatory patchset Kirill Shcherbatov 2018-12-27 11:15 ` [PATCH v1 1/4] lib: introduce json_tree_snprint_path Kirill Shcherbatov 2018-12-27 18:51 ` Vladimir Davydov 2018-12-27 11:15 ` Kirill Shcherbatov [this message] 2018-12-27 18:52 ` [PATCH v1 2/4] lib: introduce json_token_is_leaf helper Vladimir Davydov 2018-12-27 11:15 ` [PATCH v1 3/4] box: introduce bitmap_majority_test routine Kirill Shcherbatov 2018-12-27 18:59 ` Vladimir Davydov 2018-12-29 12:58 ` [tarantool-patches] " Kirill Shcherbatov 2018-12-29 13:19 ` Vladimir Davydov 2018-12-29 13:57 ` Kirill Shcherbatov 2018-12-29 16:16 ` Vladimir Davydov 2018-12-27 11:15 ` [PATCH v1 4/4] box: refactor tuple_init_field_map to use bitmap Kirill Shcherbatov 2018-12-27 11:48 ` [tarantool-patches] " Konstantin Osipov 2018-12-27 19:12 ` Vladimir Davydov 2018-12-29 12:58 ` [tarantool-patches] " Kirill Shcherbatov 2018-12-29 13:22 ` Vladimir Davydov 2018-12-29 16:25 ` Vladimir Davydov 2018-12-27 19:13 ` [PATCH v1 0/4] box: JSON preparatory patchset Vladimir Davydov
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=599faa8e6df622cffa9795a0d99073d2e789bbd9.1545909237.git.kshcherbatov@tarantool.org \ --to=kshcherbatov@tarantool.org \ --cc=kostja@tarantool.org \ --cc=tarantool-patches@freelists.org \ --cc=vdavydov.dev@gmail.com \ --subject='Re: [PATCH v1 2/4] lib: introduce json_token_is_leaf helper' \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: link
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox