huangs | 7a2fea25 | 2015-12-08 01:27:46 | [diff] [blame] | 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 | |
| 5 | #include "courgette/consecutive_range_visitor.h" |
| 6 | |
avi | ab98dcc9 | 2015-12-21 19:35:33 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | |
huangs | 7a2fea25 | 2015-12-08 01:27:46 | [diff] [blame] | 9 | #include <string> |
| 10 | |
| 11 | #include "testing/gtest/include/gtest/gtest.h" |
| 12 | |
| 13 | namespace courgette { |
| 14 | |
| 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()); |
| 21 | vis.advance(); |
| 22 | |
| 23 | EXPECT_TRUE(vis.has_more()); |
| 24 | EXPECT_EQ('B', *vis.cur()); |
| 25 | EXPECT_EQ(1U, vis.repeat()); |
| 26 | vis.advance(); |
| 27 | |
| 28 | EXPECT_TRUE(vis.has_more()); |
| 29 | EXPECT_EQ('Z', *vis.cur()); |
| 30 | EXPECT_EQ(4U, vis.repeat()); |
| 31 | vis.advance(); |
| 32 | |
| 33 | EXPECT_TRUE(vis.has_more()); |
| 34 | EXPECT_EQ('O', *vis.cur()); |
| 35 | EXPECT_EQ(2U, vis.repeat()); |
| 36 | vis.advance(); |
| 37 | |
| 38 | EXPECT_FALSE(vis.has_more()); |
| 39 | } |
| 40 | |
| 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()); |
| 49 | vis.advance(); |
| 50 | } |
| 51 | EXPECT_FALSE(vis.has_more()); |
| 52 | } |
| 53 | |
| 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()); |
| 61 | vis.advance(); |
| 62 | EXPECT_FALSE(vis.has_more()); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | TEST(ConsecutiveRangeVisitorTest, Empty) { |
| 67 | std::string s; |
| 68 | ConsecutiveRangeVisitor<std::string::iterator> vis(s.begin(), s.end()); |
| 69 | EXPECT_FALSE(vis.has_more()); |
| 70 | } |
| 71 | |
| 72 | } // namespace courgette |