From 8b7f91cd0f9d03cea9fb5bd088b5f93f5e2d770d Mon Sep 17 00:00:00 2001 From: henri Date: Wed, 22 May 2013 14:35:24 +0000 Subject: [PATCH] Moved gh_lists tests from nsock/src/ to nsock/tests/ --- nsock/src/gh_list.c | 119 ----------------------------- nsock/tests/Makefile | 3 +- nsock/tests/ghlists.c | 156 +++++++++++++++++++++++++++++++++++++++ nsock/tests/tests_main.c | 3 + 4 files changed, 161 insertions(+), 120 deletions(-) create mode 100644 nsock/tests/ghlists.c diff --git a/nsock/src/gh_list.c b/nsock/src/gh_list.c index 018bd5ca9..2bc917457 100644 --- a/nsock/src/gh_list.c +++ b/nsock/src/gh_list.c @@ -83,125 +83,6 @@ } while (0) - -#ifdef GH_LIST_MAIN -int main(int argc, char *argv[]) { - gh_list lists[16]; - gh_list_elem *current, *next; - int num = 0; - int ret; - int i; - - for(i=0; i < 16; i++) - gh_list_init(&lists[i]); - - for(num=25000; num < 50000; num++) { - for(i=0; i < 16; i++) { - gh_list_append(&lists[i], (void *)num); - } - } - - for(num=24999; num >= 0; num--) { - for(i=0; i < 16; i++) { - gh_list_prepend(&lists[i], (void *)num); - } - } - - for(num=0; num < 50000; num++) { - for(i=0; i < 16; i++) { - ret = (int) gh_list_pop(&lists[i]); - if (ret != num) - fatal("prepend_test: Bogus return value %d when expected %d\n", ret, num); - } - } - for(i=0; i < 16; i++) { - ret = (int) gh_list_pop(&lists[i]); - if (ret != 0) - fatal("Ret is bogus for list %d", i); - } - - printf("Done with first set\n"); - - for(num=24999; num >= 0; num--) { - for(i=0; i < 16; i++) { - gh_list_prepend(&lists[i], (void *)num); - } - } - - for(num=25000; num < 50000; num++) { - for(i=0; i < 16; i++) { - gh_list_append(&lists[i], (void *)num); - } - } - - for(num=0; num < 50000; num++) { - for(i=0; i < 16; i++) { - ret = (int) gh_list_pop(&lists[i]); - if (ret != num) - fatal("prepend_test: Bogus return value %d when expected %d\n", ret, num); - } - } - - printf("Done with second set\n"); - for(num=25000; num < 50000; num++) { - for(i=0; i < 16; i++) { - gh_list_append(&lists[i], (void *)num); - } - } - - for(num=24999; num >= 0; num--) { - for(i=0; i < 16; i++) { - gh_list_prepend(&lists[i], (void *)num); - } - } - - for(num=0; num < 50000; num++) { - for(i=0; i < 16; i++) { - ret = (int) gh_list_pop(&lists[i]); - if (ret != num) - fatal("prepend_test: Bogus return value %d when expected %d\n", ret, num); - } - } - - printf("Done with third set ...\n"); - - for(num=24999; num >= 0; num--) { - for(i=0; i < 16; i++) { - gh_list_prepend(&lists[i], (void *)num); - } - } - - for(num=25000; num < 50000; num++) { - for(i=0; i < 16; i++) { - gh_list_append(&lists[i], (void *)num); - } - } - - for(i=0; i < 16; i++) { - num=0; - for(current = GH_LIST_FIRST_ELEM(&lists[i]); current; - current = next) { - next = GH_LIST_ELEM_NEXT(current); - if ((int)GH_LIST_ELEM_DATA(current) != num) - fatal("Got %d when I expected %d\n", (int)GH_LIST_ELEM_DATA(current), num); - gh_list_remove_elem(&lists[i], current); - num++; - } - if (num != 50000) - fatal("Number is %d, even though %d was expected", num, 50000); - - if (GH_LIST_COUNT(&lists[i]) != 0) { - fatal("List should be empty, but instead it has %d members!\n", GH_LIST_COUNT(&lists[i])); - } - } - - printf("Done with fourth set, freeing buffers\n"); - for(i=0; i < 16; i++) { - gh_list_free(&lists[i]); - } -} -#endif /* GH_LIST_MAIN */ - static inline struct gh_list_elem *get_free_buffer(struct gh_list *list) { struct gh_list_elem *newelem; int i; diff --git a/nsock/tests/Makefile b/nsock/tests/Makefile index c86c330e7..8d0a29765 100644 --- a/nsock/tests/Makefile +++ b/nsock/tests/Makefile @@ -15,7 +15,8 @@ SRC = tests_main.c \ basic.c \ timer.c \ logs.c \ - connect.c + connect.c \ + ghlists.c OBJ = $(SRC:.c=.o) diff --git a/nsock/tests/ghlists.c b/nsock/tests/ghlists.c new file mode 100644 index 000000000..5384d112f --- /dev/null +++ b/nsock/tests/ghlists.c @@ -0,0 +1,156 @@ +/* + * Nsock regression test suite + * Same license as nmap -- see http://nmap.org/book/man-legal.html + */ + +#include "test-common.h" +#include "../src/gh_list.h" +#include +#include + + +#define INT2PTR(i) ((void *)(intptr_t)(i)) +#define PTR2INT(p) ((intptr_t)(void *)(p)) + +#define LIST_COUNT 16 + + +static int ghlist_stress(void *tdata) { + gh_list lists[LIST_COUNT]; + gh_list_elem *current, *next; + int num = 0; + int ret; + int i; + + for (i=0; i < LIST_COUNT; i++) + gh_list_init(&lists[i]); + + for (num=25000; num < 50000; num++) { + for (i=0; i < LIST_COUNT; i++) { + gh_list_append(&lists[i], INT2PTR(num)); + } + } + + for (num=24999; num >= 0; num--) { + for (i=0; i < LIST_COUNT; i++) { + gh_list_prepend(&lists[i], INT2PTR(num)); + } + } + + for (num=0; num < 50000; num++) { + for (i=0; i < LIST_COUNT; i++) { + ret = PTR2INT(gh_list_pop(&lists[i])); + if (ret != num) { + fprintf(stderr, "prepend_test: Bogus return value %d when expected %d\n", + ret, num); + return -EINVAL; + } + } + } + for (i=0; i < LIST_COUNT; i++) { + ret = PTR2INT(gh_list_pop(&lists[i])); + if (ret != 0) { + fprintf(stderr, "Ret is bogus for list %d", i); + return -EINVAL; + } + } + + for (num=24999; num >= 0; num--) { + for (i=0; i < LIST_COUNT; i++) { + gh_list_prepend(&lists[i], INT2PTR(num)); + } + } + + for (num=25000; num < 50000; num++) { + for (i=0; i < LIST_COUNT; i++) { + gh_list_append(&lists[i], INT2PTR(num)); + } + } + + for (num=0; num < 50000; num++) { + for (i=0; i < LIST_COUNT; i++) { + ret = PTR2INT(gh_list_pop(&lists[i])); + if (ret != num) { + fprintf(stderr, "prepend_test: Bogus return value %d when expected %d\n", + ret, num); + return -EINVAL; + } + } + } + + for (num=25000; num < 50000; num++) { + for (i=0; i < LIST_COUNT; i++) { + gh_list_append(&lists[i], INT2PTR(num)); + } + } + + for (num=24999; num >= 0; num--) { + for (i=0; i < LIST_COUNT; i++) { + gh_list_prepend(&lists[i], INT2PTR(num)); + } + } + + for (num=0; num < 50000; num++) { + for (i=0; i < LIST_COUNT; i++) { + ret = PTR2INT(gh_list_pop(&lists[i])); + if (ret != num) { + fprintf(stderr, "prepend_test: Bogus return value %d when expected %d\n", + ret, num); + return -EINVAL; + } + } + } + + for (num=24999; num >= 0; num--) { + for (i=0; i < LIST_COUNT; i++) { + gh_list_prepend(&lists[i], INT2PTR(num)); + } + } + + for (num=25000; num < 50000; num++) { + for (i=0; i < LIST_COUNT; i++) { + gh_list_append(&lists[i], INT2PTR(num)); + } + } + + for (i=0; i < LIST_COUNT; i++) { + num=0; + for (current = GH_LIST_FIRST_ELEM(&lists[i]); current; + current = next) { + int k; + + next = GH_LIST_ELEM_NEXT(current); + k = PTR2INT(GH_LIST_ELEM_DATA(current)); + if (k != num) { + fprintf(stderr, "Got %d when I expected %d\n", k, num); + return -EINVAL; + } + gh_list_remove_elem(&lists[i], current); + num++; + } + if (num != 50000) { + fprintf(stderr, "Number is %d, even though %d was expected", num, 50000); + return -EINVAL; + } + + if (GH_LIST_COUNT(&lists[i]) != 0) { + fprintf(stderr, "List should be empty, but instead it has %d members!\n", + GH_LIST_COUNT(&lists[i])); + return -EINVAL; + } + } + + for (i=0; i < LIST_COUNT; i++) { + gh_list_free(&lists[i]); + } + + return 0; +} + +const struct test_case TestGHLists = { + .t_name = "test nsock internal ghlists", + .t_setup = NULL, + .t_run = ghlist_stress, + .t_teardown = NULL +}; + diff --git a/nsock/tests/tests_main.c b/nsock/tests/tests_main.c index 530dfc9ee..8803846af 100644 --- a/nsock/tests/tests_main.c +++ b/nsock/tests/tests_main.c @@ -20,6 +20,7 @@ extern const struct test_case TestTimer; extern const struct test_case TestLogLevels; extern const struct test_case TestErrLevels; extern const struct test_case TestConnectTCP; +extern const struct test_case TestGHLists; static const struct test_case *TestCases[] = { @@ -32,6 +33,8 @@ static const struct test_case *TestCases[] = { &TestErrLevels, /* ---- connect.c */ &TestConnectTCP, + /* ---- ghlists.c */ + &TestGHLists, NULL };