summaryrefslogtreecommitdiff
path: root/src/test/check_ht.c
blob: 0298087c0338590a31e6209e6ef4db865c10f266 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <stdio.h>
#include "../ht.h"

int main(int UNUSED argc, char ** UNUSED argv)
{

  ht *htp = new_ht(HT_INIT_CAP, 0);

  NUM *temp = MYALLOC(NUM, 1), key = 1023;
  *temp = 12345;

  if (ht_insert(htp, &key, temp)) {
    fleprintf0("Fail to insert\n");
    free(temp);
    destroy_ht(htp, DESTROY_EVERY_SELF);
    return 1;
  }

  if ((temp = ht_find(htp, &key))) {
    fleprintf("We found value %ld for key %ld\n",
              *temp, key);
  } else
    fleprintf("We found no value for key %ld\n", key);

  NUM size = ht_size(htp);
  
  fleprintf("The size of the hash table is %ld\n", size);
  
  for (NUM i = 0; i < size; i++)
    fleprintf("The %ld-th element has key %ld and value %ld\n",
              i,
              *((NUM *)*(ht_keys(htp)+i)),
              *((NUM *)*(ht_values(htp)+i)));

  if (ht_delete(htp, &key, DELETE_VALUE)) {
    fleprintf("Fail to delete key %ld\n", key);
    destroy_ht(htp, DESTROY_VALUE_SELF);
    return 1;
  }

  fleprintf0("After the deletion, ");

  if ((temp = ht_find(htp, &key))) {
    eprintf("We found value %ld for key %ld\n",
            *temp, key);
    destroy_ht(htp, DESTROY_VALUE_SELF);
    return 1;
  } else {
    eprintf("We found no value for key %ld\n", key);
  }

  destroy_ht(htp, DESTROY_VALUE_SELF);
  return 0;
}