strtok_test.cpp
2.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
//===-- Unittests for strtok ---------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "src/string/strtok.h"
#include "utils/UnitTest/Test.h"
TEST(StrTokTest, NoTokenFound) {
char empty[] = "";
ASSERT_STREQ(__llvm_libc::strtok(empty, ""), nullptr);
ASSERT_STREQ(__llvm_libc::strtok(empty, "_"), nullptr);
char single[] = "_";
ASSERT_STREQ(__llvm_libc::strtok(single, ""), "_");
char multiple[] = "1,2";
ASSERT_STREQ(__llvm_libc::strtok(multiple, ":"), "1,2");
}
TEST(StrTokTest, DelimiterAsFirstCharacterShouldBeIgnored) {
char src[] = ".123";
ASSERT_STREQ(__llvm_libc::strtok(src, "."), "123");
}
TEST(StrTokTest, DelimiterIsMiddleCharacter) {
char src[] = "12,34";
ASSERT_STREQ(__llvm_libc::strtok(src, ","), "12");
}
TEST(StrTokTest, DelimiterAsLastCharacterShouldBeIgnored) {
char src[] = "1234:";
ASSERT_STREQ(__llvm_libc::strtok(src, ":"), "1234");
}
TEST(StrTokTest, MultipleDelimiters) {
char src[] = "12,.34";
ASSERT_STREQ(__llvm_libc::strtok(src, "."), "12,");
ASSERT_STREQ(__llvm_libc::strtok(src, ".,"), "12");
ASSERT_STREQ(__llvm_libc::strtok(src, ",."), "12");
ASSERT_STREQ(__llvm_libc::strtok(src, ":,."), "12");
}
TEST(StrTokTest, ShouldNotGoPastNullTerminator) {
char src[] = {'1', '2', '\0', ',', '3'};
ASSERT_STREQ(__llvm_libc::strtok(src, ","), "12");
}
TEST(StrTokTest, SubsequentCallsShouldFindFollowingDelimiters) {
char src[] = "12,34.56";
char *token = __llvm_libc::strtok(src, ",.");
ASSERT_STREQ(token, "12");
token = __llvm_libc::strtok(nullptr, ",.");
ASSERT_STREQ(token, "34");
token = __llvm_libc::strtok(nullptr, ",.");
ASSERT_STREQ(token, "56");
token = __llvm_libc::strtok(nullptr, "_:,_");
ASSERT_STREQ(token, nullptr);
// Subsequent calls after hitting the end of the string should also return
// nullptr.
token = __llvm_libc::strtok(nullptr, "_:,_");
ASSERT_STREQ(token, nullptr);
}
TEST(StrTokTest, DelimitersShouldNotBeIncludedInToken) {
char src[] = "__ab__:_cd__:__ef__:__";
char *token = __llvm_libc::strtok(src, "_:");
ASSERT_STREQ(token, "ab");
token = __llvm_libc::strtok(nullptr, ":_");
ASSERT_STREQ(token, "cd");
token = __llvm_libc::strtok(nullptr, "_:,");
ASSERT_STREQ(token, "ef");
token = __llvm_libc::strtok(nullptr, "_:,_");
ASSERT_STREQ(token, nullptr);
}