Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

odict: add odict_pl_add() #1208

Merged
merged 1 commit into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions include/re_odict.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ int odict_debug(struct re_printf *pf, const struct odict *o);

int odict_entry_add(struct odict *o, const char *key,
int type, ...);
int odict_pl_add(struct odict *od, const char *key,
const struct pl *val);
void odict_entry_del(struct odict *o, const char *key);
int odict_entry_debug(struct re_printf *pf, const struct odict_entry *e);
bool odict_compare(const struct odict *dict1, const struct odict *dict2,
Expand Down
14 changes: 14 additions & 0 deletions src/odict/entry.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,20 @@ int odict_entry_add(struct odict *o, const char *key,
}


int odict_pl_add(struct odict *od, const char *key,
const struct pl *val)
{
char *str;
int err = pl_strdup(&str, val);
if (err)
return err;

err = odict_entry_add(od, key, ODICT_STRING, str);
mem_deref(str);
return err;
}


void odict_entry_del(struct odict *o, const char *key)
{
mem_deref((struct odict_entry *)odict_lookup(o, key));
Expand Down
33 changes: 33 additions & 0 deletions test/odict.c
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,39 @@ int test_odict(void)
}


int test_odict_pl(void)
{
struct odict *od = NULL;
const struct odict_entry *e;
int err;

static struct pl pl=PL("liajsdoiausdoaudoaisudaoisdjal");

err = odict_alloc(&od, 64);
if (err)
goto out;

/* add pl */
err = odict_pl_add(od, "pl1", &pl);
TEST_ERR(err);

e = odict_lookup(od, "pl1");
TEST_ASSERT(e != NULL);

TEST_STRCMP(pl.p, pl.l,
odict_entry_str(e), str_len(odict_entry_str(e)));

mem_deref((struct odict_entry *) e);
/* entry should not exist anymore */
e = (struct odict_entry *)odict_lookup(od, "pl1");
TEST_ASSERT(e == NULL);

out:
mem_deref(od);
return err;
}


int test_odict_array(void)
{
struct odict *arr = NULL;
Expand Down
1 change: 1 addition & 0 deletions test/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ static const struct test tests[] = {
TEST(test_mqueue),
TEST(test_odict),
TEST(test_odict_array),
TEST(test_odict_pl),
TEST(test_pcp),
TEST(test_remain),
TEST(test_re_assert_se),
Expand Down
1 change: 1 addition & 0 deletions test/test.h
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ int test_net_if(void);
int test_net_dst_source_addr_get(void);
int test_odict(void);
int test_odict_array(void);
int test_odict_pl(void);
int test_pcp(void);
int test_trice_cand(void);
int test_trice_candpair(void);
Expand Down
Loading