string_splitter_unittest.cc 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // Copyright 2016 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "components/url_pattern_index/string_splitter.h"
  5. #include <string>
  6. #include <vector>
  7. #include "base/strings/string_piece.h"
  8. #include "testing/gtest/include/gtest/gtest.h"
  9. namespace url_pattern_index {
  10. namespace {
  11. bool IsTestSeparator(char c) {
  12. return c == ' ' || c == '\t' || c == ',';
  13. }
  14. }
  15. TEST(StringSplitterTest, SplitWithEmptyResult) {
  16. const char* const kStrings[] = {
  17. "", " ", "\t", ",", " \t ", ",,,,", "\t\t\t",
  18. };
  19. for (const char* string : kStrings) {
  20. auto splitter = CreateStringSplitter(string, IsTestSeparator);
  21. // Explicitly verify both operator== and operator!=.
  22. EXPECT_TRUE(splitter.begin() == splitter.end());
  23. EXPECT_FALSE(splitter.begin() != splitter.end());
  24. }
  25. }
  26. TEST(StringSplitterTest, SplitOneWord) {
  27. const char* const kLongStrings[] = {
  28. "word", " word ", " word", "word ", ",word,",
  29. "\tword\t", " word ", "word ", " word", ", word, \t",
  30. };
  31. const char* const kShortStrings[] = {
  32. "w", " w ", " w", "w ", " w ", " w", "w ", ", w, ", "w, \t",
  33. };
  34. const char kLongWord[] = "word";
  35. const char kShortWord[] = "w";
  36. auto expect_word = [](const char* text, const char* word) {
  37. auto splitter = CreateStringSplitter(text, IsTestSeparator);
  38. // Explicitly verify both operator== and operator!=.
  39. EXPECT_TRUE(splitter.begin() != splitter.end());
  40. EXPECT_FALSE(splitter.begin() == splitter.end());
  41. EXPECT_EQ(splitter.end(), ++splitter.begin());
  42. EXPECT_EQ(word, *splitter.begin());
  43. auto iterator = splitter.begin();
  44. EXPECT_EQ(splitter.begin(), iterator++);
  45. EXPECT_EQ(splitter.end(), iterator);
  46. };
  47. for (const char* string : kLongStrings)
  48. expect_word(string, kLongWord);
  49. for (const char* string : kShortStrings)
  50. expect_word(string, kShortWord);
  51. }
  52. TEST(StringSplitterTest, SplitThreeWords) {
  53. const char* const kStrings[] = {
  54. "one two three", " one two three ", " one two, three",
  55. "one,two\t\t three", "one, two, three, ",
  56. };
  57. const std::vector<base::StringPiece> kResults = {
  58. "one", "two", "three",
  59. };
  60. for (const char* string : kStrings) {
  61. auto splitter = CreateStringSplitter(string, IsTestSeparator);
  62. std::vector<base::StringPiece> tokens(splitter.begin(), splitter.end());
  63. EXPECT_EQ(kResults, tokens);
  64. }
  65. }
  66. } // namespace url_pattern_index