From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: From: Kirill Shcherbatov Subject: [PATCH v1 2/4] lib: introduce json_token_is_leaf helper Date: Thu, 27 Dec 2018 14:15:53 +0300 Message-Id: <599faa8e6df622cffa9795a0d99073d2e789bbd9.1545909237.git.kshcherbatov@tarantool.org> In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit To: tarantool-patches@freelists.org, vdavydov.dev@gmail.com Cc: kostja@tarantool.org, Kirill Shcherbatov List-ID: 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 #include #include #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