Пример #1
0
/** Run unit tests for fp_pair-to-void* map functions */
static void
test_container_fp_pair_map(void)
{
  fp_pair_map_t *map;
  fp_pair_t fp1, fp2, fp3, fp4, fp5, fp6;
  void *v;
  fp_pair_map_iter_t *iter;
  fp_pair_t k;

  map = fp_pair_map_new();
  test_assert(map);
  test_eq(fp_pair_map_size(map), 0);
  test_assert(fp_pair_map_isempty(map));

  memset(fp1.first, 0x11, DIGEST_LEN);
  memset(fp1.second, 0x12, DIGEST_LEN);
  memset(fp2.first, 0x21, DIGEST_LEN);
  memset(fp2.second, 0x22, DIGEST_LEN);
  memset(fp3.first, 0x31, DIGEST_LEN);
  memset(fp3.second, 0x32, DIGEST_LEN);
  memset(fp4.first, 0x41, DIGEST_LEN);
  memset(fp4.second, 0x42, DIGEST_LEN);
  memset(fp5.first, 0x51, DIGEST_LEN);
  memset(fp5.second, 0x52, DIGEST_LEN);
  memset(fp6.first, 0x61, DIGEST_LEN);
  memset(fp6.second, 0x62, DIGEST_LEN);

  v = fp_pair_map_set(map, &fp1, (void*)99);
  test_eq(v, NULL);
  test_assert(!fp_pair_map_isempty(map));
  v = fp_pair_map_set(map, &fp2, (void*)101);
  test_eq(v, NULL);
  v = fp_pair_map_set(map, &fp1, (void*)100);
  test_eq(v, (void*)99);
  test_eq_ptr(fp_pair_map_get(map, &fp1), (void*)100);
  test_eq_ptr(fp_pair_map_get(map, &fp2), (void*)101);
  test_eq_ptr(fp_pair_map_get(map, &fp3), NULL);
  fp_pair_map_assert_ok(map);

  v = fp_pair_map_remove(map, &fp2);
  fp_pair_map_assert_ok(map);
  test_eq_ptr(v, (void*)101);
  test_eq_ptr(fp_pair_map_get(map, &fp2), NULL);
  test_eq_ptr(fp_pair_map_remove(map, &fp2), NULL);

  fp_pair_map_set(map, &fp2, (void*)101);
  fp_pair_map_set(map, &fp3, (void*)102);
  fp_pair_map_set(map, &fp4, (void*)103);
  test_eq(fp_pair_map_size(map), 4);
  fp_pair_map_assert_ok(map);
  fp_pair_map_set(map, &fp5, (void*)104);
  fp_pair_map_set(map, &fp6, (void*)105);
  fp_pair_map_assert_ok(map);

  /* Test iterator. */
  iter = fp_pair_map_iter_init(map);
  while (!fp_pair_map_iter_done(iter)) {
    fp_pair_map_iter_get(iter, &k, &v);
    test_eq_ptr(v, fp_pair_map_get(map, &k));

    if (tor_memeq(&fp2, &k, sizeof(fp2))) {
      iter = fp_pair_map_iter_next_rmv(map, iter);
    } else {
      iter = fp_pair_map_iter_next(map, iter);
    }
  }

  /* Make sure we removed fp2, but not the others. */
  test_eq_ptr(fp_pair_map_get(map, &fp2), NULL);
  test_eq_ptr(fp_pair_map_get(map, &fp5), (void*)104);

  fp_pair_map_assert_ok(map);
  /* Clean up after ourselves. */
  fp_pair_map_free(map, NULL);
  map = NULL;

 done:
  if (map)
    fp_pair_map_free(map, NULL);
}
Пример #2
0
/** Run unit tests for fp_pair-to-void* map functions */
static void
test_container_fp_pair_map(void *arg)
{
  fp_pair_map_t *map;
  fp_pair_t fp1, fp2, fp3, fp4, fp5, fp6;
  void *v;
  fp_pair_map_iter_t *iter;
  fp_pair_t k;
  char *v99 = tor_strdup("99");
  char *v100 = tor_strdup("v100");
  char *v101 = tor_strdup("v101");
  char *v102 = tor_strdup("v102");
  char *v103 = tor_strdup("v103");
  char *v104 = tor_strdup("v104");
  char *v105 = tor_strdup("v105");

  (void)arg;
  map = fp_pair_map_new();
  tt_assert(map);
  tt_int_op(fp_pair_map_size(map),OP_EQ, 0);
  tt_assert(fp_pair_map_isempty(map));

  memset(fp1.first, 0x11, DIGEST_LEN);
  memset(fp1.second, 0x12, DIGEST_LEN);
  memset(fp2.first, 0x21, DIGEST_LEN);
  memset(fp2.second, 0x22, DIGEST_LEN);
  memset(fp3.first, 0x31, DIGEST_LEN);
  memset(fp3.second, 0x32, DIGEST_LEN);
  memset(fp4.first, 0x41, DIGEST_LEN);
  memset(fp4.second, 0x42, DIGEST_LEN);
  memset(fp5.first, 0x51, DIGEST_LEN);
  memset(fp5.second, 0x52, DIGEST_LEN);
  memset(fp6.first, 0x61, DIGEST_LEN);
  memset(fp6.second, 0x62, DIGEST_LEN);

  v = fp_pair_map_set(map, &fp1, v99);
  tt_ptr_op(v, OP_EQ, NULL);
  tt_assert(!fp_pair_map_isempty(map));
  v = fp_pair_map_set(map, &fp2, v101);
  tt_ptr_op(v, OP_EQ, NULL);
  v = fp_pair_map_set(map, &fp1, v100);
  tt_ptr_op(v, OP_EQ, v99);
  tt_ptr_op(fp_pair_map_get(map, &fp1),OP_EQ, v100);
  tt_ptr_op(fp_pair_map_get(map, &fp2),OP_EQ, v101);
  tt_ptr_op(fp_pair_map_get(map, &fp3),OP_EQ, NULL);
  fp_pair_map_assert_ok(map);

  v = fp_pair_map_remove(map, &fp2);
  fp_pair_map_assert_ok(map);
  tt_ptr_op(v,OP_EQ, v101);
  tt_ptr_op(fp_pair_map_get(map, &fp2),OP_EQ, NULL);
  tt_ptr_op(fp_pair_map_remove(map, &fp2),OP_EQ, NULL);

  fp_pair_map_set(map, &fp2, v101);
  fp_pair_map_set(map, &fp3, v102);
  fp_pair_map_set(map, &fp4, v103);
  tt_int_op(fp_pair_map_size(map),OP_EQ, 4);
  fp_pair_map_assert_ok(map);
  fp_pair_map_set(map, &fp5, v104);
  fp_pair_map_set(map, &fp6, v105);
  fp_pair_map_assert_ok(map);

  /* Test iterator. */
  iter = fp_pair_map_iter_init(map);
  while (!fp_pair_map_iter_done(iter)) {
    fp_pair_map_iter_get(iter, &k, &v);
    tt_ptr_op(v,OP_EQ, fp_pair_map_get(map, &k));

    if (tor_memeq(&fp2, &k, sizeof(fp2))) {
      iter = fp_pair_map_iter_next_rmv(map, iter);
    } else {
      iter = fp_pair_map_iter_next(map, iter);
    }
  }

  /* Make sure we removed fp2, but not the others. */
  tt_ptr_op(fp_pair_map_get(map, &fp2),OP_EQ, NULL);
  tt_ptr_op(fp_pair_map_get(map, &fp5),OP_EQ, v104);

  fp_pair_map_assert_ok(map);
  /* Clean up after ourselves. */
  fp_pair_map_free(map, NULL);
  map = NULL;

 done:
  if (map)
    fp_pair_map_free(map, NULL);
  tor_free(v99);
  tor_free(v100);
  tor_free(v101);
  tor_free(v102);
  tor_free(v103);
  tor_free(v104);
  tor_free(v105);
}