Chromium Code Reviews| Index: courgette/consecutive_range_visitor_unittest.cc |
| diff --git a/courgette/consecutive_range_visitor_unittest.cc b/courgette/consecutive_range_visitor_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..98256cb467c8dd21e9da06afb7789903fd5692c0 |
| --- /dev/null |
| +++ b/courgette/consecutive_range_visitor_unittest.cc |
| @@ -0,0 +1,76 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include <cstring> |
| +#include <string> |
| + |
| +#include "courgette/consecutive_range_visitor.h" |
| + |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +namespace courgette { |
| + |
| +namespace { |
|
grt (UTC plus 2)
2015/12/03 15:19:09
i prefer that tests not be in the unnamed namespac
huangs
2015/12/03 19:29:06
Done.
|
| + |
| +TEST(ConsecutiveRangeVisitorTest, Basic) { |
| + std::string s = "AAAAABZZZZOO"; |
| + ConsecutiveRangeVisitor<std::string::iterator> vis(s.begin(), s.end()); |
| + EXPECT_TRUE(vis.has_more()); |
| + EXPECT_EQ('A', *vis.cur()); |
| + EXPECT_EQ(5U, vis.repeat()); |
| + vis.advance(); |
| + |
| + EXPECT_TRUE(vis.has_more()); |
| + EXPECT_EQ('B', *vis.cur()); |
| + EXPECT_EQ(1U, vis.repeat()); |
| + vis.advance(); |
| + |
| + EXPECT_TRUE(vis.has_more()); |
| + EXPECT_EQ('Z', *vis.cur()); |
| + EXPECT_EQ(4U, vis.repeat()); |
| + vis.advance(); |
| + |
| + EXPECT_TRUE(vis.has_more()); |
| + EXPECT_EQ('O', *vis.cur()); |
| + EXPECT_EQ(2U, vis.repeat()); |
| + vis.advance(); |
| + |
| + EXPECT_FALSE(vis.has_more()); |
| +} |
| + |
| +TEST(ConsecutiveRangeVisitorTest, UnitRanges) { |
| + // Unsorted, no consecutive characters. |
| + char s[] = "elephant elephant"; |
|
grt (UTC plus 2)
2015/12/03 15:19:09
const char s[] = ...;
huangs
2015/12/03 19:29:06
Done.
|
| + size_t len = ::strlen(s); |
|
grt (UTC plus 2)
2015/12/03 15:19:09
this and #include <cstring> aren't needed
huangs
2015/12/03 19:29:06
Done.
|
| + ConsecutiveRangeVisitor<char*> vis(s, s + len); |
|
grt (UTC plus 2)
2015/12/03 15:19:09
as per http://chromium-cpp.appspot.com/#library-wh
huangs
2015/12/03 19:29:06
Done, but <iterator> is already included in the .h
|
| + for (size_t i = 0; i < len; ++i) { |
|
grt (UTC plus 2)
2015/12/03 15:19:08
simpler to loop until the string terminator is fou
huangs
2015/12/03 19:29:06
Done.
|
| + EXPECT_TRUE(vis.has_more()); |
| + EXPECT_EQ(s[i], *vis.cur()); |
| + EXPECT_EQ(1U, vis.repeat()); |
| + vis.advance(); |
| + } |
| + EXPECT_FALSE(vis.has_more()); |
| +} |
| + |
| +TEST(ConsecutiveRangeVisitorTest, SingleRange) { |
| + for (size_t len = 1; len < 10; ++len) { |
| + std::vector<int> v(len, 137); |
| + ConsecutiveRangeVisitor<std::vector<int>::iterator> vis(v.begin(), v.end()); |
| + EXPECT_TRUE(vis.has_more()); |
| + EXPECT_EQ(137, *vis.cur()); |
| + EXPECT_EQ(len, vis.repeat()); |
| + vis.advance(); |
| + EXPECT_FALSE(vis.has_more()); |
| + } |
| +} |
| + |
| +TEST(ConsecutiveRangeVisitorTest, Empty) { |
| + std::string s; |
| + ConsecutiveRangeVisitor<std::string::iterator> vis(s.begin(), s.end()); |
| + EXPECT_FALSE(vis.has_more()); |
| +} |
| + |
| +} // namespace |
| + |
| +} // namespace courgette |