aboutsummaryrefslogtreecommitdiff
path: root/framework/Framework.cpp
blob: b54c0c75b64d9128962cc39ccdee0b0eee8ae185 (plain)
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*
 * Copyright (c) 2017 ARM Limited.
 *
 * SPDX-License-Identifier: MIT
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
#include "Framework.h"

#include "Exceptions.h"
#include "support/ToolchainSupport.h"

#include <chrono>
#include <iostream>
#include <sstream>
#include <type_traits>

namespace arm_compute
{
namespace test
{
namespace framework
{
std::tuple<int, int, int> Framework::count_test_results() const
{
    int passed  = 0;
    int failed  = 0;
    int crashed = 0;

    for(const auto &test : _test_results)
    {
        switch(test.second.status)
        {
            case TestResult::Status::SUCCESS:
                ++passed;
                break;
            case TestResult::Status::FAILED:
                ++failed;
                break;
            case TestResult::Status::CRASHED:
                ++crashed;
                break;
            default:
                // Do nothing
                break;
        }
    }

    return std::make_tuple(passed, failed, crashed);
}

Framework &Framework::get()
{
    static Framework instance;
    return instance;
}

void Framework::init(int num_iterations, const std::string &name_filter, const std::string &id_filter)
{
    _test_name_filter = std::regex{ name_filter };
    _test_id_filter   = std::regex{ id_filter };
    _num_iterations   = num_iterations;
}

std::string Framework::current_suite_name() const
{
    return join(_test_suite_name.cbegin(), _test_suite_name.cend(), "/");
}

void Framework::push_suite(std::string name)
{
    _test_suite_name.emplace_back(std::move(name));
}

void Framework::pop_suite()
{
    _test_suite_name.pop_back();
}

void Framework::log_test_start(const std::string &test_name)
{
    static_cast<void>(test_name);
}

void Framework::log_test_skipped(const std::string &test_name)
{
    static_cast<void>(test_name);
}

void Framework::log_test_end(const std::string &test_name)
{
    static_cast<void>(test_name);
}

void Framework::log_failed_expectation(const std::string &msg)
{
    std::cerr << "ERROR: " << msg << "\n";
}

int Framework::num_iterations() const
{
    return _num_iterations;
}

void Framework::set_num_iterations(int num_iterations)
{
    _num_iterations = num_iterations;
}

void Framework::set_throw_errors(bool throw_errors)
{
    _throw_errors = throw_errors;
}

bool Framework::throw_errors() const
{
    return _throw_errors;
}

bool Framework::is_enabled(const TestId &id) const
{
    return (std::regex_search(support::cpp11::to_string(id.first), _test_id_filter) && std::regex_search(id.second, _test_name_filter));
}

void Framework::run_test(TestCaseFactory &test_factory)
{
    const std::string test_case_name = test_factory.name();

    log_test_start(test_case_name);

    TestResult result;

    try
    {
        std::unique_ptr<TestCase> test_case = test_factory.make();

        try
        {
            test_case->do_setup();

            for(int i = 0; i < _num_iterations; ++i)
            {
                test_case->do_run();
            }

            test_case->do_teardown();

            result.status = TestResult::Status::SUCCESS;
        }
        catch(const TestError &error)
        {
            std::cerr << "FATAL ERROR: " << error.what() << "\n";
            result.status = TestResult::Status::FAILED;

            if(_throw_errors)
            {
                throw;
            }
        }
        catch(const std::exception &error)
        {
            std::cerr << "FATAL ERROR: Received unhandled error: '" << error.what() << "'\n";
            result.status = TestResult::Status::CRASHED;

            if(_throw_errors)
            {
                throw;
            }
        }
        catch(...)
        {
            std::cerr << "FATAL ERROR: Received unhandled exception\n";
            result.status = TestResult::Status::CRASHED;

            if(_throw_errors)
            {
                throw;
            }
        }
    }
    catch(const std::exception &error)
    {
        std::cerr << "FATAL ERROR: Received unhandled error during fixture creation: '" << error.what() << "'\n";

        if(_throw_errors)
        {
            throw;
        }
    }
    catch(...)
    {
        std::cerr << "FATAL ERROR: Received unhandled exception during fixture creation\n";

        if(_throw_errors)
        {
            throw;
        }
    }

    set_test_result(test_case_name, result);
    log_test_end(test_case_name);
}

bool Framework::run()
{
    // Clear old test results
    _test_results.clear();
    _runtime = std::chrono::seconds{ 0 };

    const auto start = std::chrono::high_resolution_clock::now();

    int id = 0;

    for(auto &test_factory : _test_factories)
    {
        const std::string test_case_name = test_factory->name();

        if(!is_enabled(TestId(id, test_case_name)))
        {
            log_test_skipped(test_case_name);
        }
        else
        {
            run_test(*test_factory);
        }

        ++id;
    }

    const auto end = std::chrono::high_resolution_clock::now();

    _runtime = std::chrono::duration_cast<std::chrono::seconds>(end - start);

    int passed  = 0;
    int failed  = 0;
    int crashed = 0;

    std::tie(passed, failed, crashed) = count_test_results();

    std::cout << "Executed " << _test_results.size() << " test(s) (" << passed << " passed, " << failed << " failed, " << crashed << " crashed) in " << _runtime.count() << " second(s)\n";

    return (static_cast<unsigned int>(passed) == _test_results.size());
}

void Framework::set_test_result(std::string test_case_name, TestResult result)
{
    _test_results.emplace(std::move(test_case_name), result);
}

std::vector<Framework::TestId> Framework::test_ids() const
{
    std::vector<TestId> ids;

    int id = 0;

    for(const auto &factory : _test_factories)
    {
        if(is_enabled(TestId(id, factory->name())))
        {
            ids.emplace_back(id, factory->name());
        }

        ++id;
    }

    return ids;
}
} // namespace framework
} // namespace test
} // namespace arm_compute