00001
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043 #ifndef ASRL_ASSERT_MACROS_HPP
00044 #define ASRL_ASSERT_MACROS_HPP
00045
00046 #include <stdexcept>
00047 #include <sstream>
00048
00049 namespace asrl {
00050
00051 namespace detail {
00061 inline void throw_runtime_error(std::string function, std::string file,
00062 int line, std::string message)
00063 {
00064 std::ostringstream asrl_assert_stringstream;
00065 asrl_assert_stringstream << file << ":" << line << " " << function << "() " << message;
00066
00067
00068 throw(std::runtime_error(asrl_assert_stringstream.str()));
00069 }
00070
00071 }}
00072
00073 #define ASRL_TRACE std::cout << __FILE__ << ":" << __LINE__ << " " << __FUNCTION__ << "()" << std::endl;
00074
00081 #define ASRL_THROW(message){ \
00082 std::ostringstream asrl_assert_stringstream; \
00083 asrl_assert_stringstream << message; \
00084 asrl::detail::throw_runtime_error(__FUNCTION__,__FILE__,__LINE__, asrl_assert_stringstream.str()); \
00085 }
00086
00094 #define ASRL_ASSERT(condition, message) \
00095 if(!(condition)) \
00096 { \
00097 std::ostringstream asrl_assert_stringstream; \
00098 asrl_assert_stringstream << "assert " << #condition << " failed: " << message; \
00099 asrl::detail::throw_runtime_error(__FUNCTION__,__FILE__,__LINE__, asrl_assert_stringstream.str()); \
00100 }
00101
00109 #define ASRL_ASSERT_GE_LT(value, lowerBound, upperBound, message) \
00110 if(value < lowerBound || value >= upperBound) \
00111 { \
00112 std::ostringstream asrl_assert_stringstream; \
00113 asrl_assert_stringstream << "assert " << #lowerBound << " <= " << #value << " < " << #upperBound << " failed [" << lowerBound << " <= " << value << " < " << upperBound << "]: " << message; \
00114 asrl::detail::throw_runtime_error(__FUNCTION__,__FILE__,__LINE__,asrl_assert_stringstream.str()); \
00115 }
00116
00124 #define ASRL_ASSERT_LT(value, upperBound, message) \
00125 if(value >= upperBound) \
00126 { \
00127 std::ostringstream asrl_assert_stringstream; \
00128 asrl_assert_stringstream << "assert " << #value << " < " << #upperBound << " failed [" << value << " < " << upperBound << "]: " << message; \
00129 asrl::detail::throw_runtime_error(__FUNCTION__,__FILE__,__LINE__,asrl_assert_stringstream.str()); \
00130 }
00131
00139 #define ASRL_ASSERT_GE(value, lowerBound, message) \
00140 if(value < lowerBound) \
00141 { \
00142 std::ostringstream asrl_assert_stringstream; \
00143 asrl_assert_stringstream << "assert " << #value << " >= " << #lowerBound << " failed [" << value << " >= " << lowerBound << "]: " << message; \
00144 asrl::detail::throw_runtime_error(__FUNCTION__,__FILE__,__LINE__,asrl_assert_stringstream.str()); \
00145 }
00146
00154 #define ASRL_ASSERT_LE(value, upperBound, message) \
00155 if(value > upperBound) \
00156 { \
00157 std::ostringstream asrl_assert_stringstream; \
00158 asrl_assert_stringstream << "assert " << #value << " <= " << #upperBound << " failed [" << value << " <= " << upperBound << "]: " << message; \
00159 asrl::detail::throw_runtime_error(__FUNCTION__,__FILE__,__LINE__,asrl_assert_stringstream.str()); \
00160 }
00161
00169 #define ASRL_ASSERT_GT(value, lowerBound, message) \
00170 if(value <= lowerBound) \
00171 { \
00172 std::ostringstream asrl_assert_stringstream; \
00173 asrl_assert_stringstream << "assert " << #value << " > " << #lowerBound << " failed [" << value << " > " << lowerBound << "]: " << message; \
00174 asrl::detail::throw_runtime_error(__FUNCTION__,__FILE__,__LINE__,asrl_assert_stringstream.str()); \
00175 }
00176
00184 #define ASRL_ASSERT_EQ(value, testValue, message) \
00185 if(value != testValue) \
00186 { \
00187 std::ostringstream asrl_assert_stringstream; \
00188 asrl_assert_stringstream << "assert " << #value << " == " << #testValue << " failed [" << value << " == " << testValue << "]: " << message; \
00189 asrl::detail::throw_runtime_error(__FUNCTION__,__FILE__,__LINE__,asrl_assert_stringstream.str()); \
00190 }
00191
00199 #define ASRL_ASSERT_NE(value, testValue, message) \
00200 if(value == testValue) \
00201 { \
00202 std::ostringstream asrl_assert_stringstream; \
00203 asrl_assert_stringstream << "assert " << #value << " != " << #testValue << " failed [" << value << " != " << testValue << "]: " << message; \
00204 asrl::detail::throw_runtime_error(__FUNCTION__,__FILE__,__LINE__,asrl_assert_stringstream.str()); \
00205 }
00206
00213 #define ASRL_CHECK_CUDA_ERROR(errorMessage) { \
00214 cudaError_t err = cudaGetLastError(); \
00215 if( cudaSuccess != err) { \
00216 std::ostringstream asrl_assert_stringstream; \
00217 asrl_assert_stringstream << "CUDA error: " << cudaGetErrorString( err ) << " : " << errorMessage; \
00218 asrl::detail::throw_runtime_error(__FUNCTION__,__FILE__,__LINE__,asrl_assert_stringstream.str()); \
00219 } \
00220 }
00221
00229 #define ASRL_ASSERT_EQ_TOL(value, testValue, tolerance, message) \
00230 if(fabs((double)value - (double)testValue) > (double)tolerance) \
00231 { \
00232 std::ostringstream asrl_assert_stringstream; \
00233 asrl_assert_stringstream << "assert " << #value << " == " << #testValue << " (tolerance " << #tolerance ") failed [" << value << " != " << testValue << ", " << tolerance << "]: " << message; \
00234 asrl::detail::throw_runtime_error(__FUNCTION__,__FILE__,__LINE__,asrl_assert_stringstream.str()); \
00235 }
00236
00237 #ifndef NDEBUG
00238
00246 #define ASRL_THROW_DBG(message){ \
00247 std::ostringstream asrl_assert_stringstream; \
00248 asrl_assert_stringstream << message; \
00249 asrl::detail::throw_runtime_error(__FUNCTION__,__FILE__,__LINE__, asrl_assert_stringstream.str()); \
00250 }
00251
00259 #define ASRL_ASSERT_DBG(condition, message) \
00260 if(!(condition)) \
00261 { \
00262 std::ostringstream asrl_assert_stringstream; \
00263 asrl_assert_stringstream << "debug assert " << #condition << " failed: " << message; \
00264 asrl::detail::throw_runtime_error(__FUNCTION__,__FILE__,__LINE__, asrl_assert_stringstream.str()); \
00265 }
00266
00275 #define ASRL_ASSERT_GE_LT_DBG(value, lowerBound, upperBound, message) \
00276 if(value < lowerBound || value >= upperBound) \
00277 { \
00278 std::ostringstream asrl_assert_stringstream; \
00279 asrl_assert_stringstream << "debug assert " << #lowerBound << " <= " << #value << " < " << #upperBound << " failed [" << lowerBound << " <= " << value << " < " << upperBound << "]: " << message; \
00280 asrl::detail::throw_runtime_error(__FUNCTION__,__FILE__,__LINE__,asrl_assert_stringstream.str()); \
00281 }
00282
00292 #define ASRL_ASSERT_LT_DBG(value, upperBound, message) \
00293 if(value >= upperBound) \
00294 { \
00295 std::ostringstream asrl_assert_stringstream; \
00296 asrl_assert_stringstream << "debug assert " << #value << " < " << #upperBound << " failed [" << value << " < " << upperBound << "]: " << message; \
00297 asrl::detail::throw_runtime_error(__FUNCTION__,__FILE__,__LINE__,asrl_assert_stringstream.str()); \
00298 }
00299
00309 #define ASRL_ASSERT_GE_DBG(value, lowerBound, message) \
00310 if(value < lowerBound) \
00311 { \
00312 std::ostringstream asrl_assert_stringstream; \
00313 asrl_assert_stringstream << "debug assert " << #value << " >= " << #lowerBound << " failed [" << value << " >= " << lowerBound << "]: " << message; \
00314 asrl::detail::throw_runtime_error(__FUNCTION__,__FILE__,__LINE__,asrl_assert_stringstream.str()); \
00315 }
00316
00326 #define ASRL_ASSERT_LE_DBG(value, upperBound, message) \
00327 if(value > upperBound) \
00328 { \
00329 std::ostringstream asrl_assert_stringstream; \
00330 asrl_assert_stringstream << "debug assert " << #value << " <= " << #upperBound << " failed [" << value << " <= " << upperBound << "]: " << message; \
00331 asrl::detail::throw_runtime_error(__FUNCTION__,__FILE__,__LINE__,asrl_assert_stringstream.str()); \
00332 }
00333
00334
00343 #define ASRL_ASSERT_GT_DBG(value, lowerBound, message) \
00344 if(value <= lowerBound) \
00345 { \
00346 std::ostringstream asrl_assert_stringstream; \
00347 asrl_assert_stringstream << "debug assert " << #value << " > " << #lowerBound << " failed [" << value << " > " << lowerBound << "]: " << message; \
00348 asrl::detail::throw_runtime_error(__FUNCTION__,__FILE__,__LINE__,asrl_assert_stringstream.str()); \
00349 }
00350
00351
00360 #define ASRL_ASSERT_EQ_DBG(value, testValue, message) \
00361 if(value != testValue) \
00362 { \
00363 std::ostringstream asrl_assert_stringstream; \
00364 asrl_assert_stringstream << "debug assert " << #value << " == " << #testValue << " failed [" << value << " == " << testValue << "]: " << message; \
00365 asrl::detail::throw_runtime_error(__FUNCTION__,__FILE__,__LINE__,asrl_assert_stringstream.str()); \
00366 }
00367
00376 #define ASRL_ASSERT_NE_DBG(value, testValue, message) \
00377 if(value == testValue) \
00378 { \
00379 std::ostringstream asrl_assert_stringstream; \
00380 asrl_assert_stringstream << "debug assert " << #value << " != " << #testValue << " failed [" << value << " != " << testValue << "]: " << message; \
00381 asrl::detail::throw_runtime_error(__FUNCTION__,__FILE__,__LINE__,asrl_assert_stringstream.str()); \
00382 }
00383
00384
00391 #define ASRL_CHECK_CUDA_ERROR_DBG(errorMessage) { \
00392 cudaError_t err = cudaGetLastError(); \
00393 if( cudaSuccess != err) { \
00394 std::ostringstream asrl_assert_stringstream; \
00395 asrl_assert_stringstream << "CUDA error: " << cudaGetErrorString( err ) << " : " << errorMessage; \
00396 asrl::detail::throw_runtime_error(__FUNCTION__,__FILE__,__LINE__,asrl_assert_stringstream.str()); \
00397 } \
00398 }
00399
00400
00401 #else
00402
00403 #define COUT_P(x)
00404 #define ASRL_THROW_DBG(message)
00405 #define ASRL_ASSERT_DBG(condition, message)
00406 #define ASRL_ASSERT_GE_LT_DBG(value, lowerBound, upperBound, message)
00407 #define ASRL_ASSERT_LT_DBG(value, upperBound, message)
00408 #define ASRL_ASSERT_GT_DBG(value, lowerBound, message)
00409 #define ASRL_ASSERT_LE_DBG(value, upperBound, message)
00410 #define ASRL_ASSERT_GE_DBG(value, lowerBound, message)
00411 #define ASRL_ASSERT_NE_DBG(value, testValue, message)
00412 #define ASRL_ASSERT_EQ_DBG(value, testValue, message)
00413 #define ASRL_ASSERT_EQ_TOL_DBG(value, testValue, tolerance, message)
00414 #define ASRL_CHECK_CUDA_ERROR_DBG(errorMessage)
00415
00416 #endif // DEBUG
00417
00418 #endif // ASRL_ASSERT_MACROS_HPP