-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest_table.c
115 lines (83 loc) · 2.78 KB
/
test_table.c
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
/* Copyright (c) 2011 - Eric P. Mangold
* Copyright (c) 2011 - Peter Le Bek
*
* See LICENSE.txt for details.
*/
/* Just a few extra tests for lines/branches that have no coverage yet.
* The hash-table implementation was copied from CII and is generally
* believed to be bug-free. Even though we don't have direct tests
* for a lot of the code in table.c is seems very likely that if it was
* ever broken it would cause regressions elsewhere in the test
* suite. */
/* Check - C unit testing framework */
#include <check.h>
#include "table.h"
static int cmpatom(const void *x, const void *y)
{
return x != y;
}
static unsigned hashatom(const void *key)
{
/* all items go in same bucket */
return 0;
}
START_TEST(test_new_with_size)
{
/* A size hint should be respected */
Table_T *table = Table_new(130, cmpatom, hashatom);
fail_unless( Table_num_buckets(table) == 127 );
Table_free(&table);
}
END_TEST
START_TEST(test_put_replace)
{
/* New values replace old values with the same key. */
Table_T *table = Table_new(0, cmpatom, hashatom);
Table_put(table, (void*)0x12, (void*)0x34);
Table_put(table, (void*)0x12, (void*)0x56);
fail_unless( Table_length(table) == 1 );
fail_unless( Table_get(table, (void*)0x12) == (void*)0x56);
Table_remove(table, (void*)0x12);
Table_free(&table);
}
END_TEST
START_TEST(test_put_in_same_bucket)
{
/* Our hash function always places key/vals in the same bucket -
* assert that multiple items can be placed and retreived from
* the same bucket */
Table_T *table = Table_new(0, cmpatom, hashatom);
Table_put(table, (void*)0x12, (void*)0x34);
Table_put(table, (void*)0x56, (void*)0x78);
fail_unless( Table_length(table) == 2 );
fail_unless( Table_get(table, (void*)0x12) == (void*)0x34);
fail_unless( Table_get(table, (void*)0x56) == (void*)0x78);
Table_remove(table, (void*)0x12);
Table_remove(table, (void*)0x56);
Table_free(&table);
}
END_TEST
START_TEST(test_get)
{
/* Table_get() with non-existant key returns NULL */
Table_T *table = Table_new(0, cmpatom, hashatom);
Table_put(table, (void*)0x12, (void*)0x34);
fail_unless( Table_length(table) == 1 );
fail_unless( Table_get(table, (void*)0x12) == (void*)0x34);
/* non-existant key */
fail_unless( Table_get(table, (void*)0x34) == NULL);
Table_remove(table, (void*)0x12);
Table_free(&table);
}
END_TEST
Suite *make_table_suite(void)
{
Suite *s = suite_create ("Hash-Table");
TCase *tc_table = tcase_create("hash-table");
tcase_add_test(tc_table, test_new_with_size);
tcase_add_test(tc_table, test_put_replace);
tcase_add_test(tc_table, test_put_in_same_bucket);
tcase_add_test(tc_table, test_get);
suite_add_tcase(s, tc_table);
return s;
};