aboutsummaryrefslogtreecommitdiffstats
path: root/searchsummary/src/tests/juniper/suite.h
blob: fea685731aebaebe2b6f23b53298264f3ebf117e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
/**************************************************************************
 * Author: Bård Kvalheim
 *
 * A test suite. Modified from the suite written by Chuck Allison.
 * http://www.cuj.com/archive/1809/feature.html
 *
 * Licensed to Yahoo, and relicensed under the terms of the Apache 2.0 license
 *
 * The usage of suite is simple:
 *
 * mysuite.h:
 * -----
 *
 * #include <iosfwd>
 * #include <vespa/fastlib/testsuite/suite.h>
 *
 * class MySuite : public Suite
 * {
 * public:
 *   MySuite() :
 *     Suite("My test suite. ", &cout)
 *   {
 *     AddTest(new MyTest1());
 *     AddTest(new MyTest2());
 *   }
 * };
 *
 *
 *
 * class MySuiteApp
 * {
 * public:
 *   int Main();
 * };
 *
 *
 * ---
 *
 * mysuite.cpp:
 * -----
 *
 * #include "mysuite.h"
 *
 *
 * int MyTestApp::Main() {
 *   MyTestSuite mts;
 *   mts.Run();
 *   mts.Report();
 *   mts.Free();
 * }
 *
 * ---
 *
 **************************************************************************/

#pragma once

#include "test.h"   // includes <string>, <iosfwd>


#include <vector>

#include <iostream>
#include <stdexcept>
#include <cassert>


namespace fast::testsuite {

class TestSuiteError;

class Suite
{
public:
    Suite(const std::string& name, std::ostream* osptr = 0);

    std::string    GetName() const;
    long           GetNumPassed() const;
    long           GetNumFailed() const;
    const std::ostream* GetStream() const;
    void           SetStream(std::ostream* osptr);

    void AddTest(Test* t);       //throw (TestSuiteError);
    void AddSuite(const Suite&); //throw(TestSuiteError);
    void Run();     // Calls Test::run() repeatedly
    long Report() const;
    void Free();    // deletes tests
    virtual ~Suite(void) { }

private:
    std::string m_name;
    std::ostream* m_osptr;
    std::vector<Test*> m_tests;
    void Reset();
    int GetLongestName() const;

    // Disallowed ops:
    Suite(const Suite&);
    Suite& operator=(const Suite&);
};

inline
Suite::Suite(const std::string& name, std::ostream* osptr)
    : m_name(name),
      m_osptr(osptr),
      m_tests()
{
}

inline
std::string Suite::GetName() const
{
    return m_name;
}

inline
const std::ostream* Suite::GetStream() const
{
    return m_osptr;
}

inline
void Suite::SetStream(std::ostream* osptr)
{
    m_osptr = osptr;
}


/*class TestSuiteError : public logic_error
  {
  public:
  TestSuiteError(const std::string& s = "")
  : logic_error(s)
  {}
  };*/

void Suite::AddTest(Test* t) //throw(TestSuiteError)
{
    // Make sure test has a stream:
    if (t == 0) {}
    //throw TestSuiteError("Null test in Suite::addTest");
    else if (m_osptr != 0 && t->GetStream() == 0)
        t->SetStream(m_osptr);

    m_tests.push_back(t);
    t->Reset();
}

void Suite::AddSuite(const Suite& s) //throw(TestSuiteError)
{
    for (size_t i = 0; i < s.m_tests.size(); ++i)
        AddTest(s.m_tests[i]);
}

void Suite::Free()
{
    // This is not a destructor because tests
    // don't have to be on the heap.
    for (size_t i = 0; i < m_tests.size(); ++i)
    {
        delete m_tests[i];
        m_tests[i] = 0;
    }
}

void Suite::Run()
{
    Reset();
    int longestName = GetLongestName();
    const char *nm;
    int x = 0;
    for (size_t i = 0; i < m_tests.size(); ++i) {
        assert(m_tests[i]);
        nm = m_tests[i]->get_name();
        if (nm) {
            *m_osptr << std::endl << nm << ": ";
            for (x = longestName - strlen(nm); x > 0; --x)
                *m_osptr << ' ';
            *m_osptr << std::flush;
        }
        m_tests[i]->Run();
    }
}


// Find the longest test name
int Suite::GetLongestName() const
{
    int longestName = 0, len = 0;
    const char *nm;
    for (size_t i = 0; i < m_tests.size(); ++i) {
        assert(m_tests[i]);
        nm = m_tests[i]->get_name();
        if ( nm != NULL && (len = strlen(nm)) > longestName )
            longestName = len;
    }
    return longestName;
}

long Suite::Report() const
{
    if (m_osptr) {
        int longestName = GetLongestName();
        int lineLength = longestName + 8 + 16 + 10;
        long totFail = 0;
        int x = 0;
        *m_osptr << std::endl << std::endl
                 << "Suite \"" << m_name << "\"" << std::endl;
        for (x = 0; x < lineLength; ++x)
            *m_osptr << '=';
        *m_osptr << "=";

        // Write the individual reports
        for (size_t i = 0; i < m_tests.size(); ++i) {
            assert(m_tests[i]);
            const char *nm = m_tests[i]->get_name();
            totFail += m_tests[i]->Report(longestName -
                                          (nm ? strlen(nm) : longestName));
        }

        for (x = 0; x < lineLength; ++x)
            *m_osptr << '=';
        *m_osptr << "=\n";
        return totFail;
    }
    else
        return GetNumFailed();
}

long Suite::GetNumPassed() const
{
    long totPass = 0;
    for (size_t i = 0; i < m_tests.size(); ++i)
    {
        assert(m_tests[i]);
        totPass += m_tests[i]->GetNumPassed();
    }
    return totPass;
}

long Suite::GetNumFailed() const
{
    long totFail = 0;
    for (size_t i = 0; i < m_tests.size(); ++i)
    {
        assert(m_tests[i]);
        totFail += m_tests[i]->GetNumFailed();
    }
    return totFail;
}

void Suite::Reset()
{
    for (size_t i = 0; i < m_tests.size(); ++i)
    {
        assert(m_tests[i]);
        m_tests[i]->Reset();
    }
}

}

using fast::testsuite::Suite;