1
/* Copyright 2012 10gen Inc.
3
* Licensed under the Apache License, Version 2.0 (the "License");
4
* you may not use this file except in compliance with the License.
5
* You may obtain a copy of the License at
7
* http://www.apache.org/licenses/LICENSE-2.0
9
* Unless required by applicable law or agreed to in writing, software
10
* distributed under the License is distributed on an "AS IS" BASIS,
11
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
* See the License for the specific language governing permissions and
13
* limitations under the License.
16
/** Unit tests for OwnedPointerVector. */
18
#include "mongo/base/owned_pointer_vector.h"
23
#include "mongo/unittest/unittest.h"
28
/** Helper class that logs its constructor argument to a static vector on destruction. */
29
class DestructionLogger {
31
DestructionLogger( const string& name ) :
34
~DestructionLogger() { _destroyedNames.push_back( _name ); }
36
static std::vector<string>& destroyedNames() { return _destroyedNames; }
40
static std::vector<string> _destroyedNames;
43
std::vector<string> DestructionLogger::_destroyedNames;
45
TEST(OwnedPointerVectorTest, OwnedPointerDestroyed) {
46
DestructionLogger::destroyedNames().clear();
48
OwnedPointerVector<DestructionLogger> owned;
49
owned.mutableVector().push_back( new DestructionLogger( "foo" ) );
52
ASSERT_EQUALS( 1U, DestructionLogger::destroyedNames().size() );
53
ASSERT_EQUALS( "foo", DestructionLogger::destroyedNames()[ 0 ] );
56
TEST(OwnedPointerVectorTest, OwnedConstPointerDestroyed) {
57
DestructionLogger::destroyedNames().clear();
59
OwnedPointerVector<const DestructionLogger> owned;
60
owned.mutableVector().push_back( new DestructionLogger( "foo" ) );
63
ASSERT_EQUALS( 1U, DestructionLogger::destroyedNames().size() );
64
ASSERT_EQUALS( "foo", DestructionLogger::destroyedNames()[ 0 ] );
67
TEST(OwnedPointerVectorTest, OwnedPointersDestroyedInOrder) {
68
DestructionLogger::destroyedNames().clear();
70
OwnedPointerVector<DestructionLogger> owned;
71
owned.mutableVector().push_back( new DestructionLogger( "first" ) );
72
owned.mutableVector().push_back( new DestructionLogger( "second" ) );
75
ASSERT_EQUALS( 2U, DestructionLogger::destroyedNames().size() );
76
ASSERT_EQUALS( "first", DestructionLogger::destroyedNames()[ 0 ] );
77
ASSERT_EQUALS( "second", DestructionLogger::destroyedNames()[ 1 ] );