Add new functions and tests: str_list_make_empty(), str_list_make_single()
authorAndrew Bartlett <abartlet@samba.org>
Wed, 13 May 2009 06:49:34 +0000 (16:49 +1000)
committerAndrew Bartlett <abartlet@samba.org>
Wed, 13 May 2009 19:56:58 +0000 (05:56 +1000)
lib/util/tests/strlist.c
lib/util/util.h
lib/util/util_strlist.c

index 86051029548e18eb498fd2c9b54723545d59a9a7..3f6cf2714bb83ca12239e5fd6a5c3705db9d0f28 100644 (file)
@@ -87,6 +87,38 @@ static bool test_list_copy(struct torture_context *tctx)
        return true;
 }
 
+static bool test_list_make_empty(struct torture_context *tctx)
+{
+       char **result;
+
+       result = str_list_make_empty(tctx);
+       torture_assert(tctx, result, "str_list_make_empty() must not return NULL");
+       torture_assert(tctx, result[0] == NULL, "first element in str_list_make_empty() result must be NULL");
+
+       result = str_list_make(tctx, NULL, NULL);
+       torture_assert(tctx, result, "str_list_make() must not return NULL");
+       torture_assert(tctx, result[0] == NULL, "first element in str_list_make(ctx, NULL, NULL) result must be NULL");
+       
+       result = str_list_make(tctx, "", NULL);
+       torture_assert(tctx, result, "str_list_make() must not return NULL");
+       torture_assert(tctx, result[0] == NULL, "first element in str_list_make(ctx, "", NULL) result must be NULL");
+       
+       return true;
+}
+
+static bool test_list_make_single(struct torture_context *tctx)
+{
+       char **result;
+
+       result = str_list_make_single(tctx, "foo");
+
+       torture_assert(tctx, result, "str_list_make_single() must not return NULL");
+       torture_assert_str_equal(tctx, result[0], "foo", "element 0");
+       torture_assert(tctx, result[1] == NULL, "second element in result must be NULL");
+       
+       return true;
+}
+
 struct torture_suite *torture_local_util_strlist(TALLOC_CTX *mem_ctx)
 {
        struct torture_suite *suite = torture_suite_create(mem_ctx, "STRLIST");
@@ -98,6 +130,8 @@ struct torture_suite *torture_local_util_strlist(TALLOC_CTX *mem_ctx)
        }
 
        torture_suite_add_simple_test(suite, "list_copy", test_list_copy);
+       torture_suite_add_simple_test(suite, "make_empty", test_list_make_empty);
+       torture_suite_add_simple_test(suite, "make_single", test_list_make_single);
 
        return suite;
 }
index f4c2b833afc858152d6233a1510cf44b380572b6..dab5ff93609ae8a8c3a970452d42b4719c141d24 100644 (file)
@@ -396,6 +396,16 @@ _PUBLIC_ bool strequal(const char *s1, const char *s2);
 #define LIST_SEP " \t,\n\r"
 #endif
 
+/**
+  build an empty (only NULL terminated) list of strings (for expansion with str_list_add() etc)
+*/
+_PUBLIC_ char **str_list_make_empty(TALLOC_CTX *mem_ctx);
+
+/**
+  place the only element 'entry' into a new, NULL terminated string list
+*/
+_PUBLIC_ char **str_list_make_single(TALLOC_CTX *mem_ctx, const char *entry);
+
 /**
   build a null terminated list of strings from a input string and a
   separator list. The separator list must contain characters less than
index 6936f189aada2a9e51085ca57de0c925fdb36845..2fcbe186be081ec3b2b940b73673a72fcf828d88 100644 (file)
  * @brief String list manipulation
  */
 
+/**
+  build an empty (only NULL terminated) list of strings (for expansion with str_list_add() etc)
+*/
+_PUBLIC_ char **str_list_make_empty(TALLOC_CTX *mem_ctx)
+{
+       int num_elements = 0;
+       char **ret = NULL;
+
+       ret = talloc_array(mem_ctx, char *, 1);
+       if (ret == NULL) {
+               return NULL;
+       }
+
+       ret[0] = NULL;
+
+       return ret;
+}
+
+/**
+  place the only element 'entry' into a new, NULL terminated string list
+*/
+_PUBLIC_ char **str_list_make_single(TALLOC_CTX *mem_ctx, const char *entry)
+{
+       int num_elements = 0;
+       char **ret = NULL;
+
+       ret = talloc_array(mem_ctx, char *, 2);
+       if (ret == NULL) {
+               return NULL;
+       }
+
+       ret[0] = talloc_strdup(ret, entry);
+       if (!ret[0]) {
+               talloc_free(ret);
+               return NULL;
+       }
+       ret[1] = NULL;
+
+       return ret;
+}
+
 /**
   build a null terminated list of strings from a input string and a
   separator list. The separator list must contain characters less than