func
Overview
Collection of general purpose functions.
Functions
cmp
Functions that interpret the result of a compare function.
diff_c
bool diff_c( int res );
Returns true if the result of a compare function means that both values are different, otherwise false.
eq_c
bool eq_c( int res );
Returns true if the result of a compare function means that the first and second value are equal, otherwise false.
gt_c
bool gt_c( int res );
Returns true if the result of a compare function means that the first value is greater than the second value, otherwise false.
gte_c
bool gte_c( int res );
Returns true if the result of a compare function means that the first value is greater than or equal the second value, otherwise false.
lt_c
bool lt_c( int res );
Returns true if the result of a compare function means that the first value is less than the second value, otherwise false.
lte_c
bool lte_c( int res );
Returns true if the result of a compare function means that the first value is less than or equal the second value, otherwise false.
concat
concat2_c_
#define concat2_c_( A, B )
Concatenates both tokens via ##-operator.
#include "clingo/lang/expect.h"
#include "clingo/lang/func.h"
int main( void )
{
init_tap_c_();
int64_t foobar = 42;
expect_c_( concat2_c_( foo, bar ) == 42 );
return finish_tap_c_();
}
concat3_c_
#define concat3_c_( A, B, C )
Concatenates three tokens via ##-operator.
#include "clingo/lang/expect.h"
#include "clingo/lang/func.h"
int main( void )
{
init_tap_c_();
int64_t foobar = 42;
expect_c_( concat3_c_( fo, ob, ar ) == 42 );
return finish_tap_c_();
}
xconcat2_c_
#define xconcat2_c_( A, B )
Concatenates the result of the expansion of the macros A and/or B.
#include "clingo/lang/expect.h"
#include "clingo/lang/func.h"
int main( void )
{
init_tap_c_();
#define foo 123
#define bar 789
expect_c_( xconcat2_c_( foo, bar ) == 123789 );
return finish_tap_c_();
}
xconcat3_c_
#define xconcat3_c_( A, B, C )
Concatenates the result of the expansion of the macros A, B and/or C.
#include "clingo/lang/expect.h"
#include "clingo/lang/func.h"
int main( void )
{
init_tap_c_();
#define fo 12
#define ob 56
#define ar 90
expect_c_( xconcat3_c_( fo, ob, ar ) == 125690 );
return finish_tap_c_();
}
constraints
Constraints are macro functions that wrap assert and abort the program if the constraint is not fulfilled. The name of a constraint function must allways start with the prefix "must_".
must_be_c_
#define must_be_c_( Expression )
Constraint that requires the Expression to be true.
must_exist_c_
#define must_exist_c_( Ptr )
Constraint that requires that the pointer Ptr is not NULL.
must_be_positive_c_
#define must_be_positive_c_( Value )
Constraint that requires Value to be greater or equal 0.
must_be_in_range_c_
#define must_be_in_range_c_( First, Value, Last )
Constraint that requires Value to be in the range between First and Last.
(First ⇐
Value ⇐
Last)
flags
Macro functions to work with bitflags.
#include "clingo/lang/expect.h"
#include "clingo/lang/func.h"
#define cFLAGS_ \
XMAP_C_( c_1, 1 << 0 ) \
XMAP_C_( c_2, 1 << 1 ) \
XMAP_C_( c_3, 1 << 2 ) \
XMAP_C_( c_4, 1 << 3 ) \
XMAP_C_( c_5, 1 << 4 ) \
XMAP_C_( c_6, 1 << 5 ) \
XMAP_C_( c_7, 1 << 6 ) \
XMAP_C_( c_8, 1 << 7 )
#define XMAP_C_( N, I ) N = I,
enum c_Flags { cFLAGS_ };
#undef XMAP_C_
int main( void )
{
init_tap_c_();
uint8_t config = 0;
expect_c_( not has_c_( config, c_1|c_2|c_3|c_4|c_5|c_6|c_7|c_8 ) );
config = set_c_( config, c_1|c_2|c_3|c_6|c_7 );
expect_c_( has_c_( config, c_1|c_2|c_3|c_6|c_7 ) );
expect_c_( not has_c_( config, c_4|c_5|c_8 ) );
config = unset_c_( config, c_2|c_3|c_7 );
expect_c_( has_c_( config, c_1|c_6 ) );
expect_c_( not has_c_( config, c_2|c_3|c_4|c_5|c_7|c_8 ) );
return finish_tap_c_();
}
has_c_
#define has_c_( Config, Flags )
Checks if the Config bitflag has all Flags set.
set_c_
#define set_c_( Config, Flags )
Sets the Flags on the Config bitflag.
unset_c_
#define unset_c_( Config, Flags )
Unsets the Flags on the Config bitflag.
loop
once_c_
#define once_c_( Var )
Macro to define a for statement to loop one time. Var will be the name of the int64_t loop control variable.
#include "clingo/lang/expect.h"
#include "clingo/lang/func.h"
int main( void )
{
init_tap_c_();
int64_t count = 0;
once_c_( xyz )
{
++count;
}
expect_c_( count == 1 );
return finish_tap_c_();
}
times_c_
#define times_c_( N, Var )
Macro to define a for statement to loop N times. Var will be the name of the int64_t loop controle variable.
#include "clingo/io/cRecorder.h"
#include "clingo/lang/expect.h"
#include "clingo/lang/func.h"
int main( void )
{
init_tap_c_();
cRecorder* rec = &recorder_c_( 128 );
times_c_( 3, i )
{
if ( i < 2 )
{
record_chars_c_( rec, "hello, " );
}
else
{
record_chars_c_( rec, "hello!" );
}
}
expect_c_( recorded_is_c( rec, "hello, hello, hello!" ) );
return finish_tap_c_();
}
num
Functions that check or modify numbers.
in_range_c_
#define in_range_c_( Min, Value, Max )
Returns true if Value is greater or equal than Min and less or equal than Max, otherwise false.
#include "clingo/lang/expect.h"
#include "clingo/lang/func.h"
int main( void )
{
init_tap_c_();
expect_c_( !in_range_c_( 0, -10, 5 ) );
expect_c_( in_range_c_( 0, 0, 5 ) );
expect_c_( in_range_c_( 0, 3, 5 ) );
expect_c_( in_range_c_( 0, 5, 5 ) );
expect_c_( !in_range_c_( 0, 10, 5 ) );
expect_c_( !in_range_c_( -1.0, -1.5, 1.0 ) );
expect_c_( in_range_c_( -1.0, -1.0, 1.0 ) );
expect_c_( in_range_c_( -1.0, -0.5, 1.0 ) );
expect_c_( in_range_c_( -1.0, 1.0, 1.0 ) );
expect_c_( !in_range_c_( -1.0, 1.5, 1.0 ) );
return finish_tap_c_();
}
is_sign_c_
#define is_sign_c_( Value )
Checks if Value represants a sign.
#include "clingo/lang/expect.h"
#include "clingo/lang/func.h"
int main( void )
{
init_tap_c_();
expect_c_( is_sign_c_( '+' ) );
expect_c_( not is_sign_c_( '~' ) );
expect_c_( is_sign_c_( '-' ) );
return finish_tap_c_();
}
sign_c_
#define sign_c_( Value )
Returns the sign of a number Value as char. '+' for positive values and '-' for negative values.
#include "clingo/lang/expect.h"
#include "clingo/lang/func.h"
int main( void )
{
init_tap_c_();
expect_c_( '+' == sign_c_( 100 ) );
expect_c_( '+' == sign_c_( 0 ) );
expect_c_( '-' == sign_c_( -100 ) );
return finish_tap_c_();
}
stringify
Macro functions that stringify code.
stringify_c_
#define stringify_c_( A )
Turns a code fragment into a string constant whose contents are the text for the code fragment.
#include "clingo/lang/expect.h"
#include "clingo/lang/func.h"
#include "clingo/type/cChars.h"
#define expect_( Str, Exp ) \
expect_c_( chars_is_c( c_c( Str ), (Exp) ) )
int main( void )
{
init_tap_c_();
#define x 2
char const* str = stringify_c_( x + 2 );
expect_( str, "x + 2" );
return finish_tap_c_();
}
xstringify_c_
#define xstringify_c_( A )
Stringifies the result of the expansion of a macro A.
#include "clingo/lang/expect.h"
#include "clingo/lang/func.h"
#include "clingo/type/cChars.h"
#define expect_( Str, Exp ) \
expect_c_( chars_is_c( c_c( Str ), (Exp) ) )
int main( void )
{
init_tap_c_();
#define x 2
char const* str = xstringify_c_( x + 2 );
expect_( str, "2 + 2" );
return finish_tap_c_();
}
swap
Functions that allow to swap values or memory.
swap_c_
#define swap_c_( Type, X, Y, Prefix )
Swaps the values between the variables X and Y. Both must have the same Type, and the Prefix value will be used as prefix for the temporary variable.
#include "clingo/lang/func.h"
#include "clingo/lang/expect.h"
#include "clingo/type/cRange.h" // cRange
int main( void )
{
init_tap_c_();
// ----------------------------------------------------------------- swap int
int x = 5;
int y = 9;
swap_c_( int, x, y, tmp_int );
expect_c_( x == 9 );
expect_c_( y == 5 );
// --------------------------------------------------------------- swap float
float fx = 1.0;
float fy = 99.0;
swap_c_( float, fx, fy, tmp_float );
expect_c_( fx == 99.0 );
expect_c_( fy == 1.0 );
// ------------------------------------------------------------- swap structs
cRange a = { .min = 1, .max = 5 };
cRange z = { .min = 4, .max = 5 };
swap_c_( cRange, a, z, tmp_range );
expect_c_( a.min == 4 );
expect_c_( a.max == 5 );
expect_c_( z.min == 1 );
expect_c_( z.max == 5 );
return finish_tap_c_();
}
va_args
nargs_c_
#define nargs_c_( ... )
Macro function that determines the number of arguments that the variable argument contains. The number of arguments must contain at least 1 and at most 255 elements.
The function uses the macro function nargs_helper_c_ for the implementation.
#include "clingo/lang/expect.h"
#include "clingo/lang/func.h"
int main( void )
{
init_tap_c_();
expect_c_( nargs_c_( 9 ) == 1 );
expect_c_( nargs_c_( 9, 8, 7 ) == 3 );
int res = nargs_c_( 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 );
expect_c_( res == 10 );
res = nargs_c_(
9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 // 20
);
expect_c_( res == 20 );
res = nargs_c_(
1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, // 20
1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, // 40
1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, // 60
1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, // 80
1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, // 100
1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, // 120
1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, // 140
1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, // 160
1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, // 180
1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6 // 196
);
expect_c_( res == 196 );
res = nargs_c_(
1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, // 20
1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, // 40
1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 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1, 2, 3, 4, 5, 6, 7, 8, 9, 0, // 200
1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, // 220
1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, // 240
1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 11 // 256
);
expect_c_( res == 11 );
return finish_tap_c_();
}
nargs_helper_c_
#define nargs_helper_c_( ... )
Macro function that is used be nargs_c_, should not be used.