data tree FEATURE schema-ordering of data

All (leaf-)list instances preserve their input
order, new instances inserted at the end.
Opaque nodes are at the end, after all the
standard nodes.

Also, now first instances of (leaf-)lists
are inserted into hash table twice, second
time without their keys/value in the hash.
diff --git a/tests/utests/data/test_diff.c b/tests/utests/data/test_diff.c
index bb52594..c84ce86 100644
--- a/tests/utests/data/test_diff.c
+++ b/tests/utests/data/test_diff.c
@@ -500,8 +500,8 @@
                          "<list><name>c</name><value>3</value></list>"
                        "</df>";
     const char *xml3 = "<df xmlns=\"urn:libyang:tests:defaults\">"
-                         "<list><name>a</name><value>2</value></list>"
                          "<list><name>b</name><value>-2</value></list>"
+                         "<list><name>a</name><value>2</value></list>"
                        "</df>";
 
     assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(st->ctx, xml1, LYD_XML, LYD_PARSE_ONLY, 0, &st->first));
@@ -562,7 +562,7 @@
     lyd_print_mem(&st->xml1, st->second, LYD_XML, LYD_PRINT_WITHSIBLINGS);
     free(st->xml2);
     lyd_print_mem(&st->xml2, st->third, LYD_XML, LYD_PRINT_WITHSIBLINGS);
-    /* TODO ordering assert_string_equal(st->xml1, st->xml2); */
+    assert_string_equal(st->xml1, st->xml2);
 
     /* merge */
     assert_int_equal(lyd_diff_merge_all(st->diff2, &st->diff1), LY_SUCCESS);
@@ -835,7 +835,7 @@
                          "<foo>41</foo><dllist>4</dllist>"
                        "</df>";
     const char *xml3 = "<df xmlns=\"urn:libyang:tests:defaults\">"
-                         "<foo>42</foo><dllist>1</dllist><dllist>4</dllist>"
+                         "<foo>42</foo><dllist>4</dllist><dllist>1</dllist>"
                        "</df>";
 
     mod = ly_ctx_get_module_implemented(st->ctx, "defaults");
@@ -867,8 +867,7 @@
     assert_int_equal(lyd_diff_apply_all(&st->first, st->diff1), LY_SUCCESS);
     lyd_print_mem(&st->xml1, st->first, LYD_XML, LYD_PRINT_WITHSIBLINGS);
     lyd_print_mem(&st->xml2, st->second, LYD_XML, LYD_PRINT_WITHSIBLINGS);
-    /* TODO just an ordering problem
-    assert_string_equal(st->xml1, st->xml2);*/
+    assert_string_equal(st->xml1, st->xml2);
 
     /* diff2 */
     assert_int_equal(lyd_diff_siblings(st->second, st->third, LYD_DIFF_DEFAULTS, &st->diff2), LY_SUCCESS);
@@ -888,7 +887,7 @@
     lyd_print_mem(&st->xml1, st->second, LYD_XML, LYD_PRINT_WITHSIBLINGS);
     free(st->xml2);
     lyd_print_mem(&st->xml2, st->third, LYD_XML, LYD_PRINT_WITHSIBLINGS);
-    /* TODO ordering assert_string_equal(st->xml1, st->xml2); */
+    assert_string_equal(st->xml1, st->xml2);
 
     /* merge */
     assert_int_equal(lyd_diff_merge_all(st->diff2, &st->diff1), LY_SUCCESS);
diff --git a/tests/utests/data/test_merge.c b/tests/utests/data/test_merge.c
index 2ded52e..95e2f86 100644
--- a/tests/utests/data/test_merge.c
+++ b/tests/utests/data/test_merge.c
@@ -191,25 +191,24 @@
         "<module>"
             "<name>yang</name>"
             "<revision>2016-02-11</revision>"
-            "<conformance-type>implement</conformance-type>"
             "<namespace>urn:ietf:params:xml:ns:yang:1</namespace>"
+            "<conformance-type>implement</conformance-type>"
         "</module>"
         "<module>"
             "<name>ietf-yang-library</name>"
             "<revision>2016-02-01</revision>"
-            "<conformance-type>implement</conformance-type>"
             "<namespace>urn:ietf:params:xml:ns:yang:ietf-yang-library</namespace>"
+            "<conformance-type>implement</conformance-type>"
         "</module>"
         "<module>"
             "<name>ietf-netconf-acm</name>"
             "<revision>2012-02-22</revision>"
-            "<conformance-type>implement</conformance-type>"
             "<namespace>urn:ietf:params:xml:ns:yang:ietf-netconf-acm</namespace>"
+            "<conformance-type>implement</conformance-type>"
         "</module>"
         "<module>"
             "<name>ietf-netconf</name>"
             "<revision>2011-06-01</revision>"
-            "<conformance-type>implement</conformance-type>"
             "<namespace>urn:ietf:params:xml:ns:netconf:base:1.0</namespace>"
             "<feature>writable-running</feature>"
             "<feature>candidate</feature>"
@@ -217,18 +216,19 @@
             "<feature>validate</feature>"
             "<feature>startup</feature>"
             "<feature>xpath</feature>"
+            "<conformance-type>implement</conformance-type>"
         "</module>"
         "<module>"
             "<name>ietf-netconf-monitoring</name>"
             "<revision>2010-10-04</revision>"
-            "<conformance-type>implement</conformance-type>"
             "<namespace>urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring</namespace>"
+            "<conformance-type>implement</conformance-type>"
         "</module>"
         "<module>"
             "<name>ietf-netconf-with-defaults</name>"
             "<revision>2011-06-01</revision>"
-            "<conformance-type>implement</conformance-type>"
             "<namespace>urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults</namespace>"
+            "<conformance-type>implement</conformance-type>"
         "</module>"
     "</modules-state>";
     struct ly_in *in = NULL;
@@ -574,7 +574,8 @@
     st->source = NULL;
 
     /* c should be replaced and now be default */
-    assert_true(lyd_node_children(st->target, 0)->flags & LYD_DEFAULT);
+    assert_string_equal(lyd_node_children(st->target, 0)->prev->schema->name, "c");
+    assert_true(lyd_node_children(st->target, 0)->prev->flags & LYD_DEFAULT);
 }
 
 static void
diff --git a/tests/utests/data/test_parser_xml.c b/tests/utests/data/test_parser_xml.c
index 06510da..b7a6952 100644
--- a/tests/utests/data/test_parser_xml.c
+++ b/tests/utests/data/test_parser_xml.c
@@ -441,17 +441,18 @@
     assert_string_equal(node->schema->name, "edit-config");
     node = lyd_node_children(node, 0)->next;
     assert_string_equal(node->schema->name, "config");
+
     node = ((struct lyd_node_any *)node)->value.tree;
-    /* l1 key c has invalid value */
-    assert_null(node->schema);
-    assert_string_equal(((struct lyd_node_opaq *)node)->name, "l1");
-    node = node->next;
     assert_non_null(node->schema);
     assert_string_equal(node->schema->name, "cp");
     node = lyd_node_children(node, 0);
     /* z has no value */
     assert_null(node->schema);
     assert_string_equal(((struct lyd_node_opaq *)node)->name, "z");
+    node = node->parent->next;
+    /* l1 key c has invalid value so it is at the end */
+    assert_null(node->schema);
+    assert_string_equal(((struct lyd_node_opaq *)node)->name, "l1");
 
     lyd_print_tree(out, tree, LYD_XML, 0);
     assert_string_equal(str,
@@ -461,14 +462,14 @@
                     "<running/>"
                 "</target>"
                 "<config>"
+                    "<cp xmlns=\"urn:tests:a\">"
+                        "<z xmlns:nc=\"urn:ietf:params:xml:ns:netconf:base:1.0\" nc:operation=\"delete\"/>"
+                    "</cp>"
                     "<l1 xmlns=\"urn:tests:a\" xmlns:nc=\"urn:ietf:params:xml:ns:netconf:base:1.0\" nc:operation=\"replace\">"
                         "<a>val_a</a>"
                         "<b>val_b</b>"
                         "<c>val_c</c>"
                     "</l1>"
-                    "<cp xmlns=\"urn:tests:a\">"
-                        "<z xmlns:nc=\"urn:ietf:params:xml:ns:netconf:base:1.0\" nc:operation=\"delete\"/>"
-                    "</cp>"
                 "</config>"
             "</edit-config>"
         "</rpc>");
diff --git a/tests/utests/data/test_printer_xml.c b/tests/utests/data/test_printer_xml.c
index f6d8089..54792f9 100644
--- a/tests/utests/data/test_printer_xml.c
+++ b/tests/utests/data/test_printer_xml.c
@@ -286,30 +286,30 @@
 
     assert_int_equal(LY_SUCCESS, lyd_print_all(out, tree, LYD_XML, LYD_PRINT_WD_ALL));
     assert_int_equal(strlen(printed), ly_out_printed(out));
-    data = "<c xmlns=\"urn:defaults\">aa</c><a xmlns=\"urn:defaults\" xmlns:d=\"urn:defaults\">/d:b</a>";
+    data = "<a xmlns=\"urn:defaults\" xmlns:d=\"urn:defaults\">/d:b</a><c xmlns=\"urn:defaults\">aa</c>";
     assert_string_equal(printed, data);
     ly_out_reset(out);
 
     assert_int_equal(LY_SUCCESS, lyd_print_all(out, tree, LYD_XML, LYD_PRINT_WD_ALL_TAG));
     assert_int_equal(strlen(printed), ly_out_printed(out));
-    data = "<c xmlns=\"urn:defaults\">aa</c>"
-        "<a xmlns=\"urn:defaults\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\""
-        " ncwd:default=\"true\" xmlns:d=\"urn:defaults\">/d:b</a>";
+    data = "<a xmlns=\"urn:defaults\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\""
+        " ncwd:default=\"true\" xmlns:d=\"urn:defaults\">/d:b</a>"
+        "<c xmlns=\"urn:defaults\">aa</c>";
     assert_string_equal(printed, data);
     ly_out_reset(out);
 
     assert_int_equal(LY_SUCCESS, lyd_print_all(out, tree, LYD_XML, LYD_PRINT_WD_IMPL_TAG));
     assert_int_equal(strlen(printed), ly_out_printed(out));
-    data = "<c xmlns=\"urn:defaults\">aa</c>"
-        "<a xmlns=\"urn:defaults\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\""
-        " ncwd:default=\"true\" xmlns:d=\"urn:defaults\">/d:b</a>";
+    data = "<a xmlns=\"urn:defaults\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\""
+        " ncwd:default=\"true\" xmlns:d=\"urn:defaults\">/d:b</a>"
+        "<c xmlns=\"urn:defaults\">aa</c>";
     assert_string_equal(printed, data);
     ly_out_reset(out);
 
     lyd_free_all(tree);
 
     /* string value equal to the default but default is an unresolved instance-identifier, so they are not considered equal */
-    data = "<c xmlns=\"urn:defaults\">aa</c><a xmlns=\"urn:defaults\">/d:b</a>";
+    data = "<a xmlns=\"urn:defaults\">/d:b</a><c xmlns=\"urn:defaults\">aa</c>";
     assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(s->ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
 
     assert_int_equal(LY_SUCCESS, lyd_print_all(out, tree, LYD_XML, LYD_PRINT_WD_TRIM));
@@ -335,33 +335,33 @@
     lyd_free_all(tree);
 
     /* instance-identifier value equal to the default, should be considered equal */
-    data = "<c xmlns=\"urn:defaults\">aa</c><a xmlns=\"urn:defaults\" xmlns:d=\"urn:defaults\">/d:b</a><b xmlns=\"urn:defaults\">val</b>";
+    data = "<a xmlns=\"urn:defaults\" xmlns:d=\"urn:defaults\">/d:b</a><b xmlns=\"urn:defaults\">val</b><c xmlns=\"urn:defaults\">aa</c>";
     assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(s->ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
 
     assert_int_equal(LY_SUCCESS, lyd_print_all(out, tree, LYD_XML, LYD_PRINT_WD_TRIM));
     assert_int_equal(strlen(printed), ly_out_printed(out));
-    data = "<c xmlns=\"urn:defaults\">aa</c><b xmlns=\"urn:defaults\">val</b>";
+    data = "<b xmlns=\"urn:defaults\">val</b><c xmlns=\"urn:defaults\">aa</c>";
     assert_string_equal(printed, data);
     ly_out_reset(out);
 
     assert_int_equal(LY_SUCCESS, lyd_print_all(out, tree, LYD_XML, LYD_PRINT_WD_ALL));
     assert_int_equal(strlen(printed), ly_out_printed(out));
-    data = "<c xmlns=\"urn:defaults\">aa</c><a xmlns=\"urn:defaults\" xmlns:d=\"urn:defaults\">/d:b</a><b xmlns=\"urn:defaults\">val</b>";
+    data = "<a xmlns=\"urn:defaults\" xmlns:d=\"urn:defaults\">/d:b</a><b xmlns=\"urn:defaults\">val</b><c xmlns=\"urn:defaults\">aa</c>";
     assert_string_equal(printed, data);
     ly_out_reset(out);
 
     assert_int_equal(LY_SUCCESS, lyd_print_all(out, tree, LYD_XML, LYD_PRINT_WD_ALL_TAG));
     assert_int_equal(strlen(printed), ly_out_printed(out));
-    data = "<c xmlns=\"urn:defaults\">aa</c>"
-        "<a xmlns=\"urn:defaults\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\""
+    data = "<a xmlns=\"urn:defaults\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\""
         " ncwd:default=\"true\" xmlns:d=\"urn:defaults\">/d:b</a>"
-        "<b xmlns=\"urn:defaults\">val</b>";
+        "<b xmlns=\"urn:defaults\">val</b>"
+        "<c xmlns=\"urn:defaults\">aa</c>";
     assert_string_equal(printed, data);
     ly_out_reset(out);
 
     assert_int_equal(LY_SUCCESS, lyd_print_all(out, tree, LYD_XML, LYD_PRINT_WD_IMPL_TAG));
     assert_int_equal(strlen(printed), ly_out_printed(out));
-    data = "<c xmlns=\"urn:defaults\">aa</c><a xmlns=\"urn:defaults\" xmlns:d=\"urn:defaults\">/d:b</a><b xmlns=\"urn:defaults\">val</b>";
+    data = "<a xmlns=\"urn:defaults\" xmlns:d=\"urn:defaults\">/d:b</a><b xmlns=\"urn:defaults\">val</b><c xmlns=\"urn:defaults\">aa</c>";
     assert_string_equal(printed, data);
     ly_out_reset(out);
 
diff --git a/tests/utests/data/test_types.c b/tests/utests/data/test_types.c
index 6d223dc..1a501c9 100644
--- a/tests/utests/data/test_types.c
+++ b/tests/utests/data/test_types.c
@@ -1014,9 +1014,8 @@
 
     TEST_DATA("<str-norestr xmlns=\"urn:tests:types\">y</str-norestr>"
               "<c xmlns=\"urn:tests:leafrefs\"><l><id>x</id><value>x</value><lr1>y</lr1></l></c>", LY_SUCCESS, "");
-    tree = tree->prev;
     assert_int_equal(LYS_CONTAINER, tree->schema->nodetype);
-    leaf = (struct lyd_node_term*)(lyd_node_children(lyd_node_children(tree, 0), 0)->prev);
+    leaf = (struct lyd_node_term*)(lyd_node_children(lyd_node_children(tree, 0)->next, 0)->prev);
     assert_int_equal(LYS_LEAF, leaf->schema->nodetype);
     assert_string_equal("lr1", leaf->schema->name);
     assert_string_equal("y", leaf->value.canonical_cache);
@@ -1025,9 +1024,8 @@
     TEST_DATA("<str-norestr xmlns=\"urn:tests:types\">y</str-norestr>"
               "<c xmlns=\"urn:tests:leafrefs\"><l><id>y</id><value>y</value></l>"
               "<l><id>x</id><value>x</value><lr2>y</lr2></l></c>", LY_SUCCESS, "");
-    tree = tree->prev;
     assert_int_equal(LYS_CONTAINER, tree->schema->nodetype);
-    leaf = (struct lyd_node_term*)(lyd_node_children(lyd_node_children(tree, 0)->prev->prev, 0)->prev);
+    leaf = (struct lyd_node_term*)(lyd_node_children(lyd_node_children(tree, 0)->prev, 0)->prev);
     assert_int_equal(LYS_LEAF, leaf->schema->nodetype);
     assert_string_equal("lr2", leaf->schema->name);
     assert_string_equal("y", leaf->value.canonical_cache);
@@ -1037,7 +1035,6 @@
               "<list xmlns=\"urn:tests:types\"><id>y</id><targets>c</targets><targets>d</targets></list>"
               "<c xmlns=\"urn:tests:leafrefs\"><x><x>y</x></x>"
               "<l><id>x</id><value>x</value><lr3>c</lr3></l></c>", LY_SUCCESS, "");
-    tree = tree->prev;
     assert_int_equal(LYS_CONTAINER, tree->schema->nodetype);
     leaf = (struct lyd_node_term*)(lyd_node_children(lyd_node_children(tree, 0)->prev, 0)->prev);
     assert_int_equal(LYS_LEAF, leaf->schema->nodetype);
diff --git a/tests/utests/data/test_validation.c b/tests/utests/data/test_validation.c
index e0c4de8..f223a6d 100644
--- a/tests/utests/data/test_validation.c
+++ b/tests/utests/data/test_validation.c
@@ -1073,12 +1073,13 @@
 
     /* create another explicit case and validate */
     assert_int_equal(lyd_new_term(NULL, mod, "l", "value", &node), LY_SUCCESS);
-    assert_int_equal(lyd_insert_sibling(tree, node), LY_SUCCESS);
+    assert_int_equal(lyd_insert_sibling(tree, node, &tree), LY_SUCCESS);
     assert_int_equal(lyd_validate_all(&tree, ctx, LYD_VALIDATE_PRESENT, &diff), LY_SUCCESS);
 
     /* check data tree */
     lyd_print_all(out, tree, LYD_XML, LYD_PRINT_WD_IMPL_TAG);
     assert_string_equal(str,
+        "<l xmlns=\"urn:tests:f\">value</l>"
         "<d xmlns=\"urn:tests:f\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">15</d>"
         "<ll2 xmlns=\"urn:tests:f\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">dflt1</ll2>"
         "<ll2 xmlns=\"urn:tests:f\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">dflt2</ll2>"
@@ -1089,8 +1090,7 @@
             "<d xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">15</d>"
             "<ll2 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">dflt1</ll2>"
             "<ll2 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">dflt2</ll2>"
-        "</cont>"
-        "<l xmlns=\"urn:tests:f\">value</l>");
+        "</cont>");
     ly_out_reset(out);
 
     /* check diff */
@@ -1105,14 +1105,17 @@
 
     /* create explicit leaf-list and leaf and validate */
     assert_int_equal(lyd_new_term(NULL, mod, "d", "15", &node), LY_SUCCESS);
-    assert_int_equal(lyd_insert_sibling(tree, node), LY_SUCCESS);
+    assert_int_equal(lyd_insert_sibling(tree, node, &tree), LY_SUCCESS);
     assert_int_equal(lyd_new_term(NULL, mod, "ll2", "dflt2", &node), LY_SUCCESS);
-    assert_int_equal(lyd_insert_sibling(tree, node), LY_SUCCESS);
+    assert_int_equal(lyd_insert_sibling(tree, node, &tree), LY_SUCCESS);
     assert_int_equal(lyd_validate_all(&tree, ctx, LYD_VALIDATE_PRESENT, &diff), LY_SUCCESS);
 
     /* check data tree */
     lyd_print_all(out, tree, LYD_XML, LYD_PRINT_WD_IMPL_TAG);
     assert_string_equal(str,
+        "<l xmlns=\"urn:tests:f\">value</l>"
+        "<d xmlns=\"urn:tests:f\">15</d>"
+        "<ll2 xmlns=\"urn:tests:f\">dflt2</ll2>"
         "<cont xmlns=\"urn:tests:f\">"
             "<ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def1</ll1>"
             "<ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def2</ll1>"
@@ -1120,10 +1123,7 @@
             "<d xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">15</d>"
             "<ll2 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">dflt1</ll2>"
             "<ll2 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">dflt2</ll2>"
-        "</cont>"
-        "<l xmlns=\"urn:tests:f\">value</l>"
-        "<d xmlns=\"urn:tests:f\">15</d>"
-        "<ll2 xmlns=\"urn:tests:f\">dflt2</ll2>");
+        "</cont>");
     ly_out_reset(out);
 
     /* check diff */
@@ -1138,13 +1138,15 @@
 
     /* create first explicit container, which should become implicit */
     assert_int_equal(lyd_new_inner(NULL, mod, "cont", &node), LY_SUCCESS);
-    assert_int_equal(lyd_insert_before(tree, node), LY_SUCCESS);
-    tree = tree->prev;
+    assert_int_equal(lyd_insert_sibling(tree, node, &tree), LY_SUCCESS);
     assert_int_equal(lyd_validate_all(&tree, ctx, LYD_VALIDATE_PRESENT, &diff), LY_SUCCESS);
 
     /* check data tree */
     lyd_print_all(out, tree, LYD_XML, LYD_PRINT_WD_IMPL_TAG);
     assert_string_equal(str,
+        "<l xmlns=\"urn:tests:f\">value</l>"
+        "<d xmlns=\"urn:tests:f\">15</d>"
+        "<ll2 xmlns=\"urn:tests:f\">dflt2</ll2>"
         "<cont xmlns=\"urn:tests:f\">"
             "<ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def1</ll1>"
             "<ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def2</ll1>"
@@ -1152,10 +1154,7 @@
             "<d xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">15</d>"
             "<ll2 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">dflt1</ll2>"
             "<ll2 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">dflt2</ll2>"
-        "</cont>"
-        "<l xmlns=\"urn:tests:f\">value</l>"
-        "<d xmlns=\"urn:tests:f\">15</d>"
-        "<ll2 xmlns=\"urn:tests:f\">dflt2</ll2>");
+        "</cont>");
     ly_out_reset(out);
 
     /* check diff */
@@ -1163,12 +1162,15 @@
 
     /* create second explicit container, which should become implicit, so the first tree node should be removed */
     assert_int_equal(lyd_new_inner(NULL, mod, "cont", &node), LY_SUCCESS);
-    assert_int_equal(lyd_insert_after(tree, node), LY_SUCCESS);
+    assert_int_equal(lyd_insert_sibling(tree, node, &tree), LY_SUCCESS);
     assert_int_equal(lyd_validate_all(&tree, ctx, LYD_VALIDATE_PRESENT, &diff), LY_SUCCESS);
 
     /* check data tree */
     lyd_print_all(out, tree, LYD_XML, LYD_PRINT_WD_IMPL_TAG);
     assert_string_equal(str,
+        "<l xmlns=\"urn:tests:f\">value</l>"
+        "<d xmlns=\"urn:tests:f\">15</d>"
+        "<ll2 xmlns=\"urn:tests:f\">dflt2</ll2>"
         "<cont xmlns=\"urn:tests:f\">"
             "<ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def1</ll1>"
             "<ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def2</ll1>"
@@ -1176,32 +1178,29 @@
             "<d xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">15</d>"
             "<ll2 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">dflt1</ll2>"
             "<ll2 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">dflt2</ll2>"
-        "</cont>"
-        "<l xmlns=\"urn:tests:f\">value</l>"
-        "<d xmlns=\"urn:tests:f\">15</d>"
-        "<ll2 xmlns=\"urn:tests:f\">dflt2</ll2>");
+        "</cont>");
     ly_out_reset(out);
 
     /* check diff */
     assert_null(diff);
 
     /* similar changes for nested defaults */
-    assert_int_equal(lyd_new_term(tree, NULL, "ll1", "def3", NULL), LY_SUCCESS);
-    assert_int_equal(lyd_new_term(tree, NULL, "d", "5", NULL), LY_SUCCESS);
-    assert_int_equal(lyd_new_term(tree, NULL, "ll2", "non-dflt", NULL), LY_SUCCESS);
+    assert_int_equal(lyd_new_term(tree->prev, NULL, "ll1", "def3", NULL), LY_SUCCESS);
+    assert_int_equal(lyd_new_term(tree->prev, NULL, "d", "5", NULL), LY_SUCCESS);
+    assert_int_equal(lyd_new_term(tree->prev, NULL, "ll2", "non-dflt", NULL), LY_SUCCESS);
     assert_int_equal(lyd_validate_all(&tree, ctx, LYD_VALIDATE_PRESENT, &diff), LY_SUCCESS);
 
     /* check data tree */
     lyd_print_all(out, tree, LYD_XML, LYD_PRINT_WD_IMPL_TAG);
     assert_string_equal(str,
+        "<l xmlns=\"urn:tests:f\">value</l>"
+        "<d xmlns=\"urn:tests:f\">15</d>"
+        "<ll2 xmlns=\"urn:tests:f\">dflt2</ll2>"
         "<cont xmlns=\"urn:tests:f\">"
             "<ll1>def3</ll1>"
             "<d>5</d>"
             "<ll2>non-dflt</ll2>"
-        "</cont>"
-        "<l xmlns=\"urn:tests:f\">value</l>"
-        "<d xmlns=\"urn:tests:f\">15</d>"
-        "<ll2 xmlns=\"urn:tests:f\">dflt2</ll2>");
+        "</cont>");
     ly_out_reset(out);
 
     /* check diff */