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
33static apr_array_header_t *a1 = NULL;
34static apr_table_t *t1 = NULL;
35
36static 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
45static void table_make(abts_case *tc, void *data)
46{
47    t1 = apr_table_make(p, 5);
48    ABTS_PTR_NOTNULL(tc, t1);
49}
50
51static 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
60static void table_set(abts_case *tc, void *data)
61{
62    const char *val;
63
64    apr_table_set(t1, "setkey", "bar");
65    apr_table_set(t1, "setkey", "2ndtry");
66    val = apr_table_get(t1, "setkey");
67    ABTS_STR_EQUAL(tc, "2ndtry", val);
68}
69
70static void table_getnotthere(abts_case *tc, void *data)
71{
72    const char *val;
73
74    val = apr_table_get(t1, "keynotthere");
75    ABTS_PTR_EQUAL(tc, NULL, (void *)val);
76}
77
78static void table_add(abts_case *tc, void *data)
79{
80    const char *val;
81
82    apr_table_add(t1, "addkey", "bar");
83    apr_table_add(t1, "addkey", "foo");
84    val = apr_table_get(t1, "addkey");
85    ABTS_STR_EQUAL(tc, "bar", val);
86
87}
88
89static void table_nelts(abts_case *tc, void *data)
90{
91    const char *val;
92    apr_table_t *t = apr_table_make(p, 1);
93
94    apr_table_set(t, "abc", "def");
95    apr_table_set(t, "def", "abc");
96    apr_table_set(t, "foo", "zzz");
97    val = apr_table_get(t, "foo");
98    ABTS_STR_EQUAL(tc, "zzz", val);
99    val = apr_table_get(t, "abc");
100    ABTS_STR_EQUAL(tc, "def", val);
101    val = apr_table_get(t, "def");
102    ABTS_STR_EQUAL(tc, "abc", val);
103    ABTS_INT_EQUAL(tc, 3, apr_table_elts(t)->nelts);
104}
105
106static void table_clear(abts_case *tc, void *data)
107{
108    apr_table_clear(t1);
109    ABTS_INT_EQUAL(tc, 0, apr_table_elts(t1)->nelts);
110}
111
112static void table_unset(abts_case *tc, void *data)
113{
114    const char *val;
115    apr_table_t *t = apr_table_make(p, 1);
116
117    apr_table_set(t, "a", "1");
118    apr_table_set(t, "b", "2");
119    apr_table_unset(t, "b");
120    ABTS_INT_EQUAL(tc, 1, apr_table_elts(t)->nelts);
121    val = apr_table_get(t, "a");
122    ABTS_STR_EQUAL(tc, "1", val);
123    val = apr_table_get(t, "b");
124    ABTS_PTR_EQUAL(tc, (void *)NULL, (void *)val);
125}
126
127static void table_overlap(abts_case *tc, void *data)
128{
129    const char *val;
130    apr_table_t *t1 = apr_table_make(p, 1);
131    apr_table_t *t2 = apr_table_make(p, 1);
132
133    apr_table_addn(t1, "a", "0");
134    apr_table_addn(t1, "g", "7");
135    apr_table_addn(t2, "a", "1");
136    apr_table_addn(t2, "b", "2");
137    apr_table_addn(t2, "c", "3");
138    apr_table_addn(t2, "b", "2.0");
139    apr_table_addn(t2, "d", "4");
140    apr_table_addn(t2, "e", "5");
141    apr_table_addn(t2, "b", "2.");
142    apr_table_addn(t2, "f", "6");
143    apr_table_overlap(t1, t2, APR_OVERLAP_TABLES_SET);
144
145    ABTS_INT_EQUAL(tc, 7, apr_table_elts(t1)->nelts);
146    val = apr_table_get(t1, "a");
147    ABTS_STR_EQUAL(tc, "1", val);
148    val = apr_table_get(t1, "b");
149    ABTS_STR_EQUAL(tc, "2.", val);
150    val = apr_table_get(t1, "c");
151    ABTS_STR_EQUAL(tc, "3", val);
152    val = apr_table_get(t1, "d");
153    ABTS_STR_EQUAL(tc, "4", val);
154    val = apr_table_get(t1, "e");
155    ABTS_STR_EQUAL(tc, "5", val);
156    val = apr_table_get(t1, "f");
157    ABTS_STR_EQUAL(tc, "6", val);
158    val = apr_table_get(t1, "g");
159    ABTS_STR_EQUAL(tc, "7", val);
160}
161
162static void table_overlap2(abts_case *tc, void *data)
163{
164    apr_pool_t *subp;
165    apr_table_t *t1, *t2;
166
167    apr_pool_create(&subp, p);
168
169    t1 = apr_table_make(subp, 1);
170    t2 = apr_table_make(p, 1);
171    apr_table_addn(t1, "t1", "one");
172    apr_table_addn(t2, "t2", "two");
173
174    apr_table_overlap(t1, t2, APR_OVERLAP_TABLES_SET);
175
176    ABTS_INT_EQUAL(tc, 2, apr_table_elts(t1)->nelts);
177
178    ABTS_STR_EQUAL(tc, "one", apr_table_get(t1, "t1"));
179    ABTS_STR_EQUAL(tc, "two", apr_table_get(t1, "t2"));
180
181}
182
183abts_suite *testtable(abts_suite *suite)
184{
185    suite = ADD_SUITE(suite)
186
187    abts_run_test(suite, array_clear, NULL);
188    abts_run_test(suite, table_make, NULL);
189    abts_run_test(suite, table_get, NULL);
190    abts_run_test(suite, table_set, NULL);
191    abts_run_test(suite, table_getnotthere, NULL);
192    abts_run_test(suite, table_add, NULL);
193    abts_run_test(suite, table_nelts, NULL);
194    abts_run_test(suite, table_clear, NULL);
195    abts_run_test(suite, table_unset, NULL);
196    abts_run_test(suite, table_overlap, NULL);
197    abts_run_test(suite, table_overlap2, NULL);
198
199    return suite;
200}
201
202