Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libc / test / src / string / strtok_test.cpp
blobb82065309e00c65dbc72049acaf683c8c5010fe3
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 //===----------------------------------------------------------------------===//
9 #include "src/string/strtok.h"
10 #include "test/UnitTest/Test.h"
12 TEST(LlvmLibcStrTokTest, NoTokenFound) {
13 char empty[] = "";
14 ASSERT_STREQ(LIBC_NAMESPACE::strtok(empty, ""), nullptr);
15 ASSERT_STREQ(LIBC_NAMESPACE::strtok(empty, "_"), nullptr);
17 char single[] = "_";
18 ASSERT_STREQ(LIBC_NAMESPACE::strtok(single, ""), "_");
20 char multiple[] = "1,2";
21 ASSERT_STREQ(LIBC_NAMESPACE::strtok(multiple, ":"), "1,2");
24 TEST(LlvmLibcStrTokTest, DelimiterAsFirstCharacterShouldBeIgnored) {
25 char src[] = ".123";
26 ASSERT_STREQ(LIBC_NAMESPACE::strtok(src, "."), "123");
29 TEST(LlvmLibcStrTokTest, DelimiterIsMiddleCharacter) {
30 char src[] = "12,34";
31 ASSERT_STREQ(LIBC_NAMESPACE::strtok(src, ","), "12");
34 TEST(LlvmLibcStrTokTest, DelimiterAsLastCharacterShouldBeIgnored) {
35 char src[] = "1234:";
36 ASSERT_STREQ(LIBC_NAMESPACE::strtok(src, ":"), "1234");
39 TEST(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");
47 TEST(LlvmLibcStrTokTest, ShouldNotGoPastNullTerminator) {
48 char src[] = {'1', '2', '\0', ',', '3'};
49 ASSERT_STREQ(LIBC_NAMESPACE::strtok(src, ","), "12");
52 TEST(LlvmLibcStrTokTest, SubsequentCallsShouldFindFollowingDelimiters) {
53 char src[] = "12,34.56";
54 char *token = LIBC_NAMESPACE::strtok(src, ",.");
55 ASSERT_STREQ(token, "12");
56 token = LIBC_NAMESPACE::strtok(nullptr, ",.");
57 ASSERT_STREQ(token, "34");
58 token = LIBC_NAMESPACE::strtok(nullptr, ",.");
59 ASSERT_STREQ(token, "56");
60 token = LIBC_NAMESPACE::strtok(nullptr, "_:,_");
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(nullptr, "_:,_");
65 ASSERT_STREQ(token, nullptr);
68 TEST(LlvmLibcStrTokTest, DelimitersShouldNotBeIncludedInToken) {
69 char src[] = "__ab__:_cd__:__ef__:__";
70 char *token = LIBC_NAMESPACE::strtok(src, "_:");
71 ASSERT_STREQ(token, "ab");
72 token = LIBC_NAMESPACE::strtok(nullptr, ":_");
73 ASSERT_STREQ(token, "cd");
74 token = LIBC_NAMESPACE::strtok(nullptr, "_:,");
75 ASSERT_STREQ(token, "ef");
76 token = LIBC_NAMESPACE::strtok(nullptr, "_:,_");
77 ASSERT_STREQ(token, nullptr);