Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Use xbt_strdup instead of strdup
[simgrid.git] / src / xbt / dynar.c
index b7ed051..fc937cb 100644 (file)
@@ -1,6 +1,6 @@
 /* a generic DYNamic ARray implementation.                                  */
 
-/* Copyright (c) 2004-2013. The SimGrid Team.
+/* Copyright (c) 2004-2014. The SimGrid Team.
  * All rights reserved.                                                     */
 
 /* This program is free software; you can redistribute it and/or modify it
@@ -82,17 +82,6 @@ _xbt_dynar_get_elm(void *const dst,
   memcpy(dst, elm, dynar->elmsize);
 }
 
-static XBT_INLINE
-    void
-_xbt_dynar_put_elm(const xbt_dynar_t dynar,
-                   const unsigned long idx, const void *const src)
-{
-  void *const elm = _xbt_dynar_elm(dynar, idx);
-  const unsigned long elmsize = dynar->elmsize;
-
-  memcpy(elm, src, elmsize);
-}
-
 void xbt_dynar_dump(xbt_dynar_t dynar)
 {
   XBT_INFO("Dynar dump: size=%lu; used=%lu; elmsize=%lu; data=%p; free_f=%p",
@@ -838,6 +827,20 @@ XBT_TEST_UNIT("int", test_dynar_int, "Dynars of integers")
     xbt_test_log("Pop %d, length=%lu", cpt, xbt_dynar_length(d));
   }
 
+  int* pi;
+  xbt_dynar_foreach_ptr(d, cursor, pi) {
+    *pi = 0;
+  }
+  xbt_dynar_foreach(d, cursor, i) {
+    xbt_test_assert(i == 0, "The value is not the same as the expected one.");
+  }
+  xbt_dynar_foreach_ptr(d, cursor, pi) {
+    *pi = 1;
+  }
+  xbt_dynar_foreach(d, cursor, i) {
+    xbt_test_assert(i == 1, "The value is not the same as the expected one.");
+  }
+
   /* 5. Free the resources */
   xbt_dynar_free(&d);           /* This code is used both as example and as regression test, so we try to */
   xbt_dynar_free(&d);           /* free the struct twice here to check that it's ok, but freeing  it only once */
@@ -1137,22 +1140,22 @@ XBT_TEST_UNIT("string", test_dynar_string, "Dynars of strings")
   /* 1. Populate the dynar */
   for (cpt = 0; cpt < NB_ELEM; cpt++) {
     sprintf(buf, "%d", cpt);
-    s1 = strdup(buf);
+    s1 = xbt_strdup(buf);
     xbt_dynar_push(d, &s1);
   }
   for (cpt = 0; cpt < NB_ELEM; cpt++) {
     sprintf(buf, "%d", cpt);
-    s1 = strdup(buf);
+    s1 = xbt_strdup(buf);
     xbt_dynar_replace(d, cpt, &s1);
   }
   for (cpt = 0; cpt < NB_ELEM; cpt++) {
     sprintf(buf, "%d", cpt);
-    s1 = strdup(buf);
+    s1 = xbt_strdup(buf);
     xbt_dynar_replace(d, cpt, &s1);
   }
   for (cpt = 0; cpt < NB_ELEM; cpt++) {
     sprintf(buf, "%d", cpt);
-    s1 = strdup(buf);
+    s1 = xbt_strdup(buf);
     xbt_dynar_replace(d, cpt, &s1);
   }
   for (cpt = 0; cpt < NB_ELEM; cpt++) {
@@ -1171,7 +1174,7 @@ XBT_TEST_UNIT("string", test_dynar_string, "Dynars of strings")
   d = xbt_dynar_new(sizeof(char **), &xbt_free_ref);
   for (cpt = 0; cpt < NB_ELEM; cpt++) {
     sprintf(buf, "%d", cpt);
-    s1 = strdup(buf);
+    s1 = xbt_strdup(buf);
     xbt_dynar_unshift(d, &s1);
   }
   /* 2. Traverse the dynar with the macro */
@@ -1202,12 +1205,12 @@ XBT_TEST_UNIT("string", test_dynar_string, "Dynars of strings")
   d = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
   for (cpt = 0; cpt < NB_ELEM; cpt++) {
     sprintf(buf, "%d", cpt);
-    s1 = strdup(buf);
+    s1 = xbt_strdup(buf);
     xbt_dynar_push(d, &s1);
   }
   for (cpt = 0; cpt < NB_ELEM / 5; cpt++) {
     sprintf(buf, "%d", cpt);
-    s1 = strdup(buf);
+    s1 = xbt_strdup(buf);
     xbt_dynar_insert_at(d, NB_ELEM / 2, &s1);
   }
 
@@ -1245,7 +1248,7 @@ XBT_TEST_UNIT("string", test_dynar_string, "Dynars of strings")
   d = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
   for (cpt = 0; cpt < NB_ELEM; cpt++) {
     sprintf(buf, "%d", cpt);
-    s1 = strdup(buf);
+    s1 = xbt_strdup(buf);
     xbt_dynar_push(d, &s1);
   }
   for (cpt = 2 * (NB_ELEM / 5); cpt < 4 * (NB_ELEM / 5); cpt++) {