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

source code of libc/test/src/string/strtok_test.cpp