summaryrefslogtreecommitdiffstats
path: root/amend/test_symtab.c
blob: 017d18ccdf68c47f673a15cbdf6e471910b0f4ac (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
/*
 * Copyright (C) 2007 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <stdlib.h>
#undef NDEBUG
#include <assert.h>
#include "symtab.h"

int
test_symtab()
{
    SymbolTable *tab;
    void *cookie;
    int ret;

    /* Test creation */
    tab = createSymbolTable();
    assert(tab != NULL);

    /* Smoke-test deletion */
    deleteSymbolTable(tab);


    tab = createSymbolTable();
    assert(tab != NULL);


    /* table parameter must be non-NULL. */
    ret = addToSymbolTable(NULL, NULL, 0, NULL);
    assert(ret < 0);

    /* symbol parameter must be non-NULL. */
    ret = addToSymbolTable(tab, NULL, 0, NULL);
    assert(ret < 0);
    
    /* cookie parameter must be non-NULL. */
    ret = addToSymbolTable(tab, "null", 0, NULL);
    assert(ret < 0);


    /* table parameter must be non-NULL. */
    cookie = findInSymbolTable(NULL, NULL, 0);
    assert(cookie == NULL);

    /* symbol parameter must be non-NULL. */
    cookie = findInSymbolTable(tab, NULL, 0);
    assert(cookie == NULL);


    /* Try some actual inserts.
     */
    ret = addToSymbolTable(tab, "one", 0, (void *)1);
    assert(ret == 0);

    ret = addToSymbolTable(tab, "two", 0, (void *)2);
    assert(ret == 0);

    ret = addToSymbolTable(tab, "three", 0, (void *)3);
    assert(ret == 0);

    /* Try some lookups.
     */
    cookie = findInSymbolTable(tab, "one", 0);
    assert((int)cookie == 1);

    cookie = findInSymbolTable(tab, "two", 0);
    assert((int)cookie == 2);

    cookie = findInSymbolTable(tab, "three", 0);
    assert((int)cookie == 3);

    /* Try to insert something that's already there.
     */
    ret = addToSymbolTable(tab, "one", 0, (void *)1111);
    assert(ret < 0);

    /* Make sure that the failed duplicate insert didn't
     * clobber the original cookie value.
     */
    cookie = findInSymbolTable(tab, "one", 0);
    assert((int)cookie == 1);

    /* Try looking up something that isn't there.
     */
    cookie = findInSymbolTable(tab, "FOUR", 0);
    assert(cookie == NULL);

    /* Try looking up something that's similar to an existing entry.
     */
    cookie = findInSymbolTable(tab, "on", 0);
    assert(cookie == NULL);

    cookie = findInSymbolTable(tab, "onee", 0);
    assert(cookie == NULL);

    /* Test flags.
     * Try inserting something with a different flag.
     */
    ret = addToSymbolTable(tab, "ten", 333, (void *)10);
    assert(ret == 0);

    /* Make sure it's there.
     */
    cookie = findInSymbolTable(tab, "ten", 333);
    assert((int)cookie == 10);

    /* Make sure it's not there when looked up with a different flag.
     */
    cookie = findInSymbolTable(tab, "ten", 0);
    assert(cookie == NULL);

    /* Try inserting something that has the same name as something
     * with a different flag.
     */
    ret = addToSymbolTable(tab, "one", 333, (void *)11);
    assert(ret == 0);

    /* Make sure the new entry exists.
     */
    cookie = findInSymbolTable(tab, "one", 333);
    assert((int)cookie == 11);

    /* Make sure the old entry still has the right value.
     */
    cookie = findInSymbolTable(tab, "one", 0);
    assert((int)cookie == 1);

    /* Try deleting again, now that there's stuff in the table.
     */
    deleteSymbolTable(tab);

    return 0;
}