OLD | NEW |
---|---|
(Empty) | |
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 <cstring> | |
6 #include <string> | |
7 | |
8 #include "courgette/consecutive_range_visitor.h" | |
9 | |
10 #include "testing/gtest/include/gtest/gtest.h" | |
11 | |
12 namespace courgette { | |
13 | |
14 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.
| |
15 | |
16 TEST(ConsecutiveRangeVisitorTest, Basic) { | |
17 std::string s = "AAAAABZZZZOO"; | |
18 ConsecutiveRangeVisitor<std::string::iterator> vis(s.begin(), s.end()); | |
19 EXPECT_TRUE(vis.has_more()); | |
20 EXPECT_EQ('A', *vis.cur()); | |
21 EXPECT_EQ(5U, vis.repeat()); | |
22 vis.advance(); | |
23 | |
24 EXPECT_TRUE(vis.has_more()); | |
25 EXPECT_EQ('B', *vis.cur()); | |
26 EXPECT_EQ(1U, vis.repeat()); | |
27 vis.advance(); | |
28 | |
29 EXPECT_TRUE(vis.has_more()); | |
30 EXPECT_EQ('Z', *vis.cur()); | |
31 EXPECT_EQ(4U, vis.repeat()); | |
32 vis.advance(); | |
33 | |
34 EXPECT_TRUE(vis.has_more()); | |
35 EXPECT_EQ('O', *vis.cur()); | |
36 EXPECT_EQ(2U, vis.repeat()); | |
37 vis.advance(); | |
38 | |
39 EXPECT_FALSE(vis.has_more()); | |
40 } | |
41 | |
42 TEST(ConsecutiveRangeVisitorTest, UnitRanges) { | |
43 // Unsorted, no consecutive characters. | |
44 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.
| |
45 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.
| |
46 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
| |
47 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.
| |
48 EXPECT_TRUE(vis.has_more()); | |
49 EXPECT_EQ(s[i], *vis.cur()); | |
50 EXPECT_EQ(1U, vis.repeat()); | |
51 vis.advance(); | |
52 } | |
53 EXPECT_FALSE(vis.has_more()); | |
54 } | |
55 | |
56 TEST(ConsecutiveRangeVisitorTest, SingleRange) { | |
57 for (size_t len = 1; len < 10; ++len) { | |
58 std::vector<int> v(len, 137); | |
59 ConsecutiveRangeVisitor<std::vector<int>::iterator> vis(v.begin(), v.end()); | |
60 EXPECT_TRUE(vis.has_more()); | |
61 EXPECT_EQ(137, *vis.cur()); | |
62 EXPECT_EQ(len, vis.repeat()); | |
63 vis.advance(); | |
64 EXPECT_FALSE(vis.has_more()); | |
65 } | |
66 } | |
67 | |
68 TEST(ConsecutiveRangeVisitorTest, Empty) { | |
69 std::string s; | |
70 ConsecutiveRangeVisitor<std::string::iterator> vis(s.begin(), s.end()); | |
71 EXPECT_FALSE(vis.has_more()); | |
72 } | |
73 | |
74 } // namespace | |
75 | |
76 } // namespace courgette | |
OLD | NEW |