Imported Upstream version 2.13.1
[platform/upstream/fontconfig.git] / test / test-hash.c
1 #include "../src/fchash.c"
2 #include "../src/fcstr.c"
3
4 FcChar8 *
5 FcLangNormalize (const FcChar8 *lang)
6 {
7     return NULL;
8 }
9
10 FcChar8 *
11 FcConfigHome (void)
12 {
13     return NULL;
14 }
15
16 typedef struct _Test
17 {
18     FcHashTable *table;
19 } Test;
20
21 static Test *
22 init (void)
23 {
24     Test *ret;
25
26     ret = malloc (sizeof (Test));
27     if (ret)
28     {
29         ret->table = FcHashTableCreate ((FcHashFunc) FcStrHashIgnoreCase,
30                                         (FcCompareFunc) FcStrCmp,
31                                         FcHashStrCopy,
32                                         FcHashUuidCopy,
33                                         (FcDestroyFunc) FcStrFree,
34                                         FcHashUuidFree);
35     }
36
37     return ret;
38 }
39
40 static void
41 fini (Test *test)
42 {
43     FcHashTableDestroy (test->table);
44     free (test);
45 }
46
47 static FcBool
48 test_add (Test *test, FcChar8 *key, FcBool replace)
49 {
50     uuid_t uuid;
51     void *u;
52     FcBool (*hash_add) (FcHashTable *, void *, void *);
53     FcBool ret = FcFalse;
54
55     uuid_generate_random (uuid);
56     if (replace)
57         hash_add = FcHashTableReplace;
58     else
59         hash_add = FcHashTableAdd;
60     if (!hash_add (test->table, key, uuid))
61         return FcFalse;
62     if (!FcHashTableFind (test->table, key, &u))
63         return FcFalse;
64     ret = (uuid_compare (uuid, u) == 0);
65     FcHashUuidFree (u);
66
67     return ret;
68 }
69
70 static FcBool
71 test_remove (Test *test, FcChar8 *key)
72 {
73     void *u;
74
75     if (!FcHashTableFind (test->table, key, &u))
76         return FcFalse;
77     FcHashUuidFree (u);
78     if (!FcHashTableRemove (test->table, key))
79         return FcFalse;
80     if (FcHashTableFind (test->table, key, &u))
81         return FcFalse;
82
83     return FcTrue;
84 }
85
86 int
87 main (void)
88 {
89     Test *test;
90     uuid_t uuid;
91     int ret = 0;
92
93     test = init ();
94     /* first op to add */
95     if (!test_add (test, "foo", FcFalse))
96     {
97         ret = 1;
98         goto bail;
99     }
100     /* second op to add */
101     if (!test_add (test, "bar", FcFalse))
102     {
103         ret = 1;
104         goto bail;
105     }
106     /* dup not allowed */
107     if (test_add (test, "foo", FcFalse))
108     {
109         ret = 1;
110         goto bail;
111     }
112     /* replacement */
113     if (!test_add (test, "foo", FcTrue))
114     {
115         ret = 1;
116         goto bail;
117     }
118     /* removal */
119     if (!test_remove (test, "foo"))
120     {
121         ret = 1;
122         goto bail;
123     }
124     /* not found to remove */
125     if (test_remove (test, "foo"))
126     {
127         ret = 1;
128         goto bail;
129     }
130     /* complex op in pointer */
131     if (!test_add (test, "foo", FcFalse))
132     {
133         ret = 1;
134         goto bail;
135     }
136     if (test_add (test, "foo", FcFalse))
137     {
138         ret = 1;
139         goto bail;
140     }
141     if (!test_remove (test, "foo"))
142     {
143         ret = 1;
144         goto bail;
145     }
146     if (!test_add (test, "foo", FcFalse))
147     {
148         ret = 1;
149         goto bail;
150     }
151     if (!test_remove (test, "bar"))
152     {
153         ret = 1;
154         goto bail;
155     }
156     /* completely remove */
157     if (!test_remove (test, "foo"))
158     {
159         ret = 1;
160         goto bail;
161     }
162     /* completely remove from the last one */
163     if (!test_add (test, "foo", FcFalse))
164     {
165         ret = 1;
166         goto bail;
167     }
168     if (!test_add (test, "bar", FcFalse))
169     {
170         ret = 1;
171         goto bail;
172     }
173     if (!test_remove (test, "bar"))
174     {
175         ret = 1;
176         goto bail;
177     }
178     if (!test_remove (test, "foo"))
179     {
180         ret = 1;
181         goto bail;
182     }
183 bail:
184     fini (test);
185
186     return ret;
187 }