Fix memory leak when maintaining resources. When extra resources are
[apr-util.git] / test / testxlate.c
blobcfc4d49190aac37d68c304eec15c8dc6a1346c3b
1 /* Copyright 2000-2004 The Apache Software Foundation
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
16 #include <stdio.h>
17 #include <stdlib.h>
19 #include "apr.h"
20 #include "apr_errno.h"
21 #include "apr_general.h"
22 #include "apr_strings.h"
23 #include "apr_xlate.h"
25 static const char test_utf8[] = "Edelwei\xc3\x9f";
26 static const char test_utf7[] = "Edelwei+AN8-";
27 static const char test_latin1[] = "Edelwei\xdf";
28 static const char test_latin2[] = "Edelwei\xdf";
31 static int check_status (apr_status_t status, const char *msg)
33 if (status)
35 static char buf[1024];
36 printf("ERROR: %s\n %s\n", msg,
37 apr_strerror(status, buf, sizeof(buf)));
38 return 1;
40 return 0;
43 static int test_conversion (apr_xlate_t *convset,
44 const char *inbuf,
45 const char *expected)
47 static char buf[1024];
48 int retcode = 0;
49 apr_size_t inbytes_left = strlen(inbuf) + 1;
50 apr_size_t outbytes_left = sizeof(buf) - 1;
51 apr_status_t status = apr_xlate_conv_buffer(convset,
52 inbuf,
53 &inbytes_left,
54 buf,
55 &outbytes_left);
56 retcode |= check_status(status, "apr_xlate_conv_buffer");
57 if ((!status || APR_STATUS_IS_INCOMPLETE(status))
58 && strcmp(buf, expected))
60 printf("ERROR: expected: '%s'\n actual: '%s'"
61 "\n inbytes_left: %"APR_SIZE_T_FMT"\n",
62 expected, buf, inbytes_left);
63 retcode |= 1;
65 return retcode;
68 static int one_test (const char *cs1, const char *cs2,
69 const char *str1, const char *str2,
70 apr_pool_t *pool)
72 apr_xlate_t *convset;
73 const char *msg = apr_psprintf(pool, "apr_xlate_open(%s, %s)", cs2, cs1);
74 int retcode = check_status(apr_xlate_open(&convset, cs2, cs1, pool), msg);
75 if (!retcode)
77 retcode |= test_conversion(convset, str1, str2);
78 retcode |= check_status(apr_xlate_close(convset), "apr_xlate_close");
80 printf("%s: %s -> %s\n", (retcode ? "FAIL" : "PASS"), cs1, cs2);
81 return retcode;
85 int main (int argc, char **argv)
87 apr_pool_t *pool;
88 int retcode = 0;
90 #ifndef APR_HAS_XLATE
91 puts("SKIP: apr_xlate not implemented");
92 return 0;
93 #endif
95 apr_initialize();
96 atexit(apr_terminate);
97 apr_pool_create(&pool, NULL);
99 /* 1. Identity transformation: UTF-8 -> UTF-8 */
100 retcode |= one_test("UTF-8", "UTF-8", test_utf8, test_utf8, pool);
102 /* 2. UTF-8 <-> ISO-8859-1 */
103 retcode |= one_test("UTF-8", "ISO-8859-1", test_utf8, test_latin1, pool);
104 retcode |= one_test("ISO-8859-1", "UTF-8", test_latin1, test_utf8, pool);
106 /* 3. ISO-8859-1 <-> ISO-8859-2, identity */
107 retcode |= one_test("ISO-8859-1", "ISO-8859-2",
108 test_latin1, test_latin2, pool);
109 retcode |= one_test("ISO-8859-2", "ISO-8859-1",
110 test_latin2, test_latin1, pool);
112 /* 4. Transformation using charset aliases */
113 retcode |= one_test("UTF-8", "UTF-7", test_utf8, test_utf7, pool);
114 retcode |= one_test("UTF-7", "UTF-8", test_utf7, test_utf8, pool);
116 return retcode;