consecutive_range_visitor_unittest.cc 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // Copyright 2015 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 "courgette/consecutive_range_visitor.h"
  5. #include <stddef.h>
  6. #include <string>
  7. #include "testing/gtest/include/gtest/gtest.h"
  8. namespace courgette {
  9. TEST(ConsecutiveRangeVisitorTest, Basic) {
  10. std::string s = "AAAAABZZZZOO";
  11. ConsecutiveRangeVisitor<std::string::iterator> vis(s.begin(), s.end());
  12. EXPECT_TRUE(vis.has_more());
  13. EXPECT_EQ('A', *vis.cur());
  14. EXPECT_EQ(5U, vis.repeat());
  15. vis.advance();
  16. EXPECT_TRUE(vis.has_more());
  17. EXPECT_EQ('B', *vis.cur());
  18. EXPECT_EQ(1U, vis.repeat());
  19. vis.advance();
  20. EXPECT_TRUE(vis.has_more());
  21. EXPECT_EQ('Z', *vis.cur());
  22. EXPECT_EQ(4U, vis.repeat());
  23. vis.advance();
  24. EXPECT_TRUE(vis.has_more());
  25. EXPECT_EQ('O', *vis.cur());
  26. EXPECT_EQ(2U, vis.repeat());
  27. vis.advance();
  28. EXPECT_FALSE(vis.has_more());
  29. }
  30. TEST(ConsecutiveRangeVisitorTest, UnitRanges) {
  31. // Unsorted, no consecutive characters.
  32. const char s[] = "elephant elephant";
  33. ConsecutiveRangeVisitor<const char*> vis(std::begin(s), std::end(s) - 1);
  34. for (const char* scan = &s[0]; *scan; ++scan) {
  35. EXPECT_TRUE(vis.has_more());
  36. EXPECT_EQ(*scan, *vis.cur());
  37. EXPECT_EQ(1U, vis.repeat());
  38. vis.advance();
  39. }
  40. EXPECT_FALSE(vis.has_more());
  41. }
  42. TEST(ConsecutiveRangeVisitorTest, SingleRange) {
  43. for (size_t len = 1U; len < 10U; ++len) {
  44. std::vector<int> v(len, 137);
  45. ConsecutiveRangeVisitor<std::vector<int>::iterator> vis(v.begin(), v.end());
  46. EXPECT_TRUE(vis.has_more());
  47. EXPECT_EQ(137, *vis.cur());
  48. EXPECT_EQ(len, vis.repeat());
  49. vis.advance();
  50. EXPECT_FALSE(vis.has_more());
  51. }
  52. }
  53. TEST(ConsecutiveRangeVisitorTest, Empty) {
  54. std::string s;
  55. ConsecutiveRangeVisitor<std::string::iterator> vis(s.begin(), s.end());
  56. EXPECT_FALSE(vis.has_more());
  57. }
  58. } // namespace courgette