git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Thiago Farina <tfransosi@gmail.com>
To: git@vger.kernel.org
Subject: [PATCH v3] Add test-string-list.c
Date: Tue,  7 Sep 2010 09:00:37 -0300	[thread overview]
Message-ID: <1283860837-7375-1-git-send-email-tfransosi@gmail.com> (raw)

Add a simple test that demonstrates how to create and manipulate
a list of strings using the string-list.h API.

To see the test, call it by:
./bin-wrappers/test-string-list

Or to run it on the git test suite:
cd t && sh t0070-fundamental.sh

Signed-off-by: Thiago Farina <tfransosi@gmail.com>
---
 Makefile               |    1 +
 t/t0070-fundamental.sh |    3 +
 test-string-list.c     |  110 ++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 114 insertions(+), 0 deletions(-)
 create mode 100644 test-string-list.c

diff --git a/Makefile b/Makefile
index 40fbcae..287bc2c 100644
--- a/Makefile
+++ b/Makefile
@@ -422,6 +422,7 @@ TEST_PROGRAMS_NEED_X += test-string-pool
 TEST_PROGRAMS_NEED_X += test-svn-fe
 TEST_PROGRAMS_NEED_X += test-treap
 TEST_PROGRAMS_NEED_X += test-index-version
+TEST_PROGRAMS_NEED_X += test-string-list
 
 TEST_PROGRAMS = $(patsubst %,%$X,$(TEST_PROGRAMS_NEED_X))
 
diff --git a/t/t0070-fundamental.sh b/t/t0070-fundamental.sh
index 680d7d6..c163a40 100755
--- a/t/t0070-fundamental.sh
+++ b/t/t0070-fundamental.sh
@@ -11,5 +11,8 @@ Verify wrappers and compatibility functions.
 test_expect_success 'character classes (isspace, isalpha etc.)' '
 	test-ctype
 '
+test_expect_success 'string list manipulation' '
+	test-string-list
+'
 
 test_done
diff --git a/test-string-list.c b/test-string-list.c
new file mode 100644
index 0000000..b3a6d3f
--- /dev/null
+++ b/test-string-list.c
@@ -0,0 +1,110 @@
+#include "git-compat-util.h"
+#include "string-list.h"
+
+#define TEST(name) \
+	static void test_##name(void)
+
+TEST(print_string_list)
+{
+	struct string_list list = STRING_LIST_INIT_NODUP;
+	string_list_append(&list, "foo");
+	string_list_append(&list, "bar");
+
+	print_string_list(&list, "");
+
+	string_list_clear(&list, 0);
+	assert(list.nr == 0);
+}
+
+TEST(string_list_append)
+{
+	struct string_list list = STRING_LIST_INIT_NODUP;
+	int i;
+	assert(list.nr == 0);
+
+	for (i = 0; i < 10; ++i)
+		string_list_append(&list, "foo" + i);
+
+	assert(list.nr == 10);
+
+	string_list_clear(&list, 0);
+	assert(list.nr == 0);
+}
+
+TEST(string_list_has_string)
+{
+	struct string_list list = STRING_LIST_INIT_NODUP;
+	assert(!string_list_has_string(&list, "foo"));
+	string_list_append(&list, "foo");
+	assert(string_list_has_string(&list, "foo"));
+	assert(!string_list_has_string(&list, "blah"));
+
+	string_list_clear(&list, 0);
+	assert(list.nr == 0);
+}
+
+TEST(unsorted_string_list_lookup)
+{
+	struct string_list list = STRING_LIST_INIT_NODUP;
+	assert(!unsorted_string_list_lookup(&list, "bazr"));
+
+	string_list_append(&list, "blah");
+	string_list_append(&list, "monalisa");
+	string_list_append(&list, "bzr");
+	string_list_append(&list, "barz");
+	string_list_append(&list, "john");
+
+	assert(unsorted_string_list_lookup(&list, "bzr"));
+
+	string_list_clear(&list, 0);
+	assert(list.nr == 0);
+}
+
+TEST(string_list_lookup)
+{
+	struct string_list list = STRING_LIST_INIT_NODUP;
+	int i;
+	const char *letters[] = { "a", "b", "c", "d", "e", "f" };
+	for (i = 0; i < ARRAY_SIZE(letters); ++i)
+		string_list_append(&list, letters[i]);
+
+	assert(!string_list_lookup(&list, "A"));
+	assert(string_list_lookup(&list, "a"));
+	assert(!string_list_lookup(&list, "F"));
+	assert(string_list_lookup(&list, "f"));
+
+	string_list_clear(&list, 0);
+	assert(list.nr == 0);
+}
+
+TEST(string_list_insert)
+{
+	struct string_list list = STRING_LIST_INIT_NODUP;
+	int i;
+	const char *letters[] = { "a", "b", "c", "e", "f" };
+	int index;
+	for (i = 0; i < ARRAY_SIZE(letters); ++i)
+		string_list_append(&list, letters[i]);
+
+	/* This should insert the string d between c and e.
+	 * (i.e in the index = 3.)
+	 */
+	index = string_list_find_insert_index(&list, "d", 0);
+	assert(index == 3);
+	string_list_insert(&list, "d");
+	assert(list.nr == 6);
+
+	string_list_clear(&list, 0);
+	assert(list.nr == 0);
+}
+
+int main(int argc, const char **argv)
+{
+	test_print_string_list();
+	test_string_list_append();
+	test_string_list_has_string();
+	test_unsorted_string_list_lookup();
+	test_string_list_lookup();
+	test_string_list_insert();
+	return 0;
+}
-- 
1.7.2.3.313.gcd15

                 reply	other threads:[~2010-09-07 12:00 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1283860837-7375-1-git-send-email-tfransosi@gmail.com \
    --to=tfransosi@gmail.com \
    --cc=git@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).