|
OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2010 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 <string> | |
6 #include "base/gap_buffer.h" | |
sky
2010/08/31 23:19:47
newline between 5 and 6.
varunjain
2010/11/18 03:45:57
Done.
| |
7 #include "testing/gtest/include/gtest/gtest.h" | |
8 | |
9 namespace base { | |
10 | |
11 class GapBufferTester { | |
12 public: | |
13 GapBufferTester(int a = 1, char b = 'a') | |
sky
2010/08/31 23:19:47
no default params.
varunjain
2010/11/18 03:45:57
Done.
| |
14 : a_(a), b_(b) { } | |
sky
2010/08/31 23:19:47
each member on its own line.
varunjain
2010/11/18 03:45:57
Done.
| |
15 bool operator!=(const GapBufferTester& that) { | |
sky
2010/08/31 23:19:47
Style guides says you should use explicit methods
varunjain
2010/11/18 03:45:57
these operators are no longer required. However, I
| |
16 return this->a_ != that.a_ || this->b_ != that.b_; | |
17 } | |
18 | |
19 void operator=(const GapBufferTester& that) { | |
20 a_ = that.a_; | |
21 b_ = that.b_; | |
22 } | |
23 | |
24 private: | |
25 int a_; | |
26 char b_; | |
27 }; | |
28 | |
29 template <class T> | |
30 bool CompareVectorWithArray(const T* array, int size, std::vector<T> vect) { | |
sky
2010/08/31 23:19:47
const std::vector<T>&
But I believe you can use EX
varunjain
2010/11/18 03:45:57
Done.
| |
31 if ((size_t) size == vect.size()) { | |
sky
2010/08/31 23:19:47
Use C++ style casts.
varunjain
2010/11/18 03:45:57
removed
| |
32 for (int i = 0; i < size; i++) { | |
33 T val1 = array[i]; | |
34 T val2 = vect[i]; | |
35 if (val1 != val2) { | |
36 return false; | |
37 } | |
38 } | |
39 return true; | |
40 } | |
41 return false; | |
42 } | |
43 | |
44 template <class T> | |
45 void Expectations(int current_size, int buffer_size, int contents_size, | |
sky
2010/08/31 23:19:47
each param on its own line.
varunjain
2010/11/18 03:45:57
Done.
| |
46 int gap_start, const T* contents_array, GapBuffer<T>& gap_buffer) { | |
sky
2010/08/31 23:19:47
const GapBuffer&. If it can't be const&, then it s
varunjain
2010/11/18 03:45:57
Done.
| |
47 std::vector<T> contents; | |
48 gap_buffer.GetContents(&(contents)); | |
sky
2010/08/31 23:19:47
No parens around contents, just &contents
varunjain
2010/11/18 03:45:57
Done.
| |
49 EXPECT_EQ(buffer_size, gap_buffer.GetBufferSize()); | |
50 EXPECT_EQ(current_size, gap_buffer.GetCurrentSize()); | |
51 EXPECT_EQ(gap_start, gap_buffer.GetGapStart()); | |
52 EXPECT_EQ((size_t) contents_size, contents.size()); | |
sky
2010/08/31 23:19:47
C++ style casts
varunjain
2010/11/18 03:45:57
Done.
| |
53 EXPECT_TRUE(CompareVectorWithArray(contents_array, contents_size, contents)); | |
sky
2010/08/31 23:19:47
If this test fails the generated failure isn't goi
varunjain
2010/11/18 03:45:57
converted to ExpectThat
| |
54 } | |
55 | |
56 GapBufferTester* GenerateTestData(int count) { | |
57 GapBufferTester* data = new GapBufferTester[count]; | |
58 for (int i = 0; i < count; i++) { | |
59 data[i] = GapBufferTester(i, static_cast<int>('a') + i); | |
60 } | |
61 return data; | |
62 } | |
63 | |
64 TEST(GapBufferTest, InitializationTest) { | |
65 scoped_array<int> array; | |
66 GapBuffer<int> gap_buffer(20); | |
67 array.reset(new int[0]); | |
68 Expectations(0, 20, 0, 0, array.get(), gap_buffer); | |
69 } | |
70 | |
71 TEST(GapBufferTest, InsertionTest) { | |
72 GapBuffer<wchar_t> gap_buffer(20); | |
73 gap_buffer.InsertAtGapStart('a'); | |
74 Expectations(1, 20, 1, 1, L"a", gap_buffer); | |
75 | |
76 gap_buffer.InsertAtGapStart(' '); | |
77 gap_buffer.InsertAtGapStart('t'); | |
78 gap_buffer.InsertAtGapStart('e'); | |
79 gap_buffer.InsertAtGapStart('s'); | |
80 gap_buffer.InsertAtGapStart('t'); | |
81 Expectations(6, 20, 6, 6, L"a test", gap_buffer); | |
82 } | |
83 | |
84 TEST(GapBufferTest, DeletionTest) { | |
85 scoped_array<GapBufferTester> array; | |
86 GapBuffer<GapBufferTester> gap_buffer(10); | |
87 array.reset(new GapBufferTester[0]); | |
88 Expectations(0, 10, 0, 0, array.get(), gap_buffer); | |
89 | |
90 // try to remove on empty buffer. | |
91 gap_buffer.RemoveFromGapStart(); | |
92 gap_buffer.RemoveFromGapEnd(); | |
93 Expectations(0, 10, 0, 0, array.get(), gap_buffer); | |
94 | |
95 // generate some test data. | |
96 array.reset(GenerateTestData(5)); | |
97 | |
98 // fill up the gap buffer. | |
99 for (int i = 0; i < 5; i++) { | |
100 gap_buffer.InsertAtGapStart(array[i]); | |
101 } | |
102 Expectations(5, 10, 5, 5, array.get(), gap_buffer); | |
103 | |
104 // move the gap around so that we can get data on either end of the gap. | |
105 gap_buffer.MoveGapLeft(3); | |
106 gap_buffer.MoveGapRight(1); | |
107 Expectations(5, 10, 5, 3, array.get(), gap_buffer); | |
108 | |
109 // remove some stuff. | |
110 gap_buffer.RemoveFromGapEnd(); | |
111 // update the expected array. | |
112 array.get()[3] = array[4]; | |
113 Expectations(4, 10, 4, 3, array.get(), gap_buffer); | |
114 gap_buffer.RemoveFromGapStart(); | |
115 // update the expected array. | |
116 array.get()[2] = array[3]; | |
117 Expectations(3, 10, 3, 2, array.get(), gap_buffer); | |
118 // try to remove more than available. | |
119 gap_buffer.RemoveFromGapEnd(); | |
120 gap_buffer.RemoveFromGapEnd(); | |
121 gap_buffer.RemoveFromGapEnd(); | |
122 Expectations(2, 10, 2, 2, array.get(), gap_buffer); | |
123 // now from the start. | |
124 gap_buffer.RemoveFromGapStart(); | |
125 gap_buffer.RemoveFromGapStart(); | |
126 gap_buffer.RemoveFromGapStart(); | |
127 Expectations(0, 10, 0, 0, array.get(), gap_buffer); | |
128 } | |
129 | |
130 TEST(GapBufferTest, GapMoveTest) { | |
131 // generate some test data. | |
132 scoped_array<GapBufferTester> array; | |
133 array.reset(GenerateTestData(26)); | |
134 | |
135 GapBuffer<GapBufferTester> gap_buffer(array.get(), 26); | |
136 Expectations(26, 36, 26, 26, array.get(), gap_buffer); | |
137 | |
138 // Move the gap around | |
139 gap_buffer.MoveGapLeft(20); | |
140 Expectations(26, 36, 26, 6, array.get(), gap_buffer); | |
141 gap_buffer.MoveGapRight(15); | |
142 Expectations(26, 36, 26, 21, array.get(), gap_buffer); | |
143 } | |
144 | |
145 TEST(GapBufferTest, ReziseTest) { | |
146 scoped_array<GapBufferTester> array; | |
147 GapBuffer<GapBufferTester> gap_buffer(10); | |
148 array.reset(new GapBufferTester[0]); | |
149 Expectations(0, 10, 0, 0, array.get(), gap_buffer); | |
150 | |
151 // generate some test data. | |
152 array.reset(GenerateTestData(1000)); | |
153 | |
154 // fill up the gap buffer. | |
155 for (int i = 0; i < 1000; i++) { | |
156 gap_buffer.InsertAtGapStart(array[i]); | |
157 } | |
158 | |
159 // the buffer must have resized. It increases 10% of its current size (or 10, | |
160 // which ever is greater) on every resize. The final size should be 1053. | |
161 Expectations(1000, 1053, 1000, 1000, array.get(), gap_buffer); | |
162 } | |
163 | |
164 TEST(GapBufferTest, ConstructorTest) { | |
165 // generate some test data. | |
166 scoped_array<GapBufferTester> array; | |
167 array.reset(GenerateTestData(1000)); | |
168 | |
169 GapBuffer<GapBufferTester> gap_buffer(array.get(), 1000); | |
170 Expectations(1000, 1010, 1000, 1000, array.get(), gap_buffer); | |
171 } | |
172 | |
173 } // namespace | |
OLD | NEW |