From: mquinson Date: Fri, 12 Nov 2010 15:22:18 +0000 (+0000) Subject: Test that insert/replace/remove functions of dynars are working (and fix insert when... X-Git-Tag: v3_5~292 X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/commitdiff_plain/5651e471a6df819cbeba59ba1875cbf163afc9e4 Test that insert/replace/remove functions of dynars are working (and fix insert when inserting after the end) git-svn-id: svn+ssh://scm.gforge.inria.fr/svn/simgrid/simgrid/trunk@8535 48e7efb5-ca39-0410-a469-dd3cf9ba447f --- diff --git a/src/dynar_unit.c b/src/dynar_unit.c index 2800ad965e..0a3113070f 100644 --- a/src/dynar_unit.c +++ b/src/dynar_unit.c @@ -8,7 +8,7 @@ /* GENERATED FILE, DO NOT EDIT */ /*******************************/ -#line 730 "xbt/dynar.c" +#line 754 "xbt/dynar.c" #define NB_ELEM 5000 @@ -122,12 +122,12 @@ XBT_TEST_UNIT("int", test_dynar_int, "Dynars of integers") xbt_dynar_push_as(d, int, cpt); DEBUG2("Push %d, length=%lu", cpt, xbt_dynar_length(d)); } - for (cpt = 0; cpt < 1000; cpt++) { - xbt_dynar_insert_at_as(d, 2500, int, cpt); + for (cpt = 0; cpt < NB_ELEM/5; cpt++) { + xbt_dynar_insert_at_as(d, NB_ELEM/2, int, cpt); DEBUG2("Push %d, length=%lu", cpt, xbt_dynar_length(d)); } - for (cpt = 0; cpt < 2500; cpt++) { + for (cpt = 0; cpt < NB_ELEM/2; cpt++) { xbt_dynar_shift(d, &i); xbt_test_assert2(i == cpt, "The retrieved value is not the same than the injected one at the begining (%d!=%d)", @@ -167,6 +167,71 @@ XBT_TEST_UNIT("int", test_dynar_int, "Dynars of integers") /* in your code is naturally the way to go outside a regression test */ } +/*******************************************************************************/ +/*******************************************************************************/ +/*******************************************************************************/ +XBT_TEST_UNIT("insert",test_dynar_insert,"Using the xbt_dynar_insert and xbt_dynar_remove functions") +{ + xbt_dynar_t d = xbt_dynar_new(sizeof(int), NULL); + int cursor,cpt; + + xbt_test_add1("==== Insert %d int, traverse them, remove them",NB_ELEM); + /* Populate_ints [doxygen cruft] */ + /* 1. Populate the dynar */ + for (cpt = 0; cpt < NB_ELEM; cpt++) { + xbt_dynar_insert_at(d, cpt, &cpt); + xbt_test_log2("Push %d, length=%lu", cpt, xbt_dynar_length(d)); + } + + /* 3. Traverse the dynar */ + xbt_dynar_foreach(d, cursor, cpt) { + xbt_test_assert2(cursor == cpt, + "The retrieved value is not the same than the injected one (%d!=%d)", + cursor, cpt); + } + /* end_of_traversal */ + + for (cpt = 0; cpt < NB_ELEM; cpt++) { + int val; + xbt_dynar_remove_at(d,0,&val); + xbt_test_assert2(cpt == val, + "The retrieved value is not the same than the injected one (%d!=%d)", + cursor, cpt); + } + xbt_test_assert1(xbt_dynar_length(d) == 0, + "There is still %d elements in the dynar after removing everything", + xbt_dynar_length(d)); + xbt_dynar_free(&d); + + /* ********************* */ + xbt_test_add1("==== Insert %d int in reverse order, traverse them, remove them",NB_ELEM); + d = xbt_dynar_new(sizeof(int), NULL); + for (cpt = NB_ELEM-1; cpt >=0; cpt--) { + xbt_dynar_replace(d, cpt, &cpt); + xbt_test_log2("Push %d, length=%lu", cpt, xbt_dynar_length(d)); + } + + /* 3. Traverse the dynar */ + xbt_dynar_foreach(d, cursor, cpt) { + xbt_test_assert2(cursor == cpt, + "The retrieved value is not the same than the injected one (%d!=%d)", + cursor, cpt); + } + /* end_of_traversal */ + + for (cpt =NB_ELEM-1; cpt >=0; cpt--) { + int val; + xbt_dynar_remove_at(d,xbt_dynar_length(d)-1,&val); + xbt_test_assert2(cpt == val, + "The retrieved value is not the same than the injected one (%d!=%d)", + cursor, cpt); + } + xbt_test_assert1(xbt_dynar_length(d) == 0, + "There is still %d elements in the dynar after removing everything", + xbt_dynar_length(d)); + xbt_dynar_free(&d); +} + /*******************************************************************************/ /*******************************************************************************/ /*******************************************************************************/ diff --git a/src/simgrid_units_main.c b/src/simgrid_units_main.c index 1c497acbe8..3e37f81aa1 100644 --- a/src/simgrid_units_main.c +++ b/src/simgrid_units_main.c @@ -22,6 +22,7 @@ extern xbt_test_unit_t _xbt_current_unit; /* SGU: BEGIN FILE xbt/dynar.c */ void test_dynar_int(void); + void test_dynar_insert(void); void test_dynar_double(void); void test_dynar_string(void); void test_dynar_sync_int(void); @@ -99,6 +100,7 @@ int main(int argc, char *argv[]) { /* SGU: BEGIN FILE xbt/dynar.c */ suite = xbt_test_suite_by_name("dynar","Dynar data container"); xbt_test_suite_push(suite, "int", test_dynar_int, "Dynars of integers"); + xbt_test_suite_push(suite, "insert", test_dynar_insert, "Using the xbt_dynar_insert and xbt_dynar_remove functions"); xbt_test_suite_push(suite, "double", test_dynar_double, "Dynars of doubles"); xbt_test_suite_push(suite, "string", test_dynar_string, "Dynars of strings"); xbt_test_suite_push(suite, "synchronized int", test_dynar_sync_int, "Synchronized dynars of integers"); diff --git a/src/xbt/dynar.c b/src/xbt/dynar.c index 45c3190c60..f4f1cdd26d 100644 --- a/src/xbt/dynar.c +++ b/src/xbt/dynar.c @@ -459,7 +459,7 @@ static XBT_INLINE void *_xbt_dynar_insert_at_ptr(xbt_dynar_t const dynar, void *res; unsigned long old_used; unsigned long new_used; - unsigned long nb_shift; + long nb_shift; _sanity_check_dynar(dynar); _sanity_check_idx(idx); @@ -471,9 +471,10 @@ static XBT_INLINE void *_xbt_dynar_insert_at_ptr(xbt_dynar_t const dynar, nb_shift = old_used - idx; - if (nb_shift) + if (nb_shift>0) { memmove(_xbt_dynar_elm(dynar, idx + 1), _xbt_dynar_elm(dynar, idx), nb_shift * dynar->elmsize); + } dynar->used = new_used; res = _xbt_dynar_elm(dynar, idx); @@ -862,12 +863,12 @@ XBT_TEST_UNIT("int", test_dynar_int, "Dynars of integers") xbt_dynar_push_as(d, int, cpt); DEBUG2("Push %d, length=%lu", cpt, xbt_dynar_length(d)); } - for (cpt = 0; cpt < 1000; cpt++) { - xbt_dynar_insert_at_as(d, 2500, int, cpt); + for (cpt = 0; cpt < NB_ELEM/5; cpt++) { + xbt_dynar_insert_at_as(d, NB_ELEM/2, int, cpt); DEBUG2("Push %d, length=%lu", cpt, xbt_dynar_length(d)); } - for (cpt = 0; cpt < 2500; cpt++) { + for (cpt = 0; cpt < NB_ELEM/2; cpt++) { xbt_dynar_shift(d, &i); xbt_test_assert2(i == cpt, "The retrieved value is not the same than the injected one at the begining (%d!=%d)", @@ -907,6 +908,71 @@ XBT_TEST_UNIT("int", test_dynar_int, "Dynars of integers") /* in your code is naturally the way to go outside a regression test */ } +/*******************************************************************************/ +/*******************************************************************************/ +/*******************************************************************************/ +XBT_TEST_UNIT("insert",test_dynar_insert,"Using the xbt_dynar_insert and xbt_dynar_remove functions") +{ + xbt_dynar_t d = xbt_dynar_new(sizeof(int), NULL); + int cursor,cpt; + + xbt_test_add1("==== Insert %d int, traverse them, remove them",NB_ELEM); + /* Populate_ints [doxygen cruft] */ + /* 1. Populate the dynar */ + for (cpt = 0; cpt < NB_ELEM; cpt++) { + xbt_dynar_insert_at(d, cpt, &cpt); + xbt_test_log2("Push %d, length=%lu", cpt, xbt_dynar_length(d)); + } + + /* 3. Traverse the dynar */ + xbt_dynar_foreach(d, cursor, cpt) { + xbt_test_assert2(cursor == cpt, + "The retrieved value is not the same than the injected one (%d!=%d)", + cursor, cpt); + } + /* end_of_traversal */ + + for (cpt = 0; cpt < NB_ELEM; cpt++) { + int val; + xbt_dynar_remove_at(d,0,&val); + xbt_test_assert2(cpt == val, + "The retrieved value is not the same than the injected one (%d!=%d)", + cursor, cpt); + } + xbt_test_assert1(xbt_dynar_length(d) == 0, + "There is still %d elements in the dynar after removing everything", + xbt_dynar_length(d)); + xbt_dynar_free(&d); + + /* ********************* */ + xbt_test_add1("==== Insert %d int in reverse order, traverse them, remove them",NB_ELEM); + d = xbt_dynar_new(sizeof(int), NULL); + for (cpt = NB_ELEM-1; cpt >=0; cpt--) { + xbt_dynar_replace(d, cpt, &cpt); + xbt_test_log2("Push %d, length=%lu", cpt, xbt_dynar_length(d)); + } + + /* 3. Traverse the dynar */ + xbt_dynar_foreach(d, cursor, cpt) { + xbt_test_assert2(cursor == cpt, + "The retrieved value is not the same than the injected one (%d!=%d)", + cursor, cpt); + } + /* end_of_traversal */ + + for (cpt =NB_ELEM-1; cpt >=0; cpt--) { + int val; + xbt_dynar_remove_at(d,xbt_dynar_length(d)-1,&val); + xbt_test_assert2(cpt == val, + "The retrieved value is not the same than the injected one (%d!=%d)", + cursor, cpt); + } + xbt_test_assert1(xbt_dynar_length(d) == 0, + "There is still %d elements in the dynar after removing everything", + xbt_dynar_length(d)); + xbt_dynar_free(&d); +} + /*******************************************************************************/ /*******************************************************************************/ /*******************************************************************************/