VirtualBox

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

Last change on this file since 85938 was 85348, checked in by vboxsync, 4 years ago

IPRT/tstRTStrSplit: Added macro for cleaning up test runs.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.2 KB
Line 
1/* $Id: tstRTStrSplit.cpp 85348 2020-07-15 12:12:37Z 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
38int main()
39{
40 RTTEST hTest;
41 int rc = RTTestInitAndCreate("tstRTStrSplit", &hTest);
42 if (rc)
43 return rc;
44 RTTestBanner(hTest);
45
46 /* Invalid stuff. */
47 RTTEST_CHECK_RC(hTest, RTStrSplit(NULL, 0, NULL, NULL, NULL), VERR_INVALID_POINTER);
48 RTTEST_CHECK_RC(hTest, RTStrSplit("foo", 0, NULL, NULL, NULL), VERR_INVALID_PARAMETER);
49 RTTEST_CHECK_RC(hTest, RTStrSplit("foo", 42, NULL, NULL, NULL), VERR_INVALID_POINTER);
50
51 char **papszStrings = NULL;
52 size_t cStrings = 0;
53
54#define DO_CLEANUP() \
55 for (size_t i = 0; i < cStrings; ++i) \
56 RTStrFree(papszStrings[i]); \
57 RTMemFree(papszStrings);
58 cStrings = 0;
59
60 /* Empty stuff. */
61 const char szEmpty[] = "";
62 RTTEST_CHECK_RC(hTest, RTStrSplit(szEmpty, sizeof(szEmpty), "\r\n", &papszStrings, &cStrings), VINF_SUCCESS);
63 RTTEST_CHECK(hTest, cStrings == 0);
64 DO_CLEANUP();
65
66 /* No separator given. */
67 const char szNoSep[] = "foo";
68 RTTEST_CHECK_RC(hTest, RTStrSplit(szNoSep, sizeof(szNoSep), "\r\n", &papszStrings, &cStrings), VINF_SUCCESS);
69 RTTEST_CHECK(hTest, cStrings == 1);
70 RTTEST_CHECK(hTest, RTStrICmp(papszStrings[0], "foo") == 0);
71 DO_CLEANUP();
72
73 /* Single string w/ separator. */
74 const char szWithSep[] = "foo\r\n";
75 RTTEST_CHECK_RC(hTest, RTStrSplit(szWithSep, sizeof(szWithSep), "\r\n", &papszStrings, &cStrings), VINF_SUCCESS);
76 RTTEST_CHECK(hTest, cStrings == 1);
77 RTTEST_CHECK(hTest, papszStrings && RTStrICmp(papszStrings[0], "foo") == 0);
78 DO_CLEANUP();
79
80 /* Multiple strings w/ separator. */
81 const char szWithSep2[] = "foo\r\nbar";
82 RTTEST_CHECK_RC(hTest, RTStrSplit(szWithSep2, sizeof(szWithSep2), "\r\n", &papszStrings, &cStrings), VINF_SUCCESS);
83 RTTEST_CHECK(hTest, cStrings == 2);
84 RTTEST_CHECK(hTest, cStrings == 2
85 && papszStrings
86 && RTStrICmp(papszStrings[0], "foo") == 0
87 && RTStrICmp(papszStrings[1], "bar") == 0);
88 DO_CLEANUP();
89
90 /* Multiple strings w/ two consequtive separators. */
91 const char szWithSep3[] = "foo\r\nbar\r\n\r\n";
92 RTTEST_CHECK_RC(hTest, RTStrSplit(szWithSep3, sizeof(szWithSep3), "\r\n", &papszStrings, &cStrings), VINF_SUCCESS);
93 RTTEST_CHECK(hTest, cStrings == 2);
94 RTTEST_CHECK(hTest, cStrings == 2
95 && papszStrings
96 && RTStrICmp(papszStrings[0], "foo") == 0
97 && RTStrICmp(papszStrings[1], "bar") == 0);
98 DO_CLEANUP();
99
100 /* Multiple strings w/ two consequtive separators. */
101 const char szWithSep4[] = "foo\r\nbar\r\n\r\nbaz";
102 RTTEST_CHECK_RC(hTest, RTStrSplit(szWithSep4, sizeof(szWithSep4), "\r\n", &papszStrings, &cStrings), VINF_SUCCESS);
103 RTTEST_CHECK(hTest, cStrings == 3);
104 RTTEST_CHECK(hTest, cStrings == 3
105 && papszStrings
106 && RTStrICmp(papszStrings[0], "foo") == 0
107 && RTStrICmp(papszStrings[1], "bar") == 0
108 && RTStrICmp(papszStrings[2], "baz") == 0);
109 DO_CLEANUP();
110
111 /* Multiple strings w/ trailing separators. */
112 const char szWithSep5[] = "foo\r\nbar\r\n\r\nbaz\r\n\r\n";
113 RTTEST_CHECK_RC(hTest, RTStrSplit(szWithSep5, sizeof(szWithSep5), "\r\n", &papszStrings, &cStrings), VINF_SUCCESS);
114 RTTEST_CHECK(hTest, cStrings == 3);
115 RTTEST_CHECK(hTest, cStrings == 3
116 && papszStrings
117 && RTStrICmp(papszStrings[0], "foo") == 0
118 && RTStrICmp(papszStrings[1], "bar") == 0
119 && RTStrICmp(papszStrings[2], "baz") == 0);
120 DO_CLEANUP();
121
122#undef DO_CLEANUP
123
124 /*
125 * Summary.
126 */
127 return RTTestSummaryAndDestroy(hTest);
128}
129
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