Skip to content

Commit 0c5e192

Browse files
committed
Remove DBC macro set and rewrite assertions in usual way using standard library.
1 parent e6f8ab9 commit 0c5e192

File tree

4 files changed

+22
-128
lines changed

4 files changed

+22
-128
lines changed

Diff for: CMakeLists.txt

-5
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,6 @@ set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra")
99
set (CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS} -O0 -ggdb3 -DDEBUG")
1010
set (CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS} -O3")
1111

12-
# Set custom macro for provide Design By Contract (by default is disabled).
13-
if (DEFINED DBC_MODE)
14-
add_definitions (-DDEBUG_WITH_DBC=${DBC_MODE})
15-
endif ()
16-
1712
include_directories (${PROJECT_SOURCE_DIR}/src)
1813

1914
# Sources (nothing).

Diff for: README.md

+7-11
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,14 @@ Briefly, to solve the problem you need:
6363
Thats all! Solution of the problem is stored by into input matrix.
6464

6565
```
66-
#include <munkres-cpp/munkres.h>
67-
...
68-
munkres_cpp::Matrix<int> data {
69-
{1, 3}
70-
,{5, 9} };
66+
#include <munkres-cpp/munkres.h>
67+
...
68+
munkres_cpp::Matrix<int> data {
69+
{1, 3}
70+
,{5, 9} };
7171
72-
munkres_cpp::Munkres<int> solver;
73-
solver.solve (data);
72+
munkres_cpp::Munkres<int> solver;
73+
solver.solve (data);
7474
```
7575

7676
Examples subfolder contains set of examples which step-by-step show usage of the library.
@@ -83,10 +83,6 @@ Development
8383
For development purpose in the project implemented a variety of build targets.
8484
All of them help to continuously check correctness of algorithm implementation, performance, memory management, etc.
8585
To configure project in development mode ```-DMUNKRESCPP_DEVEL_MODE=ON``` option must be passed to CMake.
86-
Also for debug purpose defined macro which in primitive way provides basic concepts
87-
of [Design By Contract](https://en.wikipedia.org/wiki/Design_by_contract). By default the macro is disabled and
88-
assertions are not checked. The macro can be enabled in one of modes: if assertion is failed only printing debug message (1),
89-
or throwing exception (2). To configure the mode ```-DDBC_MODE=X``` option must be passed to CMake.
9086

9187
Launch of unit tests.
9288
The project contains unit tests to build and launch it performs the following steps:

Diff for: src/munkres-cpp/matrix.h

+14-14
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ Matrix<T>::Matrix ()
7474
, m_rows {0}
7575
, m_columns {0}
7676
{
77-
ENSURE (!(m_matrix && m_rows && m_columns), "Inconsistent value after initialization.");
77+
assert (!(m_matrix && m_rows && m_columns) );
7878
}
7979

8080

@@ -95,7 +95,7 @@ Matrix<T>::Matrix (const std::initializer_list<std::initializer_list<T>> & init)
9595

9696
size_t i = 0, j;
9797
for (auto row = init.begin (); row != init.end (); ++row, ++i) {
98-
REQUIRE (row->size () == m_columns, "All rows must have the same number of columns.");
98+
assert (row->size () == m_columns);
9999
j = 0;
100100
for (auto value = row->begin (); value != row->end (); ++value, ++j) {
101101
m_matrix[i][j] = *value;
@@ -177,16 +177,16 @@ Matrix<T>::~Matrix ()
177177
}
178178
m_matrix = nullptr;
179179

180-
ENSURE (m_matrix == nullptr, "Dangling pointer.");
180+
assert (m_matrix == nullptr);
181181
}
182182

183183

184184

185185
template<class T>
186186
void Matrix<T>::resize (const size_t rows, const size_t columns, const T default_value)
187187
{
188-
REQUIRE (rows > 0, "Rows must exist.");
189-
REQUIRE (columns > 0, "Columns must exist.");
188+
assert (rows > 0);
189+
assert (columns > 0);
190190

191191
if (m_matrix == nullptr) {
192192
// Alloc arrays.
@@ -239,7 +239,7 @@ void Matrix<T>::resize (const size_t rows, const size_t columns, const T default
239239
template<class T>
240240
void Matrix<T>::clear ()
241241
{
242-
REQUIRE (m_matrix != nullptr, "Attempt to clear empty matrix.");
242+
assert (m_matrix != nullptr);
243243

244244
for (size_t i = 0; i < m_rows; i++) {
245245
for (size_t j = 0; j < m_columns; j++) {
@@ -253,9 +253,9 @@ void Matrix<T>::clear ()
253253
template<class T>
254254
inline T & Matrix<T>::operator () (const size_t x, const size_t y)
255255
{
256-
REQUIRE (x < m_rows, "Row number out of bounds.");
257-
REQUIRE (y < m_columns, "Column number out of bounds.");
258-
REQUIRE (m_matrix != nullptr, "Request value from empty matrix.");
256+
assert (x < m_rows);
257+
assert (y < m_columns);
258+
assert (m_matrix != nullptr);
259259

260260
return m_matrix[x][y];
261261
}
@@ -265,9 +265,9 @@ inline T & Matrix<T>::operator () (const size_t x, const size_t y)
265265
template<class T>
266266
inline const T & Matrix<T>::operator () (const size_t x, const size_t y) const
267267
{
268-
REQUIRE (x < m_rows, "Row number out of bounds.");
269-
REQUIRE (y < m_columns, "Column number out of bounds.");
270-
REQUIRE (m_matrix != nullptr, "Request value from empty matrix.");
268+
assert (x < m_rows);
269+
assert (y < m_columns);
270+
assert (m_matrix != nullptr);
271271

272272
return m_matrix [x][y];
273273
}
@@ -277,7 +277,7 @@ inline const T & Matrix<T>::operator () (const size_t x, const size_t y) const
277277
template<class T>
278278
T Matrix<T>::min () const
279279
{
280-
REQUIRE (m_matrix && m_rows && m_columns, "Attempt to find max value in empty matrix.");
280+
assert (m_matrix && m_rows && m_columns);
281281

282282
T min = m_matrix [0][0];
283283

@@ -295,7 +295,7 @@ T Matrix<T>::min () const
295295
template<class T>
296296
T Matrix<T>::max () const
297297
{
298-
REQUIRE (m_matrix && m_rows && m_columns, "Attempt to find max value in empty matrix.");
298+
assert (m_matrix && m_rows && m_columns);
299299

300300
T max = m_matrix [0][0];
301301

Diff for: src/munkres-cpp/utils.h

+1-98
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define __MUNKRES_CPP_UTILS_H__
33

44
#include <cmath>
5+
#include <cassert>
56
#include "munkres-cpp/matrix_base.h"
67

78

@@ -87,104 +88,6 @@ bool is_data_valid (matrix_base<T> & matrix)
8788
return true;
8889
}
8990

90-
91-
92-
// Macro for trivial support of Design By Contract.
93-
#define DBC_MODE_NONE 0
94-
#define DBC_MODE_MESSAGE 1
95-
#define DBC_MODE_ASSERT 2
96-
97-
#ifndef DEBUG_WITH_DBC
98-
#define DEBUG_WITH_DBC DBC_MODE_NONE
99-
#endif//DEBUG_WITH_DBC
100-
101-
#ifdef DEBUG_WITH_DBC
102-
103-
#ifdef STRINGIFICATION
104-
#error STRINGIFICATION macro already defined!
105-
#endif//STRINGIFICATION
106-
#define STRINGIFICATION(x) #x
107-
108-
#if DEBUG_WITH_DBC == DBC_MODE_ASSERT
109-
#define REQUIRE(CONDITION, MESSAGE) \
110-
do { \
111-
if (!(CONDITION) ) { \
112-
throw std::runtime_error ( \
113-
"Precondition error: " \
114-
+ std::string (STRINGIFICATION (CONDITION) ) + " at " \
115-
+ std::string (__FILE__) + ":" + std::to_string (__LINE__) + ". " \
116-
+ std::string (MESSAGE) ); \
117-
} \
118-
} while (0);
119-
120-
#define ENSURE(CONDITION, MESSAGE) \
121-
do { \
122-
if (!(CONDITION) ) { \
123-
throw std::runtime_error ( \
124-
"Postcondition error: " \
125-
+ std::string (STRINGIFICATION (CONDITION) ) + " at " \
126-
+ std::string (__FILE__) + ":" + std::to_string (__LINE__) + ". " \
127-
+ std::string (MESSAGE) ); \
128-
} \
129-
} while (0);
130-
131-
#define INVARIANT(CONDITION, MESSAGE) \
132-
do { \
133-
if (!(CONDITION) ) { \
134-
throw std::runtime_error ( \
135-
"Invariant error: " \
136-
+ std::string (STRINGIFICATION (CONDITION) ) + " at " \
137-
+ std::string (__FILE__) + ":" + std::to_string (__LINE__) + ". " \
138-
+ std::string (MESSAGE) ); \
139-
} \
140-
} while (0);
141-
142-
#elif DEBUG_WITH_DBC == DBC_MODE_MESSAGE
143-
#define STRINGIFICATION(x) #x
144-
#define REQUIRE(CONDITION, MESSAGE) \
145-
do { \
146-
if (!(CONDITION) ) { \
147-
std::cout \
148-
<< "Precondition error: " \
149-
<< std::string (STRINGIFICATION (CONDITION) ) + " at " \
150-
<< std::string (__FILE__) << ":" << std::to_string (__LINE__) << ". " \
151-
<< std::string (MESSAGE) \
152-
<< std::endl; \
153-
} \
154-
} while (0);
155-
156-
#define ENSURE(CONDITION, MESSAGE) \
157-
do { \
158-
if (!(CONDITION) ) { \
159-
std::cout \
160-
<< "Postcondition error: " \
161-
<< std::string (STRINGIFICATION (CONDITION) ) + " at " \
162-
<< std::string (__FILE__) << ":" << std::to_string (__LINE__) << ". " \
163-
<< std::string (MESSAGE) \
164-
<< std::endl; \
165-
} \
166-
} while (0);
167-
168-
#define INVARIANT(CONDITION, MESSAGE) \
169-
do { \
170-
if (!(CONDITION) ) { \
171-
std::cout \
172-
<< "Invariant error: " \
173-
<< std::string (STRINGIFICATION (CONDITION) ) + " at " \
174-
<< std::string (__FILE__) << ":" << std::to_string (__LINE__) << ". " \
175-
<< std::string (MESSAGE) \
176-
<< std::endl; \
177-
} \
178-
} while (0);
179-
180-
#else //DEBUG_WITH_DBC || DEBUG_WITH_DBC_ASSERT
181-
#define REQUIRE(CONDITION, MESSAGE);
182-
#define ENSURE(CONDITION, MESSAGE);
183-
#define INVARIANT(CONDITION, MESSAGE);
184-
#endif//DEBUG_WITH_DBC || DEBUG_WITH_DBC_ASSERT
185-
186-
#endif//DEBUG_WITH_DBC
187-
18891
}// namespace munkres_cpp
18992

19093
#endif /* !defined(__MUNKRES_CPP_UTILS_H__) */

0 commit comments

Comments
 (0)