2018-02-16 18:11:26 +01:00
|
|
|
/*
|
|
|
|
* BreakHack - A dungeone crawler RPG
|
|
|
|
* Copyright (C) 2018 Linus Probert <linus.probert@gmail.com>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU 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 General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2018-05-06 13:27:29 +02:00
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdio.h>
|
2018-05-11 19:09:34 +02:00
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stddef.h>
|
2017-12-12 13:06:01 +01:00
|
|
|
#include <stdlib.h>
|
2018-05-11 19:09:34 +02:00
|
|
|
#include <setjmp.h>
|
|
|
|
#include <cmocka.h>
|
2017-12-19 09:06:21 +01:00
|
|
|
#include "../src/hashtable.h"
|
2018-08-05 14:38:58 +02:00
|
|
|
#include "../src/util.h"
|
2017-12-12 13:06:01 +01:00
|
|
|
|
2018-05-11 19:09:34 +02:00
|
|
|
static void test_hashtable_create(void **state)
|
2017-12-12 13:06:01 +01:00
|
|
|
{
|
2018-05-11 19:09:34 +02:00
|
|
|
(void) state;
|
|
|
|
|
2017-12-12 13:06:01 +01:00
|
|
|
Hashtable *table = ht_create(10);
|
2018-05-11 19:09:34 +02:00
|
|
|
assert_non_null( table );
|
|
|
|
assert_int_equal(table->size, 10 );
|
2017-12-12 13:06:01 +01:00
|
|
|
ht_destroy(table);
|
|
|
|
}
|
|
|
|
|
2018-05-11 19:09:34 +02:00
|
|
|
static void test_hashtable_set_get(void **state)
|
2017-12-12 13:06:01 +01:00
|
|
|
{
|
2018-05-11 19:09:34 +02:00
|
|
|
(void) state;
|
|
|
|
|
2017-12-12 13:06:01 +01:00
|
|
|
int i;
|
|
|
|
int* values0[10];
|
|
|
|
int* values1[10];
|
|
|
|
char* keys[] = {
|
|
|
|
"key0",
|
|
|
|
"key1",
|
|
|
|
"key2",
|
|
|
|
"key3",
|
|
|
|
"key4",
|
|
|
|
"key5",
|
|
|
|
"key6",
|
|
|
|
"key7",
|
|
|
|
"key8",
|
|
|
|
"key9",
|
|
|
|
};
|
|
|
|
|
|
|
|
for (i = 0; i < 10; ++i) {
|
|
|
|
values0[i] = malloc(sizeof(int));
|
|
|
|
*values0[i] = i;
|
|
|
|
|
|
|
|
values1[i] = malloc(sizeof(int));
|
|
|
|
*values1[i] = 9-i;
|
|
|
|
}
|
|
|
|
|
|
|
|
Hashtable *table = ht_create(10);
|
|
|
|
|
|
|
|
for (i = 0; i < 10; ++i) {
|
|
|
|
ht_set(table, keys[i], values0[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < 10; ++i) {
|
2018-05-11 19:09:34 +02:00
|
|
|
assert_int_equal( *values0[i], *((int*) ht_get(table, keys[i])) );
|
2017-12-12 13:06:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < 10; ++i) {
|
|
|
|
ht_set(table, keys[i], values1[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < 10; ++i) {
|
2018-05-11 19:09:34 +02:00
|
|
|
assert_int_equal( *values1[i], *((int*) ht_get(table, keys[i])) );
|
2017-12-12 13:06:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ht_destroy(table);
|
|
|
|
}
|
|
|
|
|
2018-05-06 13:27:29 +02:00
|
|
|
static bool checklist[] = {
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
false
|
|
|
|
};
|
|
|
|
|
|
|
|
static void check_number(int *num)
|
|
|
|
{
|
|
|
|
checklist[*num] = true;
|
|
|
|
}
|
|
|
|
|
2018-05-11 19:09:34 +02:00
|
|
|
static void test_hashtable_foreach(void **state)
|
2018-05-06 13:27:29 +02:00
|
|
|
{
|
2018-05-11 19:09:34 +02:00
|
|
|
(void) state;
|
|
|
|
|
2018-05-06 13:27:29 +02:00
|
|
|
Hashtable *table = ht_create(10);
|
|
|
|
|
|
|
|
for (int i = 0; i < 10; i++) {
|
|
|
|
char str[4];
|
|
|
|
int *num = malloc(sizeof(int));
|
2018-05-11 19:09:34 +02:00
|
|
|
assert_non_null( num );
|
2018-05-06 13:27:29 +02:00
|
|
|
*num = i;
|
2018-08-05 14:38:58 +02:00
|
|
|
m_sprintf(str, 4, "%d", *num);
|
2018-05-06 13:27:29 +02:00
|
|
|
ht_set(table, str, num);
|
|
|
|
}
|
|
|
|
|
|
|
|
ht_foreach(table, (void (*)(void*)) check_number);
|
|
|
|
for (int i = 0; i < 10; i++) {
|
2018-05-11 19:09:34 +02:00
|
|
|
assert_non_null(checklist[i]);
|
2018-05-06 13:27:29 +02:00
|
|
|
}
|
2018-05-09 00:21:38 +02:00
|
|
|
|
|
|
|
ht_destroy(table);
|
2018-05-06 13:27:29 +02:00
|
|
|
}
|
|
|
|
|
2018-05-11 19:09:34 +02:00
|
|
|
static void test_hashtable_remove(void **state)
|
2018-05-06 13:27:29 +02:00
|
|
|
{
|
2018-05-11 19:09:34 +02:00
|
|
|
(void) state;
|
|
|
|
|
2018-05-06 13:27:29 +02:00
|
|
|
char key1[] = "key1";
|
|
|
|
char value1[] = "value1";
|
|
|
|
char key2[] = "key2";
|
|
|
|
char value2[] = "value2";
|
|
|
|
char key3[] = "key3";
|
|
|
|
char value3[] = "value3";
|
|
|
|
char *getVal;
|
|
|
|
|
|
|
|
Hashtable *table = ht_create(10);
|
|
|
|
ht_set(table, key1, value1);
|
|
|
|
ht_set(table, key2, value2);
|
|
|
|
ht_set(table, key3, value3);
|
|
|
|
getVal = ht_remove(table, key2);
|
2018-05-11 19:09:34 +02:00
|
|
|
assert_string_equal(value2, getVal);
|
|
|
|
assert_null(ht_get(table, key2));
|
2018-05-09 00:21:38 +02:00
|
|
|
ht_remove(table, key1);
|
|
|
|
ht_remove(table, key3);
|
|
|
|
|
|
|
|
ht_destroy(table);
|
2018-05-06 13:27:29 +02:00
|
|
|
}
|
2017-12-12 13:06:01 +01:00
|
|
|
|
|
|
|
int main(void)
|
|
|
|
{
|
|
|
|
|
2018-05-11 19:09:34 +02:00
|
|
|
const struct CMUnitTest tests[] = {
|
|
|
|
cmocka_unit_test(test_hashtable_create),
|
|
|
|
cmocka_unit_test(test_hashtable_set_get),
|
|
|
|
cmocka_unit_test(test_hashtable_remove),
|
|
|
|
cmocka_unit_test(test_hashtable_foreach)
|
|
|
|
};
|
2017-12-12 13:06:01 +01:00
|
|
|
|
2018-05-11 19:09:34 +02:00
|
|
|
cmocka_run_group_tests(tests, NULL, NULL);
|
2017-12-12 13:06:01 +01:00
|
|
|
}
|