File asserter/src/archiver.cpp changed (mode: 100644) (index 2f6e4e1..e08bf26) |
7 |
7 |
|
|
8 |
8 |
using namespace std; |
using namespace std; |
9 |
9 |
|
|
10 |
|
namespace private_assert |
|
|
10 |
|
namespace private_assert { |
|
11 |
|
archiver& archiver::instance() |
11 |
12 |
{ |
{ |
12 |
|
archiver & archiver::instance() |
|
13 |
|
{ |
|
14 |
|
static archiver local; |
|
15 |
|
return local; |
|
16 |
|
} |
|
|
13 |
|
static archiver local; |
|
14 |
|
return local; |
|
15 |
|
} |
17 |
16 |
|
|
18 |
|
archiver::~archiver() |
|
19 |
|
{ |
|
20 |
|
fstream out( "result.json", fstream::out ); |
|
|
17 |
|
archiver::~archiver() |
|
18 |
|
{ |
|
19 |
|
fstream out("result.json", fstream::out); |
|
20 |
|
|
|
21 |
|
out << "{\n"; |
|
22 |
|
out << "\"passed\": " << m_passed; |
21 |
23 |
|
|
22 |
|
out << "{\n"; |
|
23 |
|
out << "\"passed\": " << m_passed; |
|
|
24 |
|
if (m_failed.begin() != m_failed.end()) { |
|
25 |
|
out << "," << endl; |
24 |
26 |
|
|
25 |
|
if (m_failed.begin() != m_failed.end()) |
|
26 |
|
{ |
|
27 |
|
out << "," << endl; |
|
|
27 |
|
auto i(m_failed.begin()); |
|
28 |
|
out << "\"failed\": [" << endl |
|
29 |
|
<< *(i++) << endl; |
|
30 |
|
while (i != m_failed.end()) |
|
31 |
|
out << ", " << *(i++) << endl; |
|
32 |
|
out << "]" << endl; |
|
33 |
|
} else { |
|
34 |
|
out << endl; |
|
35 |
|
} |
|
36 |
|
out << "}\n"; |
|
37 |
|
} |
28 |
38 |
|
|
29 |
|
auto i( m_failed.begin() ); |
|
30 |
|
out << "\"failed\": [" << endl << *(i++) << endl; |
|
31 |
|
while (i != m_failed.end()) |
|
32 |
|
out << ", " << *(i++) << endl; |
|
33 |
|
out << "]" << endl; |
|
34 |
|
} |
|
35 |
|
else { |
|
36 |
|
out << endl; |
|
37 |
|
} |
|
38 |
|
out << "}\n"; |
|
39 |
|
} |
|
|
39 |
|
void archiver::pass() |
|
40 |
|
{ |
|
41 |
|
++m_passed; |
|
42 |
|
} |
40 |
43 |
|
|
41 |
|
void archiver::pass() |
|
42 |
|
{ |
|
43 |
|
++m_passed; |
|
44 |
|
} |
|
|
44 |
|
void archiver::fail( |
|
45 |
|
const char* file, |
|
46 |
|
int line, |
|
47 |
|
const char* function, |
|
48 |
|
const char* message) |
|
49 |
|
{ |
45 |
50 |
|
|
46 |
|
void archiver::fail( |
|
47 |
|
const char * file, |
|
48 |
|
int line, |
|
49 |
|
const char * function, |
|
50 |
|
const char * message ) |
|
51 |
|
{ |
|
52 |
|
|
|
53 |
|
stringstream entry; |
|
54 |
|
entry << "{" << endl; |
|
|
51 |
|
stringstream entry; |
|
52 |
|
entry << "{" << endl; |
55 |
53 |
|
|
56 |
|
if (strlen(message)) |
|
57 |
|
{ |
|
58 |
|
entry << "\"message\":\"" << message << "\"," << endl; |
|
59 |
|
} |
|
60 |
|
entry << "\"file\":\"" << file << "\"," << endl; |
|
61 |
|
entry << "\"function\":\"" << function << "\"," << endl; |
|
62 |
|
entry << "\"line\":" << line << endl; |
|
63 |
|
entry << "}" << endl; |
|
|
54 |
|
if (strlen(message)) { |
|
55 |
|
entry << "\"message\":\"" << message << "\"," << endl; |
|
56 |
|
} |
|
57 |
|
entry << "\"file\":\"" << file << "\"," << endl; |
|
58 |
|
entry << "\"function\":\"" << function << "\"," << endl; |
|
59 |
|
entry << "\"line\":" << line << endl; |
|
60 |
|
entry << "}" << endl; |
64 |
61 |
|
|
65 |
|
m_failed.push_back( entry.str() ); |
|
66 |
|
} |
|
67 |
|
} // private_assert |
|
|
62 |
|
m_failed.push_back(entry.str()); |
|
63 |
|
} |
|
64 |
|
} // private_assert |
File asserter/src/archiver.h changed (mode: 100644) (index 3efe692..8292bfe) |
2 |
2 |
#define ARCHIVER_INCLUDE_GUARD_03K9027KSIQ |
#define ARCHIVER_INCLUDE_GUARD_03K9027KSIQ |
3 |
3 |
|
|
4 |
4 |
#include <iostream> |
#include <iostream> |
5 |
|
#include <vector> |
|
6 |
5 |
#include <string> |
#include <string> |
|
6 |
|
#include <vector> |
|
7 |
|
|
|
8 |
|
namespace private_assert { |
|
9 |
|
struct archiver { |
|
10 |
|
static archiver& instance(); |
7 |
11 |
|
|
8 |
|
namespace private_assert |
|
9 |
|
{ |
|
10 |
|
struct archiver |
|
11 |
|
{ |
|
12 |
|
static archiver & instance(); |
|
|
12 |
|
~archiver(); |
13 |
13 |
|
|
14 |
|
~archiver(); |
|
|
14 |
|
void pass(); |
|
15 |
|
void fail( |
|
16 |
|
const char*, |
|
17 |
|
int, |
|
18 |
|
const char* function, |
|
19 |
|
const char* = ""); |
15 |
20 |
|
|
16 |
|
void pass(); |
|
17 |
|
void fail( |
|
18 |
|
const char *, |
|
19 |
|
int, |
|
20 |
|
const char * function, |
|
21 |
|
const char * = ""); |
|
22 |
|
private: |
|
23 |
|
std::size_t m_passed = 0; |
|
24 |
|
std::vector< std::string > m_failed; |
|
25 |
|
}; |
|
|
21 |
|
private: |
|
22 |
|
std::size_t m_passed = 0; |
|
23 |
|
std::vector<std::string> m_failed; |
|
24 |
|
}; |
26 |
25 |
} |
} |
27 |
26 |
|
|
28 |
27 |
#endif // ARCHIVER_INCLUDE_GUARD_03K9027KSIQ |
#endif // ARCHIVER_INCLUDE_GUARD_03K9027KSIQ |
File asserter/src/asserter.cpp changed (mode: 100644 -> 100755) (index 13ac8b7..fb5c2e1) |
3 |
3 |
#include "archiver.h"
|
#include "archiver.h"
|
4 |
4 |
#include "asserter.h"
|
#include "asserter.h"
|
5 |
5 |
|
|
6 |
|
#undef SMART_ASSERT_A
|
|
|
6 |
|
#undef SMART_ASSERT_A
|
7 |
7 |
#undef SMART_ASSERT_B
|
#undef SMART_ASSERT_B
|
8 |
8 |
#undef SMART_ASSERT_OP
|
#undef SMART_ASSERT_OP
|
9 |
9 |
#undef SMART_ASSERT
|
#undef SMART_ASSERT
|
|
13 |
13 |
/////////////////////////////////////////////////////////////////////////////////////////////
|
/////////////////////////////////////////////////////////////////////////////////////////////
|
14 |
14 |
// asserter_t
|
// asserter_t
|
15 |
15 |
/////////////////////////////////////////////////////////////////////////////////////////////
|
/////////////////////////////////////////////////////////////////////////////////////////////
|
16 |
|
asserter_t::asserter_t(bool value)
|
|
17 |
|
: SMART_ASSERT_A( * this )
|
|
18 |
|
, SMART_ASSERT_B( * this )
|
|
19 |
|
, m_value( value )
|
|
20 |
|
{}
|
|
|
16 |
|
asserter_t::asserter_t(bool value)
|
|
17 |
|
: SMART_ASSERT_A(*this)
|
|
18 |
|
, SMART_ASSERT_B(*this)
|
|
19 |
|
, m_value(value)
|
|
20 |
|
{
|
|
21 |
|
}
|
21 |
22 |
|
|
22 |
23 |
/////////////////////////////////////////////////////////////////////////////////////////////
|
/////////////////////////////////////////////////////////////////////////////////////////////
|
23 |
24 |
bool asserter_t::pass() const
|
bool asserter_t::pass() const
|
24 |
|
{
|
|
25 |
|
return m_value;
|
|
|
25 |
|
{
|
|
26 |
|
return m_value;
|
26 |
27 |
}
|
}
|
27 |
28 |
|
|
28 |
29 |
/////////////////////////////////////////////////////////////////////////////////////////////
|
/////////////////////////////////////////////////////////////////////////////////////////////
|
29 |
|
const asserter_t & asserter_t::print_message(
|
|
30 |
|
const char * file,
|
|
31 |
|
int line,
|
|
32 |
|
const char * function,
|
|
33 |
|
const char * message) const
|
|
|
30 |
|
const asserter_t& asserter_t::print_message(
|
|
31 |
|
const char* file,
|
|
32 |
|
int line,
|
|
33 |
|
const char* function,
|
|
34 |
|
const char* message) const
|
34 |
35 |
{
|
{
|
35 |
|
static const char * code_red( "\x1b[31m" );
|
|
36 |
|
static const char * code_reset( "\x1b[39;49m" );
|
|
|
36 |
|
static const char* code_red("\x1b[31m");
|
|
37 |
|
static const char* code_reset("\x1b[39;49m");
|
37 |
38 |
|
|
38 |
39 |
using namespace std;
|
using namespace std;
|
39 |
40 |
|
|
40 |
|
if (pass())
|
|
41 |
|
{
|
|
42 |
|
cout << "assertion passed: " << message << endl
|
|
43 |
|
<< "file: " << file << endl
|
|
44 |
|
<< "line: " << line << endl
|
|
45 |
|
<< "function: " << function << endl;
|
|
46 |
|
}
|
|
47 |
|
else
|
|
48 |
|
{
|
|
|
41 |
|
if (!pass()) {
|
49 |
42 |
cout << code_red
|
cout << code_red
|
50 |
|
<< "assertion failed: " << message << endl
|
|
51 |
|
<< "file: " << file << endl
|
|
52 |
|
<< "line: " << line << endl
|
|
|
43 |
|
<< "assertion failed: " << message << endl
|
|
44 |
|
<< "file: " << file << endl
|
|
45 |
|
<< "line: " << line << endl
|
53 |
46 |
<< "function: " << function
|
<< "function: " << function
|
54 |
47 |
<< code_reset << endl;
|
<< code_reset << endl;
|
55 |
48 |
}
|
}
|
56 |
|
return * this;
|
|
|
49 |
|
return *this;
|
57 |
50 |
}
|
}
|
58 |
51 |
|
|
59 |
52 |
/////////////////////////////////////////////////////////////////////////////////////////////
|
/////////////////////////////////////////////////////////////////////////////////////////////
|
60 |
|
const asserter_t & asserter_t::archive_result(
|
|
61 |
|
const char * file,
|
|
62 |
|
int line,
|
|
63 |
|
const char * function,
|
|
64 |
|
const char * message ) const
|
|
|
53 |
|
const asserter_t& asserter_t::archive_result(
|
|
54 |
|
const char* file,
|
|
55 |
|
int line,
|
|
56 |
|
const char* function,
|
|
57 |
|
const char* message) const
|
65 |
58 |
{
|
{
|
66 |
|
auto & a( private_assert::archiver::instance() );
|
|
|
59 |
|
auto& a(private_assert::archiver::instance());
|
67 |
60 |
if (pass())
|
if (pass())
|
68 |
61 |
a.pass();
|
a.pass();
|
69 |
|
else
|
|
70 |
|
a.fail( file, line, function, message );
|
|
71 |
|
return * this;
|
|
|
62 |
|
else
|
|
63 |
|
a.fail(file, line, function, message);
|
|
64 |
|
return *this;
|
72 |
65 |
}
|
}
|
73 |
66 |
|
|
74 |
67 |
#endif // NDEBUG
|
#endif // NDEBUG
|
75 |
|
|
|
76 |
|
|
|
77 |
|
|
|
78 |
|
|
|
File asserter/src/asserter.h changed (mode: 100644 -> 100755) (index de41d80..0f8a8de) |
11 |
11 |
|
|
12 |
12 |
#ifdef NDEBUG
|
#ifdef NDEBUG
|
13 |
13 |
|
|
14 |
|
#define ASSERT( expr ) \
|
|
15 |
|
if (false); \
|
|
16 |
|
else \
|
|
17 |
|
struct local_t \
|
|
18 |
|
{ \
|
|
19 |
|
local_t( const asserter_t & ) {} \
|
|
20 |
|
} local_obj = asserter_t(false)
|
|
21 |
|
|
|
22 |
|
// asserter_t
|
|
23 |
|
class asserter_t
|
|
24 |
|
{
|
|
25 |
|
public:
|
|
26 |
|
template<class T> const asserter_t operator()(const T &) const { return asserter_t(); }
|
|
27 |
|
};
|
|
|
14 |
|
#define ASSERT(expr) \
|
|
15 |
|
if (false) \
|
|
16 |
|
; \
|
|
17 |
|
else \
|
|
18 |
|
struct local_t { \
|
|
19 |
|
local_t(const asserter_t&) {} \
|
|
20 |
|
} local_obj = asserter_t(false)
|
|
21 |
|
|
|
22 |
|
// asserter_t
|
|
23 |
|
class asserter_t {
|
|
24 |
|
public:
|
|
25 |
|
template <class T>
|
|
26 |
|
const asserter_t operator()(const T&) const { return asserter_t(); }
|
|
27 |
|
};
|
28 |
28 |
|
|
29 |
29 |
#else
|
#else
|
30 |
30 |
|
|
31 |
|
#define SMART_ASSERT_A(x) SMART_ASSERT_OP(x, B)
|
|
32 |
|
#define SMART_ASSERT_B(x) SMART_ASSERT_OP(x, A)
|
|
33 |
|
#define SMART_ASSERT_OP(x, next) SMART_ASSERT_A.print_current_val((x), #x).SMART_ASSERT_ ## next
|
|
34 |
|
|
|
35 |
|
#ifndef TARGET_TEST
|
|
36 |
|
#define TARGET_TEST 0
|
|
37 |
|
#endif
|
|
38 |
|
|
|
39 |
|
#define ASSERT( expr ) \
|
|
40 |
|
if (!TARGET_TEST && (expr)); \
|
|
41 |
|
else \
|
|
42 |
|
struct local_t \
|
|
43 |
|
{ \
|
|
44 |
|
local_t( const asserter_t & o ) \
|
|
45 |
|
{ \
|
|
46 |
|
if( !(o.pass()) ) \
|
|
47 |
|
assert(false); \
|
|
48 |
|
} \
|
|
49 |
|
} \
|
|
50 |
|
local_obj = \
|
|
51 |
|
asserter_t( expr ) \
|
|
52 |
|
.print_message( __FILE__, __LINE__, __FUNCTION__, #expr ) \
|
|
53 |
|
.archive_result( __FILE__, __LINE__, __FUNCTION__, #expr ) \
|
|
54 |
|
.SMART_ASSERT_A
|
|
55 |
|
|
|
56 |
|
// asserter_t
|
|
57 |
|
struct asserter_t final
|
|
58 |
|
{
|
|
59 |
|
asserter_t(bool);
|
|
60 |
|
|
|
61 |
|
bool pass() const;
|
|
62 |
|
|
|
63 |
|
const asserter_t & print_message(
|
|
64 |
|
const char * file,
|
|
65 |
|
int line,
|
|
66 |
|
const char * function,
|
|
67 |
|
const char * = "" ) const;
|
|
68 |
|
|
|
69 |
|
const asserter_t & archive_result(
|
|
70 |
|
const char * file,
|
|
71 |
|
int line,
|
|
72 |
|
const char * function,
|
|
73 |
|
const char * = "" ) const;
|
|
74 |
|
|
|
75 |
|
template<class U> const asserter_t & print_current_val(const U &, const char*) const;
|
|
76 |
|
|
|
77 |
|
asserter_t & SMART_ASSERT_A;
|
|
78 |
|
asserter_t & SMART_ASSERT_B;
|
|
79 |
|
|
|
80 |
|
private:
|
|
81 |
|
const bool m_value;
|
|
82 |
|
};
|
|
83 |
|
|
|
84 |
|
#include "asserter.hxx"
|
|
|
31 |
|
#define SMART_ASSERT_A(x) SMART_ASSERT_OP(x, B)
|
|
32 |
|
#define SMART_ASSERT_B(x) SMART_ASSERT_OP(x, A)
|
|
33 |
|
#define SMART_ASSERT_OP(x, next) SMART_ASSERT_A.print_current_val((x), #x).SMART_ASSERT_##next
|
|
34 |
|
|
|
35 |
|
#ifndef TARGET_TEST
|
|
36 |
|
#define TARGET_TEST 0
|
|
37 |
|
#endif
|
|
38 |
|
|
|
39 |
|
#define ASSERT(expr) \
|
|
40 |
|
if (!TARGET_TEST && (expr)) \
|
|
41 |
|
; \
|
|
42 |
|
else \
|
|
43 |
|
struct local_t { \
|
|
44 |
|
local_t(const asserter_t& o) \
|
|
45 |
|
{ \
|
|
46 |
|
if (!(o.pass())) \
|
|
47 |
|
assert(false); \
|
|
48 |
|
} \
|
|
49 |
|
} local_obj = asserter_t(expr) \
|
|
50 |
|
.print_message(__FILE__, __LINE__, __FUNCTION__, #expr) \
|
|
51 |
|
.archive_result(__FILE__, __LINE__, __FUNCTION__, #expr) \
|
|
52 |
|
.SMART_ASSERT_A
|
|
53 |
|
|
|
54 |
|
// asserter_t
|
|
55 |
|
struct asserter_t final {
|
|
56 |
|
asserter_t(bool);
|
|
57 |
|
|
|
58 |
|
bool pass() const;
|
|
59 |
|
|
|
60 |
|
const asserter_t& print_message(
|
|
61 |
|
const char* file,
|
|
62 |
|
int line,
|
|
63 |
|
const char* function,
|
|
64 |
|
const char* = "") const;
|
|
65 |
|
|
|
66 |
|
const asserter_t& archive_result(
|
|
67 |
|
const char* file,
|
|
68 |
|
int line,
|
|
69 |
|
const char* function,
|
|
70 |
|
const char* = "") const;
|
|
71 |
|
|
|
72 |
|
template <class U>
|
|
73 |
|
const asserter_t& print_current_val(const U&, const char*) const;
|
|
74 |
|
|
|
75 |
|
asserter_t& SMART_ASSERT_A;
|
|
76 |
|
asserter_t& SMART_ASSERT_B;
|
|
77 |
|
|
|
78 |
|
private:
|
|
79 |
|
const bool m_value;
|
|
80 |
|
};
|
|
81 |
|
|
|
82 |
|
#include "asserter.hxx"
|
85 |
83 |
|
|
86 |
84 |
#endif // NDEBUG
|
#endif // NDEBUG
|
87 |
85 |
|
|
File asserter/src/asserter.hxx changed (mode: 100644 -> 100755) (index c6a05f5..3ad5288) |
1 |
1 |
// /////////////////////////////////////////////////////////////////////////////////////////////
|
// /////////////////////////////////////////////////////////////////////////////////////////////
|
2 |
2 |
// // asserter_t
|
// // asserter_t
|
3 |
3 |
// /////////////////////////////////////////////////////////////////////////////////////////////
|
// /////////////////////////////////////////////////////////////////////////////////////////////
|
4 |
|
template<class U> const asserter_t & asserter_t::print_current_val(const U & value, const char * message) const
|
|
|
4 |
|
template <class U>
|
|
5 |
|
const asserter_t& asserter_t::print_current_val(const U& value, const char* message) const
|
5 |
6 |
{
|
{
|
6 |
|
std::cout << message << ": " << value << std::endl;
|
|
7 |
|
return * this;
|
|
|
7 |
|
if (!pass()) {
|
|
8 |
|
std::cout << message << ": " << value << std::endl;
|
|
9 |
|
}
|
|
10 |
|
return *this;
|
8 |
11 |
}
|
}
|