diff options
-rw-r--r-- | base/SConscript | 2 | ||||
-rw-r--r-- | base/build/base_unittests.vcproj | 8 | ||||
-rw-r--r-- | base/scoped_ptr.h | 39 | ||||
-rw-r--r-- | base/scoped_ptr_unittest.cc | 169 | ||||
-rw-r--r-- | base/tuple.h | 24 | ||||
-rw-r--r-- | base/tuple_unittest.cc | 109 |
6 files changed, 346 insertions, 5 deletions
diff --git a/base/SConscript b/base/SConscript index e6723e7..e5682ca2 100644 --- a/base/SConscript +++ b/base/SConscript @@ -254,6 +254,7 @@ test_files = [ 'pr_time_test.cc', 'ref_counted_unittest.cc', 'run_all_unittests.cc', + 'scoped_ptr_unittest.cc', 'sha2_unittest.cc', 'singleton_unittest.cc', 'stack_container_unittest.cc', @@ -264,6 +265,7 @@ test_files = [ 'thread_unittest.cc', 'time_unittest.cc', 'tracked_objects_test.cc', + 'tuple_unittest.cc', 'values_unittest.cc', 'waitable_event_unittest.cc', 'word_iterator_unittest.cc', diff --git a/base/build/base_unittests.vcproj b/base/build/base_unittests.vcproj index fa32036..8eb9a94 100644 --- a/base/build/base_unittests.vcproj +++ b/base/build/base_unittests.vcproj @@ -252,6 +252,10 @@ > </File> <File + RelativePath="..\scoped_ptr_unittest.cc" + > + </File> + <File RelativePath="..\sha2_unittest.cc" > </File> @@ -316,6 +320,10 @@ > </File> <File + RelativePath="..\tuple_unittest.cc" + > + </File> + <File RelativePath="..\values_unittest.cc" > </File> diff --git a/base/scoped_ptr.h b/base/scoped_ptr.h index 3abaec8..4489f2da 100644 --- a/base/scoped_ptr.h +++ b/base/scoped_ptr.h @@ -1,10 +1,40 @@ // Copyright (c) 2006-2008 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. -// All Rights Reserved. -#ifndef BASE_SCOPED_PTR_H__ -#define BASE_SCOPED_PTR_H__ +// Scopers help you manage ownership of a pointer, helping you easily manage the +// a pointer within a scope, and automatically destroying the pointer at the +// end of a scope. There are two main classes you will use, which coorespond +// to the operators new/delete and new[]/delete[]. +// +// Example usage (scoped_ptr): +// { +// scoped_ptr<Foo> foo(new Foo("wee")); +// } // foo goes out of scope, releasing the pointer with it. +// +// { +// scoped_ptr<Foo> foo; // No pointer managed. +// foo.reset(new Foo("wee")); // Now a pointer is managed. +// foo.reset(new Foo("wee2")); // Foo("wee") was destroyed. +// foo.reset(new Foo("wee3")); // Foo("wee2") was destroyed. +// foo->Method(); // Foo::Method() called. +// foo.get()->Method(); // Foo::Method() called. +// SomeFunc(foo.Release()); // SomeFunc takes owernship, foo no longer +// // manages a pointer. +// foo.reset(new Foo("wee4")); // foo manages a pointer again. +// foo.reset(); // Foo("wee4") destroyed, foo no longer +// // manages a pointer. +// } // foo wasn't managing a pointer, so nothing was destroyed. +// +// Example usage (scoped_array): +// { +// scoped_array<Foo> foo(new Foo[100]); +// foo.get()->Method(); // Foo::Method on the 0th element. +// foo[10].Method(); // Foo::Method on the 10th element. +// } + +#ifndef BASE_SCOPED_PTR_H_ +#define BASE_SCOPED_PTR_H_ // This is an implementation designed to match the anticipated future TR2 // implementation of the scoped_ptr class, and its closely-related brethren, @@ -347,5 +377,4 @@ bool operator!=(C* p, const scoped_ptr_malloc<C, FP>& b) { return p != b.get(); } -#endif // BASE_SCOPED_PTR_H__ - +#endif // BASE_SCOPED_PTR_H_ diff --git a/base/scoped_ptr_unittest.cc b/base/scoped_ptr_unittest.cc new file mode 100644 index 0000000..9ea2289 --- /dev/null +++ b/base/scoped_ptr_unittest.cc @@ -0,0 +1,169 @@ +// Copyright (c) 2006-2008 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 "base/basictypes.h" +#include "base/scoped_ptr.h" +#include "testing/gtest/include/gtest/gtest.h" + +namespace { + +class ConDecLogger { + public: + ConDecLogger() : ptr_(NULL) { } + explicit ConDecLogger(int* ptr) { set_ptr(ptr); } + ~ConDecLogger() { --*ptr_; } + + void set_ptr(int* ptr) { ptr_ = ptr; ++*ptr_; } + + int SomeMeth(int x) { return x; } + + private: + int* ptr_; + DISALLOW_COPY_AND_ASSIGN(ConDecLogger); +}; + +} // namespace + +TEST(ScopedPtrTest, ScopedPtr) { + int constructed = 0; + + { + scoped_ptr<ConDecLogger> scoper(new ConDecLogger(&constructed)); + EXPECT_EQ(1, constructed); + EXPECT_TRUE(scoper.get()); + + EXPECT_EQ(10, scoper->SomeMeth(10)); + EXPECT_EQ(10, scoper.get()->SomeMeth(10)); + EXPECT_EQ(10, (*scoper).SomeMeth(10)); + } + EXPECT_EQ(0, constructed); + + // Test reset() and release() + { + scoped_ptr<ConDecLogger> scoper(new ConDecLogger(&constructed)); + EXPECT_EQ(1, constructed); + EXPECT_TRUE(scoper.get()); + + scoper.reset(new ConDecLogger(&constructed)); + EXPECT_EQ(1, constructed); + EXPECT_TRUE(scoper.get()); + + scoper.reset(); + EXPECT_EQ(0, constructed); + EXPECT_FALSE(scoper.get()); + + scoper.reset(new ConDecLogger(&constructed)); + EXPECT_EQ(1, constructed); + EXPECT_TRUE(scoper.get()); + + ConDecLogger* take = scoper.release(); + EXPECT_EQ(1, constructed); + EXPECT_FALSE(scoper.get()); + delete take; + EXPECT_EQ(0, constructed); + + scoper.reset(new ConDecLogger(&constructed)); + EXPECT_EQ(1, constructed); + EXPECT_TRUE(scoper.get()); + } + EXPECT_EQ(0, constructed); + + // Test swap(), == and != + { + scoped_ptr<ConDecLogger> scoper1; + scoped_ptr<ConDecLogger> scoper2; + EXPECT_TRUE(scoper1 == scoper2.get()); + EXPECT_FALSE(scoper1 != scoper2.get()); + + ConDecLogger* logger = new ConDecLogger(&constructed); + scoper1.reset(logger); + EXPECT_EQ(logger, scoper1.get()); + EXPECT_FALSE(scoper2.get()); + EXPECT_FALSE(scoper1 == scoper2.get()); + EXPECT_TRUE(scoper1 != scoper2.get()); + + scoper2.swap(scoper1); + EXPECT_EQ(logger, scoper2.get()); + EXPECT_FALSE(scoper1.get()); + EXPECT_FALSE(scoper1 == scoper2.get()); + EXPECT_TRUE(scoper1 != scoper2.get()); + } + EXPECT_EQ(0, constructed); +} + +TEST(ScopedPtrTest, ScopedArray) { + static const int kNumLoggers = 12; + + int constructed = 0; + + { + scoped_array<ConDecLogger> scoper(new ConDecLogger[kNumLoggers]); + EXPECT_TRUE(scoper.get()); + EXPECT_EQ(&scoper[0], scoper.get()); + for (int i = 0; i < kNumLoggers; ++i) { + scoper[i].set_ptr(&constructed); + } + EXPECT_EQ(12, constructed); + + EXPECT_EQ(10, scoper.get()->SomeMeth(10)); + EXPECT_EQ(10, scoper[2].SomeMeth(10)); + } + EXPECT_EQ(0, constructed); + + // Test reset() and release() + { + scoped_array<ConDecLogger> scoper; + EXPECT_FALSE(scoper.get()); + scoper.release(); + EXPECT_FALSE(scoper.get()); + scoper.reset(); + EXPECT_FALSE(scoper.get()); + + scoper.reset(new ConDecLogger[kNumLoggers]); + for (int i = 0; i < kNumLoggers; ++i) { + scoper[i].set_ptr(&constructed); + } + EXPECT_EQ(12, constructed); + scoper.reset(); + EXPECT_EQ(0, constructed); + + scoper.reset(new ConDecLogger[kNumLoggers]); + for (int i = 0; i < kNumLoggers; ++i) { + scoper[i].set_ptr(&constructed); + } + EXPECT_EQ(12, constructed); + ConDecLogger* ptr = scoper.release(); + EXPECT_EQ(12, constructed); + delete[] ptr; + EXPECT_EQ(0, constructed); + } + EXPECT_EQ(0, constructed); + + // Test swap(), == and != + { + scoped_array<ConDecLogger> scoper1; + scoped_array<ConDecLogger> scoper2; + EXPECT_TRUE(scoper1 == scoper2.get()); + EXPECT_FALSE(scoper1 != scoper2.get()); + + ConDecLogger* loggers = new ConDecLogger[kNumLoggers]; + for (int i = 0; i < kNumLoggers; ++i) { + loggers[i].set_ptr(&constructed); + } + scoper1.reset(loggers); + EXPECT_EQ(loggers, scoper1.get()); + EXPECT_FALSE(scoper2.get()); + EXPECT_FALSE(scoper1 == scoper2.get()); + EXPECT_TRUE(scoper1 != scoper2.get()); + + scoper2.swap(scoper1); + EXPECT_EQ(loggers, scoper2.get()); + EXPECT_FALSE(scoper1.get()); + EXPECT_FALSE(scoper1 == scoper2.get()); + EXPECT_TRUE(scoper1 != scoper2.get()); + } + EXPECT_EQ(0, constructed); +} + +// TODO scoped_ptr_malloc diff --git a/base/tuple.h b/base/tuple.h index 08ba1e5..c54b579 100644 --- a/base/tuple.h +++ b/base/tuple.h @@ -2,6 +2,30 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +// A Tuple is a generic templatized container, similar in concept to std::pair. +// There are classes Tuple0 to Tuple5, cooresponding to the number of elements +// it contains. The convenient MakeTuple() function takes 0 to 5 arguments, +// and will construct and return the appropriate Tuple object. The functions +// DispatchToMethod and DispatchToFunction take a function pointer or instance +// and method pointer, and unpack a tuple into arguments to the call. +// +// Tuple elements are copied by value, and stored in the tuple. See the unit +// tests for more details of how/when the values are copied. +// +// Example usage: +// // These two methods of creating a Tuple are identical. +// Tuple2<int, const char*> tuple_a(1, "wee"); +// Tuple2<int, const char*> tuple_b = MakeTuple(1, "wee"); +// +// void SomeFunc(int a, const char* b) { } +// DispatchToFunction(&SomeFunc, tuple_a); // SomeFunc(1, "wee") +// DispatchToFunction( +// &SomeFunc, MakeTuple(10, "foo")); // SomeFunc(10, "foo") +// +// struct { void SomeMeth(int a, int b, int c) { } } foo; +// DispatchToMethod(&foo, &Foo::SomeMeth, MakeTuple(1, 2, 3)); +// // foo->SomeMeth(1, 2, 3); + #ifndef BASE_TUPLE_H__ #define BASE_TUPLE_H__ diff --git a/base/tuple_unittest.cc b/base/tuple_unittest.cc new file mode 100644 index 0000000..36ea560 --- /dev/null +++ b/base/tuple_unittest.cc @@ -0,0 +1,109 @@ +// Copyright (c) 2006-2008 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 "base/tuple.h" +#include "testing/gtest/include/gtest/gtest.h" + +namespace { + +void DoAdd(int a, int b, int c, int* res) { + *res = a + b + c; +} + +struct Addy { + Addy() { } + void DoAdd(int a, int b, int c, int d, int* res) { + *res = a + b + c + d; + } +}; + +} // namespace + +TEST(TupleTest, Basic) { + Tuple0 t0 = MakeTuple(); + Tuple1<int> t1(1); + Tuple2<int, const char*> t2 = MakeTuple(1, static_cast<const char*>("wee")); + Tuple3<int, int, int> t3(1, 2, 3); + Tuple4<int, int, int, int*> t4(1, 2, 3, &t1.a); + Tuple5<int, int, int, int, int*> t5(1, 2, 3, 4, &t4.a); + + EXPECT_EQ(1, t1.a); + EXPECT_EQ(1, t2.a); + EXPECT_EQ(1, t3.a); + EXPECT_EQ(2, t3.b); + EXPECT_EQ(3, t3.c); + EXPECT_EQ(1, t4.a); + EXPECT_EQ(2, t4.b); + EXPECT_EQ(3, t4.c); + EXPECT_EQ(1, t5.a); + EXPECT_EQ(2, t5.b); + EXPECT_EQ(3, t5.c); + EXPECT_EQ(4, t5.d); + + + EXPECT_EQ(1, t1.a); + DispatchToFunction(&DoAdd, t4); + EXPECT_EQ(6, t1.a); + + int res = 0; + DispatchToFunction(&DoAdd, MakeTuple(9, 8, 7, &res)); + EXPECT_EQ(24, res); + + Addy addy; + EXPECT_EQ(1, t4.a); + DispatchToMethod(&addy, &Addy::DoAdd, t5); + EXPECT_EQ(10, t4.a); +} + +namespace { + +struct CopyLogger { + CopyLogger() { ++TimesConstructed; } + CopyLogger(const CopyLogger& tocopy) { ++TimesConstructed; ++TimesCopied; } + ~CopyLogger() { } + + static int TimesCopied; + static int TimesConstructed; +}; + +void SomeLoggerMethRef(const CopyLogger& logy, const CopyLogger* ptr, bool* b) { + *b = &logy == ptr; +} + +void SomeLoggerMethCopy(CopyLogger logy, const CopyLogger* ptr, bool* b) { + *b = &logy == ptr; +} + +int CopyLogger::TimesCopied = 0; +int CopyLogger::TimesConstructed = 0; + +} // namespace + +TEST(TupleTest, Copying) { + CopyLogger logger; + EXPECT_EQ(0, CopyLogger::TimesCopied); + EXPECT_EQ(1, CopyLogger::TimesConstructed); + + bool res = false; + + // Creating the tuple should copy the class to store internally in the tuple. + Tuple3<CopyLogger, CopyLogger*, bool*> tuple(logger, &logger, &res); + tuple.b = &tuple.a; + EXPECT_EQ(2, CopyLogger::TimesConstructed); + EXPECT_EQ(1, CopyLogger::TimesCopied); + + // Our internal Logger and the one passed to the function should be the same. + res = false; + DispatchToFunction(&SomeLoggerMethRef, tuple); + EXPECT_TRUE(res); + EXPECT_EQ(2, CopyLogger::TimesConstructed); + EXPECT_EQ(1, CopyLogger::TimesCopied); + + // Now they should be different, since the function call will make a copy. + res = false; + DispatchToFunction(&SomeLoggerMethCopy, tuple); + EXPECT_FALSE(res); + EXPECT_EQ(3, CopyLogger::TimesConstructed); + EXPECT_EQ(2, CopyLogger::TimesCopied); +} |