Chromium Code Reviews| Index: base/gap_buffer_unittest.cc |
| =================================================================== |
| --- base/gap_buffer_unittest.cc (revision 0) |
| +++ base/gap_buffer_unittest.cc (revision 0) |
| @@ -0,0 +1,173 @@ |
| +// Copyright (c) 2010 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 <string> |
| +#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.
|
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +namespace base { |
| + |
| +class GapBufferTester { |
| + public: |
| + 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.
|
| + : 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.
|
| + 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
|
| + return this->a_ != that.a_ || this->b_ != that.b_; |
| + } |
| + |
| + void operator=(const GapBufferTester& that) { |
| + a_ = that.a_; |
| + b_ = that.b_; |
| + } |
| + |
| + private: |
| + int a_; |
| + char b_; |
| +}; |
| + |
| +template <class T> |
| +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.
|
| + 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
|
| + for (int i = 0; i < size; i++) { |
| + T val1 = array[i]; |
| + T val2 = vect[i]; |
| + if (val1 != val2) { |
| + return false; |
| + } |
| + } |
| + return true; |
| + } |
| + return false; |
| +} |
| + |
| +template <class T> |
| +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.
|
| + 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.
|
| + std::vector<T> contents; |
| + 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.
|
| + EXPECT_EQ(buffer_size, gap_buffer.GetBufferSize()); |
| + EXPECT_EQ(current_size, gap_buffer.GetCurrentSize()); |
| + EXPECT_EQ(gap_start, gap_buffer.GetGapStart()); |
| + 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.
|
| + 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
|
| +} |
| + |
| +GapBufferTester* GenerateTestData(int count) { |
| + GapBufferTester* data = new GapBufferTester[count]; |
| + for (int i = 0; i < count; i++) { |
| + data[i] = GapBufferTester(i, static_cast<int>('a') + i); |
| + } |
| + return data; |
| +} |
| + |
| +TEST(GapBufferTest, InitializationTest) { |
| + scoped_array<int> array; |
| + GapBuffer<int> gap_buffer(20); |
| + array.reset(new int[0]); |
| + Expectations(0, 20, 0, 0, array.get(), gap_buffer); |
| +} |
| + |
| +TEST(GapBufferTest, InsertionTest) { |
| + GapBuffer<wchar_t> gap_buffer(20); |
| + gap_buffer.InsertAtGapStart('a'); |
| + Expectations(1, 20, 1, 1, L"a", gap_buffer); |
| + |
| + gap_buffer.InsertAtGapStart(' '); |
| + gap_buffer.InsertAtGapStart('t'); |
| + gap_buffer.InsertAtGapStart('e'); |
| + gap_buffer.InsertAtGapStart('s'); |
| + gap_buffer.InsertAtGapStart('t'); |
| + Expectations(6, 20, 6, 6, L"a test", gap_buffer); |
| +} |
| + |
| +TEST(GapBufferTest, DeletionTest) { |
| + scoped_array<GapBufferTester> array; |
| + GapBuffer<GapBufferTester> gap_buffer(10); |
| + array.reset(new GapBufferTester[0]); |
| + Expectations(0, 10, 0, 0, array.get(), gap_buffer); |
| + |
| + // try to remove on empty buffer. |
| + gap_buffer.RemoveFromGapStart(); |
| + gap_buffer.RemoveFromGapEnd(); |
| + Expectations(0, 10, 0, 0, array.get(), gap_buffer); |
| + |
| + // generate some test data. |
| + array.reset(GenerateTestData(5)); |
| + |
| + // fill up the gap buffer. |
| + for (int i = 0; i < 5; i++) { |
| + gap_buffer.InsertAtGapStart(array[i]); |
| + } |
| + Expectations(5, 10, 5, 5, array.get(), gap_buffer); |
| + |
| + // move the gap around so that we can get data on either end of the gap. |
| + gap_buffer.MoveGapLeft(3); |
| + gap_buffer.MoveGapRight(1); |
| + Expectations(5, 10, 5, 3, array.get(), gap_buffer); |
| + |
| + // remove some stuff. |
| + gap_buffer.RemoveFromGapEnd(); |
| + // update the expected array. |
| + array.get()[3] = array[4]; |
| + Expectations(4, 10, 4, 3, array.get(), gap_buffer); |
| + gap_buffer.RemoveFromGapStart(); |
| + // update the expected array. |
| + array.get()[2] = array[3]; |
| + Expectations(3, 10, 3, 2, array.get(), gap_buffer); |
| + // try to remove more than available. |
| + gap_buffer.RemoveFromGapEnd(); |
| + gap_buffer.RemoveFromGapEnd(); |
| + gap_buffer.RemoveFromGapEnd(); |
| + Expectations(2, 10, 2, 2, array.get(), gap_buffer); |
| + // now from the start. |
| + gap_buffer.RemoveFromGapStart(); |
| + gap_buffer.RemoveFromGapStart(); |
| + gap_buffer.RemoveFromGapStart(); |
| + Expectations(0, 10, 0, 0, array.get(), gap_buffer); |
| +} |
| + |
| +TEST(GapBufferTest, GapMoveTest) { |
| + // generate some test data. |
| + scoped_array<GapBufferTester> array; |
| + array.reset(GenerateTestData(26)); |
| + |
| + GapBuffer<GapBufferTester> gap_buffer(array.get(), 26); |
| + Expectations(26, 36, 26, 26, array.get(), gap_buffer); |
| + |
| + // Move the gap around |
| + gap_buffer.MoveGapLeft(20); |
| + Expectations(26, 36, 26, 6, array.get(), gap_buffer); |
| + gap_buffer.MoveGapRight(15); |
| + Expectations(26, 36, 26, 21, array.get(), gap_buffer); |
| +} |
| + |
| +TEST(GapBufferTest, ReziseTest) { |
| + scoped_array<GapBufferTester> array; |
| + GapBuffer<GapBufferTester> gap_buffer(10); |
| + array.reset(new GapBufferTester[0]); |
| + Expectations(0, 10, 0, 0, array.get(), gap_buffer); |
| + |
| + // generate some test data. |
| + array.reset(GenerateTestData(1000)); |
| + |
| + // fill up the gap buffer. |
| + for (int i = 0; i < 1000; i++) { |
| + gap_buffer.InsertAtGapStart(array[i]); |
| + } |
| + |
| + // the buffer must have resized. It increases 10% of its current size (or 10, |
| + // which ever is greater) on every resize. The final size should be 1053. |
| + Expectations(1000, 1053, 1000, 1000, array.get(), gap_buffer); |
| +} |
| + |
| +TEST(GapBufferTest, ConstructorTest) { |
| + // generate some test data. |
| + scoped_array<GapBufferTester> array; |
| + array.reset(GenerateTestData(1000)); |
| + |
| + GapBuffer<GapBufferTester> gap_buffer(array.get(), 1000); |
| + Expectations(1000, 1010, 1000, 1000, array.get(), gap_buffer); |
| +} |
| + |
| +} // namespace |
| Property changes on: base/gap_buffer_unittest.cc |
| ___________________________________________________________________ |
| Added: svn:eol-style |
| + LF |