1 | /* $Id: tstRTStrSplit.cpp 86385 2020-10-01 14:26:12Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT Testcase - String splitting.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2020 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 | /*********************************************************************************************************************************
|
---|
29 | * Header Files *
|
---|
30 | *********************************************************************************************************************************/
|
---|
31 | #include <iprt/string.h>
|
---|
32 |
|
---|
33 | #include <iprt/test.h>
|
---|
34 | #include <iprt/mem.h>
|
---|
35 | #include <iprt/stream.h>
|
---|
36 |
|
---|
37 |
|
---|
38 | int main()
|
---|
39 | {
|
---|
40 | RTTEST hTest;
|
---|
41 | RTEXITCODE rcExit = RTTestInitAndCreate("tstRTStrSplit", &hTest);
|
---|
42 | if (rcExit != RTEXITCODE_SUCCESS)
|
---|
43 | return rcExit;
|
---|
44 | RTTestBanner(hTest);
|
---|
45 |
|
---|
46 | /* Invalid stuff. */
|
---|
47 | RTTestDisableAssertions(hTest);
|
---|
48 | RTTEST_CHECK_RC(hTest, RTStrSplit(NULL, 0, NULL, NULL, NULL), VERR_INVALID_POINTER);
|
---|
49 | RTTEST_CHECK_RC(hTest, RTStrSplit("foo", 0, NULL, NULL, NULL), VERR_INVALID_PARAMETER);
|
---|
50 | RTTEST_CHECK_RC(hTest, RTStrSplit("foo", 42, NULL, NULL, NULL), VERR_INVALID_POINTER);
|
---|
51 | RTTestRestoreAssertions(hTest);
|
---|
52 |
|
---|
53 | char **papszStrings = NULL;
|
---|
54 | size_t cStrings = 0;
|
---|
55 |
|
---|
56 | #define DO_CLEANUP() \
|
---|
57 | for (size_t i = 0; i < cStrings; ++i) \
|
---|
58 | RTStrFree(papszStrings[i]); \
|
---|
59 | RTMemFree(papszStrings);
|
---|
60 | cStrings = 0;
|
---|
61 |
|
---|
62 | /* Empty stuff. */
|
---|
63 | const char szEmpty[] = "";
|
---|
64 | RTTEST_CHECK_RC(hTest, RTStrSplit(szEmpty, sizeof(szEmpty), "\r\n", &papszStrings, &cStrings), VINF_SUCCESS);
|
---|
65 | RTTEST_CHECK(hTest, cStrings == 0);
|
---|
66 | DO_CLEANUP();
|
---|
67 |
|
---|
68 | /* No separator given. */
|
---|
69 | const char szNoSep[] = "foo";
|
---|
70 | RTTEST_CHECK_RC(hTest, RTStrSplit(szNoSep, sizeof(szNoSep), "\r\n", &papszStrings, &cStrings), VINF_SUCCESS);
|
---|
71 | RTTEST_CHECK(hTest, cStrings == 1);
|
---|
72 | RTTEST_CHECK(hTest, RTStrICmp(papszStrings[0], "foo") == 0);
|
---|
73 | DO_CLEANUP();
|
---|
74 |
|
---|
75 | /* Single string w/ separator. */
|
---|
76 | const char szWithSep[] = "foo\r\n";
|
---|
77 | RTTEST_CHECK_RC(hTest, RTStrSplit(szWithSep, sizeof(szWithSep), "\r\n", &papszStrings, &cStrings), VINF_SUCCESS);
|
---|
78 | RTTEST_CHECK(hTest, cStrings == 1);
|
---|
79 | RTTEST_CHECK(hTest, papszStrings && RTStrICmp(papszStrings[0], "foo") == 0);
|
---|
80 | DO_CLEANUP();
|
---|
81 |
|
---|
82 | /* Multiple strings w/ separator. */
|
---|
83 | const char szWithSep2[] = "foo\r\nbar";
|
---|
84 | RTTEST_CHECK_RC(hTest, RTStrSplit(szWithSep2, sizeof(szWithSep2), "\r\n", &papszStrings, &cStrings), VINF_SUCCESS);
|
---|
85 | RTTEST_CHECK(hTest, cStrings == 2);
|
---|
86 | RTTEST_CHECK(hTest, cStrings == 2
|
---|
87 | && papszStrings
|
---|
88 | && RTStrICmp(papszStrings[0], "foo") == 0
|
---|
89 | && RTStrICmp(papszStrings[1], "bar") == 0);
|
---|
90 | DO_CLEANUP();
|
---|
91 |
|
---|
92 | /* Multiple strings w/ two consequtive separators. */
|
---|
93 | const char szWithSep3[] = "foo\r\nbar\r\n\r\n";
|
---|
94 | RTTEST_CHECK_RC(hTest, RTStrSplit(szWithSep3, sizeof(szWithSep3), "\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 szWithSep4[] = "foo\r\nbar\r\n\r\nbaz";
|
---|
104 | RTTEST_CHECK_RC(hTest, RTStrSplit(szWithSep4, sizeof(szWithSep4), "\r\n", &papszStrings, &cStrings), VINF_SUCCESS);
|
---|
105 | RTTEST_CHECK(hTest, cStrings == 3);
|
---|
106 | RTTEST_CHECK(hTest, cStrings == 3
|
---|
107 | && papszStrings
|
---|
108 | && RTStrICmp(papszStrings[0], "foo") == 0
|
---|
109 | && RTStrICmp(papszStrings[1], "bar") == 0
|
---|
110 | && RTStrICmp(papszStrings[2], "baz") == 0);
|
---|
111 | DO_CLEANUP();
|
---|
112 |
|
---|
113 | /* Multiple strings w/ trailing separators. */
|
---|
114 | const char szWithSep5[] = "foo\r\nbar\r\n\r\nbaz\r\n\r\n";
|
---|
115 | RTTEST_CHECK_RC(hTest, RTStrSplit(szWithSep5, sizeof(szWithSep5), "\r\n", &papszStrings, &cStrings), VINF_SUCCESS);
|
---|
116 | RTTEST_CHECK(hTest, cStrings == 3);
|
---|
117 | RTTEST_CHECK(hTest, cStrings == 3
|
---|
118 | && papszStrings
|
---|
119 | && RTStrICmp(papszStrings[0], "foo") == 0
|
---|
120 | && RTStrICmp(papszStrings[1], "bar") == 0
|
---|
121 | && RTStrICmp(papszStrings[2], "baz") == 0);
|
---|
122 | DO_CLEANUP();
|
---|
123 |
|
---|
124 | #undef DO_CLEANUP
|
---|
125 |
|
---|
126 | /*
|
---|
127 | * Summary.
|
---|
128 | */
|
---|
129 | return RTTestSummaryAndDestroy(hTest);
|
---|
130 | }
|
---|
131 |
|
---|