xref: /aosp_15_r20/external/cronet/third_party/apache-portable-runtime/src/test/testfmt.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_portable.h"
20 #include "apr_strings.h"
21 
ssize_t_fmt(abts_case * tc,void * data)22 static 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 
size_t_fmt(abts_case * tc,void * data)33 static 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 
time_t_fmt(abts_case * tc,void * data)44 static void time_t_fmt(abts_case *tc, void *data)
45 {
46     char buf[100];
47     apr_time_t var = 1;
48 
49     sprintf(buf, "%" APR_TIME_T_FMT, var);
50     ABTS_STR_EQUAL(tc, "1", buf);
51     apr_snprintf(buf, sizeof(buf), "%" APR_TIME_T_FMT, var);
52     ABTS_STR_EQUAL(tc, "1", buf);
53 }
54 
off_t_fmt(abts_case * tc,void * data)55 static void off_t_fmt(abts_case *tc, void *data)
56 {
57     char buf[100];
58     apr_off_t var = 0;
59 
60     sprintf(buf, "%" APR_OFF_T_FMT, var);
61     ABTS_STR_EQUAL(tc, "0", buf);
62     apr_snprintf(buf, sizeof(buf), "%" APR_OFF_T_FMT, var);
63     ABTS_STR_EQUAL(tc, "0", buf);
64 }
65 
pid_t_fmt(abts_case * tc,void * data)66 static void pid_t_fmt(abts_case *tc, void *data)
67 {
68     char buf[100];
69     pid_t var = 0;
70 
71     sprintf(buf, "%" APR_PID_T_FMT, var);
72     ABTS_STR_EQUAL(tc, "0", buf);
73     apr_snprintf(buf, sizeof(buf), "%" APR_PID_T_FMT, var);
74     ABTS_STR_EQUAL(tc, "0", buf);
75 }
76 
int64_t_fmt(abts_case * tc,void * data)77 static void int64_t_fmt(abts_case *tc, void *data)
78 {
79     char buf[100];
80     apr_int64_t var = 0;
81 
82     sprintf(buf, "%" APR_INT64_T_FMT, var);
83     ABTS_STR_EQUAL(tc, "0", buf);
84     apr_snprintf(buf, sizeof(buf), "%" APR_INT64_T_FMT, var);
85     ABTS_STR_EQUAL(tc, "0", buf);
86 }
87 
uint64_t_fmt(abts_case * tc,void * data)88 static void uint64_t_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_FMT, var);
94     ABTS_STR_EQUAL(tc, "14000000", buf);
95     apr_snprintf(buf, sizeof(buf), "%" APR_UINT64_T_FMT, var);
96     ABTS_STR_EQUAL(tc, "14000000", buf);
97 }
98 
uint64_t_hex_fmt(abts_case * tc,void * data)99 static void uint64_t_hex_fmt(abts_case *tc, void *data)
100 {
101     char buf[100];
102     apr_uint64_t var = APR_UINT64_C(14000000);
103 
104     sprintf(buf, "%" APR_UINT64_T_HEX_FMT, var);
105     ABTS_STR_EQUAL(tc, "d59f80", buf);
106     apr_snprintf(buf, sizeof(buf), "%" APR_UINT64_T_HEX_FMT, var);
107     ABTS_STR_EQUAL(tc, "d59f80", buf);
108 }
109 
more_int64_fmts(abts_case * tc,void * data)110 static void more_int64_fmts(abts_case *tc, void *data)
111 {
112     char buf[100];
113     apr_int64_t i = APR_INT64_C(-42);
114     apr_int64_t ibig = APR_INT64_C(-314159265358979323);
115     apr_uint64_t ui = APR_UINT64_C(42);
116     apr_uint64_t big = APR_UINT64_C(10267677267010969076);
117 
118     apr_snprintf(buf, sizeof buf, "%" APR_INT64_T_FMT, i);
119     ABTS_STR_EQUAL(tc, "-42", buf);
120 
121     apr_snprintf(buf, sizeof buf, "%" APR_UINT64_T_FMT, ui);
122     ABTS_STR_EQUAL(tc, "42", buf);
123 
124     apr_snprintf(buf, sizeof buf, "%" APR_UINT64_T_FMT, big);
125     ABTS_STR_EQUAL(tc, "10267677267010969076", buf);
126 
127     apr_snprintf(buf, sizeof buf, "%" APR_INT64_T_FMT, ibig);
128     ABTS_STR_EQUAL(tc, "-314159265358979323", buf);
129 }
130 
error_fmt(abts_case * tc,void * data)131 static void error_fmt(abts_case *tc, void *data)
132 {
133     char ebuf[150], sbuf[150], *s;
134     apr_status_t rv;
135 
136     rv = APR_SUCCESS;
137     apr_strerror(rv, ebuf, sizeof ebuf);
138     apr_snprintf(sbuf, sizeof sbuf, "%pm", &rv);
139     ABTS_STR_EQUAL(tc, sbuf, ebuf);
140 
141     rv = APR_ENOTIMPL;
142     s = apr_pstrcat(p, "foo-",
143                     apr_strerror(rv, ebuf, sizeof ebuf),
144                     "-bar", NULL);
145     apr_snprintf(sbuf, sizeof sbuf, "foo-%pm-bar", &rv);
146     ABTS_STR_EQUAL(tc, sbuf, s);
147 }
148 
testfmt(abts_suite * suite)149 abts_suite *testfmt(abts_suite *suite)
150 {
151     suite = ADD_SUITE(suite)
152 
153     abts_run_test(suite, ssize_t_fmt, NULL);
154     abts_run_test(suite, size_t_fmt, NULL);
155     abts_run_test(suite, time_t_fmt, NULL);
156     abts_run_test(suite, off_t_fmt, NULL);
157     abts_run_test(suite, pid_t_fmt, NULL);
158     abts_run_test(suite, int64_t_fmt, NULL);
159     abts_run_test(suite, uint64_t_fmt, NULL);
160     abts_run_test(suite, uint64_t_hex_fmt, NULL);
161     abts_run_test(suite, more_int64_fmts, NULL);
162     abts_run_test(suite, error_fmt, NULL);
163 
164     return suite;
165 }
166 
167