VirtualBox

source: vbox/trunk/src/VBox/Runtime/testcase/tstRTStrAlloc.cpp@ 98102

Last change on this file since 98102 was 96407, checked in by vboxsync, 2 years ago

scm copyright and license note update

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 8.4 KB
Line 
1/* $Id: tstRTStrAlloc.cpp 96407 2022-08-22 17:43:14Z vboxsync $ */
2/** @file
3 * IPRT Testcase - String allocation APIs and related manipulators.
4 */
5
6/*
7 * Copyright (C) 2010-2022 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * The contents of this file may alternatively be used under the terms
26 * of the Common Development and Distribution License Version 1.0
27 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
28 * in the VirtualBox distribution, in which case the provisions of the
29 * CDDL are applicable instead of those of the GPL.
30 *
31 * You may elect to license modified versions of this file under the
32 * terms and conditions of either the GPL or the CDDL or both.
33 *
34 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
35 */
36
37
38/*********************************************************************************************************************************
39* Header Files *
40*********************************************************************************************************************************/
41#include <iprt/string.h>
42
43#include <iprt/asm.h>
44#include <iprt/errcore.h>
45#include <iprt/test.h>
46
47
48/**
49 * Basic API checks.
50 * We'll return if any of these fails.
51 */
52static void tst1(void)
53{
54 RTTestISub("Basics");
55 char *psz;
56 int rc = VINF_SUCCESS;
57
58 /* RTStrAlloc */
59 RTTESTI_CHECK(psz = RTStrAlloc(0));
60 RTTESTI_CHECK(psz && !*psz);
61 RTStrFree(psz);
62
63 RTTESTI_CHECK(psz = RTStrAlloc(1));
64 RTTESTI_CHECK(psz && !*psz);
65 RTStrFree(psz);
66
67 RTTESTI_CHECK(psz = RTStrAlloc(128));
68 RTTESTI_CHECK(psz && !*psz);
69 RTStrFree(psz);
70
71 /* RTStrAllocEx */
72 psz = (char*)"asdfasdf";
73 RTTESTI_CHECK_RC(RTStrAllocEx(&psz, 0), VINF_SUCCESS);
74 RTTESTI_CHECK(psz && !*psz);
75 RTStrFree(psz);
76
77 RTTESTI_CHECK_RC(RTStrAllocEx(&psz, 1), VINF_SUCCESS);
78 RTTESTI_CHECK(psz && !*psz);
79 RTStrFree(psz);
80
81 RTTESTI_CHECK_RC(RTStrAllocEx(&psz, 128), VINF_SUCCESS);
82 RTTESTI_CHECK(psz && !*psz);
83 RTStrFree(psz);
84
85 /* RTStrRealloc */
86 psz = NULL;
87 RTTESTI_CHECK_RC(RTStrRealloc(&psz, 10), VINF_SUCCESS);
88 RTTESTI_CHECK(psz && !psz[0]);
89 RTTESTI_CHECK(psz && !psz[9]);
90 RTStrFree(psz);
91
92 psz = NULL;
93 RTTESTI_CHECK_RC(RTStrRealloc(&psz, 0), VINF_SUCCESS);
94 RTTESTI_CHECK(!psz);
95
96 psz = NULL;
97 RTTESTI_CHECK_RC(RTStrRealloc(&psz, 128), VINF_SUCCESS);
98 RTTESTI_CHECK(psz && !psz[0]);
99 RTTESTI_CHECK(psz && !psz[127]);
100 if (psz)
101 {
102 memset(psz, 'a', 127);
103 RTTESTI_CHECK_RC(rc = RTStrRealloc(&psz, 160), VINF_SUCCESS);
104 if (RT_SUCCESS(rc) && psz)
105 {
106 RTTESTI_CHECK(!psz[127]);
107 RTTESTI_CHECK(!psz[159]);
108 RTTESTI_CHECK(ASMMemIsAllU8(psz, 127, 'a'));
109 memset(psz, 'b', 159);
110
111 RTTESTI_CHECK_RC(rc = RTStrRealloc(&psz, 79), VINF_SUCCESS);
112 if (RT_SUCCESS(rc))
113 {
114 RTTESTI_CHECK(!psz[78]);
115 RTTESTI_CHECK(ASMMemIsAllU8(psz, 78, 'b'));
116
117 RTTESTI_CHECK_RC(rc = RTStrRealloc(&psz, 0), VINF_SUCCESS);
118 RTTESTI_CHECK(!psz);
119 }
120 }
121 }
122 RTStrFree(psz);
123
124 /* RTStrDup */
125 RTTESTI_CHECK(psz = RTStrDup(""));
126 RTTESTI_CHECK(psz && *psz == '\0');
127 RTStrFree(psz);
128
129 RTTESTI_CHECK(psz = RTStrDup("abcdefghijklmnop"));
130 RTTESTI_CHECK(!RTStrCmp(psz, "abcdefghijklmnop"));
131 RTStrFree(psz);
132
133 /* RTStrDupEx */
134 psz = NULL;
135 RTTESTI_CHECK_RC(RTStrDupEx(&psz, ""), VINF_SUCCESS);
136 RTTESTI_CHECK(RT_FAILURE(rc) || *psz == '\0');
137 if (RT_SUCCESS(rc))
138 RTStrFree(psz);
139
140 psz = (char*)"asdfasdfasdfasdf";
141 RTTESTI_CHECK_RC(rc = RTStrDupEx(&psz, "abcdefghijklmnop"), VINF_SUCCESS);
142 RTTESTI_CHECK(RT_FAILURE(rc) || !RTStrCmp(psz, "abcdefghijklmnop"));
143 if (RT_SUCCESS(rc))
144 RTStrFree(psz);
145
146 /* RTStrDupN */
147 RTTESTI_CHECK(psz = RTStrDupN("abcdefg", 3));
148 RTTESTI_CHECK(!RTStrCmp(psz, "abc"));
149 RTStrFree(psz);
150
151 RTTESTI_CHECK(psz = RTStrDupN("abc", 100000));
152 RTTESTI_CHECK(!RTStrCmp(psz, "abc"));
153 RTStrFree(psz);
154
155 RTTESTI_CHECK(psz = RTStrDupN("abc", 0));
156 RTTESTI_CHECK(psz && *psz == '\0');
157 RTStrFree(psz);
158
159 /* RTStrAAppend */
160 RTTESTI_CHECK(psz = RTStrDup("abc"));
161 RTTESTI_CHECK_RC(RTStrAAppend(&psz, "def"), VINF_SUCCESS);
162 RTTESTI_CHECK(!RTStrCmp(psz, "abcdef"));
163 RTStrFree(psz);
164
165 RTTESTI_CHECK(psz = RTStrDup("abc"));
166 RTTESTI_CHECK_RC(RTStrAAppend(&psz, ""), VINF_SUCCESS);
167 RTTESTI_CHECK(!RTStrCmp(psz, "abc"));
168 RTTESTI_CHECK_RC(RTStrAAppend(&psz, NULL), VINF_SUCCESS);
169 RTTESTI_CHECK(!RTStrCmp(psz, "abc"));
170 RTStrFree(psz);
171
172 psz = NULL;
173 RTTESTI_CHECK_RC(RTStrAAppend(&psz, "xyz"), VINF_SUCCESS);
174 RTTESTI_CHECK(!RTStrCmp(psz, "xyz"));
175 RTStrFree(psz);
176
177 /* RTStrAAppendN */
178 RTTESTI_CHECK(psz = RTStrDup("abc"));
179 RTTESTI_CHECK_RC(RTStrAAppendN(&psz, "def", 1), VINF_SUCCESS);
180 RTTESTI_CHECK(!RTStrCmp(psz, "abcd"));
181 RTStrFree(psz);
182
183 RTTESTI_CHECK(psz = RTStrDup("abc"));
184 RTTESTI_CHECK_RC(RTStrAAppendN(&psz, "", 0), VINF_SUCCESS);
185 RTTESTI_CHECK(!RTStrCmp(psz, "abc"));
186 RTTESTI_CHECK_RC(RTStrAAppendN(&psz, "", RTSTR_MAX), VINF_SUCCESS);
187 RTTESTI_CHECK(!RTStrCmp(psz, "abc"));
188 RTTESTI_CHECK_RC(RTStrAAppendN(&psz, NULL, 0), VINF_SUCCESS);
189 RTTESTI_CHECK(!RTStrCmp(psz, "abc"));
190 RTStrFree(psz);
191
192 psz = NULL;
193 RTTESTI_CHECK_RC(RTStrAAppendN(&psz, "abc", 2), VINF_SUCCESS);
194 RTTESTI_CHECK(!RTStrCmp(psz, "ab"));
195 RTTESTI_CHECK_RC(RTStrAAppendN(&psz, "cdefghijklm", 1), VINF_SUCCESS);
196 RTTESTI_CHECK(!RTStrCmp(psz, "abc"));
197 RTTESTI_CHECK_RC(RTStrAAppendN(&psz, "defghijklm", RTSTR_MAX), VINF_SUCCESS);
198 RTTESTI_CHECK(!RTStrCmp(psz, "abcdefghijklm"));
199 RTStrFree(psz);
200
201 /* RTStrAAppendExN / RTStrAAppendExNV */
202 psz = NULL;
203 RTTESTI_CHECK_RC(RTStrAAppendExN(&psz, 5, "a", (size_t)1, "bc", (size_t)1, "cdefg", RTSTR_MAX, "hijkl", (size_t)2, "jklmnopqrstuvwxyz", RTSTR_MAX), VINF_SUCCESS);
204 RTTESTI_CHECK(!RTStrCmp(psz, "abcdefghijklmnopqrstuvwxyz"));
205 RTTESTI_CHECK_RC(RTStrAAppendExN(&psz, 0), VINF_SUCCESS);
206 RTTESTI_CHECK(!RTStrCmp(psz, "abcdefghijklmnopqrstuvwxyz"));
207 RTTESTI_CHECK_RC(RTStrAAppendExN(&psz, 2, NULL, (size_t)0, "", (size_t)0), VINF_SUCCESS);
208 RTTESTI_CHECK(!RTStrCmp(psz, "abcdefghijklmnopqrstuvwxyz"));
209 RTTESTI_CHECK_RC(RTStrAAppendExN(&psz, 1, "-", (size_t)1), VINF_SUCCESS);
210 RTTESTI_CHECK(!RTStrCmp(psz, "abcdefghijklmnopqrstuvwxyz-"));
211 RTStrFree(psz);
212
213 /* RTStrATruncate */
214 psz = NULL;
215 RTTESTI_CHECK_RC(RTStrATruncate(&psz, 0), VINF_SUCCESS);
216 RTTESTI_CHECK(!psz);
217
218 RTTESTI_CHECK(psz = RTStrDup(""));
219 RTTESTI_CHECK_RC(RTStrATruncate(&psz, 0), VINF_SUCCESS);
220 RTStrFree(psz);
221
222 RTTESTI_CHECK(psz = RTStrDup("1234567890"));
223 RTTESTI_CHECK_RC(RTStrATruncate(&psz, 5), VINF_SUCCESS);
224 RTTESTI_CHECK(!RTStrCmp(psz, "12345"));
225 RTStrFree(psz);
226
227 psz = NULL;
228 for (uint32_t i = 0; i < 128; i++)
229 RTTESTI_CHECK_RC_RETV(RTStrAAppend(&psz, "abcdefghijklmnopqrstuvwxyz"), VINF_SUCCESS);
230 RTTESTI_CHECK_RC(RTStrATruncate(&psz, sizeof("abcdefghijklmnopqrstuvwxyz") - 1), VINF_SUCCESS);
231 RTTESTI_CHECK(!RTStrCmp(psz, "abcdefghijklmnopqrstuvwxyz"));
232 RTTESTI_CHECK_RC(RTStrATruncate(&psz, 6), VINF_SUCCESS);
233 RTTESTI_CHECK(!RTStrCmp(psz, "abcdef"));
234 RTTESTI_CHECK_RC(RTStrATruncate(&psz, 1), VINF_SUCCESS);
235 RTTESTI_CHECK(!RTStrCmp(psz, "a"));
236 RTTESTI_CHECK_RC(RTStrATruncate(&psz, 0), VINF_SUCCESS);
237 RTTESTI_CHECK(!RTStrCmp(psz, ""));
238 RTStrFree(psz);
239
240}
241
242
243int main()
244{
245 RTTEST hTest;
246 int rc = RTTestInitAndCreate("tstRTStrAlloc", &hTest);
247 if (rc)
248 return rc;
249 RTTestBanner(hTest);
250
251 tst1();
252
253 return RTTestSummaryAndDestroy(hTest);
254}
255
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette