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