示例#1
0
static void
test_boolean_ops (void (*function)(struct stringi_set *a, struct stringi_set *b,
                                   unsigned int *a_pat, unsigned int *b_pat))
{
  enum { MAX_STRINGS = 7 };
  unsigned int a_pat, b_pat;

  for (a_pat = 0; a_pat < (1u << MAX_STRINGS); a_pat++)
    for (b_pat = 0; b_pat < (1u << MAX_STRINGS); b_pat++)
      {
        unsigned int new_a_pat = a_pat;
        unsigned int new_b_pat = b_pat;
        struct stringi_set a, b;
        int a_strings[MAX_STRINGS], b_strings[MAX_STRINGS];
        size_t i, n_a, n_b;

        stringi_set_init (&a);
        stringi_set_init (&b);
        for (i = 0; i < MAX_STRINGS; i++)
          {
            if (a_pat & (1u << i))
              stringi_set_insert (&a, make_string (i));
            if (b_pat & (1u << i))
              stringi_set_insert (&b, make_string (i));
          }

        function (&a, &b, &new_a_pat, &new_b_pat);

        n_a = n_b = 0;
        for (i = 0; i < MAX_STRINGS; i++)
          {
            if (new_a_pat & (1u << i))
              a_strings[n_a++] = i;
            if (new_b_pat & (1u << i))
              b_strings[n_b++] = i;
          }
        check_stringi_set (&a, a_strings, n_a);
        check_stringi_set (&b, b_strings, n_b);
        stringi_set_destroy (&a);
        stringi_set_destroy (&b);
      }
}
示例#2
0
/* Initializes SET as a new string set that initially contains the same strings
   as OLD. */
void
stringi_set_clone (struct stringi_set *set, const struct stringi_set *old)
{
  const struct stringi_set_node *node;
  const char *s;

  stringi_set_init (set);
  hmap_reserve (&set->hmap, stringi_set_count (old));
  STRINGI_SET_FOR_EACH (s, node, old)
    stringi_set_insert__ (set, xstrdup (s), node->hmap_node.hash);
}
示例#3
0
/* Inserts the CNT strings from 0 to CNT - 1 (inclusive) into a set in the
   order specified by INSERTIONS, then deletes them in the order specified by
   DELETIONS, checking the set's contents for correctness after each
   operation.  */
static void
test_insert_delete (const int insertions[],
                    const int deletions[],
                    size_t cnt)
{
  struct stringi_set set;
  size_t i;

  stringi_set_init (&set);
  check_stringi_set (&set, NULL, 0);
  for (i = 0; i < cnt; i++)
    {
      check (stringi_set_insert (&set, make_string (insertions[i])));
      check_stringi_set (&set, insertions, i + 1);
    }
  for (i = 0; i < cnt; i++)
    {
      check (stringi_set_delete (&set, make_string (deletions[i])));
      check_stringi_set (&set, deletions + i + 1, cnt - i - 1);
    }
  stringi_set_destroy (&set);
}
示例#4
0
/* Inserts strings into a set in ascending order, then delete in ascending
   order. */
static void
test_insert_ordered (void)
{
  const int max_elems = 64;
  int *values;
  struct stringi_set set;
  int i;

  stringi_set_init (&set);
  values = xnmalloc (max_elems, sizeof *values);
  for (i = 0; i < max_elems; i++)
    {
      values[i] = i;
      stringi_set_insert_nocopy (&set, xstrdup (make_string (i)));
      check_stringi_set (&set, values, i + 1);
    }
  for (i = 0; i < max_elems; i++)
    {
      stringi_set_delete (&set, make_string (i));
      check_stringi_set (&set, values + i + 1, max_elems - i - 1);
    }
  stringi_set_destroy (&set);
  free (values);
}