aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/dexon-foundation/mcl/include/cybozu/test.hpp
blob: 7dfffab960ee56289713aae9fe91407a6ce3571b (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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
#pragma once
/**
    @file
    @brief unit test class

    @author MITSUNARI Shigeo(@herumi)
*/

#include <stdio.h>
#include <string.h>
#include <string>
#include <list>
#include <iostream>
#include <utility>
#if defined(_MSC_VER) && (MSC_VER <= 1500)
    #include <cybozu/inttype.hpp>
#else
    #include <stdint.h>
#endif

namespace cybozu { namespace test {

class AutoRun {
    typedef void (*Func)();
    typedef std::list<std::pair<const char*, Func> > UnitTestList;
public:
    AutoRun()
        : init_(0)
        , term_(0)
        , okCount_(0)
        , ngCount_(0)
        , exceptionCount_(0)
    {
    }
    void setup(Func init, Func term)
    {
        init_ = init;
        term_ = term;
    }
    void append(const char *name, Func func)
    {
        list_.push_back(std::make_pair(name, func));
    }
    void set(bool isOK)
    {
        if (isOK) {
            okCount_++;
        } else {
            ngCount_++;
        }
    }
    std::string getBaseName(const std::string& name) const
    {
#ifdef _WIN32
        const char sep = '\\';
#else
        const char sep = '/';
#endif
        size_t pos = name.find_last_of(sep);
        std::string ret = name.substr(pos + 1);
        pos = ret.find('.');
        return ret.substr(0, pos);
    }
    int run(int, char *argv[])
    {
        std::string msg;
        try {
            if (init_) init_();
            for (UnitTestList::const_iterator i = list_.begin(), ie = list_.end(); i != ie; ++i) {
                std::cout << "ctest:module=" << i->first << std::endl;
                try {
                    (i->second)();
                } catch (std::exception& e) {
                    exceptionCount_++;
                    std::cout << "ctest:  " << i->first << " is stopped by exception " << e.what() << std::endl;
                } catch (...) {
                    exceptionCount_++;
                    std::cout << "ctest:  " << i->first << " is stopped by unknown exception" << std::endl;
                }
            }
            if (term_) term_();
        } catch (std::exception& e) {
            msg = std::string("ctest:err:") + e.what();
        } catch (...) {
            msg = "ctest:err: catch unknown exception";
        }
        fflush(stdout);
        if (msg.empty()) {
            int err = ngCount_ + exceptionCount_;
            int total = okCount_ + err;
            std::cout << "ctest:name=" << getBaseName(*argv)
                      << ", module=" << list_.size()
                      << ", total=" << total
                      << ", ok=" << okCount_
                      << ", ng=" << ngCount_
                      << ", exception=" << exceptionCount_ << std::endl;
            return err > 0 ? 1 : 0;
        } else {
            std::cout << msg << std::endl;
            return 1;
        }
    }
    static inline AutoRun& getInstance()
    {
        static AutoRun instance;
        return instance;
    }
private:
    Func init_;
    Func term_;
    int okCount_;
    int ngCount_;
    int exceptionCount_;
    UnitTestList list_;
};

static AutoRun& autoRun = AutoRun::getInstance();

inline void test(bool ret, const std::string& msg, const std::string& param, const char *file, int line)
{
    autoRun.set(ret);
    if (!ret) {
        printf("%s(%d):ctest:%s(%s);\n", file, line, msg.c_str(), param.c_str());
    }
}

template<typename T, typename U>
bool isEqual(const T& lhs, const U& rhs)
{
    return lhs == rhs;
}

// avoid warning of comparision of integers of different signs
inline bool isEqual(size_t lhs, int rhs)
{
    return lhs == size_t(rhs);
}
inline bool isEqual(int lhs, size_t rhs)
{
    return size_t(lhs) == rhs;
}
inline bool isEqual(const char *lhs, const char *rhs)
{
    return strcmp(lhs, rhs) == 0;
}
inline bool isEqual(char *lhs, const char *rhs)
{
    return strcmp(lhs, rhs) == 0;
}
inline bool isEqual(const char *lhs, char *rhs)
{
    return strcmp(lhs, rhs) == 0;
}
inline bool isEqual(char *lhs, char *rhs)
{
    return strcmp(lhs, rhs) == 0;
}
// avoid to compare float directly
inline bool isEqual(float lhs, float rhs)
{
    union fi {
        float f;
        uint32_t i;
    } lfi, rfi;
    lfi.f = lhs;
    rfi.f = rhs;
    return lfi.i == rfi.i;
}
// avoid to compare double directly
inline bool isEqual(double lhs, double rhs)
{
    union di {
        double d;
        uint64_t i;
    } ldi, rdi;
    ldi.d = lhs;
    rdi.d = rhs;
    return ldi.i == rdi.i;
}

} } // cybozu::test

#ifndef CYBOZU_TEST_DISABLE_AUTO_RUN
int main(int argc, char *argv[])
{
    return cybozu::test::autoRun.run(argc, argv);
}
#endif

/**
    alert if !x
    @param x [in]
*/
#define CYBOZU_TEST_ASSERT(x) cybozu::test::test(!!(x), "CYBOZU_TEST_ASSERT", #x, __FILE__, __LINE__)

/**
    alert if x != y
    @param x [in]
    @param y [in]
*/
#define CYBOZU_TEST_EQUAL(x, y) { \
    bool _cybozu_eq = cybozu::test::isEqual(x, y); \
    cybozu::test::test(_cybozu_eq, "CYBOZU_TEST_EQUAL", #x ", " #y, __FILE__, __LINE__); \
    if (!_cybozu_eq) { \
        std::cout << "ctest:  lhs=" << (x) << std::endl; \
        std::cout << "ctest:  rhs=" << (y) << std::endl; \
    } \
}
/**
    alert if fabs(x, y) >= eps
    @param x [in]
    @param y [in]
*/
#define CYBOZU_TEST_NEAR(x, y, eps) { \
    bool _cybozu_isNear = fabs((x) - (y)) < eps; \
    cybozu::test::test(_cybozu_isNear, "CYBOZU_TEST_NEAR", #x ", " #y, __FILE__, __LINE__); \
    if (!_cybozu_isNear) { \
        std::cout << "ctest:  lhs=" << (x) << std::endl; \
        std::cout << "ctest:  rhs=" << (y) << std::endl; \
    } \
}

#define CYBOZU_TEST_EQUAL_POINTER(x, y) { \
    bool _cybozu_eq = x == y; \
    cybozu::test::test(_cybozu_eq, "CYBOZU_TEST_EQUAL_POINTER", #x ", " #y, __FILE__, __LINE__); \
    if (!_cybozu_eq) { \
        std::cout << "ctest:  lhs=" << static_cast<const void*>(x) << std::endl; \
        std::cout << "ctest:  rhs=" << static_cast<const void*>(y) << std::endl; \
    } \
}
/**
    alert if x[] != y[]
    @param x [in]
    @param y [in]
    @param n [in]
*/
#define CYBOZU_TEST_EQUAL_ARRAY(x, y, n) { \
    for (size_t _cybozu_test_i = 0, _cybozu_ie = (size_t)(n); _cybozu_test_i < _cybozu_ie; _cybozu_test_i++) { \
        bool _cybozu_eq = cybozu::test::isEqual((x)[_cybozu_test_i], (y)[_cybozu_test_i]); \
        cybozu::test::test(_cybozu_eq, "CYBOZU_TEST_EQUAL_ARRAY", #x ", " #y ", " #n, __FILE__, __LINE__); \
        if (!_cybozu_eq) { \
            std::cout << "ctest:  i=" << _cybozu_test_i << std::endl; \
            std::cout << "ctest:  lhs=" << (x)[_cybozu_test_i] << std::endl; \
            std::cout << "ctest:  rhs=" << (y)[_cybozu_test_i] << std::endl; \
        } \
    } \
}

/**
    always alert
    @param msg [in]
*/
#define CYBOZU_TEST_FAIL(msg) cybozu::test::test(false, "CYBOZU_TEST_FAIL", msg, __FILE__, __LINE__)

/**
    verify message in exception
*/
#define CYBOZU_TEST_EXCEPTION_MESSAGE(statement, Exception, msg) \
{ \
    int _cybozu_ret = 0; \
    std::string _cybozu_errMsg; \
    try { \
        statement; \
        _cybozu_ret = 1; \
    } catch (const Exception& _cybozu_e) { \
        _cybozu_errMsg = _cybozu_e.what(); \
        if (_cybozu_errMsg.find(msg) == std::string::npos) { \
            _cybozu_ret = 2; \
        } \
    } catch (...) { \
        _cybozu_ret = 3; \
    } \
    if (_cybozu_ret) { \
        cybozu::test::test(false, "CYBOZU_TEST_EXCEPTION_MESSAGE", #statement ", " #Exception ", " #msg, __FILE__, __LINE__); \
        if (_cybozu_ret == 1) { \
            std::cout << "ctest:  no exception" << std::endl; \
        } else if (_cybozu_ret == 2) { \
            std::cout << "ctest:  bad exception msg:" << _cybozu_errMsg << std::endl; \
        } else { \
            std::cout << "ctest:  unexpected exception" << std::endl; \
        } \
    } else { \
        cybozu::test::autoRun.set(true); \
    } \
}

#define CYBOZU_TEST_EXCEPTION(statement, Exception) \
{ \
    int _cybozu_ret = 0; \
    try { \
        statement; \
        _cybozu_ret = 1; \
    } catch (const Exception&) { \
    } catch (...) { \
        _cybozu_ret = 2; \
    } \
    if (_cybozu_ret) { \
        cybozu::test::test(false, "CYBOZU_TEST_EXCEPTION", #statement ", " #Exception, __FILE__, __LINE__); \
        if (_cybozu_ret == 1) { \
            std::cout << "ctest:  no exception" << std::endl; \
        } else { \
            std::cout << "ctest:  unexpected exception" << std::endl; \
        } \
    } else { \
        cybozu::test::autoRun.set(true); \
    } \
}

/**
    verify statement does not throw
*/
#define CYBOZU_TEST_NO_EXCEPTION(statement) \
try { \
    statement; \
    cybozu::test::autoRun.set(true); \
} catch (...) { \
    cybozu::test::test(false, "CYBOZU_TEST_NO_EXCEPTION", #statement, __FILE__, __LINE__); \
}

/**
    append auto unit test
    @param name [in] module name
*/
#define CYBOZU_TEST_AUTO(name) \
void cybozu_test_ ## name(); \
struct cybozu_test_local_ ## name { \
    cybozu_test_local_ ## name() \
    { \
        cybozu::test::autoRun.append(#name, cybozu_test_ ## name); \
    } \
} cybozu_test_local_instance_ ## name; \
void cybozu_test_ ## name()

/**
    append auto unit test with fixture
    @param name [in] module name
*/
#define CYBOZU_TEST_AUTO_WITH_FIXTURE(name, Fixture) \
void cybozu_test_ ## name(); \
void cybozu_test_real_ ## name() \
{ \
    Fixture f; \
    cybozu_test_ ## name(); \
} \
struct cybozu_test_local_ ## name { \
    cybozu_test_local_ ## name() \
    { \
        cybozu::test::autoRun.append(#name, cybozu_test_real_ ## name); \
    } \
} cybozu_test_local_instance_ ## name; \
void cybozu_test_ ## name()

/**
    setup fixture
    @param Fixture [in] class name of fixture
    @note cstr of Fixture is called before test and dstr of Fixture is called after test
*/
#define CYBOZU_TEST_SETUP_FIXTURE(Fixture) \
Fixture *cybozu_test_local_fixture; \
void cybozu_test_local_init() \
{ \
    cybozu_test_local_fixture = new Fixture(); \
} \
void cybozu_test_local_term() \
{ \
    delete cybozu_test_local_fixture; \
} \
struct cybozu_test_local_fixture_setup_ { \
    cybozu_test_local_fixture_setup_() \
    { \
        cybozu::test::autoRun.setup(cybozu_test_local_init, cybozu_test_local_term); \
    } \
} cybozu_test_local_fixture_setup_instance_;