Exemplo n.º 1
0
static void
test_similar_strings(void **state) {
    (void) state; /* unused */

    const char *ret = NULL;

    ret = lydict_insert(ctx, "aaab", 4);
    if (!ret) {
        fail();
    }
    assert_string_equal(ret, "aaab");

    ret = lydict_insert(ctx, "aaa", 3);
    if (!ret) {
        fail();
    }
    assert_string_equal(ret, "aaa");

    ret = lydict_insert(ctx, "bbb", 3);
    if (!ret) {
        fail();
    }
    assert_string_equal(ret, "bbb");

    ret = lydict_insert(ctx, "bbba", 4);
    if (!ret) {
        fail();
    }
    assert_string_equal(ret, "bbba");

    lydict_remove(ctx, "aaa");
    lydict_remove(ctx, "aaab");
    lydict_remove(ctx, "bbb");
    lydict_remove(ctx, "bbba");
}
Exemplo n.º 2
0
static void
test_lydict_insert(void **state)
{
    (void) state; /* unused */
    const char *value = "x";
    const char *string;
    size_t len = 1;

    string = lydict_insert(ctx, value, len);
    if (!string) {
        fail();
    }

    assert_string_equal(value, string);
    value = "bubba";
    len = 5;

    string = lydict_insert(ctx, value, len);
    if (!string) {
        fail();
    }

    assert_string_equal(value, string);
    lydict_remove(ctx, "bubba");
    lydict_remove(ctx, "x");
}
Exemplo n.º 3
0
static void
test_lydict_remove(void **state)
{
    (void) state; /* unused */
    char *value = NULL, *value2;
    const char *str;

    value = strdup("new_name");
    if (!value) {
        fail();
    }
    value2 = strdup("new_name");
    if (!value2) {
        fail();
    }

    const char *string;
    string = lydict_insert_zc(ctx, value); /* 1st instance */
    if (!string) {
        free(value);
        fail();
    }

    assert_string_equal("new_name", string);
    str = lydict_insert(ctx, "new_name", 0); /* 2nd instance */
    assert_ptr_equal(str, string);
    lydict_remove(ctx, string); /* remove 2nd instance */
    lydict_remove(ctx, string); /* remove 1st instance */
    /* string content is supposed to be invalid since now! */
    str = lydict_insert_zc(ctx, value2);
    assert_ptr_not_equal(str, NULL);
    assert_ptr_not_equal(str, string);
    lydict_remove(ctx, str);
}
Exemplo n.º 4
0
API struct lyd_node *
lyd_new_leaf(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *val_str)
{
    struct lyd_node_leaf_list *ret;
    const struct lys_node *snode = NULL, *siblings;
    struct lys_type *stype, *type;
    int found;

    if ((!parent && !module) || !name) {
        ly_errno = LY_EINVAL;
        return NULL;
    }

    if (!parent) {
        siblings = module->data;
    } else {
        if (!parent->schema) {
            ly_errno = LY_EINVAL;
            return NULL;
        }
        siblings = parent->schema->child;
    }

    if (lys_get_data_sibling(module, siblings, name, LYS_LEAFLIST | LYS_LEAF, &snode) || !snode) {
        ly_errno = LY_EINVAL;
        return NULL;
    }

    /* create the new leaf */
    ret = calloc(1, sizeof *ret);
    if (!ret) {
        LOGMEM;
        return NULL;
    }
    ret->schema = (struct lys_node *)snode;
    ret->prev = (struct lyd_node *)ret;
    ret->value_str = lydict_insert((module ? module->ctx : parent->schema->module->ctx), val_str, 0);

    /* resolve the type correctly */
    stype = &((struct lys_node_leaf *)snode)->type;
    if (stype->base == LY_TYPE_UNION) {
        found = 0;
        type = NULL;
        while ((type = lyp_get_next_union_type(stype, type, &found))) {
            ret->value_type = type->base;
            if (!lyp_parse_value(ret, type, 1, NULL, UINT_MAX)) {
                /* success! */
                break;
            }
            found = 0;
        }

        if (!type) {
            /* fail */
            ly_errno = LY_EINVAL;
            lyd_free((struct lyd_node *)ret);
            return NULL;
        }
    } else {
        ret->value_type = stype->base;
        if (lyp_parse_value(ret, stype, 1, NULL, 0)) {
            lyd_free((struct lyd_node *)ret);
            ly_errno = LY_EINVAL;
            return NULL;
        }
    }

    /* connect to parent */
    if (parent) {
        if (lyd_insert(parent, (struct lyd_node *)ret)) {
            lyd_free((struct lyd_node *)ret);
            return NULL;
        }
    }

    return (struct lyd_node *)ret;

}