tests CHANGE improve tests of identities compilation
diff --git a/src/tree_schema.c b/src/tree_schema.c
index fe38709..2fd21d0 100644
--- a/src/tree_schema.c
+++ b/src/tree_schema.c
@@ -464,6 +464,13 @@
 }
 
 static void
+lysc_ext_instance_free(struct ly_ctx *ctx, struct lysc_ext_instance *ext, int dict)
+{
+    FREE_STRING(ctx, ext->argument, dict);
+    FREE_ARRAY(ctx, ext->exts, lysc_ext_instance_free);
+}
+
+static void
 lysc_iffeature_free(struct ly_ctx *UNUSED(ctx), struct lysc_iffeature *iff, int UNUSED(dict))
 {
     LY_ARRAY_FREE(iff->features);
@@ -471,11 +478,29 @@
 }
 
 static void
+lysc_import_free(struct ly_ctx *ctx, struct lysc_import *import, int dict)
+{
+    /* imported module is freed directly from the context's list */
+    FREE_STRING(ctx, import->prefix, dict);
+    FREE_ARRAY(ctx, import->exts, lysc_ext_instance_free);
+}
+
+static void
+lysc_ident_free(struct ly_ctx *ctx, struct lysc_ident *ident, int dict)
+{
+    FREE_STRING(ctx, ident->name, dict);
+    FREE_ARRAY(ctx, ident->iffeatures, lysc_iffeature_free);
+    LY_ARRAY_FREE(ident->derived);
+    FREE_ARRAY(ctx, ident->exts, lysc_ext_instance_free);
+}
+
+static void
 lysc_feature_free(struct ly_ctx *ctx, struct lysc_feature *feat, int dict)
 {
     FREE_STRING(ctx, feat->name, dict);
     FREE_ARRAY(ctx, feat->iffeatures, lysc_iffeature_free);
     LY_ARRAY_FREE(feat->depfeatures);
+    FREE_ARRAY(ctx, feat->exts, lysc_ext_instance_free);
 }
 
 static void
@@ -490,9 +515,11 @@
     FREE_STRING(ctx, module->ns, dict);
     FREE_STRING(ctx, module->prefix, dict);
 
-
+    FREE_ARRAY(ctx, module->imports, lysc_import_free);
     FREE_ARRAY(ctx, module->features, lysc_feature_free);
+    FREE_ARRAY(ctx, module->identities, lysc_ident_free);
 
+    FREE_ARRAY(ctx, module->exts, lysc_ext_instance_free);
 
     free(module);
 }
@@ -1121,6 +1148,9 @@
     struct lysc_ident **dident;
 
     for (i = 0; i < LY_ARRAY_SIZE(idents_p); ++i) {
+        if (!idents_p[i].bases) {
+            continue;
+        }
         for (u = 0; u < LY_ARRAY_SIZE(idents_p[i].bases); ++u) {
             s = strchr(idents_p[i].bases[u], ':');
             if (s) {
@@ -1201,7 +1231,7 @@
         return LY_EINVAL;
     }
 
-    ctx.mod = mod_c = calloc(1, sizeof *mod_c);
+    ctx.mod = *sc = mod_c = calloc(1, sizeof *mod_c);
     LY_CHECK_ERR_RET(!mod_c, LOGMEM(sp->ctx), LY_EMEM);
     mod_c->ctx = sp->ctx;
     mod_c->version = sp->version;
@@ -1231,7 +1261,6 @@
         lysp_module_free_(sp, 0);
     }
 
-    (*sc) = mod_c;
     return LY_SUCCESS;
 
 error:
diff --git a/tests/src/test_tree_schema_compile.c b/tests/src/test_tree_schema_compile.c
index 9448a46..7edecc2 100644
--- a/tests/src/test_tree_schema_compile.c
+++ b/tests/src/test_tree_schema_compile.c
@@ -203,11 +203,50 @@
     ly_ctx_destroy(ctx, NULL);
 }
 
+static void
+test_identity(void **state)
+{
+    (void) state; /* unused */
+
+    struct ly_ctx *ctx;
+    const struct lys_module *mod1, *mod2;
+    const char *mod1_str = "module a {namespace urn:a;prefix a; identity a1;}";
+    const char *mod2_str = "module b {namespace urn:b;prefix b; import a {prefix a;}identity b1; identity b2; identity b3 {base b1; base b:b2; base a:a1;} identity b4 {base b:b1; base b3;}}";
+
+    assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
+    assert_non_null(mod1 = lys_parse_mem(ctx, mod1_str, LYS_IN_YANG));
+    assert_non_null(mod2 = lys_parse_mem(ctx, mod2_str, LYS_IN_YANG));
+    assert_int_equal(LY_SUCCESS, lys_compile(mod2->parsed, 0, (struct lysc_module**)&mod2->compiled));
+
+    assert_non_null(mod1->compiled);
+    assert_non_null(mod1->compiled->identities);
+    assert_non_null(mod2->compiled);
+    assert_non_null(mod2->compiled->identities);
+
+    assert_non_null(mod1->compiled->identities[0].derived);
+    assert_int_equal(1, LY_ARRAY_SIZE(mod1->compiled->identities[0].derived));
+    assert_ptr_equal(mod1->compiled->identities[0].derived[0], &mod2->compiled->identities[2]);
+    assert_non_null(mod2->compiled->identities[0].derived);
+    assert_int_equal(2, LY_ARRAY_SIZE(mod2->compiled->identities[0].derived));
+    assert_ptr_equal(mod2->compiled->identities[0].derived[0], &mod2->compiled->identities[2]);
+    assert_ptr_equal(mod2->compiled->identities[0].derived[1], &mod2->compiled->identities[3]);
+    assert_non_null(mod2->compiled->identities[1].derived);
+    assert_int_equal(1, LY_ARRAY_SIZE(mod2->compiled->identities[1].derived));
+    assert_ptr_equal(mod2->compiled->identities[1].derived[0], &mod2->compiled->identities[2]);
+    assert_non_null(mod2->compiled->identities[2].derived);
+    assert_int_equal(1, LY_ARRAY_SIZE(mod2->compiled->identities[2].derived));
+    assert_ptr_equal(mod2->compiled->identities[2].derived[0], &mod2->compiled->identities[3]);
+
+
+    ly_ctx_destroy(ctx, NULL);
+}
+
 int main(void)
 {
     const struct CMUnitTest tests[] = {
         cmocka_unit_test_setup(test_module, logger_setup),
         cmocka_unit_test_setup(test_feature, logger_setup),
+        cmocka_unit_test_setup(test_identity, logger_setup),
     };
 
     return cmocka_run_group_tests(tests, NULL, NULL);