treewide: Replace <name>_cnt by n_<name>s and <name>_cap by allocated_<name>.
[pspp] / tests / libpspp / stringi-map-test.c
index 20b8ce5c2c8e85039be225bae5b377be1d94e19b..63b70ba80d0d6380ac3b66dc576c749c4ddfbe54 100644 (file)
@@ -69,58 +69,6 @@ check_func (bool ok, int line)
    terminates. */
 #define check(EXPR) check_func ((EXPR), __LINE__)
 
-/* Prints a message about memory exhaustion and exits with a
-   failure code. */
-static void
-xalloc_die (void)
-{
-  printf ("virtual memory exhausted\n");
-  exit (EXIT_FAILURE);
-}
-
-static void *xmalloc (size_t n) MALLOC_LIKE;
-static void *xnmalloc (size_t n, size_t m) MALLOC_LIKE;
-static void *xmemdup (const void *p, size_t n) MALLOC_LIKE;
-
-/* Allocates and returns N bytes of memory. */
-static void *
-xmalloc (size_t n)
-{
-  if (n != 0)
-    {
-      void *p = malloc (n);
-      if (p == NULL)
-        xalloc_die ();
-
-      return p;
-    }
-  else
-    return NULL;
-}
-
-static void *
-xmemdup (const void *p, size_t n)
-{
-  void *q = xmalloc (n);
-  memcpy (q, p, n);
-  return q;
-}
-
-/* Clone STRING.  */
-static char *
-xstrdup (const char *string)
-{
-  return xmemdup (string, strlen (string) + 1);
-}
-
-/* Allocates and returns N * M bytes of memory. */
-static void *
-xnmalloc (size_t n, size_t m)
-{
-  if ((size_t) -1 / m <= n)
-    xalloc_die ();
-  return xmalloc (n * m);
-}
 \f
 /* Support routines. */
 
@@ -275,7 +223,7 @@ check_map_contains (struct stringi_map *map,
 
   check (stringi_map_contains (map, key));
 
-  node = stringi_map_find_node (map, key);
+  node = stringi_map_find_node (map, key, strlen (key));
   check (node != NULL);
   check (!utf8_strcasecmp (key, stringi_map_node_get_key (node)));
   check (!strcmp (value, stringi_map_node_get_value (node)));
@@ -323,7 +271,7 @@ check_stringi_map (struct stringi_map *map, const int data[], size_t cnt)
 
   check (!stringi_map_contains (map, "xxx"));
   check (stringi_map_find (map, "0") == NULL);
-  check (stringi_map_find_node (map, "") == NULL);
+  check (stringi_map_find_node (map, "", 0) == NULL);
   check (!stringi_map_delete (map, "xyz"));
 
   if (cnt == 0)
@@ -399,7 +347,7 @@ test_insert_any_remove_any (void)
   for (cnt = 0; cnt <= max_elems; cnt++)
     {
       int *insertions, *deletions;
-      unsigned int ins_perm_cnt;
+      unsigned int ins_n_perms;
       int i;
 
       insertions = xnmalloc (cnt, sizeof *insertions);
@@ -407,24 +355,24 @@ test_insert_any_remove_any (void)
       for (i = 0; i < cnt; i++)
         insertions[i] = i | random_value (i, basis);
 
-      for (ins_perm_cnt = 0;
-           ins_perm_cnt == 0 || next_permutation (insertions, cnt);
-           ins_perm_cnt++)
+      for (ins_n_perms = 0;
+           ins_n_perms == 0 || next_permutation (insertions, cnt);
+           ins_n_perms++)
         {
-          unsigned int del_perm_cnt;
+          unsigned int del_n_perms;
           int i;
 
           for (i = 0; i < cnt; i++)
             deletions[i] = i | random_value (i, basis);
 
-          for (del_perm_cnt = 0;
-               del_perm_cnt == 0 || next_permutation (deletions, cnt);
-               del_perm_cnt++)
+          for (del_n_perms = 0;
+               del_n_perms == 0 || next_permutation (deletions, cnt);
+               del_n_perms++)
             test_insert_delete (insertions, deletions, cnt);
 
-          check (del_perm_cnt == factorial (cnt));
+          check (del_n_perms == factorial (cnt));
         }
-      check (ins_perm_cnt == factorial (cnt));
+      check (ins_n_perms == factorial (cnt));
 
       free (insertions);
       free (deletions);
@@ -442,18 +390,18 @@ test_insert_any_remove_same (void)
   for (cnt = 0; cnt <= max_elems; cnt++)
     {
       int *values;
-      unsigned int permutation_cnt;
+      unsigned int n_permutations;
       int i;
 
       values = xnmalloc (cnt, sizeof *values);
       for (i = 0; i < cnt; i++)
         values[i] = i | random_value (i, 1);
 
-      for (permutation_cnt = 0;
-           permutation_cnt == 0 || next_permutation (values, cnt);
-           permutation_cnt++)
+      for (n_permutations = 0;
+           n_permutations == 0 || next_permutation (values, cnt);
+           n_permutations++)
         test_insert_delete (values, values, cnt);
-      check (permutation_cnt == factorial (cnt));
+      check (n_permutations == factorial (cnt));
 
       free (values);
     }
@@ -471,7 +419,7 @@ test_insert_any_remove_reverse (void)
   for (cnt = 0; cnt <= max_elems; cnt++)
     {
       int *insertions, *deletions;
-      unsigned int permutation_cnt;
+      unsigned int n_permutations;
       int i;
 
       insertions = xnmalloc (cnt, sizeof *insertions);
@@ -479,16 +427,16 @@ test_insert_any_remove_reverse (void)
       for (i = 0; i < cnt; i++)
         insertions[i] = i | random_value (i, 2);
 
-      for (permutation_cnt = 0;
-           permutation_cnt == 0 || next_permutation (insertions, cnt);
-           permutation_cnt++)
+      for (n_permutations = 0;
+           n_permutations == 0 || next_permutation (insertions, cnt);
+           n_permutations++)
         {
           memcpy (deletions, insertions, sizeof *insertions * cnt);
           reverse (deletions, cnt);
 
           test_insert_delete (insertions, deletions, cnt);
         }
-      check (permutation_cnt == factorial (cnt));
+      check (n_permutations == factorial (cnt));
 
       free (insertions);
       free (deletions);
@@ -741,11 +689,12 @@ node_swap_value_cb (struct stringi_map *map, int data[], int n)
 
   for (i = 0; i < n; i++)
     {
+      const char *key = make_key (data[i]);
       const char *value = make_value (data[i]);
       struct stringi_map_node *node;
       char *old_value;
 
-      node = stringi_map_find_node (map, make_key (data[i]));
+      node = stringi_map_find_node (map, key, strlen (key));
       check (node != NULL);
       check (!strcmp (stringi_map_node_get_value (node), value));
       data[i] = (data[i] & KEY_MASK) | random_value (i, 15);