Line data Source code
1 : /*
2 : * File:
3 : * tests/unittest_main.h
4 : *
5 : * License:
6 : * Copyright (c) 2006-2019 Made to Order Software Corp. All Rights Reserved
7 : *
8 : * https://snapwebsites.org/
9 : * contact@m2osw.com
10 : *
11 : * This program is free software; you can redistribute it and/or modify
12 : * it under the terms of the GNU General Public License as published by
13 : * the Free Software Foundation; either version 2 of the License, or
14 : * (at your option) any later version.
15 : *
16 : * This program is distributed in the hope that it will be useful,
17 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : * GNU General Public License for more details.
20 : *
21 : * You should have received a copy of the GNU General Public License along
22 : * with this program; if not, write to the Free Software Foundation, Inc.,
23 : * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 : *
25 : * Authors:
26 : * Alexis Wilke alexis@m2osw.com
27 : */
28 : #ifndef UNIT_TEST_MAIN_H
29 : #define UNIT_TEST_MAIN_H
30 : #include <string>
31 : #include <cstring>
32 : #include <cstdlib>
33 : #include <iostream>
34 :
35 : #include <boost/preprocessor/stringize.hpp>
36 :
37 : #pragma GCC diagnostic push
38 : #pragma GCC diagnostic ignored "-Wctor-dtor-privacy"
39 : #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
40 : #pragma GCC diagnostic ignored "-Woverloaded-virtual"
41 : #include <catch2/catch.hpp>
42 :
43 : // The message() can be used to verify that version
44 : //
45 : //#pragma message("Building unit tests with Catch2 version " BOOST_PP_STRINGIZE(CATCH_VERSION_MAJOR) "." BOOST_PP_STRINGIZE(CATCH_VERSION_MINOR) "." BOOST_PP_STRINGIZE(CATCH_VERSION_PATCH))
46 :
47 : #pragma GCC diagnostic pop
48 :
49 : #ifdef _MSC_VER
50 : // The POSIX name for this item is deprecated. Instead, use the ISO C++ conformant name: ... See online help for details.
51 : // This is because of the putenv() and strdup() used in the class below
52 : #pragma warning(disable: 4996)
53 : #endif
54 :
55 : namespace unittest
56 : {
57 :
58 : extern bool g_verbose;
59 :
60 : class obj_setenv
61 : {
62 : public:
63 : obj_setenv(const std::string& var)
64 : : f_copy(strdup(var.c_str()))
65 : {
66 : putenv(f_copy);
67 : std::string::size_type p(var.find_first_of('='));
68 : f_name = var.substr(0, p);
69 : }
70 : obj_setenv(obj_setenv const & rhs) = delete;
71 : obj_setenv & operator = (obj_setenv const & rhs) = delete;
72 : ~obj_setenv()
73 : {
74 : putenv(strdup((f_name + "=").c_str()));
75 : free(f_copy);
76 : }
77 :
78 : private:
79 : char * f_copy = nullptr;
80 : std::string f_name = std::string();
81 : };
82 :
83 :
84 2098472 : inline char32_t rand_char(bool full_range = false)
85 : {
86 2098472 : char32_t const max((full_range ? 0x0110000 : 0x0010000) - (0xE000 - 0xD800));
87 :
88 : char32_t wc;
89 38 : do
90 : {
91 2098510 : wc = ((rand() << 16) ^ rand()) % max;
92 : }
93 2098510 : while(wc == 0);
94 2098472 : if(wc >= 0xD800)
95 : {
96 : // skip the surrogates
97 : //
98 272939 : wc += 0xE000 - 0xD800;
99 : }
100 :
101 2098472 : return wc;
102 : }
103 :
104 :
105 : #define CATCH_START_SECTION(name) \
106 : CATCH_SECTION(name) { if(unittest::g_verbose) { std::cout << name << std::endl; }
107 :
108 : #define CATCH_END_SECTION() }
109 :
110 :
111 :
112 : }
113 : // unittest namespace
114 : #endif
115 : // vim: ts=4 sw=4 et
|