blob: 1aab6fc590e5614d45a6132c2348f875b01b393c [file] [log] [blame]
Michal Vasko004d3152020-06-11 19:59:22 +02001/**
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002 * @file test_tree_schema.c
3 * @author: Radek Krejci <rkrejci@cesnet.cz>
4 * @brief unit tests for functions from tress_data.c
5 *
6 * Copyright (c) 2018-2019 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
Radek Iša56ca9e42020-09-08 18:42:00 +020014#define _UTEST_MAIN_
15#include "utests.h"
Radek Krejci1f05b6a2019-07-18 16:15:06 +020016
Michal Vasko38c2ba72020-07-27 14:46:59 +020017#include "common.h"
Radek Krejcib4ac5a92020-11-23 17:54:33 +010018#include "libyang.h"
Michal Vasko38c2ba72020-07-27 14:46:59 +020019#include "path.h"
20#include "xpath.h"
Radek Krejci1f05b6a2019-07-18 16:15:06 +020021
Radek Krejci1f05b6a2019-07-18 16:15:06 +020022static int
23setup(void **state)
24{
Radek Iša56ca9e42020-09-08 18:42:00 +020025 const char *schema = "module a {namespace urn:tests:a;prefix a;yang-version 1.1;"
Radek Krejcica989142020-11-05 11:32:22 +010026 "leaf bar {type string;}"
Radek Krejci1f05b6a2019-07-18 16:15:06 +020027 "list l1 { key \"a b\"; leaf a {type string;} leaf b {type string;} leaf c {type string;}}"
28 "leaf foo { type string;}"
29 "leaf-list ll { type string;}"
30 "container c {leaf-list x {type string;}}"
31 "anydata any {config false;}"
Michal Vasko38c2ba72020-07-27 14:46:59 +020032 "list l2 {config false;"
Radek Krejcib4ac5a92020-11-23 17:54:33 +010033 " container c{leaf x {type string;} leaf-list d {type string;}}"
Michal Vasko38c2ba72020-07-27 14:46:59 +020034 "}}";
Radek Krejci1f05b6a2019-07-18 16:15:06 +020035
Radek Iša56ca9e42020-09-08 18:42:00 +020036 UTEST_SETUP;
Radek Krejci1f05b6a2019-07-18 16:15:06 +020037
Radek Iša56ca9e42020-09-08 18:42:00 +020038 UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, NULL);
Radek Krejci1f05b6a2019-07-18 16:15:06 +020039
40 return 0;
41}
42
Radek Iša56ca9e42020-09-08 18:42:00 +020043#define CHECK_PARSE_LYD(INPUT, PARSE_OPTION, VALIDATE_OPTION, TREE) \
44 CHECK_PARSE_LYD_PARAM(INPUT, LYD_XML, PARSE_OPTION, VALIDATE_OPTION, LY_SUCCESS, TREE)
Radek Krejci1f05b6a2019-07-18 16:15:06 +020045
46static void
47test_compare(void **state)
48{
Radek Krejci1f05b6a2019-07-18 16:15:06 +020049 struct lyd_node *tree1, *tree2;
Radek Iša56ca9e42020-09-08 18:42:00 +020050 const char *data1;
51 const char *data2;
Radek Krejci1f05b6a2019-07-18 16:15:06 +020052
Michal Vasko8f359bf2020-07-28 10:41:15 +020053 assert_int_equal(LY_SUCCESS, lyd_compare_single(NULL, NULL, 0));
Radek Krejci1f05b6a2019-07-18 16:15:06 +020054
Radek Iša56ca9e42020-09-08 18:42:00 +020055 data1 = "<l1 xmlns=\"urn:tests:a\"><a>a</a><b>b</b><c>x</c></l1>";
56 data2 = "<l1 xmlns=\"urn:tests:a\"><a>a</a><b>b</b><c>y</c></l1>";
57 CHECK_PARSE_LYD(data1, 0, LYD_VALIDATE_PRESENT, tree1);
58 CHECK_PARSE_LYD(data2, 0, LYD_VALIDATE_PRESENT, tree2);
Michal Vasko8f359bf2020-07-28 10:41:15 +020059 assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1, tree2, 0));
60 assert_int_equal(LY_ENOT, lyd_compare_single(tree1, tree2, LYD_COMPARE_FULL_RECURSION));
Radek Krejcib4ac5a92020-11-23 17:54:33 +010061 assert_int_equal(LY_ENOT, lyd_compare_single(((struct lyd_node_inner *)tree1)->child, tree2, 0));
Radek Krejci1f05b6a2019-07-18 16:15:06 +020062 lyd_free_all(tree1);
63 lyd_free_all(tree2);
64
65 data1 = "<l2 xmlns=\"urn:tests:a\"><c><x>a</x></c></l2><l2 xmlns=\"urn:tests:a\"><c><x>b</x></c></l2>";
66 data2 = "<l2 xmlns=\"urn:tests:a\"><c><x>b</x></c></l2>";
Radek Iša56ca9e42020-09-08 18:42:00 +020067 CHECK_PARSE_LYD(data1, 0, LYD_VALIDATE_PRESENT, tree1);
68 CHECK_PARSE_LYD(data2, 0, LYD_VALIDATE_PRESENT, tree2);
Michal Vasko26123192020-11-09 21:02:34 +010069 assert_int_equal(LY_ENOT, lyd_compare_single(tree1->next, tree2->next, 0));
70 assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1->next->next, tree2->next, 0));
Radek Krejci1f05b6a2019-07-18 16:15:06 +020071 lyd_free_all(tree1);
72 lyd_free_all(tree2);
73
74 data1 = "<ll xmlns=\"urn:tests:a\">a</ll><ll xmlns=\"urn:tests:a\">b</ll>";
75 data2 = "<ll xmlns=\"urn:tests:a\">b</ll>";
Radek Iša56ca9e42020-09-08 18:42:00 +020076 CHECK_PARSE_LYD(data1, 0, LYD_VALIDATE_PRESENT, tree1);
77 CHECK_PARSE_LYD(data2, 0, LYD_VALIDATE_PRESENT, tree2);
Michal Vasko8f359bf2020-07-28 10:41:15 +020078 assert_int_equal(LY_ENOT, lyd_compare_single(tree1, tree2, 0));
79 assert_int_equal(LY_ENOT, lyd_compare_single(NULL, tree2, 0));
80 assert_int_equal(LY_ENOT, lyd_compare_single(tree1, NULL, 0));
81 assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1->next, tree2, 0));
Radek Krejci1f05b6a2019-07-18 16:15:06 +020082 lyd_free_all(tree1);
83 lyd_free_all(tree2);
84
85 data1 = "<c xmlns=\"urn:tests:a\"><x>x</x></c>";
86 data2 = "<c xmlns=\"urn:tests:a\"><x>y</x></c>";
Radek Iša56ca9e42020-09-08 18:42:00 +020087 CHECK_PARSE_LYD(data1, 0, LYD_VALIDATE_PRESENT, tree1);
88 CHECK_PARSE_LYD(data2, 0, LYD_VALIDATE_PRESENT, tree2);
Michal Vasko8f359bf2020-07-28 10:41:15 +020089 assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1, tree2, 0));
90 assert_int_equal(LY_ENOT, lyd_compare_single(tree1, tree2, LYD_COMPARE_FULL_RECURSION));
Radek Krejci1f05b6a2019-07-18 16:15:06 +020091 lyd_free_all(tree1);
92 lyd_free_all(tree2);
93
94 data1 = "<c xmlns=\"urn:tests:a\"><x>x</x></c>";
95 data2 = "<c xmlns=\"urn:tests:a\"><x>x</x><x>y</x></c>";
Radek Iša56ca9e42020-09-08 18:42:00 +020096 CHECK_PARSE_LYD(data1, 0, LYD_VALIDATE_PRESENT, tree1);
97 CHECK_PARSE_LYD(data2, 0, LYD_VALIDATE_PRESENT, tree2);
Michal Vasko8f359bf2020-07-28 10:41:15 +020098 assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1, tree2, 0));
99 assert_int_equal(LY_ENOT, lyd_compare_single(tree1, tree2, LYD_COMPARE_FULL_RECURSION));
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200100 lyd_free_all(tree1);
101 lyd_free_all(tree2);
102
103 data1 = "<any xmlns=\"urn:tests:a\"><x>x</x></any>";
104 data2 = "<any xmlns=\"urn:tests:a\"><x>x</x><x>y</x></any>";
Radek Iša56ca9e42020-09-08 18:42:00 +0200105 CHECK_PARSE_LYD(data1, 0, LYD_VALIDATE_PRESENT, tree1);
106 CHECK_PARSE_LYD(data2, 0, LYD_VALIDATE_PRESENT, tree2);
Michal Vasko26123192020-11-09 21:02:34 +0100107 assert_int_equal(LY_ENOT, lyd_compare_single(tree1->next, tree2->next, 0));
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200108 lyd_free_all(tree1);
109 data1 = "<any xmlns=\"urn:tests:a\"><x>x</x><x>y</x></any>";
Radek Iša56ca9e42020-09-08 18:42:00 +0200110 CHECK_PARSE_LYD(data1, 0, LYD_VALIDATE_PRESENT, tree1);
Michal Vasko8f359bf2020-07-28 10:41:15 +0200111 assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1, tree2, 0));
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200112 lyd_free_all(tree1);
113 lyd_free_all(tree2);
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200114}
115
Radek Krejci22ebdba2019-07-25 13:59:43 +0200116static void
117test_dup(void **state)
118{
Radek Krejci22ebdba2019-07-25 13:59:43 +0200119 struct lyd_node *tree1, *tree2;
120 const char *result;
Radek Iša56ca9e42020-09-08 18:42:00 +0200121 const char *data;
Radek Krejci22ebdba2019-07-25 13:59:43 +0200122
Radek Iša56ca9e42020-09-08 18:42:00 +0200123 data = "<l1 xmlns=\"urn:tests:a\"><a>a</a><b>b</b><c>x</c></l1>";
124 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree1);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200125 assert_int_equal(LY_SUCCESS, lyd_dup_single(tree1, NULL, LYD_DUP_RECURSIVE, &tree2));
Michal Vasko8f359bf2020-07-28 10:41:15 +0200126 assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1, tree2, LYD_COMPARE_FULL_RECURSION));
Radek Krejci22ebdba2019-07-25 13:59:43 +0200127 lyd_free_all(tree1);
128 lyd_free_all(tree2);
129
130 data = "<l1 xmlns=\"urn:tests:a\"><a>a</a><b>b</b><c>x</c></l1>";
131 result = "<l1 xmlns=\"urn:tests:a\"><a>a</a><b>b</b></l1>";
Radek Iša56ca9e42020-09-08 18:42:00 +0200132 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree1);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200133 assert_int_equal(LY_SUCCESS, lyd_dup_single(tree1, NULL, 0, &tree2));
Radek Krejci22ebdba2019-07-25 13:59:43 +0200134 lyd_free_all(tree1);
Radek Iša56ca9e42020-09-08 18:42:00 +0200135 CHECK_PARSE_LYD(result, 0, LYD_VALIDATE_PRESENT, tree1);
Michal Vasko8f359bf2020-07-28 10:41:15 +0200136 assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1, tree2, LYD_COMPARE_FULL_RECURSION));
Radek Krejci22ebdba2019-07-25 13:59:43 +0200137 lyd_free_all(tree1);
138 lyd_free_all(tree2);
139
140 data = "<l2 xmlns=\"urn:tests:a\"><c><x>a</x></c></l2><l2 xmlns=\"urn:tests:a\"><c><x>b</x></c></l2>";
141 result = "<l2 xmlns=\"urn:tests:a\"><c><x>a</x></c></l2>";
Radek Iša56ca9e42020-09-08 18:42:00 +0200142 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree1);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200143 assert_int_equal(LY_SUCCESS, lyd_dup_siblings(tree1, NULL, LYD_DUP_RECURSIVE, &tree2));
Michal Vasko26123192020-11-09 21:02:34 +0100144 assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1->next, tree2->next, LYD_COMPARE_FULL_RECURSION));
Radek Krejci22ebdba2019-07-25 13:59:43 +0200145 lyd_free_all(tree2);
Michal Vasko26123192020-11-09 21:02:34 +0100146 assert_int_equal(LY_SUCCESS, lyd_dup_single(tree1->next, NULL, LYD_DUP_RECURSIVE, &tree2));
Radek Krejci22ebdba2019-07-25 13:59:43 +0200147 lyd_free_all(tree1);
Radek Iša56ca9e42020-09-08 18:42:00 +0200148 CHECK_PARSE_LYD(result, 0, LYD_VALIDATE_PRESENT, tree1);
Michal Vasko26123192020-11-09 21:02:34 +0100149 assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1->next, tree2, LYD_COMPARE_FULL_RECURSION));
Radek Krejci22ebdba2019-07-25 13:59:43 +0200150 lyd_free_all(tree2);
151
Michal Vasko26123192020-11-09 21:02:34 +0100152 assert_int_equal(LY_SUCCESS, lyd_dup_single(tree1->next, NULL, 0, &tree2));
Radek Krejci22ebdba2019-07-25 13:59:43 +0200153 lyd_free_all(tree1);
154 result = "<l2 xmlns=\"urn:tests:a\"/>";
Radek Iša56ca9e42020-09-08 18:42:00 +0200155 CHECK_PARSE_LYD_PARAM(result, LYD_XML, LYD_PARSE_ONLY, 0, LY_SUCCESS, tree1);
Michal Vasko8f359bf2020-07-28 10:41:15 +0200156 assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1, tree2, LYD_COMPARE_FULL_RECURSION));
Radek Krejci22ebdba2019-07-25 13:59:43 +0200157 lyd_free_all(tree1);
158 lyd_free_all(tree2);
159
160 data = "<any xmlns=\"urn:tests:a\"><c><a>a</a></c></any>";
Radek Iša56ca9e42020-09-08 18:42:00 +0200161 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree1);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200162 assert_int_equal(LY_SUCCESS, lyd_dup_single(tree1, NULL, 0, &tree2));
Michal Vasko8f359bf2020-07-28 10:41:15 +0200163 assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1, tree2, LYD_COMPARE_FULL_RECURSION));
Radek Krejci22ebdba2019-07-25 13:59:43 +0200164 lyd_free_all(tree1);
165 lyd_free_all(tree2);
166
167 data = "<l2 xmlns=\"urn:tests:a\"><c><x>b</x></c></l2>";
Radek Iša56ca9e42020-09-08 18:42:00 +0200168 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree1);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100169 assert_int_equal(LY_SUCCESS, lyd_dup_single(((struct lyd_node_inner *)((struct lyd_node_inner *)tree1->next)->child)->child, NULL,
170 LYD_DUP_WITH_PARENTS, &tree2));
Radek Iša56ca9e42020-09-08 18:42:00 +0200171 int unsigned flag = LYS_CONFIG_R | LYS_SET_ENUM;
172
173 CHECK_LYSC_NODE(tree2->schema, NULL, 0, flag, 1, "x", 1, LYS_LEAF, 1, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100174 assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1->next, (struct lyd_node *)tree2->parent->parent,
Michal Vasko26123192020-11-09 21:02:34 +0100175 LYD_COMPARE_FULL_RECURSION));
Radek Krejci22ebdba2019-07-25 13:59:43 +0200176 lyd_free_all(tree1);
177 lyd_free_all(tree2);
178
179 data = "<l1 xmlns=\"urn:tests:a\"><a>a</a><b>b</b><c>c</c></l1>";
Radek Iša56ca9e42020-09-08 18:42:00 +0200180 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree1);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100181 assert_int_equal(LY_SUCCESS, lyd_dup_single(((struct lyd_node_inner *)tree1)->child->prev, NULL,
182 LYD_DUP_WITH_PARENTS, &tree2));
Radek Iša56ca9e42020-09-08 18:42:00 +0200183 flag = LYS_CONFIG_W | LYS_SET_ENUM;
184 CHECK_LYSC_NODE(tree2->schema, NULL, 0, flag, 1, "c", 0, LYS_LEAF, 1, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100185 assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1, (struct lyd_node *)tree2->parent, LYD_COMPARE_FULL_RECURSION));
Radek Krejci22ebdba2019-07-25 13:59:43 +0200186 lyd_free_all(tree1);
187 lyd_free_all(tree2);
188
189 data = "<l2 xmlns=\"urn:tests:a\"><c><x>b</x></c></l2>";
Radek Iša56ca9e42020-09-08 18:42:00 +0200190 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree1);
Michal Vasko26123192020-11-09 21:02:34 +0100191 assert_int_equal(LY_SUCCESS, lyd_dup_single(tree1->next, NULL, 0, &tree2));
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100192 assert_int_equal(LY_SUCCESS, lyd_dup_single(((struct lyd_node_inner *)((struct lyd_node_inner *)tree1->next)->child)->child,
193 (struct lyd_node_inner *)tree2, LYD_DUP_WITH_PARENTS, NULL));
Michal Vasko26123192020-11-09 21:02:34 +0100194 assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1->next, tree2, LYD_COMPARE_FULL_RECURSION));
Radek Krejci22ebdba2019-07-25 13:59:43 +0200195 lyd_free_all(tree1);
196 lyd_free_all(tree2);
197
198 /* invalid */
199 data = "<l1 xmlns=\"urn:tests:a\"><a>a</a><b>b</b><c>c</c></l1><l2 xmlns=\"urn:tests:a\"><c><x>b</x></c></l2>";
Radek Iša56ca9e42020-09-08 18:42:00 +0200200 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree1);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100201 assert_int_equal(LY_EINVAL, lyd_dup_single(((struct lyd_node_inner *)tree1)->child->prev,
202 (struct lyd_node_inner *)tree1->next, LYD_DUP_WITH_PARENTS, NULL));
Radek Krejci22ebdba2019-07-25 13:59:43 +0200203 lyd_free_all(tree1);
Radek Krejci22ebdba2019-07-25 13:59:43 +0200204}
205
Michal Vasko38c2ba72020-07-27 14:46:59 +0200206static void
207test_target(void **state)
208{
Michal Vasko38c2ba72020-07-27 14:46:59 +0200209 const struct lyd_node_term *term;
210 struct lyd_node *tree;
211 struct lyxp_expr *exp;
212 struct ly_path *path;
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200213 const char *path_str = "/a:l2[2]/c/d[3]";
Michal Vasko38c2ba72020-07-27 14:46:59 +0200214 const char *data =
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100215 "<l2 xmlns=\"urn:tests:a\"><c>"
216 " <d>a</d>"
217 " </c></l2>"
218 "<l2 xmlns=\"urn:tests:a\"><c>"
219 " <d>a</d>"
220 " <d>b</d>"
221 " <d>b</d>"
222 " <d>c</d>"
223 "</c></l2>"
224 "<l2 xmlns=\"urn:tests:a\"><c>"
225 "</c></l2>";
Michal Vasko38c2ba72020-07-27 14:46:59 +0200226
Radek Iša56ca9e42020-09-08 18:42:00 +0200227 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
228 assert_int_equal(LY_SUCCESS, ly_path_parse(UTEST_LYCTX, NULL, path_str, strlen(path_str), LY_PATH_BEGIN_EITHER, LY_PATH_LREF_FALSE,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100229 LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &exp));
Radek Iša56ca9e42020-09-08 18:42:00 +0200230 assert_int_equal(LY_SUCCESS, ly_path_compile(UTEST_LYCTX, NULL, NULL, exp, LY_PATH_LREF_FALSE, LY_PATH_OPER_INPUT,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100231 LY_PATH_TARGET_SINGLE, LY_PREF_JSON, NULL, NULL, &path));
Michal Vasko38c2ba72020-07-27 14:46:59 +0200232 term = lyd_target(path, tree);
233
Radek Iša56ca9e42020-09-08 18:42:00 +0200234 const int unsigned flag = LYS_CONFIG_R | LYS_SET_ENUM | LYS_ORDBY_SYSTEM;
235
236 CHECK_LYSC_NODE(term->schema, NULL, 0, flag, 1, "d", 0, LYS_LEAFLIST, 1, 0, NULL, 0);
Michal Vaskob7be7a82020-08-20 09:09:04 +0200237 assert_string_equal(LYD_CANON_VALUE(term), "b");
238 assert_string_equal(LYD_CANON_VALUE(term->prev), "b");
Michal Vasko38c2ba72020-07-27 14:46:59 +0200239
240 lyd_free_all(tree);
Radek Iša56ca9e42020-09-08 18:42:00 +0200241 ly_path_free(UTEST_LYCTX, path);
242 lyxp_expr_free(UTEST_LYCTX, exp);
Michal Vasko38c2ba72020-07-27 14:46:59 +0200243}
244
Radek Krejcica989142020-11-05 11:32:22 +0100245static void
246test_list_pos(void **state)
247{
Radek Krejcica989142020-11-05 11:32:22 +0100248 const char *data;
249 struct lyd_node *tree;
250
251 data = "<bar xmlns=\"urn:tests:a\">test</bar>"
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100252 "<l1 xmlns=\"urn:tests:a\"><a>one</a><b>one</b></l1>"
253 "<l1 xmlns=\"urn:tests:a\"><a>two</a><b>two</b></l1>"
254 "<foo xmlns=\"urn:tests:a\">test</foo>";
Radek Iša56ca9e42020-09-08 18:42:00 +0200255 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(UTEST_LYCTX, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
Radek Krejcica989142020-11-05 11:32:22 +0100256 assert_int_equal(0, lyd_list_pos(tree));
257 assert_int_equal(1, lyd_list_pos(tree->next));
258 assert_int_equal(2, lyd_list_pos(tree->next->next));
259 assert_int_equal(0, lyd_list_pos(tree->next->next->next));
260 lyd_free_all(tree);
261
262 data = "<ll xmlns=\"urn:tests:a\">one</ll>"
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100263 "<ll xmlns=\"urn:tests:a\">two</ll>"
264 "<ll xmlns=\"urn:tests:a\">three</ll>";
Radek Iša56ca9e42020-09-08 18:42:00 +0200265 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(UTEST_LYCTX, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
Radek Krejcica989142020-11-05 11:32:22 +0100266 assert_int_equal(1, lyd_list_pos(tree));
267 assert_int_equal(2, lyd_list_pos(tree->next));
268 assert_int_equal(3, lyd_list_pos(tree->next->next));
269 lyd_free_all(tree);
270
271 data = "<ll xmlns=\"urn:tests:a\">one</ll>"
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100272 "<l1 xmlns=\"urn:tests:a\"><a>one</a><b>one</b></l1>"
273 "<ll xmlns=\"urn:tests:a\">two</ll>"
274 "<l1 xmlns=\"urn:tests:a\"><a>two</a><b>two</b></l1>"
275 "<ll xmlns=\"urn:tests:a\">three</ll>"
276 "<l1 xmlns=\"urn:tests:a\"><a>three</a><b>three</b></l1>";
Radek Iša56ca9e42020-09-08 18:42:00 +0200277 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(UTEST_LYCTX, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
Radek Krejcica989142020-11-05 11:32:22 +0100278 assert_string_equal("l1", tree->schema->name);
279 assert_int_equal(1, lyd_list_pos(tree));
280 assert_int_equal(2, lyd_list_pos(tree->next));
281 assert_int_equal(3, lyd_list_pos(tree->next->next));
282 assert_string_equal("ll", tree->next->next->next->schema->name);
283 assert_int_equal(1, lyd_list_pos(tree->next->next->next));
284 assert_int_equal(2, lyd_list_pos(tree->next->next->next->next));
285 assert_int_equal(3, lyd_list_pos(tree->next->next->next->next->next));
286 lyd_free_all(tree);
Radek Krejcica989142020-11-05 11:32:22 +0100287}
288
Radek Krejci4233f9b2020-11-05 12:38:35 +0100289static void
290test_first_sibling(void **state)
291{
Radek Krejci4233f9b2020-11-05 12:38:35 +0100292 const char *data;
293 struct lyd_node *tree;
294 struct lyd_node_inner *parent;
295
296 data = "<bar xmlns=\"urn:tests:a\">test</bar>"
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100297 "<l1 xmlns=\"urn:tests:a\"><a>one</a><b>one</b><c>one</c></l1>"
298 "<foo xmlns=\"urn:tests:a\">test</foo>";
Radek Iša56ca9e42020-09-08 18:42:00 +0200299 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(UTEST_LYCTX, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
Radek Krejci4233f9b2020-11-05 12:38:35 +0100300 assert_ptr_equal(tree, lyd_first_sibling(tree->next));
301 assert_ptr_equal(tree, lyd_first_sibling(tree));
302 assert_ptr_equal(tree, lyd_first_sibling(tree->prev));
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100303 parent = (struct lyd_node_inner *)tree->next;
Radek Krejci4233f9b2020-11-05 12:38:35 +0100304 assert_int_equal(LYS_LIST, parent->schema->nodetype);
305 assert_ptr_equal(parent->child, lyd_first_sibling(parent->child->next));
306 assert_ptr_equal(parent->child, lyd_first_sibling(parent->child));
307 assert_ptr_equal(parent->child, lyd_first_sibling(parent->child->prev));
308 lyd_free_all(tree);
Radek Krejci4233f9b2020-11-05 12:38:35 +0100309}
310
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100311int
312main(void)
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200313{
314 const struct CMUnitTest tests[] = {
Radek Iša56ca9e42020-09-08 18:42:00 +0200315 UTEST(test_compare, setup),
316 UTEST(test_dup, setup),
317 UTEST(test_target, setup),
318 UTEST(test_list_pos, setup),
319 UTEST(test_first_sibling, setup),
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200320 };
321
322 return cmocka_run_group_tests(tests, NULL, NULL);
323}