VirtualBox

source: vbox/trunk/src/VBox/Runtime/testcase/tstRTStrSplit.cpp@ 107044

Last change on this file since 107044 was 106061, checked in by vboxsync, 2 months ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.6 KB
Line 
1/* $Id: tstRTStrSplit.cpp 106061 2024-09-16 14:03:52Z vboxsync $ */
2/** @file
3 * IPRT Testcase - String splitting.
4 */
5
6/*
7 * Copyright (C) 2020-2024 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/test.h>
44#include <iprt/mem.h>
45#include <iprt/stream.h>
46
47
48int main()
49{
50 RTTEST hTest;
51 RTEXITCODE rcExit = RTTestInitAndCreate("tstRTStrSplit", &hTest);
52 if (rcExit != RTEXITCODE_SUCCESS)
53 return rcExit;
54 RTTestBanner(hTest);
55
56 /* Invalid stuff. */
57 RTTestDisableAssertions(hTest);
58 RTTEST_CHECK_RC(hTest, RTStrSplit(NULL, 0, NULL, NULL, NULL), VERR_INVALID_POINTER);
59 RTTEST_CHECK_RC(hTest, RTStrSplit("foo", 0, NULL, NULL, NULL), VERR_INVALID_PARAMETER);
60 RTTEST_CHECK_RC(hTest, RTStrSplit("foo", 42, NULL, NULL, NULL), VERR_INVALID_POINTER);
61 RTTestRestoreAssertions(hTest);
62
63 char **papszStrings = NULL;
64 size_t cStrings = 0;
65
66#define DO_CLEANUP() \
67 for (size_t i = 0; i < cStrings; ++i) \
68 RTStrFree(papszStrings[i]); \
69 RTMemFree(papszStrings);
70 cStrings = 0;
71
72 /* Empty stuff. */
73 const char szEmpty[] = "";
74 RTTEST_CHECK_RC(hTest, RTStrSplit(szEmpty, sizeof(szEmpty), "\r\n", &papszStrings, &cStrings), VINF_SUCCESS);
75 RTTEST_CHECK(hTest, cStrings == 0);
76 DO_CLEANUP();
77
78 /* No separator given. */
79 const char szNoSep[] = "foo";
80 RTTEST_CHECK_RC(hTest, RTStrSplit(szNoSep, sizeof(szNoSep), "\r\n", &papszStrings, &cStrings), VINF_SUCCESS);
81 RTTEST_CHECK(hTest, cStrings == 1);
82 RTTEST_CHECK(hTest, RTStrICmp(papszStrings[0], "foo") == 0);
83 DO_CLEANUP();
84
85 /* Single string w/ separator. */
86 const char szWithSep[] = "foo\r\n";
87 RTTEST_CHECK_RC(hTest, RTStrSplit(szWithSep, sizeof(szWithSep), "\r\n", &papszStrings, &cStrings), VINF_SUCCESS);
88 RTTEST_CHECK(hTest, cStrings == 1);
89 RTTEST_CHECK(hTest, papszStrings && RTStrICmp(papszStrings[0], "foo") == 0);
90 DO_CLEANUP();
91
92 /* Multiple strings w/ separator. */
93 const char szWithSep2[] = "foo\r\nbar";
94 RTTEST_CHECK_RC(hTest, RTStrSplit(szWithSep2, sizeof(szWithSep2), "\r\n", &papszStrings, &cStrings), VINF_SUCCESS);
95 RTTEST_CHECK(hTest, cStrings == 2);
96 RTTEST_CHECK(hTest, cStrings == 2
97 && papszStrings
98 && RTStrICmp(papszStrings[0], "foo") == 0
99 && RTStrICmp(papszStrings[1], "bar") == 0);
100 DO_CLEANUP();
101
102 /* Multiple strings w/ two consequtive separators. */
103 const char szWithSep3[] = "foo\r\nbar\r\n\r\n";
104 RTTEST_CHECK_RC(hTest, RTStrSplit(szWithSep3, sizeof(szWithSep3), "\r\n", &papszStrings, &cStrings), VINF_SUCCESS);
105 RTTEST_CHECK(hTest, cStrings == 2);
106 RTTEST_CHECK(hTest, cStrings == 2
107 && papszStrings
108 && RTStrICmp(papszStrings[0], "foo") == 0
109 && RTStrICmp(papszStrings[1], "bar") == 0);
110 DO_CLEANUP();
111
112 /* Multiple strings w/ two consequtive separators. */
113 const char szWithSep4[] = "foo\r\nbar\r\n\r\nbaz";
114 RTTEST_CHECK_RC(hTest, RTStrSplit(szWithSep4, sizeof(szWithSep4), "\r\n", &papszStrings, &cStrings), VINF_SUCCESS);
115 RTTEST_CHECK(hTest, cStrings == 3);
116 RTTEST_CHECK(hTest, cStrings == 3
117 && papszStrings
118 && RTStrICmp(papszStrings[0], "foo") == 0
119 && RTStrICmp(papszStrings[1], "bar") == 0
120 && RTStrICmp(papszStrings[2], "baz") == 0);
121 DO_CLEANUP();
122
123 /* Multiple strings w/ trailing separators. */
124 const char szWithSep5[] = "foo\r\nbar\r\n\r\nbaz\r\n\r\n";
125 RTTEST_CHECK_RC(hTest, RTStrSplit(szWithSep5, sizeof(szWithSep5), "\r\n", &papszStrings, &cStrings), VINF_SUCCESS);
126 RTTEST_CHECK(hTest, cStrings == 3);
127 RTTEST_CHECK(hTest, cStrings == 3
128 && papszStrings
129 && RTStrICmp(papszStrings[0], "foo") == 0
130 && RTStrICmp(papszStrings[1], "bar") == 0
131 && RTStrICmp(papszStrings[2], "baz") == 0);
132 DO_CLEANUP();
133
134#undef DO_CLEANUP
135
136 /*
137 * Summary.
138 */
139 return RTTestSummaryAndDestroy(hTest);
140}
141
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