Beispiel #1
0
/* Test simple insert and erase operations. */
static void test_simple_add_and_erase(void) {
  census_ht_option opt = {CENSUS_HT_UINT64, 7, NULL, NULL, NULL, NULL};
  census_ht *ht = census_ht_create(&opt);
  GPR_ASSERT(ht != NULL);
  GPR_ASSERT(census_ht_get_size(ht) == 0);
  {
    census_ht_key key;
    int val = 3;
    key.val = 2;
    census_ht_insert(ht, key, (void *)&val);
    GPR_ASSERT(census_ht_get_size(ht) == 1);
    census_ht_erase(ht, key);
    GPR_ASSERT(census_ht_get_size(ht) == 0);
    /* Erasing a key from an empty table should be noop. */
    census_ht_erase(ht, key);
    GPR_ASSERT(census_ht_get_size(ht) == 0);
    /* Erasing a non-existant key from a table should be noop. */
    census_ht_insert(ht, key, (void *)&val);
    key.val = 3;
    census_ht_insert(ht, key, (void *)&val);
    key.val = 9;
    census_ht_insert(ht, key, (void *)&val);
    GPR_ASSERT(census_ht_get_size(ht) == 3);
    key.val = 1;
    census_ht_erase(ht, key);
    /* size unchanged after deleting non-existant key. */
    GPR_ASSERT(census_ht_get_size(ht) == 3);
    /* size decrease by 1 after deleting an existant key. */
    key.val = 2;
    census_ht_erase(ht, key);
    GPR_ASSERT(census_ht_get_size(ht) == 2);
  }
  census_ht_destroy(ht);
}
Beispiel #2
0
void census_tracing_end_op(census_op_id op_id) {
  census_trace_obj* trace = NULL;
  gpr_mu_lock(&g_mu);
  trace = census_ht_find(g_trace_store, op_id_as_key(&op_id));
  if (trace != NULL) {
    trace->rpc_stats.elapsed_time_ms = gpr_timespec_to_micros(
        gpr_time_sub(gpr_now(GPR_CLOCK_REALTIME), trace->ts));
    gpr_log(GPR_DEBUG, "End tracing for id %lu, method %s, latency %f us",
            op_id_2_uint64(&op_id), trace->method,
            trace->rpc_stats.elapsed_time_ms);
    census_ht_erase(g_trace_store, op_id_as_key(&op_id));
  }
  gpr_mu_unlock(&g_mu);
}
Beispiel #3
0
static void test_table_with_string_key(void) {
  census_ht_option opt = {CENSUS_HT_POINTER, 7,    &hash64,
                          &cmp_str_keys,     NULL, NULL};
  census_ht *ht = census_ht_create(&opt);
  const char *keys[] = {
      "k1", "a",   "000", "apple", "banana_a_long_long_long_banana",
      "%$", "111", "foo", "b"};
  const int vals[] = {0, 1, 2, 3, 4, 5, 6, 7, 8};
  int i = 0;
  GPR_ASSERT(ht != NULL);
  GPR_ASSERT(census_ht_get_size(ht) == 0);
  for (i = 0; i < 9; i++) {
    census_ht_key key;
    key.ptr = (void *)(keys[i]);
    census_ht_insert(ht, key, (void *)(vals + i));
  }
  GPR_ASSERT(census_ht_get_size(ht) == 9);
  for (i = 0; i < 9; i++) {
    census_ht_key key;
    int *val_ptr;
    key.ptr = (void *)(keys[i]);
    val_ptr = census_ht_find(ht, key);
    GPR_ASSERT(*val_ptr == vals[i]);
  }
  {
    /* inserts duplicate keys */
    census_ht_key key;
    int *val_ptr = NULL;
    key.ptr = (void *)(keys[2]);
    census_ht_insert(ht, key, (void *)(vals + 8));
    /* expect value to be over written by new insertion */
    GPR_ASSERT(census_ht_get_size(ht) == 9);
    val_ptr = census_ht_find(ht, key);
    GPR_ASSERT(*val_ptr == vals[8]);
  }
  for (i = 0; i < 9; i++) {
    census_ht_key key;
    int *val_ptr;
    uint32_t expected_tbl_sz = 9 - i;
    GPR_ASSERT(census_ht_get_size(ht) == expected_tbl_sz);
    key.ptr = (void *)(keys[i]);
    val_ptr = census_ht_find(ht, key);
    GPR_ASSERT(val_ptr != NULL);
    census_ht_erase(ht, key);
    GPR_ASSERT(census_ht_get_size(ht) == expected_tbl_sz - 1);
    val_ptr = census_ht_find(ht, key);
    GPR_ASSERT(val_ptr == NULL);
  }
  census_ht_destroy(ht);
}
Beispiel #4
0
static void test_insertion_and_deletion_with_high_collision_rate(void) {
  census_ht_option opt = {CENSUS_HT_POINTER, 13,   &force_collision,
                          &cmp_str_keys,     NULL, NULL};
  census_ht *ht = census_ht_create(&opt);
  char key_str[1000][GPR_LTOA_MIN_BUFSIZE];
  uint64_t val = 0;
  unsigned i = 0;
  for (i = 0; i < 1000; i++) {
    census_ht_key key;
    key.ptr = key_str[i];
    gpr_ltoa(i, key_str[i]);
    census_ht_insert(ht, key, (void *)(&val));
    gpr_log(GPR_INFO, "%d\n", i);
    GPR_ASSERT(census_ht_get_size(ht) == (i + 1));
  }
  for (i = 0; i < 1000; i++) {
    census_ht_key key;
    key.ptr = key_str[i];
    census_ht_erase(ht, key);
    GPR_ASSERT(census_ht_get_size(ht) == (999 - i));
  }
  census_ht_destroy(ht);
}