Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Gauthier Quesnel
bits
Commits
5e300f34
Commit
5e300f34
authored
Jul 18, 2018
by
Gauthier Quesnel
Browse files
assert!
parent
dc059b1a
Pipeline
#218
passed with stage
in 50 seconds
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
include/bits/assert.hpp
0 → 100644
View file @
5e300f34
/* Copyright (C) 2018 INRA
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef ORG_VLEPROJECT_BITS_ASSERT_HPP
#define ORG_VLEPROJECT_BITS_ASSERT_HPP
#include <exception>
#include <cstdio>
#define stringify_detail(x) #x
#define stringify(x) vle_stringify_detail(x)
#if defined(__clang__) || defined(__GNUC__)
#define likely(x) __builtin_expect(!!(x), 1)
#define unlikely(x) __builtin_expect(!!(x), 0)
#else
#define likely(x) (!!(x))
#define unlikely(x) (!!(x))
#endif
//
// In internal API, we prefer abort application when precondition,
// postcondition or assertion fail. For external API, user can select to use
// exception or return with message in terminal.
//
namespace
bits
{
namespace
details
{
void
fail_fast
(
const
char
*
type
,
const
char
*
cond
,
const
char
*
file
,
const
char
*
line
)
{
std
::
fprintf
(
stderr
,
"%s [%s] failure at %s: %s
\n
"
,
type
,
cond
,
file
,
line
);
std
::
terminate
();
}
}
// namespace details
}
// namespace bits
#define BITS_CONTRACT_CHECK(type, cond) \
(likely(cond) ? static_cast<void>(0) \
: bits::details::fail_fast(type, cond, __FILE__, stringify(__LINE__)
#ifdef NDEBUG
#define expects(cond)
#define ensures(cond)
#define assert(cond)
#else
#define expects(cond) BITS_CONTRACT_CHECK("Precondition", cond)
#define ensures(cond) BITS_CONTRACT_CHECK("Postcondition", cond)
#define assert(cond) BITS_CONTRACT_CHECK("Assertion", cond)
#endif
#endif
\ No newline at end of file
test/parameter.cpp
View file @
5e300f34
...
...
@@ -23,6 +23,7 @@
#include <tuple>
#include <vector>
#include <bits/assert.hpp>
#include <bits/getopt.hpp>
#include <bits/tagged-union.hpp>
#include <bits/unit-test.hpp>
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment