~ubuntu-branches/ubuntu/trusty/mongodb/trusty-proposed

« back to all changes in this revision

Viewing changes to dbtests/framework.h

  • Committer: Bazaar Package Importer
  • Author(s): Antonin Kral
  • Date: 2010-01-29 19:48:45 UTC
  • Revision ID: james.westby@ubuntu.com-20100129194845-8wbmkf626fwcavc9
Tags: upstream-1.3.1
ImportĀ upstreamĀ versionĀ 1.3.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// framework.h
 
2
 
 
3
/**
 
4
*    Copyright (C) 2008 10gen Inc.
 
5
*
 
6
*    This program is free software: you can redistribute it and/or  modify
 
7
*    it under the terms of the GNU Affero General Public License, version 3,
 
8
*    as published by the Free Software Foundation.
 
9
*
 
10
*    This program is distributed in the hope that it will be useful,
 
11
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
*    GNU Affero General Public License for more details.
 
14
*
 
15
*    You should have received a copy of the GNU Affero General Public License
 
16
*    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
17
*/
 
18
 
 
19
/*
 
20
 
 
21
  simple portable regression system
 
22
 */
 
23
 
 
24
#include "../stdafx.h"
 
25
 
 
26
#define ASSERT_EXCEPTION(a,b)                                       \
 
27
    try {                                                           \
 
28
        a;                                                          \
 
29
        mongo::regression::assert_fail( #a , __FILE__ , __LINE__ ); \
 
30
    } catch ( b& ){                                               \
 
31
        mongo::regression::assert_pass();                           \
 
32
    }
 
33
 
 
34
 
 
35
 
 
36
#define ASSERT_EQUALS(a,b) (mongo::regression::MyAsserts( #a , #b , __FILE__ , __LINE__ ) ).ae( (a) , (b) )
 
37
#define ASSERT(x) (void)( (!(!(x))) ? mongo::regression::assert_pass() : mongo::regression::assert_fail( #x , __FILE__ , __LINE__ ) )
 
38
#define FAIL(x) mongo::regression::fail( #x , __FILE__ , __LINE__ )
 
39
 
 
40
#include "../db/instance.h"
 
41
 
 
42
namespace mongo {
 
43
 
 
44
    namespace regression {
 
45
 
 
46
        class Result;
 
47
 
 
48
        string demangleName( const type_info& typeinfo );
 
49
 
 
50
        class TestCase {
 
51
        public:
 
52
            virtual ~TestCase(){}
 
53
            virtual void run() = 0;
 
54
            virtual string getName() = 0;
 
55
        };
 
56
 
 
57
        template< class T >
 
58
        class TestHolderBase : public TestCase {
 
59
        public:
 
60
            TestHolderBase(){}
 
61
            virtual ~TestHolderBase(){}
 
62
            virtual void run(){
 
63
                auto_ptr<T> t;
 
64
                t.reset( create() );
 
65
                t->run();
 
66
            }
 
67
            virtual T * create() = 0;
 
68
            virtual string getName(){
 
69
                return demangleName( typeid(T) );
 
70
            }
 
71
        };
 
72
 
 
73
        template< class T >
 
74
        class TestHolder0 : public TestHolderBase<T> {
 
75
        public:
 
76
            virtual T * create(){
 
77
                return new T();
 
78
            }
 
79
        };
 
80
 
 
81
        template< class T , typename A  >
 
82
        class TestHolder1 : public TestHolderBase<T> {
 
83
        public:
 
84
            TestHolder1( const A& a ) : _a(a){}
 
85
            virtual T * create(){
 
86
                return new T( _a );
 
87
            }
 
88
            const A& _a;
 
89
        };
 
90
 
 
91
        class Suite {
 
92
        public:
 
93
            Suite( string name ) : _name( name ){
 
94
                registerSuite( name , this );
 
95
                _ran = 0;
 
96
            }
 
97
 
 
98
            virtual ~Suite() {
 
99
                if ( _ran ){
 
100
                    DBDirectClient c;
 
101
                    c.dropDatabase( "unittests" );
 
102
                }
 
103
            }
 
104
 
 
105
            template<class T>
 
106
            void add(){
 
107
                _tests.push_back( new TestHolder0<T>() );
 
108
            }
 
109
 
 
110
            template<class T , typename A >
 
111
            void add( const A& a ){
 
112
                _tests.push_back( new TestHolder1<T,A>(a) );
 
113
            }
 
114
 
 
115
            Result * run();
 
116
 
 
117
            static int run( vector<string> suites );
 
118
            static int run( int argc , char ** argv , string default_dbpath );
 
119
 
 
120
 
 
121
        protected:
 
122
            virtual void setupTests() = 0;
 
123
 
 
124
        private:
 
125
            string _name;
 
126
            list<TestCase*> _tests;
 
127
            bool _ran;
 
128
 
 
129
            static map<string,Suite*> * _suites;
 
130
 
 
131
            void registerSuite( string name , Suite * s );
 
132
        };
 
133
 
 
134
        void assert_pass();
 
135
        void assert_fail( const char * exp , const char * file , unsigned line );
 
136
        void fail( const char * exp , const char * file , unsigned line );
 
137
 
 
138
        class MyAssertionException : boost::noncopyable {
 
139
        public:
 
140
            MyAssertionException(){
 
141
                ss << "assertion: ";
 
142
            }
 
143
            stringstream ss;
 
144
        };
 
145
 
 
146
 
 
147
 
 
148
        class MyAsserts {
 
149
        public:
 
150
            MyAsserts( const char * aexp , const char * bexp , const char * file , unsigned line )
 
151
                : _aexp( aexp ) , _bexp( bexp ) , _file( file ) , _line( line ){
 
152
 
 
153
            }
 
154
            
 
155
            template<typename A,typename B>
 
156
            void ae( A a , B b ){
 
157
                _gotAssert();
 
158
                if ( a == b )
 
159
                    return;
 
160
                
 
161
                printLocation();
 
162
                    
 
163
                MyAssertionException * e = getBase();
 
164
                e->ss << a << " != " << b << endl;
 
165
                log() << e->ss.str() << endl;
 
166
                throw e;
 
167
            }
 
168
            
 
169
            void printLocation();
 
170
            
 
171
        private:
 
172
            
 
173
            void _gotAssert();
 
174
            
 
175
            MyAssertionException * getBase();
 
176
            
 
177
            string _aexp;
 
178
            string _bexp;
 
179
            string _file;
 
180
            unsigned _line;
 
181
        };
 
182
 
 
183
    }
 
184
}