diff --git a/.github/workflows/trigger-gitlab-ci.yml b/.github/workflows/trigger-gitlab-ci.yml index 3c8fcbf..8cd4d11 100644 --- a/.github/workflows/trigger-gitlab-ci.yml +++ b/.github/workflows/trigger-gitlab-ci.yml @@ -1,7 +1,10 @@ name: trigger-gitlab-ci run-name: ${{ github.event_name }} triggered by ${{ github.actor }} -on: [push] +on: + push: + branches: + - master jobs: trigger-gitlab-ci: diff --git a/src/dictionary.c b/src/dictionary.c index fc42819..71ee2d6 100644 --- a/src/dictionary.c +++ b/src/dictionary.c @@ -204,6 +204,9 @@ const char * dictionary_get(const dictionary * d, const char * key, const char * unsigned hash ; size_t i ; + if(d == NULL || key == NULL) + return def ; + hash = dictionary_hash(key); for (i=0 ; isize ; i++) { if (d->key[i]==NULL) diff --git a/test/test_dictionary.c b/test/test_dictionary.c index 5f91923..725f91b 100644 --- a/test/test_dictionary.c +++ b/test/test_dictionary.c @@ -239,3 +239,52 @@ void Test_dictionary_dump(CuTest *tc) dictionary_del(dic); } + +void Test_dictionary_get(CuTest *tc) +{ + dictionary *dic; + int i, j; + char sec_name[32]; + char key_name[64]; + + /*NULL test*/ + CuAssertPtrEquals(tc, NULL, dictionary_get(NULL, NULL, NULL)); + CuAssertPtrEquals(tc, NULL, dictionary_get(NULL, "string", NULL)); + + /*Check the def return element*/ + dic = dictionary_new(DICTMINSZ); + CuAssertPtrNotNull(tc, dic); + CuAssertPtrEquals(tc, NULL, dictionary_get(dic, "dummy", NULL)); + CuAssertStrEquals(tc, "def", dictionary_get(dic, NULL, "def")); + CuAssertStrEquals(tc, "def", dictionary_get(dic, "dummy", "def")); + + /*Populate the dictionary*/ + for (i = 1; i < 3; ++i) + { + sprintf(sec_name, "sec%d", i); + dictionary_set(dic, sec_name, ""); + for (j = 1; j < 5; ++j) + { + sprintf(key_name, "%s:key%d", sec_name, j); + dictionary_set(dic, key_name, "dummy_value"); + CuAssertStrEquals(tc, "dummy_value", + dictionary_get(dic, key_name, "string")); + } + } + + /*Test get dictionary section value*/ + CuAssertStrEquals(tc, "", + dictionary_get(dic, "sec1", NULL)); + CuAssertStrEquals(tc, "", + dictionary_get(dic, "sec1", "def")); + + /*delete and set a key in a dictionary*/ + dictionary_unset(dic, "sec1:key4"); + CuAssertStrEquals(tc, "def", + dictionary_get(dic, "sec1:key4", "def")); + dictionary_set(dic, "sec1:key4", "dummy_value"); + CuAssertStrEquals(tc, "dummy_value", + dictionary_get(dic, "sec1:key4", "def")); + + dictionary_del(dic); +}