1 | //===-- Unittests for strsep ----------------------------------------------===// |
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | |
9 | #include "src/string/strsep.h" |
10 | #include "test/UnitTest/Test.h" |
11 | |
12 | TEST(LlvmLibcStrsepTest, NullSrc) { |
13 | char *string = nullptr; |
14 | EXPECT_STREQ(LIBC_NAMESPACE::strsep(&string, "" ), nullptr); |
15 | } |
16 | |
17 | TEST(LlvmLibcStrsepTest, NoTokenFound) { |
18 | { |
19 | char s[] = "" ; |
20 | char *string = s, *orig = s; |
21 | EXPECT_STREQ(LIBC_NAMESPACE::strsep(&string, "" ), nullptr); |
22 | EXPECT_EQ(orig, string); |
23 | } |
24 | { |
25 | char s[] = "abcde" ; |
26 | char *string = s, *orig = s; |
27 | EXPECT_STREQ(LIBC_NAMESPACE::strsep(&string, "" ), orig); |
28 | EXPECT_EQ(string, orig + 5); |
29 | } |
30 | { |
31 | char s[] = "abcde" ; |
32 | char *string = s, *orig = s; |
33 | EXPECT_STREQ(LIBC_NAMESPACE::strsep(&string, "fghijk" ), orig); |
34 | EXPECT_EQ(string, orig + 5); |
35 | } |
36 | } |
37 | |
38 | TEST(LlvmLibcStrsepTest, TokenFound) { |
39 | char s[] = "abacd" ; |
40 | char *string = s; |
41 | EXPECT_STREQ(LIBC_NAMESPACE::strsep(&string, "c" ), "aba" ); |
42 | EXPECT_STREQ(string, "d" ); |
43 | } |
44 | |
45 | TEST(LlvmLibcStrsepTest, DelimitersShouldNotBeIncludedInToken) { |
46 | char s[] = "__ab__:cd_:_ef_:_" ; |
47 | char *string = s; |
48 | const char *expected[] = {"" , "" , "ab" , "" , "" , "cd" , "" , |
49 | "" , "ef" , "" , "" , "" , nullptr}; |
50 | for (int i = 0; expected[i]; i++) { |
51 | ASSERT_STREQ(LIBC_NAMESPACE::strsep(&string, "_:" ), expected[i]); |
52 | } |
53 | } |
54 | |