1 //===-- Unittests for strtok ---------------------------------------------===//
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/strtok.h"
10 #include "utils/UnitTest/Test.h"
11 
TEST(StrTokTest,NoTokenFound)12 TEST(StrTokTest, NoTokenFound) {
13   char empty[] = "";
14   ASSERT_STREQ(__llvm_libc::strtok(empty, ""), nullptr);
15   ASSERT_STREQ(__llvm_libc::strtok(empty, "_"), nullptr);
16 
17   char single[] = "_";
18   ASSERT_STREQ(__llvm_libc::strtok(single, ""), "_");
19 
20   char multiple[] = "1,2";
21   ASSERT_STREQ(__llvm_libc::strtok(multiple, ":"), "1,2");
22 }
23 
TEST(StrTokTest,DelimiterAsFirstCharacterShouldBeIgnored)24 TEST(StrTokTest, DelimiterAsFirstCharacterShouldBeIgnored) {
25   char src[] = ".123";
26   ASSERT_STREQ(__llvm_libc::strtok(src, "."), "123");
27 }
28 
TEST(StrTokTest,DelimiterIsMiddleCharacter)29 TEST(StrTokTest, DelimiterIsMiddleCharacter) {
30   char src[] = "12,34";
31   ASSERT_STREQ(__llvm_libc::strtok(src, ","), "12");
32 }
33 
TEST(StrTokTest,DelimiterAsLastCharacterShouldBeIgnored)34 TEST(StrTokTest, DelimiterAsLastCharacterShouldBeIgnored) {
35   char src[] = "1234:";
36   ASSERT_STREQ(__llvm_libc::strtok(src, ":"), "1234");
37 }
38 
TEST(StrTokTest,MultipleDelimiters)39 TEST(StrTokTest, MultipleDelimiters) {
40   char src[] = "12,.34";
41   ASSERT_STREQ(__llvm_libc::strtok(src, "."), "12,");
42   ASSERT_STREQ(__llvm_libc::strtok(src, ".,"), "12");
43   ASSERT_STREQ(__llvm_libc::strtok(src, ",."), "12");
44   ASSERT_STREQ(__llvm_libc::strtok(src, ":,."), "12");
45 }
46 
TEST(StrTokTest,ShouldNotGoPastNullTerminator)47 TEST(StrTokTest, ShouldNotGoPastNullTerminator) {
48   char src[] = {'1', '2', '\0', ',', '3'};
49   ASSERT_STREQ(__llvm_libc::strtok(src, ","), "12");
50 }
51 
TEST(StrTokTest,SubsequentCallsShouldFindFollowingDelimiters)52 TEST(StrTokTest, SubsequentCallsShouldFindFollowingDelimiters) {
53   char src[] = "12,34.56";
54   char *token = __llvm_libc::strtok(src, ",.");
55   ASSERT_STREQ(token, "12");
56   token = __llvm_libc::strtok(nullptr, ",.");
57   ASSERT_STREQ(token, "34");
58   token = __llvm_libc::strtok(nullptr, ",.");
59   ASSERT_STREQ(token, "56");
60   token = __llvm_libc::strtok(nullptr, "_:,_");
61   ASSERT_STREQ(token, nullptr);
62   // Subsequent calls after hitting the end of the string should also return
63   // nullptr.
64   token = __llvm_libc::strtok(nullptr, "_:,_");
65   ASSERT_STREQ(token, nullptr);
66 }
67 
TEST(StrTokTest,DelimitersShouldNotBeIncludedInToken)68 TEST(StrTokTest, DelimitersShouldNotBeIncludedInToken) {
69   char src[] = "__ab__:_cd__:__ef__:__";
70   char *token = __llvm_libc::strtok(src, "_:");
71   ASSERT_STREQ(token, "ab");
72   token = __llvm_libc::strtok(nullptr, ":_");
73   ASSERT_STREQ(token, "cd");
74   token = __llvm_libc::strtok(nullptr, "_:,");
75   ASSERT_STREQ(token, "ef");
76   token = __llvm_libc::strtok(nullptr, "_:,_");
77   ASSERT_STREQ(token, nullptr);
78 }
79