1 | //===-- Unittests for strspn ----------------------------------------------===// |
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/strspn.h" |
10 | |
11 | #include "test/UnitTest/Test.h" |
12 | |
13 | TEST(LlvmLibcStrSpnTest, EmptyStringShouldReturnZeroLengthSpan) { |
14 | // The search should not include the null terminator. |
15 | EXPECT_EQ(LIBC_NAMESPACE::strspn("" , "" ), size_t{0}); |
16 | EXPECT_EQ(LIBC_NAMESPACE::strspn("_" , "" ), size_t{0}); |
17 | EXPECT_EQ(LIBC_NAMESPACE::strspn("" , "_" ), size_t{0}); |
18 | } |
19 | |
20 | TEST(LlvmLibcStrSpnTest, ShouldNotSpanAnythingAfterNullTerminator) { |
21 | const char src[4] = {'a', 'b', '\0', 'c'}; |
22 | EXPECT_EQ(LIBC_NAMESPACE::strspn(src, "ab" ), size_t{2}); |
23 | EXPECT_EQ(LIBC_NAMESPACE::strspn(src, "c" ), size_t{0}); |
24 | |
25 | // Same goes for the segment to be searched for. |
26 | const char segment[4] = {'1', '2', '\0', '3'}; |
27 | EXPECT_EQ(LIBC_NAMESPACE::strspn("123" , segment), size_t{2}); |
28 | } |
29 | |
30 | TEST(LlvmLibcStrSpnTest, SpanEachIndividualCharacter) { |
31 | const char *src = "12345" ; |
32 | EXPECT_EQ(LIBC_NAMESPACE::strspn(src, "1" ), size_t{1}); |
33 | // Since '1' is not within the segment, the span |
34 | // size should remain zero. |
35 | EXPECT_EQ(LIBC_NAMESPACE::strspn(src, "2" ), size_t{0}); |
36 | EXPECT_EQ(LIBC_NAMESPACE::strspn(src, "3" ), size_t{0}); |
37 | EXPECT_EQ(LIBC_NAMESPACE::strspn(src, "4" ), size_t{0}); |
38 | EXPECT_EQ(LIBC_NAMESPACE::strspn(src, "5" ), size_t{0}); |
39 | } |
40 | |
41 | TEST(LlvmLibcStrSpnTest, UnmatchedCharacterShouldNotBeCountedInSpan) { |
42 | EXPECT_EQ(LIBC_NAMESPACE::strspn("a" , "b" ), size_t{0}); |
43 | EXPECT_EQ(LIBC_NAMESPACE::strspn("abcdef" , "1" ), size_t{0}); |
44 | EXPECT_EQ(LIBC_NAMESPACE::strspn("123" , "4" ), size_t{0}); |
45 | } |
46 | |
47 | TEST(LlvmLibcStrSpnTest, SequentialCharactersShouldSpan) { |
48 | const char *src = "abcde" ; |
49 | EXPECT_EQ(LIBC_NAMESPACE::strspn(src, "a" ), size_t{1}); |
50 | EXPECT_EQ(LIBC_NAMESPACE::strspn(src, "ab" ), size_t{2}); |
51 | EXPECT_EQ(LIBC_NAMESPACE::strspn(src, "abc" ), size_t{3}); |
52 | EXPECT_EQ(LIBC_NAMESPACE::strspn(src, "abcd" ), size_t{4}); |
53 | EXPECT_EQ(LIBC_NAMESPACE::strspn(src, "abcde" ), size_t{5}); |
54 | // Same thing for when the roles are reversed. |
55 | EXPECT_EQ(LIBC_NAMESPACE::strspn("abcde" , src), size_t{5}); |
56 | EXPECT_EQ(LIBC_NAMESPACE::strspn("abcd" , src), size_t{4}); |
57 | EXPECT_EQ(LIBC_NAMESPACE::strspn("abc" , src), size_t{3}); |
58 | EXPECT_EQ(LIBC_NAMESPACE::strspn("ab" , src), size_t{2}); |
59 | EXPECT_EQ(LIBC_NAMESPACE::strspn("a" , src), size_t{1}); |
60 | } |
61 | |
62 | TEST(LlvmLibcStrSpnTest, NonSequentialCharactersShouldNotSpan) { |
63 | const char *src = "123456789" ; |
64 | EXPECT_EQ(LIBC_NAMESPACE::strspn(src, "_1_abc_2_def_3_" ), size_t{3}); |
65 | // Only spans 4 since '5' is not within the span. |
66 | EXPECT_EQ(LIBC_NAMESPACE::strspn(src, "67__34abc12" ), size_t{4}); |
67 | } |
68 | |
69 | TEST(LlvmLibcStrSpnTest, ReverseCharacters) { |
70 | // Since these are still sequential, this should span. |
71 | EXPECT_EQ(LIBC_NAMESPACE::strspn("12345" , "54321" ), size_t{5}); |
72 | // Does not span any since '1' is not within the span. |
73 | EXPECT_EQ(LIBC_NAMESPACE::strspn("12345" , "432" ), size_t{0}); |
74 | // Only spans 1 since '2' is not within the span. |
75 | EXPECT_EQ(LIBC_NAMESPACE::strspn("12345" , "51" ), size_t{1}); |
76 | } |
77 | |
78 | TEST(LlvmLibcStrSpnTest, DuplicatedCharactersToBeSearchedForShouldStillMatch) { |
79 | // Only a single character, so only spans 1. |
80 | EXPECT_EQ(LIBC_NAMESPACE::strspn("a" , "aa" ), size_t{1}); |
81 | // This should count once for each 'a' in the source string. |
82 | EXPECT_EQ(LIBC_NAMESPACE::strspn("aa" , "aa" ), size_t{2}); |
83 | EXPECT_EQ(LIBC_NAMESPACE::strspn("aaa" , "aa" ), size_t{3}); |
84 | EXPECT_EQ(LIBC_NAMESPACE::strspn("aaaa" , "aa" ), size_t{4}); |
85 | } |
86 | |