VirtualBox

source: vbox/trunk/src/VBox/Runtime/testcase/tstRTStrCatCopy.cpp@ 53428

Last change on this file since 53428 was 33678, checked in by vboxsync, 14 years ago

IPRT: Added RTStrCat and RTStrCatEx.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 8.9 KB
Line 
1/* $Id: tstRTStrCatCopy.cpp 33678 2010-11-02 10:30:46Z vboxsync $ */
2/** @file
3 * IPRT Testcase - String Concatenation and Copy.
4 */
5
6/*
7 * Copyright (C) 2010 Oracle Corporation
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
27/*******************************************************************************
28* Header Files *
29*******************************************************************************/
30#include <iprt/string.h>
31
32#include <iprt/test.h>
33
34
35static void testCopy1(RTTEST hTest)
36{
37 RTTestISub("RTStrCopy");
38
39 char *pszBuf4H = (char *)RTTestGuardedAllocHead(hTest, 4);
40 char *pszBuf4T = (char *)RTTestGuardedAllocTail(hTest, 4);
41 RTTESTI_CHECK_RC(RTStrCopy(pszBuf4H, 4, "abc"), VINF_SUCCESS);
42 RTTESTI_CHECK(strcmp(pszBuf4H, "abc") == 0);
43 RTTESTI_CHECK_RC(RTStrCopy(pszBuf4T, 4, "abc"), VINF_SUCCESS);
44 RTTESTI_CHECK(strcmp(pszBuf4T, "abc") == 0);
45
46 RT_BZERO(pszBuf4H, 4); RT_BZERO(pszBuf4T, 4);
47 RTTESTI_CHECK_RC(RTStrCopy(pszBuf4H, 4, "abcd"), VERR_BUFFER_OVERFLOW);
48 RTTESTI_CHECK(strcmp(pszBuf4H, "abc") == 0);
49 RTTESTI_CHECK_RC(RTStrCopy(pszBuf4T, 4, "abcd"), VERR_BUFFER_OVERFLOW);
50 RTTESTI_CHECK(strcmp(pszBuf4T, "abc") == 0);
51}
52
53
54static void testCopyEx1(RTTEST hTest)
55{
56 RTTestISub("RTStrCopyEx");
57
58 char *pszBuf4H = (char *)RTTestGuardedAllocHead(hTest, 4);
59 char *pszBuf4T = (char *)RTTestGuardedAllocTail(hTest, 4);
60 RTTESTI_CHECK_RC(RTStrCopyEx(pszBuf4H, 4, "abc", RTSTR_MAX), VINF_SUCCESS);
61 RTTESTI_CHECK(strcmp(pszBuf4H, "abc") == 0);
62 RTTESTI_CHECK_RC(RTStrCopyEx(pszBuf4T, 4, "abc", RTSTR_MAX), VINF_SUCCESS);
63 RTTESTI_CHECK(strcmp(pszBuf4T, "abc") == 0);
64
65 RT_BZERO(pszBuf4H, 4); RT_BZERO(pszBuf4T, 4);
66 RTTESTI_CHECK_RC(RTStrCopyEx(pszBuf4H, 4, "abcd", RTSTR_MAX), VERR_BUFFER_OVERFLOW);
67 RTTESTI_CHECK(strcmp(pszBuf4H, "abc") == 0);
68 RTTESTI_CHECK_RC(RTStrCopyEx(pszBuf4T, 4, "abcd", RTSTR_MAX), VERR_BUFFER_OVERFLOW);
69 RTTESTI_CHECK(strcmp(pszBuf4T, "abc") == 0);
70
71 RT_BZERO(pszBuf4H, 4); RT_BZERO(pszBuf4T, 4);
72 RTTESTI_CHECK_RC(RTStrCopyEx(pszBuf4H, 4, "abcd", 3), VINF_SUCCESS);
73 RTTESTI_CHECK(strcmp(pszBuf4H, "abc") == 0);
74 RTTESTI_CHECK_RC(RTStrCopyEx(pszBuf4T, 4, "abcd", 3), VINF_SUCCESS);
75 RTTESTI_CHECK(strcmp(pszBuf4T, "abc") == 0);
76
77 RT_BZERO(pszBuf4H, 4); RT_BZERO(pszBuf4T, 4);
78 RTTESTI_CHECK_RC(RTStrCopyEx(pszBuf4H, 4, "abcd", 2), VINF_SUCCESS);
79 RTTESTI_CHECK(strcmp(pszBuf4H, "ab") == 0);
80 RTTESTI_CHECK_RC(RTStrCopyEx(pszBuf4T, 4, "abcd", 2), VINF_SUCCESS);
81 RTTESTI_CHECK(strcmp(pszBuf4T, "ab") == 0);
82}
83
84
85static void testCat1(RTTEST hTest)
86{
87 RTTestISub("RTStrCat");
88
89 char *pszBuf4H = (char *)RTTestGuardedAllocHead(hTest, 4);
90 char *pszBuf4T = (char *)RTTestGuardedAllocTail(hTest, 4);
91 memset(pszBuf4T, 0xff, 4); *pszBuf4T = '\0';
92 RTTESTI_CHECK_RC(RTStrCat(pszBuf4H, 4, "abc"), VINF_SUCCESS);
93 RTTESTI_CHECK(strcmp(pszBuf4H, "abc") == 0);
94 memset(pszBuf4H, 0xff, 4); *pszBuf4H = '\0';
95 RTTESTI_CHECK_RC(RTStrCat(pszBuf4T, 4, "abc"), VINF_SUCCESS);
96 RTTESTI_CHECK(strcmp(pszBuf4T, "abc") == 0);
97
98 memset(pszBuf4T, 0xff, 4); strcpy(pszBuf4T, "a");
99 memset(pszBuf4H, 0xff, 4); strcpy(pszBuf4H, "a");
100 RTTESTI_CHECK_RC(RTStrCat(pszBuf4H, 4, "bc"), VINF_SUCCESS);
101 RTTESTI_CHECK(strcmp(pszBuf4H, "abc") == 0);
102 RTTESTI_CHECK_RC(RTStrCat(pszBuf4T, 4, "bc"), VINF_SUCCESS);
103 RTTESTI_CHECK(strcmp(pszBuf4T, "abc") == 0);
104
105 memset(pszBuf4T, 0xff, 4); strcpy(pszBuf4T, "ab");
106 memset(pszBuf4H, 0xff, 4); strcpy(pszBuf4H, "ab");
107 RTTESTI_CHECK_RC(RTStrCat(pszBuf4H, 4, "c"), VINF_SUCCESS);
108 RTTESTI_CHECK(strcmp(pszBuf4H, "abc") == 0);
109 RTTESTI_CHECK_RC(RTStrCat(pszBuf4T, 4, "c"), VINF_SUCCESS);
110 RTTESTI_CHECK(strcmp(pszBuf4T, "abc") == 0);
111
112 memset(pszBuf4T, 0xff, 4); strcpy(pszBuf4T, "abc");
113 memset(pszBuf4H, 0xff, 4); strcpy(pszBuf4H, "abc");
114 RTTESTI_CHECK_RC(RTStrCat(pszBuf4H, 4, ""), VINF_SUCCESS);
115 RTTESTI_CHECK(strcmp(pszBuf4H, "abc") == 0);
116 RTTESTI_CHECK_RC(RTStrCat(pszBuf4T, 4, ""), VINF_SUCCESS);
117 RTTESTI_CHECK(strcmp(pszBuf4T, "abc") == 0);
118
119 memset(pszBuf4T, 0xff, 4); strcpy(pszBuf4T, "");
120 memset(pszBuf4H, 0xff, 4); strcpy(pszBuf4H, "");
121 RTTESTI_CHECK_RC(RTStrCat(pszBuf4H, 4, "abcd"), VERR_BUFFER_OVERFLOW);
122 RTTESTI_CHECK(strcmp(pszBuf4H, "abc") == 0);
123 RTTESTI_CHECK_RC(RTStrCat(pszBuf4T, 4, "abcd"), VERR_BUFFER_OVERFLOW);
124 RTTESTI_CHECK(strcmp(pszBuf4T, "abc") == 0);
125
126 memset(pszBuf4T, 0xff, 4); strcpy(pszBuf4T, "ab");
127 memset(pszBuf4H, 0xff, 4); strcpy(pszBuf4H, "ab");
128 RTTESTI_CHECK_RC(RTStrCat(pszBuf4H, 4, "cd"), VERR_BUFFER_OVERFLOW);
129 RTTESTI_CHECK(strcmp(pszBuf4H, "abc") == 0);
130 RTTESTI_CHECK_RC(RTStrCat(pszBuf4T, 4, "cd"), VERR_BUFFER_OVERFLOW);
131 RTTESTI_CHECK(strcmp(pszBuf4T, "abc") == 0);
132
133 memset(pszBuf4T, 0xff, 4); strcpy(pszBuf4T, "abc");
134 memset(pszBuf4H, 0xff, 4); strcpy(pszBuf4H, "abc");
135 RTTESTI_CHECK_RC(RTStrCat(pszBuf4H, 4, "d"), VERR_BUFFER_OVERFLOW);
136 RTTESTI_CHECK(strcmp(pszBuf4H, "abc") == 0);
137 RTTESTI_CHECK_RC(RTStrCat(pszBuf4T, 4, "d"), VERR_BUFFER_OVERFLOW);
138 RTTESTI_CHECK(strcmp(pszBuf4T, "abc") == 0);
139}
140
141
142static void testCatEx1(RTTEST hTest)
143{
144 RTTestISub("RTStrCatEx");
145
146 char *pszBuf4H = (char *)RTTestGuardedAllocHead(hTest, 4);
147 char *pszBuf4T = (char *)RTTestGuardedAllocTail(hTest, 4);
148 memset(pszBuf4T, 0xff, 4); *pszBuf4T = '\0';
149 RTTESTI_CHECK_RC(RTStrCatEx(pszBuf4H, 4, "abc", RTSTR_MAX), VINF_SUCCESS);
150 RTTESTI_CHECK(strcmp(pszBuf4H, "abc") == 0);
151 memset(pszBuf4H, 0xff, 4); *pszBuf4H = '\0';
152 RTTESTI_CHECK_RC(RTStrCatEx(pszBuf4T, 4, "abc", RTSTR_MAX), VINF_SUCCESS);
153 RTTESTI_CHECK(strcmp(pszBuf4T, "abc") == 0);
154
155 memset(pszBuf4T, 0xff, 4); strcpy(pszBuf4T, "a");
156 memset(pszBuf4H, 0xff, 4); strcpy(pszBuf4H, "a");
157 RTTESTI_CHECK_RC(RTStrCatEx(pszBuf4H, 4, "bc", 2), VINF_SUCCESS);
158 RTTESTI_CHECK(strcmp(pszBuf4H, "abc") == 0);
159 RTTESTI_CHECK_RC(RTStrCatEx(pszBuf4T, 4, "bc", 2), VINF_SUCCESS);
160 RTTESTI_CHECK(strcmp(pszBuf4T, "abc") == 0);
161
162 memset(pszBuf4T, 0xff, 4); strcpy(pszBuf4T, "ab");
163 memset(pszBuf4H, 0xff, 4); strcpy(pszBuf4H, "ab");
164 RTTESTI_CHECK_RC(RTStrCatEx(pszBuf4H, 4, "c", 1), VINF_SUCCESS);
165 RTTESTI_CHECK(strcmp(pszBuf4H, "abc") == 0);
166 RTTESTI_CHECK_RC(RTStrCatEx(pszBuf4T, 4, "c", 1), VINF_SUCCESS);
167 RTTESTI_CHECK(strcmp(pszBuf4T, "abc") == 0);
168
169 memset(pszBuf4T, 0xff, 4); strcpy(pszBuf4T, "abc");
170 memset(pszBuf4H, 0xff, 4); strcpy(pszBuf4H, "abc");
171 RTTESTI_CHECK_RC(RTStrCatEx(pszBuf4H, 4, "defg", 0), VINF_SUCCESS);
172 RTTESTI_CHECK(strcmp(pszBuf4H, "abc") == 0);
173 RTTESTI_CHECK_RC(RTStrCatEx(pszBuf4T, 4, "defg", 0), VINF_SUCCESS);
174 RTTESTI_CHECK(strcmp(pszBuf4T, "abc") == 0);
175
176 memset(pszBuf4T, 0xff, 4); strcpy(pszBuf4T, "");
177 memset(pszBuf4H, 0xff, 4); strcpy(pszBuf4H, "");
178 RTTESTI_CHECK_RC(RTStrCatEx(pszBuf4H, 4, "abcd", 4), VERR_BUFFER_OVERFLOW);
179 RTTESTI_CHECK(strcmp(pszBuf4H, "abc") == 0);
180 RTTESTI_CHECK_RC(RTStrCatEx(pszBuf4T, 4, "abcd", 4), VERR_BUFFER_OVERFLOW);
181 RTTESTI_CHECK(strcmp(pszBuf4T, "abc") == 0);
182
183 memset(pszBuf4T, 0xff, 4); strcpy(pszBuf4T, "ab");
184 memset(pszBuf4H, 0xff, 4); strcpy(pszBuf4H, "ab");
185 RTTESTI_CHECK_RC(RTStrCatEx(pszBuf4H, 4, "cdefg", 2), VERR_BUFFER_OVERFLOW);
186 RTTESTI_CHECK(strcmp(pszBuf4H, "abc") == 0);
187 RTTESTI_CHECK_RC(RTStrCatEx(pszBuf4T, 4, "cdefg", 2), VERR_BUFFER_OVERFLOW);
188 RTTESTI_CHECK(strcmp(pszBuf4T, "abc") == 0);
189
190 memset(pszBuf4T, 0xff, 4); strcpy(pszBuf4T, "abc");
191 memset(pszBuf4H, 0xff, 4); strcpy(pszBuf4H, "abc");
192 RTTESTI_CHECK_RC(RTStrCatEx(pszBuf4H, 4, "de", 1), VERR_BUFFER_OVERFLOW);
193 RTTESTI_CHECK(strcmp(pszBuf4H, "abc") == 0);
194 RTTESTI_CHECK_RC(RTStrCatEx(pszBuf4T, 4, "de", 1), VERR_BUFFER_OVERFLOW);
195 RTTESTI_CHECK(strcmp(pszBuf4T, "abc") == 0);
196}
197
198
199
200int main()
201{
202 RTTEST hTest;
203 RTEXITCODE rcExit = RTTestInitAndCreate("tstRTStrCatCopy", &hTest);
204 if (rcExit != RTEXITCODE_SUCCESS)
205 return rcExit;
206
207 testCopy1(hTest);
208 testCopyEx1(hTest);
209 testCat1(hTest);
210 testCatEx1(hTest);
211
212 return RTTestSummaryAndDestroy(hTest);
213}
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