[PATCH] lib/util: Add tests for strv

Jeremy Allison jra at samba.org
Fri Feb 26 18:15:54 UTC 2016


On Fri, Feb 26, 2016 at 04:05:32PM +1100, Martin Schwenke wrote:
> I thought I would learn how to write tests for something in
> lib/util.  :-)
> 
> Please review and perhaps push...
> 
> peace & happiness,
> martin

Really nice job Martin - thanks ! More tests are always good :-).

Pushed.

> From f606ad87f3adc5e60102b51ba43bb1611f5ec204 Mon Sep 17 00:00:00 2001
> From: Martin Schwenke <martin at meltin.net>
> Date: Fri, 26 Feb 2016 15:23:31 +1100
> Subject: [PATCH] lib/util: Add tests for strv
> 
> These are blackbox tests against most of the API.
> 
> It would be possible to write tests that check the internals of the
> strv are as expected but that probably doesn't add much value.
> 
> Signed-off-by: Martin Schwenke <martin at meltin.net>
> ---
>  lib/util/tests/strv.c               | 169 ++++++++++++++++++++++++++++++++++++
>  source4/torture/local/local.c       |   1 +
>  source4/torture/local/wscript_build |   1 +
>  3 files changed, 171 insertions(+)
>  create mode 100644 lib/util/tests/strv.c
> 
> diff --git a/lib/util/tests/strv.c b/lib/util/tests/strv.c
> new file mode 100644
> index 0000000..4030c44
> --- /dev/null
> +++ b/lib/util/tests/strv.c
> @@ -0,0 +1,169 @@
> +/*
> + * Tests for strv
> + *
> + * Copyright Martin Schwenke <martin at meltin.net> 2016
> + *
> + * This program is free software; you can redistribute it and/or modify
> + * it under the terms of the GNU General Public License as published by
> + * the Free Software Foundation; either version 3 of the License, or
> + * (at your option) any later version.
> + *
> + * This program is distributed in the hope that it will be useful,
> + * but WITHOUT ANY WARRANTY; without even the implied warranty of
> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> + * GNU General Public License for more details.
> + *
> + * You should have received a copy of the GNU General Public License
> + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
> + */
> +
> +
> +#include <talloc.h>
> +
> +#include "replace.h"
> +
> +#include "libcli/util/ntstatus.h"
> +#include "torture/torture.h"
> +#include "lib/util/data_blob.h"
> +#include "torture/local/proto.h"
> +
> +#include "lib/util/strv.h"
> +
> +static bool test_strv_empty(struct torture_context *tctx)
> +{
> +	/* NULL strv contains 0 entries */
> +	torture_assert_int_equal(tctx,
> +				 strv_count(NULL),
> +				 0,
> +				 "strv_count() on NULL failed");
> +
> +	/* NULL strv has no next entry */
> +	torture_assert(tctx,
> +		       strv_next(NULL, NULL) == NULL,
> +		       "strv_next() on NULL failed");
> +
> +	return true;
> +}
> +
> +static bool test_strv_single(struct torture_context *tctx)
> +{
> +	const char *data = "foo";
> +	char *strv = NULL;
> +	char *t;
> +	int ret;
> +
> +	/* Add an item */
> +	ret = strv_add(tctx, &strv, data);
> +	torture_assert(tctx, ret == 0, "strv_add() failed");
> +
> +	/* Is there 1 item? */
> +	torture_assert_int_equal(tctx,
> +				 strv_count(strv), 1,
> +				 "strv_count() failed");
> +
> +	/* Is the expected item the first one? */
> +	t = strv_next(strv, NULL);
> +	torture_assert(tctx,
> +		       strcmp(t, data) == 0,
> +		       "strv_next() failed");
> +
> +	/* Can the expected item be found? */
> +	t = strv_find(strv, data);
> +	torture_assert(tctx,
> +		       strcmp(t, data) == 0,
> +		       "strv_next() failed");
> +
> +	/* Delete it */
> +	strv_delete(&strv, t);
> +
> +	/* Should have no items */
> +	torture_assert_int_equal(tctx,
> +				 strv_count(strv), 0,
> +				 "strv_count() failed");
> +	return true;
> +}
> +
> +static bool test_strv_multi(struct torture_context *tctx)
> +{
> +	const char *data[5] = { "foo", "bar", "", "samba", "x"};
> +	char *strv = NULL;
> +	char *t;
> +	int i, ret;
> +	const int num = sizeof(data) / sizeof(data[0]);
> +
> +	/* Add items */
> +	for (i = 0; i < num; i++) {
> +		ret = strv_add(tctx, &strv, data[i]);
> +		torture_assert(tctx, ret == 0, "strv_add() failed");
> +	}
> +
> +	torture_assert_int_equal(tctx,
> +				 strv_count(strv), num,
> +				 "strv_count() failed");
> +
> +	/* Check that strv_next() finds the expected values */
> +	t = NULL;
> +	for (i = 0; i < num; i++) {
> +		t = strv_next(strv, t);
> +		torture_assert(tctx,
> +			       strcmp(t, data[i]) == 0,
> +			       "strv_next() failed");
> +	}
> +
> +
> +	/* Check that strv_next() finds the expected values */
> +	t = NULL;
> +	for (i = 0; i < num; i++) {
> +		t = strv_next(strv, t);
> +		torture_assert(tctx,
> +			       strcmp(t, data[i]) == 0,
> +			       "strv_next() failed");
> +	}
> +
> +	/* Find each item, delete it, check count */
> +	for (i = 0; i < num; i++) {
> +		t = strv_find(strv, data[i]);
> +		torture_assert(tctx,
> +			       strcmp(t, data[i]) == 0,
> +			       "strv_next() failed");
> +		strv_delete(&strv, t);
> +		torture_assert_int_equal(tctx,
> +					 strv_count(strv), num - i - 1,
> +					 "strv_delete() failed");
> +	}
> +
> +	/* Add items */
> +	for (i = 0; i < num; i++) {
> +		ret = strv_add(tctx, &strv, data[i]);
> +		torture_assert(tctx, ret == 0, "strv_add() failed");
> +	}
> +
> +	torture_assert_int_equal(tctx,
> +				 strv_count(strv), num,
> +				 "strv_count() failed");
> +
> +	/* Find items in reverse, delete, check count */
> +	for (i = num - 1; i >= 0; i--) {
> +		t = strv_find(strv, data[i]);
> +		torture_assert(tctx,
> +			       strcmp(t, data[i]) == 0,
> +			       "strv_next() failed");
> +		strv_delete(&strv, t);
> +		torture_assert_int_equal(tctx,
> +					 strv_count(strv), i,
> +					 "strv_delete() failed");
> +	}
> +
> +	return true;
> +}
> +
> +struct torture_suite *torture_local_util_strv(TALLOC_CTX *mem_ctx)
> +{
> +	struct torture_suite *suite = torture_suite_create(mem_ctx, "strv");
> +
> +	torture_suite_add_simple_test(suite, "strv_empty",  test_strv_empty);
> +	torture_suite_add_simple_test(suite, "strv_single", test_strv_single);
> +	torture_suite_add_simple_test(suite, "strv_multi",  test_strv_multi);
> +
> +	return suite;
> +}
> diff --git a/source4/torture/local/local.c b/source4/torture/local/local.c
> index 3988988..1f68aac 100644
> --- a/source4/torture/local/local.c
> +++ b/source4/torture/local/local.c
> @@ -41,6 +41,7 @@
>  	torture_local_util_data_blob, 
>  	torture_local_util_asn1,
>  	torture_local_util_anonymous_shared,
> +	torture_local_util_strv,
>  	torture_local_idtree, 
>  	torture_local_dlinklist,
>  	torture_local_genrand, 
> diff --git a/source4/torture/local/wscript_build b/source4/torture/local/wscript_build
> index eb45df8..ef1fb30 100644
> --- a/source4/torture/local/wscript_build
> +++ b/source4/torture/local/wscript_build
> @@ -17,6 +17,7 @@ TORTURE_LOCAL_SOURCE = '''../../../lib/util/charset/tests/iconv.c
>  	dbspeed.c torture.c ../ldb/ldb.c ../../dsdb/common/tests/dsdb_dn.c
>  	../../dsdb/schema/tests/schema_syntax.c
>  	../../../lib/util/tests/anonymous_shared.c
> +	../../../lib/util/tests/strv.c
>  	verif_trailer.c
>  	nss_tests.c
>  	fsrvp_state.c'''
> -- 
> 2.7.0
> 




More information about the samba-technical mailing list