Skip to content

Commit

Permalink
odict: add odict_pl_add() (#1208)
Browse files Browse the repository at this point in the history
  • Loading branch information
cspiel1 authored Nov 6, 2024
1 parent 7ef4199 commit ce310c9
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 0 deletions.
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

0 comments on commit ce310c9

Please sign in to comment.