File: tokenized_string_char_iterator_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (149 lines) | stat: -rw-r--r-- 4,517 bytes parent folder | download | duplicates (7)
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chromeos/ash/components/string_matching/tokenized_string_char_iterator.h"

#include <string>
#include <vector>

#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace ash::string_matching {

namespace {

// Returns a string represents the current state of |iter|. The state string
// has three fields. The first is the current char. The second is the offset of
// the current char in terms of the original text of the TokenizedString. The
// last one is optional and only shows up when IsFirstCharOfToken returns true.
std::string GetIterateState(const TokenizedStringCharIterator& iter) {
  return base::StringPrintf(
      "%s%d%s", base::UTF16ToUTF8(std::u16string(1, iter.Get())).c_str(),
      iter.GetArrayPos(), iter.IsFirstCharOfToken() ? "!" : "");
}

void TestBeyondTheEnd(TokenizedStringCharIterator* iter) {
  ASSERT_TRUE(iter->end());
  ASSERT_FALSE(iter->NextChar());
  ASSERT_FALSE(iter->NextToken());

  // Don't care what it returns, but this shouldn't crash.
  iter->Get();
}

void TestEveryChar(const std::string& text, const std::string& expects) {
  TokenizedString tokens(base::UTF8ToUTF16(text));
  TokenizedStringCharIterator iter(tokens);

  std::vector<std::string> results;
  while (!iter.end()) {
    results.push_back(GetIterateState(iter));
    iter.NextChar();
  }

  EXPECT_EQ(expects, base::JoinString(results, " "));
  TestBeyondTheEnd(&iter);
}

void TestNextToken(const std::string& text, const std::string& expects) {
  TokenizedString tokens(base::UTF8ToUTF16(text));
  TokenizedStringCharIterator iter(tokens);

  std::vector<std::string> results;
  while (!iter.end()) {
    results.push_back(GetIterateState(iter));
    iter.NextToken();
  }

  EXPECT_EQ(expects, base::JoinString(results, " "));
  TestBeyondTheEnd(&iter);
}

void TestFirstTwoCharInEveryToken(const std::string& text,
                                  const std::string& expects) {
  TokenizedString tokens(base::UTF8ToUTF16(text));
  TokenizedStringCharIterator iter(tokens);

  std::vector<std::string> results;
  while (!iter.end()) {
    results.push_back(GetIterateState(iter));
    if (iter.NextChar())
      results.push_back(GetIterateState(iter));

    iter.NextToken();
  }

  EXPECT_EQ(expects, base::JoinString(results, " "));
  TestBeyondTheEnd(&iter);
}

}  // namespace

TEST(TokenizedStringCharIteratorTest, NoTerms) {
  const char* text;

  text = "";
  TestEveryChar(text, "");
  TestNextToken(text, "");
  TestFirstTwoCharInEveryToken(text, "");

  text = "!@#$%^&*()<<<**>>>";
  TestEveryChar(text, "");
  TestNextToken(text, "");
  TestFirstTwoCharInEveryToken(text, "");
}

TEST(TokenizedStringCharIteratorTest, Basic) {
  const char* text;

  text = "c";
  TestEveryChar(text, "c0!");
  TestNextToken(text, "c0!");
  TestFirstTwoCharInEveryToken(text, "c0!");

  text = "Simple";
  TestEveryChar(text, "s0! i1 m2 p3 l4 e5");
  TestNextToken(text, "s0!");
  TestFirstTwoCharInEveryToken(text, "s0! i1");

  text = "ScratchPad";
  TestEveryChar(text, "s0! c1 r2 a3 t4 c5 h6 p7! a8 d9");
  TestNextToken(text, "s0! p7!");
  TestFirstTwoCharInEveryToken(text, "s0! c1 p7! a8");

  text = "Chess2.0";
  TestEveryChar(text, "c0! h1 e2 s3 s4 25! .6 07");
  TestNextToken(text, "c0! 25!");
  TestFirstTwoCharInEveryToken(text, "c0! h1 25! .6");

  text = "Cut the rope";
  TestEveryChar(text, "c0! u1 t2 t4! h5 e6 r8! o9 p10 e11");
  TestNextToken(text, "c0! t4! r8!");
  TestFirstTwoCharInEveryToken(text, "c0! u1 t4! h5 r8! o9");

  text = "AutoCAD WS";
  TestEveryChar(text, "a0! u1 t2 o3 c4! a5 d6 w8! s9");
  TestNextToken(text, "a0! c4! w8!");
  TestFirstTwoCharInEveryToken(text, "a0! u1 c4! a5 w8! s9");

  text = "Great TweetDeck";
  TestEveryChar(text, "g0! r1 e2 a3 t4 t6! w7 e8 e9 t10 d11! e12 c13 k14");
  TestNextToken(text, "g0! t6! d11!");
  TestFirstTwoCharInEveryToken(text, "g0! r1 t6! w7 d11! e12");

  text = "Draw-It!";
  TestEveryChar(text, "d0! r1 a2 w3 i5! t6");
  TestNextToken(text, "d0! i5!");
  TestFirstTwoCharInEveryToken(text, "d0! r1 i5! t6");

  text = "Faxing & Signing";
  TestEveryChar(text, "f0! a1 x2 i3 n4 g5 s9! i10 g11 n12 i13 n14 g15");
  TestNextToken(text, "f0! s9!");
  TestFirstTwoCharInEveryToken(text, "f0! a1 s9! i10");
}

}  // namespace ash::string_matching