summaryrefslogtreecommitdiff
path: root/test/hash_table_test.c
blob: e2f702724b6e11f200afefe1fc1372c195dffcd8 (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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
 * Papo IRC Server
 * Copyright (C) 2021 Johnny Richard
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */
#define MUNIT_ENABLE_ASSERT_ALIASES
#include "munit/munit.c"
#include "../hash_table.h"

#include <stdlib.h>

static MunitResult
test_create_new(const  MunitParameter params[],
                void  *user_data_or_fixture)
{
  hash_table_t* table = hash_table_new();

  assert_not_null(table);
  assert_int(table->capacity, >, 0);
  assert_int(table->length, ==, 0);

  hash_table_destroy(table);
  return MUNIT_OK;
}

static MunitResult
test_insert_and_get(const  MunitParameter params[],
                    void  *user_data_or_fixture)
{
  hash_table_t* table = hash_table_new();

  hash_table_insert(table, "key1", "value1");
  hash_table_insert(table, "key2", "value2");

  char* value = hash_table_get(table, "key1");
  assert_string_equal("value1", value);

  value = hash_table_get(table, "key2");
  assert_string_equal("value2", value);

  value = hash_table_get(table, "invalid_key");
  assert_null(value);

  hash_table_destroy(table);
  return MUNIT_OK;
}

static MunitResult
test_remove(const  MunitParameter params[],
            void  *user_data_or_fixture)
{
  hash_table_t* table = hash_table_new();

  hash_table_insert(table, "key", "value");

  char* value = hash_table_get(table, "key");
  assert_string_equal("value", value);

  bool removed = hash_table_remove(table, "key");

  assert_int(hash_table_length(table), ==, 0);
  assert_null(hash_table_get(table, "key"));
  assert_true(removed);

  hash_table_destroy(table);
  return MUNIT_OK;
}

static MunitResult
test_length(const  MunitParameter params[],
            void  *user_data_or_fixture)
{
  hash_table_t* table = hash_table_new();

  hash_table_insert(table, "key1", "value1");
  hash_table_insert(table, "key2", "value2");

  assert_int(2, ==, hash_table_length(table));

  hash_table_destroy(table);
  return MUNIT_OK;
}

static MunitResult
test_iterator(const  MunitParameter params[],
              void  *user_data_or_fixture)
{
  hash_table_t* table = hash_table_new();

  hash_table_insert(table, "key1", "value1");
  hash_table_insert(table, "key2", "value2");

  hash_table_iterator_t it = hash_table_get_iterator(table);

  size_t count;
  for (count = 0; hash_table_iterator_next(&it); ++count) {
    if (strcmp(it.key, "key1") != 0 &&
        strcmp(it.key, "key2") != 0) {
      fprintf(stderr, "key not found: %s\n", it.key);
      hash_table_destroy(table);
      return MUNIT_FAIL;
    }

    if (strcmp(it.value, "value1") != 0 &&
        strcmp(it.value, "value2") != 0) {
      fprintf(stderr, "value not found: %s\n", it.value);
      hash_table_destroy(table);
      return MUNIT_FAIL;
    }
  }

  assert_int(hash_table_length(table), ==, count);

  hash_table_destroy(table);
  return MUNIT_OK;
}

static MunitTest tests[] = {
  { "/test_create_new", test_create_new, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL },
  { "/test_insert_and_get", test_insert_and_get, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL },
  { "/test_remove", test_remove, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL },
  { "/test_length", test_length, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL },
  { "/test_iterator", test_iterator, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL },
  { NULL, NULL, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL }
};

static const MunitSuite suite = {
  "/hash_table", tests, NULL, 1, MUNIT_SUITE_OPTION_NONE
};

int
main(int argc, char *argv[])
{
  return munit_suite_main(&suite, NULL, argc, argv);
  return EXIT_SUCCESS;
}