├── .travis.yml ├── LICENSE ├── README.md ├── assume.cpp ├── assume.hpp ├── sample.cc └── tests.cxx /.travis.yml: -------------------------------------------------------------------------------- 1 | language: cpp 2 | sudo: required 3 | 4 | compiler: 5 | - clang 6 | - gcc 7 | 8 | install: 9 | - wget --quiet -O - https://raw.githubusercontent.com/r-lyeh/depot/master/travis.pre.sh | bash -x 10 | 11 | script: 12 | - wget --quiet -O - https://raw.githubusercontent.com/r-lyeh/depot/master/travis.build.sh | bash -x 13 | - wget --quiet -O - https://raw.githubusercontent.com/r-lyeh/depot/master/travis.run.sh | bash -x 14 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2015 r-lyeh (https://github.com/r-lyeh) 2 | 3 | This software is provided 'as-is', without any express or implied 4 | warranty. In no event will the authors be held liable for any damages 5 | arising from the use of this software. 6 | 7 | Permission is granted to anyone to use this software for any purpose, 8 | including commercial applications, and to alter it and redistribute it 9 | freely, subject to the following restrictions: 10 | 11 | 1. The origin of this software must not be misrepresented; you must not 12 | claim that you wrote the original software. If you use this software 13 | in a product, an acknowledgment in the product documentation would be 14 | appreciated but is not required. 15 | 2. Altered source versions must be plainly marked as such, and must not be 16 | misrepresented as being the original software. 17 | 3. This notice may not be removed or altered from any source distribution. 18 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Assume 2 | ======= 3 | 4 | - Assume is a smarter assert replacement (C++03). 5 | - Assume is handy. LHS/RHS values are printed as long as they are `ostream` friendly. 6 | - Assume is cross-platform. Crash handler fallbacks to `assert()` symbol. 7 | - Assume is header only. 8 | - Assume is zlib/libpng licensed. 9 | 10 | ## Sample 11 | 12 | ```c++ 13 | #include "assume.hpp" 14 | 15 | int main() { 16 | int a = 1, b = 2; 17 | 18 | assume( a < b ); 19 | assume( a > b ); 20 | } 21 | ``` 22 | 23 | ## Showcase 24 | 25 | ```c++ 26 | #~/> g++ sample.cc && ./a.out 27 | says: expression failed! (a > b) -> (1 > 2) -> (unexpected) at sample.cc:7 28 | ``` 29 | 30 | ## Changelog 31 | 32 | - v1.0.1 (2015/09/19) 33 | - Force flushed pipes 34 | - v1.0.0 (2015/08/07) 35 | - Initial C++03 version 36 | -------------------------------------------------------------------------------- /assume.cpp: -------------------------------------------------------------------------------- 1 | #include "assume.hpp" 2 | -------------------------------------------------------------------------------- /assume.hpp: -------------------------------------------------------------------------------- 1 | // Smarter assert replacement for LHS/RHS values. zlib/libpng licensed. 2 | // - rlyeh ~~ listening to Tuber / Desert Overcrowded 3 | 4 | /* Private API */ 5 | 6 | #ifndef ASSUME_HPP_HEADER 7 | #define ASSUME_HPP_HEADER 8 | 9 | #define ASSUME_VERSION "1.0.1" /* (2015/09/19) Force flushed pipes 10 | #define ASSUME_VERSION "1.0.0" // (2015/08/07) Initial C++03 version */ 11 | 12 | #include 13 | #include 14 | #include 15 | 16 | #include 17 | #include 18 | #include 19 | 20 | namespace assume { 21 | class check { 22 | bool ok; 23 | std::deque< std::string > xpr; 24 | template static std::string to_str( const T &t ) { std::stringstream ss; return (ss << t) ? ss.str() : "??"; } 25 | public: 26 | check( const char *const text, const char *const file, int line, bool result ) 27 | : xpr(4), ok(result) 28 | { xpr[0] = std::string(file) + ':' + to_str(line); xpr[2] = text; } 29 | 30 | ~check() { 31 | if( xpr.empty() ) return; 32 | operator bool(); 33 | xpr[0] = xpr[0] + xpr[1]; 34 | xpr.erase( xpr.begin() + 1 ); 35 | if( !ok ) { 36 | xpr[2] = xpr[2].substr( xpr[2][2] == ' ' ? 3 : 4 ); 37 | xpr[1].resize( (xpr[1] != xpr[2]) * xpr[1].size() ); 38 | std::string buf; 39 | buf = " says: expression failed! (" + xpr[1] + ") -> (" + xpr[2] + ") -> (unexpected) at " + xpr[0] + "\n"; 40 | fprintf(stdout, "%s", buf.c_str() ); fflush( stdout ); fclose( stdout ); 41 | fprintf(stderr, "%s", buf.c_str() ); fflush( stderr ); fclose( stderr ); 42 | // assert fallback here 43 | assert( !" says: expression failed!" ); 44 | // user defined fallbacks here 45 | for(;;) {} 46 | }; 47 | } 48 | # define assume$(OP) \ 49 | template check &operator OP( const T &rhs ) { return xpr[3] += " "#OP" " + to_str(rhs), *this; } \ 50 | template check &operator OP( const char (&rhs)[N] ) { return xpr[3] += " "#OP" " + to_str(rhs), *this; } 51 | operator bool() { 52 | if( xpr.size() >= 3 && xpr[3].size() >= 6 ) { 53 | char signR = xpr[3].at(2); 54 | bool equal = xpr[3].substr( 4 + xpr[3].size()/2 ) == xpr[3].substr( 3, xpr[3].size()/2 - 3 ); 55 | ok = ( signR == '=' ? equal : ( signR == '!' ? !equal : ok ) ); 56 | } 57 | return ok; 58 | } 59 | assume$(<) assume$(<=) assume$(>) assume$(>=) assume$(!=) assume$(==) assume$(&&) assume$(||) 60 | # undef assume$ 61 | }; 62 | } 63 | 64 | #endif 65 | 66 | /* Public API */ 67 | 68 | #include 69 | 70 | #ifdef assume 71 | #undef assume 72 | #endif 73 | 74 | #if !(defined(NDEBUG) || defined(_NDEBUG)) 75 | #define assume(...) ( !!(__VA_ARGS__) ? \ 76 | ( assume::check(#__VA_ARGS__,__FILE__,__LINE__,1) < __VA_ARGS__ ) : \ 77 | ( assume::check(#__VA_ARGS__,__FILE__,__LINE__,0) < __VA_ARGS__ ) ) 78 | #else 79 | #define assume assert 80 | #endif 81 | -------------------------------------------------------------------------------- /sample.cc: -------------------------------------------------------------------------------- 1 | #include "assume.hpp" 2 | 3 | int main() { 4 | int a = 1, b = 2; 5 | 6 | assume( a < b ); 7 | assume( a > b ); 8 | } 9 | -------------------------------------------------------------------------------- /tests.cxx: -------------------------------------------------------------------------------- 1 | #include "assume.hpp" 2 | #include "assume.hpp" // check header can be included twice 3 | 4 | void cmp( int x ) { 5 | assume( x ); 6 | assume( 0 == !x ); 7 | assume( !x == 0 ); 8 | assume( !!x ); 9 | assume( !!x ); 10 | } 11 | void cmpL( int x, int y ) { 12 | cmp(x); 13 | cmp(y); 14 | assume( x < y ); 15 | assume( y > x ); 16 | assume( x <= y ); 17 | assume( y >= x ); 18 | assume( x != y ); 19 | assume( y != x ); 20 | 21 | assume( !(x > y) ); 22 | assume( !(y < x) ); 23 | assume( !(x >= y) ); 24 | assume( !(y <= x) ); 25 | assume( !(x == y) ); 26 | assume( !(y == x) ); 27 | } 28 | void cmpG( int x, int y ) { 29 | cmp(x); 30 | cmp(y); 31 | assume( x > y ); 32 | assume( y < x ); 33 | assume( x >= y ); 34 | assume( y <= x ); 35 | assume( x != y ); 36 | assume( y != x ); 37 | 38 | assume( !(x < y) ); 39 | assume( !(y > x) ); 40 | assume( !(x <= y) ); 41 | assume( !(y >= x) ); 42 | assume( !(x == y) ); 43 | assume( !(y == x) ); 44 | } 45 | void cmpE( int x, int y ) { 46 | cmp(x); 47 | cmp(y); 48 | assume( x == y ); 49 | assume( y == x ); 50 | 51 | assume( !(x != y) ); 52 | assume( !(y != x) ); 53 | } 54 | 55 | int main() { 56 | int a = 1, b = 2; 57 | assume( a < b ); 58 | assume( b > a ); 59 | assume( a <= b ); 60 | assume( b >= a ); 61 | assume( a != b ); 62 | assume( b != a ); 63 | 64 | int z = 0, o = 1, t = 2; 65 | assume(o); 66 | assume(o+o); 67 | assume(!z); 68 | assume( o == o ); 69 | assume( z != o ); 70 | assume( o < t ); 71 | assume( o <= t ); 72 | assume( t > o ); 73 | assume( t >= o ); 74 | 75 | cmpL(8335,10002); 76 | cmpG(10002,8335); 77 | cmpE(8335,8335); 78 | } 79 | --------------------------------------------------------------------------------