VirtualBox

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

最後變更 在這個檔案從85154是 85154,由 vboxsync 提交於 5 年 前

IPRT: Added RTStrSplit + testcases.

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 4.4 KB
 
1/* $Id: tstRTStrSplit.cpp 85154 2020-07-09 17:22:51Z 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
37int 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, NULL, 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 RTTEST_CHECK(hTest, papszStrings == NULL);
58
59 /* No separator given. */
60 const char szNoSep[] = "foo";
61 RTTEST_CHECK_RC(hTest, RTStrSplit(szNoSep, sizeof(szNoSep), "\r\n", &papszStrings, &cStrings), VINF_SUCCESS);
62 RTTEST_CHECK(hTest, cStrings == 0);
63 RTTEST_CHECK(hTest, papszStrings == NULL);
64
65 /* Single string w/ separator. */
66 const char szWithSep[] = "foo\r\n";
67 RTTEST_CHECK_RC(hTest, RTStrSplit(szWithSep, sizeof(szWithSep), "\r\n", &papszStrings, &cStrings), VINF_SUCCESS);
68 RTTEST_CHECK(hTest, cStrings == 1);
69 RTTEST_CHECK(hTest, papszStrings && RTStrICmp(papszStrings[0], "foo") == 0);
70
71 /* Multiple strings w/ separators. */
72 const char szWithSep2[] = "foo\r\nbar\r\n";
73 RTTEST_CHECK_RC(hTest, RTStrSplit(szWithSep2, sizeof(szWithSep2), "\r\n", &papszStrings, &cStrings), VINF_SUCCESS);
74 RTTEST_CHECK(hTest, cStrings == 2);
75 RTTEST_CHECK(hTest, cStrings == 2
76 && papszStrings
77 && RTStrICmp(papszStrings[0], "foo") == 0
78 && RTStrICmp(papszStrings[1], "bar") == 0);
79
80 /* Multiple strings w/ two consequtive separators. */
81 const char szWithSep3[] = "foo\r\nbar\r\n\r\n";
82 RTTEST_CHECK_RC(hTest, RTStrSplit(szWithSep3, sizeof(szWithSep3), "\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
89 /* Multiple strings w/ two consequtive separators. */
90 const char szWithSep4[] = "foo\r\nbar\r\n\r\nbaz\r\n";
91 RTTEST_CHECK_RC(hTest, RTStrSplit(szWithSep4, sizeof(szWithSep4), "\r\n", &papszStrings, &cStrings), VINF_SUCCESS);
92 RTTEST_CHECK(hTest, cStrings == 3);
93 RTTEST_CHECK(hTest, cStrings == 3
94 && papszStrings
95 && RTStrICmp(papszStrings[0], "foo") == 0
96 && RTStrICmp(papszStrings[1], "bar") == 0
97 && RTStrICmp(papszStrings[2], "baz") == 0);
98
99 /*
100 * Summary.
101 */
102 return RTTestSummaryAndDestroy(hTest);
103}
104
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette