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_portable.h"
20#include "apr_strings.h"
21
22static void ssize_t_fmt(abts_case *tc, void *data)
23{
24    char buf[100];
25    apr_ssize_t var = 0;
26
27    sprintf(buf, "%" APR_SSIZE_T_FMT, var);
28    ABTS_STR_EQUAL(tc, "0", buf);
29    apr_snprintf(buf, sizeof(buf), "%" APR_SSIZE_T_FMT, var);
30    ABTS_STR_EQUAL(tc, "0", buf);
31}
32
33static void size_t_fmt(abts_case *tc, void *data)
34{
35    char buf[100];
36    apr_size_t var = 0;
37
38    sprintf(buf, "%" APR_SIZE_T_FMT, var);
39    ABTS_STR_EQUAL(tc, "0", buf);
40    apr_snprintf(buf, sizeof(buf), "%" APR_SIZE_T_FMT, var);
41    ABTS_STR_EQUAL(tc, "0", buf);
42}
43
44static void off_t_fmt(abts_case *tc, void *data)
45{
46    char buf[100];
47    apr_off_t var = 0;
48
49    sprintf(buf, "%" APR_OFF_T_FMT, var);
50    ABTS_STR_EQUAL(tc, "0", buf);
51    apr_snprintf(buf, sizeof(buf), "%" APR_OFF_T_FMT, var);
52    ABTS_STR_EQUAL(tc, "0", buf);
53}
54
55static void pid_t_fmt(abts_case *tc, void *data)
56{
57    char buf[100];
58    pid_t var = 0;
59
60    sprintf(buf, "%" APR_PID_T_FMT, var);
61    ABTS_STR_EQUAL(tc, "0", buf);
62    apr_snprintf(buf, sizeof(buf), "%" APR_PID_T_FMT, var);
63    ABTS_STR_EQUAL(tc, "0", buf);
64}
65
66static void int64_t_fmt(abts_case *tc, void *data)
67{
68    char buf[100];
69    apr_int64_t var = 0;
70
71    sprintf(buf, "%" APR_INT64_T_FMT, var);
72    ABTS_STR_EQUAL(tc, "0", buf);
73    apr_snprintf(buf, sizeof(buf), "%" APR_INT64_T_FMT, var);
74    ABTS_STR_EQUAL(tc, "0", buf);
75}
76
77static void uint64_t_fmt(abts_case *tc, void *data)
78{
79    char buf[100];
80    apr_uint64_t var = APR_UINT64_C(14000000);
81
82    sprintf(buf, "%" APR_UINT64_T_FMT, var);
83    ABTS_STR_EQUAL(tc, "14000000", buf);
84    apr_snprintf(buf, sizeof(buf), "%" APR_UINT64_T_FMT, var);
85    ABTS_STR_EQUAL(tc, "14000000", buf);
86}
87
88static void uint64_t_hex_fmt(abts_case *tc, void *data)
89{
90    char buf[100];
91    apr_uint64_t var = APR_UINT64_C(14000000);
92
93    sprintf(buf, "%" APR_UINT64_T_HEX_FMT, var);
94    ABTS_STR_EQUAL(tc, "d59f80", buf);
95    apr_snprintf(buf, sizeof(buf), "%" APR_UINT64_T_HEX_FMT, var);
96    ABTS_STR_EQUAL(tc, "d59f80", buf);
97}
98
99static void more_int64_fmts(abts_case *tc, void *data)
100{
101    char buf[100];
102    apr_int64_t i = APR_INT64_C(-42);
103    apr_int64_t ibig = APR_INT64_C(-314159265358979323);
104    apr_uint64_t ui = APR_UINT64_C(42);
105    apr_uint64_t big = APR_UINT64_C(10267677267010969076);
106
107    apr_snprintf(buf, sizeof buf, "%" APR_INT64_T_FMT, i);
108    ABTS_STR_EQUAL(tc, "-42", buf);
109
110    apr_snprintf(buf, sizeof buf, "%" APR_UINT64_T_FMT, ui);
111    ABTS_STR_EQUAL(tc, "42", buf);
112
113    apr_snprintf(buf, sizeof buf, "%" APR_UINT64_T_FMT, big);
114    ABTS_STR_EQUAL(tc, "10267677267010969076", buf);
115
116    apr_snprintf(buf, sizeof buf, "%" APR_INT64_T_FMT, ibig);
117    ABTS_STR_EQUAL(tc, "-314159265358979323", buf);
118}
119
120static void error_fmt(abts_case *tc, void *data)
121{
122    char ebuf[150], sbuf[150], *s;
123    apr_status_t rv;
124
125    rv = APR_SUCCESS;
126    apr_strerror(rv, ebuf, sizeof ebuf);
127    apr_snprintf(sbuf, sizeof sbuf, "%pm", &rv);
128    ABTS_STR_EQUAL(tc, sbuf, ebuf);
129
130    rv = APR_ENOTIMPL;
131    s = apr_pstrcat(p, "foo-",
132                    apr_strerror(rv, ebuf, sizeof ebuf),
133                    "-bar", NULL);
134    apr_snprintf(sbuf, sizeof sbuf, "foo-%pm-bar", &rv);
135    ABTS_STR_EQUAL(tc, sbuf, s);
136}
137
138abts_suite *testfmt(abts_suite *suite)
139{
140    suite = ADD_SUITE(suite)
141
142    abts_run_test(suite, ssize_t_fmt, NULL);
143    abts_run_test(suite, size_t_fmt, NULL);
144    abts_run_test(suite, off_t_fmt, NULL);
145    abts_run_test(suite, pid_t_fmt, NULL);
146    abts_run_test(suite, int64_t_fmt, NULL);
147    abts_run_test(suite, uint64_t_fmt, NULL);
148    abts_run_test(suite, uint64_t_hex_fmt, NULL);
149    abts_run_test(suite, more_int64_fmts, NULL);
150    abts_run_test(suite, error_fmt, NULL);
151
152    return suite;
153}
154
155