xref: /aosp_15_r20/external/cronet/third_party/apache-portable-runtime/src/test/testtable.c (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2  * contributor license agreements.  See the NOTICE file distributed with
3  * this work for additional information regarding copyright ownership.
4  * The ASF licenses this file to You under the Apache License, Version 2.0
5  * (the "License"); you may not use this file except in compliance with
6  * the License.  You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "testutil.h"
18 #include "apr.h"
19 #include "apr_strings.h"
20 #include "apr_general.h"
21 #include "apr_pools.h"
22 #include "apr_tables.h"
23 #if APR_HAVE_STDIO_H
24 #include <stdio.h>
25 #endif
26 #if APR_HAVE_STDLIB_H
27 #include <stdlib.h>
28 #endif
29 #if APR_HAVE_STRING_H
30 #include <string.h>
31 #endif
32 
33 static apr_array_header_t *a1 = NULL;
34 static apr_table_t *t1 = NULL;
35 
array_clear(abts_case * tc,void * data)36 static void array_clear(abts_case *tc, void *data)
37 {
38     a1 = apr_array_make(p, 2, sizeof(const char *));
39     APR_ARRAY_PUSH(a1, const char *) = "foo";
40     APR_ARRAY_PUSH(a1, const char *) = "bar";
41     apr_array_clear(a1);
42     ABTS_INT_EQUAL(tc, 0, a1->nelts);
43 }
44 
table_make(abts_case * tc,void * data)45 static void table_make(abts_case *tc, void *data)
46 {
47     t1 = apr_table_make(p, 5);
48     ABTS_PTR_NOTNULL(tc, t1);
49 }
50 
table_get(abts_case * tc,void * data)51 static void table_get(abts_case *tc, void *data)
52 {
53     const char *val;
54 
55     apr_table_set(t1, "foo", "bar");
56     val = apr_table_get(t1, "foo");
57     ABTS_STR_EQUAL(tc, "bar", val);
58 }
59 
table_getm(abts_case * tc,void * data)60 static void table_getm(abts_case *tc, void *data)
61 {
62     const char *orig, *val;
63     apr_pool_t *subp;
64 
65     apr_pool_create(&subp, p);
66 
67     orig = "bar";
68     apr_table_setn(t1, "foo", orig);
69     val = apr_table_getm(subp, t1, "foo");
70     ABTS_PTR_EQUAL(tc, orig, val);
71     ABTS_STR_EQUAL(tc, "bar", val);
72     apr_table_add(t1, "foo", "baz");
73     val = apr_table_getm(subp, t1, "foo");
74     ABTS_STR_EQUAL(tc, "bar,baz", val);
75 
76     apr_pool_destroy(subp);
77 }
78 
table_set(abts_case * tc,void * data)79 static void table_set(abts_case *tc, void *data)
80 {
81     const char *val;
82 
83     apr_table_set(t1, "setkey", "bar");
84     apr_table_set(t1, "setkey", "2ndtry");
85     val = apr_table_get(t1, "setkey");
86     ABTS_STR_EQUAL(tc, "2ndtry", val);
87 }
88 
table_getnotthere(abts_case * tc,void * data)89 static void table_getnotthere(abts_case *tc, void *data)
90 {
91     const char *val;
92 
93     val = apr_table_get(t1, "keynotthere");
94     ABTS_PTR_EQUAL(tc, NULL, (void *)val);
95 }
96 
table_add(abts_case * tc,void * data)97 static void table_add(abts_case *tc, void *data)
98 {
99     const char *val;
100 
101     apr_table_add(t1, "addkey", "bar");
102     apr_table_add(t1, "addkey", "foo");
103     val = apr_table_get(t1, "addkey");
104     ABTS_STR_EQUAL(tc, "bar", val);
105 
106 }
107 
table_nelts(abts_case * tc,void * data)108 static void table_nelts(abts_case *tc, void *data)
109 {
110     const char *val;
111     apr_table_t *t = apr_table_make(p, 1);
112 
113     apr_table_set(t, "abc", "def");
114     apr_table_set(t, "def", "abc");
115     apr_table_set(t, "foo", "zzz");
116     val = apr_table_get(t, "foo");
117     ABTS_STR_EQUAL(tc, "zzz", val);
118     val = apr_table_get(t, "abc");
119     ABTS_STR_EQUAL(tc, "def", val);
120     val = apr_table_get(t, "def");
121     ABTS_STR_EQUAL(tc, "abc", val);
122     ABTS_INT_EQUAL(tc, 3, apr_table_elts(t)->nelts);
123 }
124 
table_clear(abts_case * tc,void * data)125 static void table_clear(abts_case *tc, void *data)
126 {
127     apr_table_clear(t1);
128     ABTS_INT_EQUAL(tc, 0, apr_table_elts(t1)->nelts);
129 }
130 
table_unset(abts_case * tc,void * data)131 static void table_unset(abts_case *tc, void *data)
132 {
133     const char *val;
134     apr_table_t *t = apr_table_make(p, 1);
135 
136     apr_table_set(t, "a", "1");
137     apr_table_set(t, "b", "2");
138     apr_table_unset(t, "b");
139     ABTS_INT_EQUAL(tc, 1, apr_table_elts(t)->nelts);
140     val = apr_table_get(t, "a");
141     ABTS_STR_EQUAL(tc, "1", val);
142     val = apr_table_get(t, "b");
143     ABTS_PTR_EQUAL(tc, (void *)NULL, (void *)val);
144 }
145 
table_overlap(abts_case * tc,void * data)146 static void table_overlap(abts_case *tc, void *data)
147 {
148     const char *val;
149     apr_table_t *t1 = apr_table_make(p, 1);
150     apr_table_t *t2 = apr_table_make(p, 1);
151 
152     apr_table_addn(t1, "a", "0");
153     apr_table_addn(t1, "g", "7");
154     apr_table_addn(t2, "a", "1");
155     apr_table_addn(t2, "b", "2");
156     apr_table_addn(t2, "c", "3");
157     apr_table_addn(t2, "b", "2.0");
158     apr_table_addn(t2, "d", "4");
159     apr_table_addn(t2, "e", "5");
160     apr_table_addn(t2, "b", "2.");
161     apr_table_addn(t2, "f", "6");
162     apr_table_overlap(t1, t2, APR_OVERLAP_TABLES_SET);
163 
164     ABTS_INT_EQUAL(tc, 7, apr_table_elts(t1)->nelts);
165     val = apr_table_get(t1, "a");
166     ABTS_STR_EQUAL(tc, "1", val);
167     val = apr_table_get(t1, "b");
168     ABTS_STR_EQUAL(tc, "2.", val);
169     val = apr_table_get(t1, "c");
170     ABTS_STR_EQUAL(tc, "3", val);
171     val = apr_table_get(t1, "d");
172     ABTS_STR_EQUAL(tc, "4", val);
173     val = apr_table_get(t1, "e");
174     ABTS_STR_EQUAL(tc, "5", val);
175     val = apr_table_get(t1, "f");
176     ABTS_STR_EQUAL(tc, "6", val);
177     val = apr_table_get(t1, "g");
178     ABTS_STR_EQUAL(tc, "7", val);
179 }
180 
table_overlap2(abts_case * tc,void * data)181 static void table_overlap2(abts_case *tc, void *data)
182 {
183     apr_pool_t *subp;
184     apr_table_t *t1, *t2;
185 
186     apr_pool_create(&subp, p);
187 
188     t1 = apr_table_make(subp, 1);
189     t2 = apr_table_make(p, 1);
190     apr_table_addn(t1, "t1", "one");
191     apr_table_addn(t2, "t2", "two");
192 
193     apr_table_overlap(t1, t2, APR_OVERLAP_TABLES_SET);
194 
195     ABTS_INT_EQUAL(tc, 2, apr_table_elts(t1)->nelts);
196 
197     ABTS_STR_EQUAL(tc, "one", apr_table_get(t1, "t1"));
198     ABTS_STR_EQUAL(tc, "two", apr_table_get(t1, "t2"));
199 
200 }
201 
testtable(abts_suite * suite)202 abts_suite *testtable(abts_suite *suite)
203 {
204     suite = ADD_SUITE(suite)
205 
206     abts_run_test(suite, array_clear, NULL);
207     abts_run_test(suite, table_make, NULL);
208     abts_run_test(suite, table_get, NULL);
209     abts_run_test(suite, table_getm, NULL);
210     abts_run_test(suite, table_set, NULL);
211     abts_run_test(suite, table_getnotthere, NULL);
212     abts_run_test(suite, table_add, NULL);
213     abts_run_test(suite, table_nelts, NULL);
214     abts_run_test(suite, table_clear, NULL);
215     abts_run_test(suite, table_unset, NULL);
216     abts_run_test(suite, table_overlap, NULL);
217     abts_run_test(suite, table_overlap2, NULL);
218 
219     return suite;
220 }
221 
222