示例#1
0
END_TEST

START_TEST (replace_element_should_fail_if_invalid_position)
{

  int num = 10, num2 = 20;

  vector *v = vector_new(sizeof(int), NULL, 2);
  vector_append(v, &num);

  fail_unless(vector_replace(v, 1, &num2) == VECT_REPLACE_INVALID_POSITION);
  fail_unless(vector_replace(v, -3, &num2) == VECT_REPLACE_INVALID_POSITION);

  vector_free(v);
}
示例#2
0
文件: hash.c 项目: david672orford/PPR
/* NB. This only copies the hash, NOT the structures or whatever
 * which might be pointed to by keys/values in the hash. Beware.
 */
hash
copy_hash (pool pool, hash h)
{
  hash new_h;
  int b, i;

  new_h = c2_pmalloc (pool, sizeof *new_h);
  new_h->pool = pool;
  new_h->key_size = h->key_size;
  new_h->value_size = h->value_size;
  new_h->buckets = copy_vector (pool, h->buckets);

  /* Copy the buckets. */
  for (b = 0; b < vector_size (new_h->buckets); ++b)
	{
	  vector v;

	  vector_get (new_h->buckets, b, v);

	  if (v)
		{
		  v = copy_vector (pool, v);
		  vector_replace (new_h->buckets, b, v);

		  /* Copy the keys/values in this vector. */
		  for (i = 0; i < vector_size (v); ++i)
			{
			  struct hash_bucket_entry entry;

			  vector_get (v, i, entry);
			  entry.key = pmemdup (pool, entry.key, h->key_size);
			  entry.value = pmemdup (pool, entry.value, h->value_size);
			  vector_replace (v, i, entry);
			}
		}
	}

  return new_h;
}
示例#3
0
文件: hash.c 项目: david672orford/PPR
int
_hash_insert (hash h, const void *key, const void *value)
{
  int b, i;
  vector bucket;
  struct hash_bucket_entry entry;

  /* Find the appropriate bucket. */
  b = HASH (key, h->key_size, vector_size (h->buckets));
  vector_get (h->buckets, b, bucket);

  /* If there is no bucket there, we have to allocate a fresh vector. */
  if (bucket == 0)
	{
	  bucket = new_vector (h->pool, struct hash_bucket_entry);
	  vector_replace (h->buckets, b, bucket);
	}
示例#4
0
END_TEST

START_TEST (replace_element_should_call_free_function)
{

  char *elem1 = strdup("first");
  char *elem2 = strdup("second");
  char *elem3 = strdup("third");

  vector *v = vector_new(sizeof(char *), free_string, 4);
  vector_append(v, &elem1);
  vector_append(v, &elem2);

  fail_unless(vector_replace(v, 0, &elem3) == VECT_OK);
  fail_unless(elem3 == *(char **)vector_get(v, 0));

  vector_free(v);
}
示例#5
0
END_TEST

START_TEST (replace_element_on_specific_position)
{

  char elem1 = 'a', elem2 = 'b', elem3 = 'c';

  vector *v = vector_new(sizeof(char), NULL, 4);
  vector_append(v, &elem1);
  vector_append(v, &elem2);

  fail_unless(vector_replace(v, 1, &elem3) == 0);
  fail_unless(2 == vector_length(v));
  fail_unless(elem1 == *(char *)vector_get(v, 0));
  fail_unless(elem3 == *(char *)vector_get(v, 1));

  vector_free(v);
}