[NFC][Coroutines] Use structured binding with llvm::enumerate in CoroSplit (#116879)
[llvm-project.git] / lldb / unittests / Utility / StringLexerTest.cpp
blobf7a81bddceae1ab97fa32e4cd7cccb46166c1e22
1 //===-- StringLexerTest.cpp -----------------------------------------------===//
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 "lldb/Utility/StringLexer.h"
10 #include "gtest/gtest.h"
12 using namespace lldb_private;
14 TEST(StringLexerTest, GetUnlexed) {
15 StringLexer l("foo");
16 EXPECT_EQ("foo", l.GetUnlexed());
17 l.Next();
18 EXPECT_EQ("oo", l.GetUnlexed());
19 l.Next();
20 l.Next();
21 EXPECT_EQ("", l.GetUnlexed());
24 TEST(StringLexerTest, HasAtLeast) {
25 StringLexer l("foo");
26 EXPECT_FALSE(l.HasAtLeast(5));
27 EXPECT_FALSE(l.HasAtLeast(4));
28 EXPECT_TRUE(l.HasAtLeast(3));
29 EXPECT_TRUE(l.HasAtLeast(2));
30 EXPECT_TRUE(l.HasAtLeast(1));
32 l.Next();
33 EXPECT_FALSE(l.HasAtLeast(5));
34 EXPECT_FALSE(l.HasAtLeast(4));
35 EXPECT_FALSE(l.HasAtLeast(3));
36 EXPECT_TRUE(l.HasAtLeast(2));
37 EXPECT_TRUE(l.HasAtLeast(1));
39 l.Next();
40 l.Next();
41 EXPECT_FALSE(l.HasAtLeast(5));
42 EXPECT_FALSE(l.HasAtLeast(4));
43 EXPECT_FALSE(l.HasAtLeast(3));
44 EXPECT_FALSE(l.HasAtLeast(2));
45 EXPECT_FALSE(l.HasAtLeast(1));
48 TEST(StringLexerTest, AdvanceIf) {
49 StringLexer l("foobar");
51 EXPECT_FALSE(l.AdvanceIf("oo"));
52 // Skip the "fo" part.
53 EXPECT_TRUE(l.AdvanceIf("fo"));
54 EXPECT_FALSE(l.AdvanceIf("obarz"));
55 // Skip the remaining string.
56 EXPECT_TRUE(l.AdvanceIf("obar"));
58 EXPECT_FALSE(l.AdvanceIf("obarz"));
59 EXPECT_FALSE(l.AdvanceIf("foo"));
60 EXPECT_FALSE(l.AdvanceIf("o"));
61 EXPECT_FALSE(l.AdvanceIf(" "));
64 TEST(StringLexerTest, PutBack) {
65 StringLexer l("foo");
67 l.Next();
68 l.PutBack(1);
69 EXPECT_EQ("foo", l.GetUnlexed());
71 l.Next();
72 l.Next();
73 l.Next();
74 l.PutBack(2);
75 EXPECT_EQ("oo", l.GetUnlexed());
77 l.PutBack(1);
78 EXPECT_EQ("foo", l.GetUnlexed());
81 TEST(StringLexerTest, Peek) {
82 StringLexer l("foo");
84 EXPECT_EQ('f', l.Peek());
85 l.Next();
86 EXPECT_EQ('o', l.Peek());
87 l.Next();
88 EXPECT_EQ('o', l.Peek());
91 TEST(StringLexerTest, Next) {
92 StringLexer l("foo");
93 EXPECT_EQ('f', l.Next());
94 EXPECT_EQ('o', l.Next());
95 EXPECT_EQ('o', l.Next());
98 TEST(StringLexerTest, NextIf) {
99 StringLexer l("foo");
101 EXPECT_FALSE(l.NextIf('\0'));
102 EXPECT_FALSE(l.NextIf(' '));
103 EXPECT_FALSE(l.NextIf('o'));
105 EXPECT_TRUE(l.NextIf('f'));
107 EXPECT_FALSE(l.NextIf('\0'));
108 EXPECT_FALSE(l.NextIf(' '));
109 EXPECT_FALSE(l.NextIf('f'));
111 EXPECT_TRUE(l.NextIf('o'));
113 EXPECT_FALSE(l.NextIf('\0'));
114 EXPECT_FALSE(l.NextIf(' '));
115 EXPECT_FALSE(l.NextIf('f'));
117 EXPECT_TRUE(l.NextIf('o'));
120 TEST(StringLexerTest, NextIfList) {
121 StringLexer l("foo");
123 EXPECT_FALSE(l.NextIf({'\0', ' ', 'o'}).first);
125 auto r = l.NextIf({'f'});
126 EXPECT_TRUE(r.first);
127 EXPECT_EQ('f', r.second);
129 EXPECT_FALSE(l.NextIf({'\0', ' ', 'f'}).first);
131 r = l.NextIf({'f', 'o'});
132 EXPECT_TRUE(r.first);
133 EXPECT_EQ('o', r.second);
135 EXPECT_FALSE(l.NextIf({'\0', ' ', 'f'}).first);
137 r = l.NextIf({'*', 'f', 'o', 'o'});
138 EXPECT_TRUE(r.first);
139 EXPECT_EQ('o', r.second);