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@xxxxxxxxx> --- 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 -- To unsubscribe from this list: send the line "unsubscribe git" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html