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