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.
5 #include "courgette/consecutive_range_visitor.h"
11 #include "testing/gtest/include/gtest/gtest.h"
15 TEST(ConsecutiveRangeVisitorTest, Basic) {
16 std::string s = "AAAAABZZZZOO";
17 ConsecutiveRangeVisitor<std::string::iterator> vis(s.begin(), s.end());
18 EXPECT_TRUE(vis.has_more());
19 EXPECT_EQ('A', *vis.cur());
20 EXPECT_EQ(5U, vis.repeat());
23 EXPECT_TRUE(vis.has_more());
24 EXPECT_EQ('B', *vis.cur());
25 EXPECT_EQ(1U, vis.repeat());
28 EXPECT_TRUE(vis.has_more());
29 EXPECT_EQ('Z', *vis.cur());
30 EXPECT_EQ(4U, vis.repeat());
33 EXPECT_TRUE(vis.has_more());
34 EXPECT_EQ('O', *vis.cur());
35 EXPECT_EQ(2U, vis.repeat());
38 EXPECT_FALSE(vis.has_more());
41 TEST(ConsecutiveRangeVisitorTest, UnitRanges) {
42 // Unsorted, no consecutive characters.
43 const char s[] = "elephant elephant";
44 ConsecutiveRangeVisitor<const char*> vis(std::begin(s), std::end(s) - 1);
45 for (const char* scan = &s[0]; *scan; ++scan) {
46 EXPECT_TRUE(vis.has_more());
47 EXPECT_EQ(*scan, *vis.cur());
48 EXPECT_EQ(1U, vis.repeat());
51 EXPECT_FALSE(vis.has_more());
54 TEST(ConsecutiveRangeVisitorTest, SingleRange) {
55 for (size_t len = 1U; len < 10U; ++len) {
56 std::vector<int> v(len, 137);
57 ConsecutiveRangeVisitor<std::vector<int>::iterator> vis(v.begin(), v.end());
58 EXPECT_TRUE(vis.has_more());
59 EXPECT_EQ(137, *vis.cur());
60 EXPECT_EQ(len, vis.repeat());
62 EXPECT_FALSE(vis.has_more());
66 TEST(ConsecutiveRangeVisitorTest, Empty) {
68 ConsecutiveRangeVisitor<std::string::iterator> vis(s.begin(), s.end());
69 EXPECT_FALSE(vis.has_more());
72 } // namespace courgette