schema tree CHANGE initial schema compilation code
diff --git a/tests/src/CMakeLists.txt b/tests/src/CMakeLists.txt
index bce0d9a..b369789 100644
--- a/tests/src/CMakeLists.txt
+++ b/tests/src/CMakeLists.txt
@@ -4,13 +4,15 @@
     src_context
     src_hash_table
     src_xml
-    src_parser_yang)
+    src_parser_yang
+    src_tree_schema_compile)
 set(local_tests_wraps
     " "
     "-Wl,--wrap=realloc"
     "-Wl,--wrap=ly_set_add"
     " "
     " "
+    " "
     " ")
 set(tests ${tests} ${local_tests} PARENT_SCOPE)
 set(tests_wraps ${tests_wraps} ${local_tests_wraps} PARENT_SCOPE)
diff --git a/tests/src/test_parser_yang.c b/tests/src/test_parser_yang.c
index 1020b0a..e9d814a 100644
--- a/tests/src/test_parser_yang.c
+++ b/tests/src/test_parser_yang.c
@@ -865,6 +865,7 @@
 test_identity(void **state)
 {
     (void) state; /* unused */
+    int dict = 1; /* magic variable for FREE macros */
 
     struct ly_parser_ctx ctx;
     struct lysp_ident *ident = NULL;
@@ -909,6 +910,7 @@
 test_feature(void **state)
 {
     (void) state; /* unused */
+    int dict = 1; /* magic variable for FREE macros */
 
     struct ly_parser_ctx ctx;
     struct lysp_feature *features = NULL;
@@ -953,6 +955,7 @@
 test_deviation(void **state)
 {
     (void) state; /* unused */
+    int dict = 1; /* magic variable for FREE macros */
 
     struct ly_parser_ctx ctx;
     struct lysp_deviation *d = NULL;
@@ -1016,7 +1019,7 @@
     /* invalid cardinality */
 #define TEST_DUP(TYPE, MEMBER, VALUE1, VALUE2) \
     TEST_DUP_GENERIC(TYPE" {", MEMBER, VALUE1, VALUE2, parse_deviate, \
-                     &d, "1", lysp_deviate_free(ctx.ctx, d); free(d); d = NULL)
+                     &d, "1", lysp_deviate_free(ctx.ctx, d, 1); free(d); d = NULL)
 
     TEST_DUP("add", "config", "true", "false");
     TEST_DUP("replace", "default", "int8", "uint8");
@@ -1031,29 +1034,29 @@
     assert_int_equal(LY_SUCCESS, parse_deviate(&ctx, &str, &d));
     assert_non_null(d);
     assert_string_equal(" ...", str);
-    lysp_deviate_free(ctx.ctx, d); free(d); d = NULL;
+    lysp_deviate_free(ctx.ctx, d, 1); free(d); d = NULL;
     str = " add {units meters; must 1; must 2; unique x; unique y; default a; default b; config true; mandatory true; min-elements 1; max-elements 2; prefix:ext;} ...";
     assert_int_equal(LY_SUCCESS, parse_deviate(&ctx, &str, &d));
     assert_non_null(d);
     assert_string_equal(" ...", str);
-    lysp_deviate_free(ctx.ctx, d); free(d); d = NULL;
+    lysp_deviate_free(ctx.ctx, d, 1); free(d); d = NULL;
     str = " delete {units meters; must 1; must 2; unique x; unique y; default a; default b; prefix:ext;} ...";
     assert_int_equal(LY_SUCCESS, parse_deviate(&ctx, &str, &d));
     assert_non_null(d);
     assert_string_equal(" ...", str);
-    lysp_deviate_free(ctx.ctx, d); free(d); d = NULL;
+    lysp_deviate_free(ctx.ctx, d, 1); free(d); d = NULL;
     str = " replace {type string; units meters; default a; config true; mandatory true; min-elements 1; max-elements 2; prefix:ext;} ...";
     assert_int_equal(LY_SUCCESS, parse_deviate(&ctx, &str, &d));
     assert_non_null(d);
     assert_string_equal(" ...", str);
-    lysp_deviate_free(ctx.ctx, d); free(d); d = NULL;
+    lysp_deviate_free(ctx.ctx, d, 1); free(d); d = NULL;
 
     /* invalid substatements */
 #define TEST_NOT_SUP(DEV, STMT, VALUE) \
     str = " "DEV" {"STMT" "VALUE";}..."; \
     assert_int_equal(LY_EVALID, parse_deviate(&ctx, &str, &d)); \
     logbuf_assert("Deviate \""DEV"\" does not support keyword \""STMT"\". Line number 1."); \
-    lysp_deviate_free(ctx.ctx, d); free(d); d = NULL
+    lysp_deviate_free(ctx.ctx, d, 1); free(d); d = NULL
 
     TEST_NOT_SUP("not-supported", "units", "meters");
     TEST_NOT_SUP("not-supported", "must", "1");
diff --git a/tests/src/test_tree_schema_compile.c b/tests/src/test_tree_schema_compile.c
new file mode 100644
index 0000000..291e515
--- /dev/null
+++ b/tests/src/test_tree_schema_compile.c
@@ -0,0 +1,113 @@
+/*
+ * @file test_parser_yang.c
+ * @author: Radek Krejci <rkrejci@cesnet.cz>
+ * @brief unit tests for functions from parser_yang.c
+ *
+ * Copyright (c) 2018 CESNET, z.s.p.o.
+ *
+ * This source code is licensed under BSD 3-Clause License (the "License").
+ * You may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     https://opensource.org/licenses/BSD-3-Clause
+ */
+
+#define _BSD_SOURCE
+#define _DEFAULT_SOURCE
+#include <stdarg.h>
+#include <stddef.h>
+#include <setjmp.h>
+#include <cmocka.h>
+
+#include <stdio.h>
+#include <string.h>
+
+#include "libyang.h"
+#include "../../src/parser_yang.c"
+#include "../../src/tree_schema.c"
+
+#define BUFSIZE 1024
+char logbuf[BUFSIZE] = {0};
+
+/* set to 0 to printing error messages to stderr instead of checking them in code */
+#define ENABLE_LOGGER_CHECKING 1
+
+#if ENABLE_LOGGER_CHECKING
+static void
+logger(LY_LOG_LEVEL level, const char *msg, const char *path)
+{
+    (void) level; /* unused */
+
+    if (path) {
+        snprintf(logbuf, BUFSIZE - 1, "%s %s", msg, path);
+    } else {
+        strncpy(logbuf, msg, BUFSIZE - 1);
+    }
+}
+#endif
+
+static int
+logger_setup(void **state)
+{
+    (void) state; /* unused */
+#if ENABLE_LOGGER_CHECKING
+    ly_set_log_clb(logger, 1);
+#endif
+    return 0;
+}
+
+void
+logbuf_clean(void)
+{
+    logbuf[0] = '\0';
+}
+
+#if ENABLE_LOGGER_CHECKING
+#   define logbuf_assert(str) assert_string_equal(logbuf, str)
+#else
+#   define logbuf_assert(str)
+#endif
+
+static void
+test_module(void **state)
+{
+    (void) state; /* unused */
+
+    const char *str;
+    struct ly_ctx *ctx;
+    struct lys_module mod = {0};
+
+    str = "module test {namespace urn:test; prefix t;"
+          "feature f1;}";
+    assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx));
+
+    assert_int_equal(LY_EINVAL, lys_compile(NULL, 0, NULL));
+    logbuf_assert("Invalid argument sc (lys_compile()).");
+    assert_int_equal(LY_EINVAL, lys_compile(NULL, 0, &mod.compiled));
+    logbuf_assert("Invalid argument sp (lys_compile()).");
+    assert_int_equal(LY_SUCCESS, yang_parse(ctx, str, &mod.parsed));
+    assert_int_equal(LY_SUCCESS, lys_compile(mod.parsed, 0, &mod.compiled));
+    assert_non_null(mod.compiled);
+    assert_ptr_equal(mod.parsed->name, mod.compiled->name);
+    assert_ptr_equal(mod.parsed->ns, mod.compiled->ns);
+
+    lysc_module_free(mod.compiled);
+
+    assert_int_equal(LY_SUCCESS, lys_compile(mod.parsed, LYSC_OPT_FREE_SP, &mod.compiled));
+    assert_non_null(mod.compiled);
+    assert_string_equal("test", mod.compiled->name);
+    assert_string_equal("urn:test", mod.compiled->ns);
+
+    lysc_module_free(mod.compiled);
+    ly_ctx_destroy(ctx, NULL);
+}
+
+
+int main(void)
+{
+    const struct CMUnitTest tests[] = {
+        cmocka_unit_test_setup(test_module, logger_setup),
+    };
+
+    return cmocka_run_group_tests(tests, NULL, NULL);
+}