1 | /* $Id: tstRTStrSplit.cpp 85345 2020-07-15 08:03:03Z 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.alldomusa.eu.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/stream.h>
|
---|
35 |
|
---|
36 |
|
---|
37 | int main()
|
---|
38 | {
|
---|
39 | RTTEST hTest;
|
---|
40 | int rc = RTTestInitAndCreate("tstRTStrSplit", &hTest);
|
---|
41 | if (rc)
|
---|
42 | return rc;
|
---|
43 | RTTestBanner(hTest);
|
---|
44 |
|
---|
45 | /* Invalid stuff. */
|
---|
46 | RTTEST_CHECK_RC(hTest, RTStrSplit(NULL, 0, NULL, NULL, NULL), VERR_INVALID_POINTER);
|
---|
47 | RTTEST_CHECK_RC(hTest, RTStrSplit("foo", 0, NULL, NULL, NULL), VERR_INVALID_PARAMETER);
|
---|
48 | RTTEST_CHECK_RC(hTest, RTStrSplit("foo", 42, NULL, NULL, NULL), VERR_INVALID_POINTER);
|
---|
49 |
|
---|
50 | char **papszStrings = NULL;
|
---|
51 | size_t cStrings = 0;
|
---|
52 |
|
---|
53 | /* Empty stuff. */
|
---|
54 | const char szEmpty[] = "";
|
---|
55 | RTTEST_CHECK_RC(hTest, RTStrSplit(szEmpty, sizeof(szEmpty), "\r\n", &papszStrings, &cStrings), VINF_SUCCESS);
|
---|
56 | RTTEST_CHECK(hTest, cStrings == 0);
|
---|
57 |
|
---|
58 | /* No separator given. */
|
---|
59 | const char szNoSep[] = "foo";
|
---|
60 | RTTEST_CHECK_RC(hTest, RTStrSplit(szNoSep, sizeof(szNoSep), "\r\n", &papszStrings, &cStrings), VINF_SUCCESS);
|
---|
61 | RTTEST_CHECK(hTest, cStrings == 1);
|
---|
62 | RTTEST_CHECK(hTest, RTStrICmp(papszStrings[0], "foo") == 0);
|
---|
63 |
|
---|
64 | /* Single string w/ separator. */
|
---|
65 | const char szWithSep[] = "foo\r\n";
|
---|
66 | RTTEST_CHECK_RC(hTest, RTStrSplit(szWithSep, sizeof(szWithSep), "\r\n", &papszStrings, &cStrings), VINF_SUCCESS);
|
---|
67 | RTTEST_CHECK(hTest, cStrings == 1);
|
---|
68 | RTTEST_CHECK(hTest, papszStrings && RTStrICmp(papszStrings[0], "foo") == 0);
|
---|
69 |
|
---|
70 | /* Multiple strings w/ separator. */
|
---|
71 | const char szWithSep2[] = "foo\r\nbar";
|
---|
72 | RTTEST_CHECK_RC(hTest, RTStrSplit(szWithSep2, sizeof(szWithSep2), "\r\n", &papszStrings, &cStrings), VINF_SUCCESS);
|
---|
73 | RTTEST_CHECK(hTest, cStrings == 2);
|
---|
74 | RTTEST_CHECK(hTest, cStrings == 2
|
---|
75 | && papszStrings
|
---|
76 | && RTStrICmp(papszStrings[0], "foo") == 0
|
---|
77 | && RTStrICmp(papszStrings[1], "bar") == 0);
|
---|
78 |
|
---|
79 | /* Multiple strings w/ two consequtive separators. */
|
---|
80 | const char szWithSep3[] = "foo\r\nbar\r\n\r\n";
|
---|
81 | RTTEST_CHECK_RC(hTest, RTStrSplit(szWithSep3, sizeof(szWithSep3), "\r\n", &papszStrings, &cStrings), VINF_SUCCESS);
|
---|
82 | RTTEST_CHECK(hTest, cStrings == 2);
|
---|
83 | RTTEST_CHECK(hTest, cStrings == 2
|
---|
84 | && papszStrings
|
---|
85 | && RTStrICmp(papszStrings[0], "foo") == 0
|
---|
86 | && RTStrICmp(papszStrings[1], "bar") == 0);
|
---|
87 |
|
---|
88 | /* Multiple strings w/ two consequtive separators. */
|
---|
89 | const char szWithSep4[] = "foo\r\nbar\r\n\r\nbaz";
|
---|
90 | RTTEST_CHECK_RC(hTest, RTStrSplit(szWithSep4, sizeof(szWithSep4), "\r\n", &papszStrings, &cStrings), VINF_SUCCESS);
|
---|
91 | RTTEST_CHECK(hTest, cStrings == 3);
|
---|
92 | RTTEST_CHECK(hTest, cStrings == 3
|
---|
93 | && papszStrings
|
---|
94 | && RTStrICmp(papszStrings[0], "foo") == 0
|
---|
95 | && RTStrICmp(papszStrings[1], "bar") == 0
|
---|
96 | && RTStrICmp(papszStrings[2], "baz") == 0);
|
---|
97 |
|
---|
98 | /* Multiple strings w/ trailing separators. */
|
---|
99 | const char szWithSep5[] = "foo\r\nbar\r\n\r\nbaz\r\n\r\n";
|
---|
100 | RTTEST_CHECK_RC(hTest, RTStrSplit(szWithSep5, sizeof(szWithSep5), "\r\n", &papszStrings, &cStrings), VINF_SUCCESS);
|
---|
101 | RTTEST_CHECK(hTest, cStrings == 3);
|
---|
102 | RTTEST_CHECK(hTest, cStrings == 3
|
---|
103 | && papszStrings
|
---|
104 | && RTStrICmp(papszStrings[0], "foo") == 0
|
---|
105 | && RTStrICmp(papszStrings[1], "bar") == 0
|
---|
106 | && RTStrICmp(papszStrings[2], "baz") == 0);
|
---|
107 | /*
|
---|
108 | * Summary.
|
---|
109 | */
|
---|
110 | return RTTestSummaryAndDestroy(hTest);
|
---|
111 | }
|
---|
112 |
|
---|