Verify parameter has same value for multiple expected calls in gmock - c++

Given some mock function which takes at least one parameter:
MOCK_METHOD1(fun, void(int p));
How can i EXPECT_CALL two identical calls in terms of the value of parameter p? I don't care what the value of p actually is, as long as it is the same for both calls to the function fun. I have no way to predict the value of p in the test case.

Option #1
EXPECT_CALL( mock, fun( testing::Truly( []( int p ) {
static int initial = p;
return initial == p;
} ) ) )
.Times( 2 );
Option #2
int p = 0;
testing::Sequence seq;
EXPECT_CALL( mock, fun( _ ) )
.InSequence( seq )
.WillOnce( testing::SaveArg< 0 >( &p ) );
EXPECT_CALL( mock, fun( testing::Eq( testing::ByRef( p ) ) ) )
.Times( 1 )
.InSequence( seq );

Related

How to create a group of constants and check if a variable is or isn't included in that group?

I want to create an if code to check if variable x is a member of a defined group of constant named a, for example a = { 1 , 2 , 3 , 4 }, then use something like if (x != a).
I only know to use it like this
if ( ( x != 1 ) || ( x != 2 ) || ( x != 3 ) || ( x != 4 ) )
You can use functions like this
bool exist_in_group(int value, const int* group,int group_size)
{
bool res{false};
for(int i=0;i<group_size;i++)
{
if(group[i] == value)
res = true;
}
return res;
}
This function check if value exist in your array(group) or not

Test function for checking an optional argument

Related to my previous question, I have tried to make a function present() for checking the presence of an optional argument. However, the following code
proc present( x ) { return x.type != void; }
proc test( a: ?T = _void )
{
writeln();
writeln( "test| a = ", a );
writeln( "test| condition = ", a.type != void );
writeln( "test| present( a ) = ", present( a ) );
if present( a ) // error (Line 1)
// if a.type != void // works (Line 2)
{
a = 10;
}
}
// no optional arg
test();
// pass an optional array
var arr: [1..5] int;
test( a = arr );
writeln();
writeln( "main| arr = ", arr );
gives a compile-time error
mytest.chpl:3: In function 'test':
mytest.chpl:13: error: illegal lvalue in assignment
mytest.chpl:13: error: a void variable cannot be assigned
which says that the line a = 10; is problematic. On the other hand, if I use Line 2 instead of Line 1, the code works as expected:
test| a =
test| condition = false
test| present( a ) = false
test| a = 0 0 0 0 0
test| condition = true
test| present( a ) = true
main| arr = 10 10 10 10 10
Also, if I replace Line 1 or 2 by if isArray( a ), the code also works. Does this mean that we need to let the compiler explicitly know that the line a = 10; is not reached when a is _void? (In other words, is present() not sufficient to let the compiler know it because the test condition is "hidden" inside present()?)
Does this mean that we need to let the compiler explicitly know that
the line a = 10; is not reached when a is _void? (In other words, is
present() not sufficient to let the compiler know it because the test
condition is "hidden" inside present()?)
Yes, that's right. The compiler needs to know at compile-time that the body of that if should be compiled only in the case that the argument not void. Putting the x.type != void check in that conditional is a reasonable solution but if you want to have a function to compute if that conditional should be evaluated, you can do so. Just mark present as a param function - which means that it returns a value that should be known at compile-time. Here is the complete example:
proc present( x ) param { return x.type != void; }
proc test( a: ?T = _void )
{
writeln();
writeln( "test| a = ", a );
writeln( "test| condition = ", a.type != void );
writeln( "test| present( a ) = ", present( a ) );
if present( a )
{
a = 10;
}
}
// no optional arg
test();
// pass an optional array
var arr: [1..5] int;
test( a = arr );
writeln();
writeln( "main| arr = ", arr );
If you would like to read more about the language design in this area, see "The Param Return Intent" subsection in "Procedures" chapter section "Return Intents" of the language specification.

std:unordered_map insertion <value, Struct>; Arithmatic exception den__ is zero

I have a code where I am inserting a struct in a unordered_map.
INFO Info( v1, UINT_MAX, v3, v4, v5 );
hmap_.insert( std::pair< key,INFO >( v1, Info ) );
at the above line I put a break point using gdb and Print the value of all the variables: All are fine gets printed as expected.
(gdb) p Info
$4 = {v1_ = valid_value, v2 = valid_value, v3 = , v4 = valid_value, v4 = valid_value}
Now when I go into the insert function of hash map using
(gdb) s
I get
(gdb) s
std::pair<unsigned long, namespace::class::Info>::pair<unsigned long&, namespace::class::Info&, true> (this=0x7fffffffd7e0, __x=#0x7fffffffd768: 1100000000000549, __y=...) at /usr/include/c++/7/bits/stl_pair.h:331
331 : first(std::forward<_U1>(__x)), second(std::forward<_U2>(__y)) { }
(gdb) n
Thread 1 "test_1" received signal SIGFPE, Arithmetic exception.
0x000055555556b105 in std::__detail::_Mod_range_hashing::operator() (this=0x555555e09ec0, __num=1100000000000549, __den=0) at /usr/include/c++/7/bits/hashtable_policy.h:448
448 { return __num % __den; }
As it can be seen in the gdb: while hashing it makes the denominator, which it produces from the second variable whose value I have printed and its correct, Zero (__den =0 ) !
I have a struct Info in another file with a normal constructor:
struct INFO
{
Info():
v1_( 0 ), v2_( UINT_MAX ), v_3( INVALID ),
v4_( 0 ), v5_( 0 )
{}
Info( value_type1 v1, const value_type2& v2,
value_type3 v3, value_type4 v4, value_type5 v5 ):
v1_( v1 ), v2_( v2 ),
v3_( side ), v4_( v4 ), v5_( v5 )
{}
value_type1 v1_ ;
value_type2 v2_ ;
value_type3 v3_ ;
value_type4 v4_ ;
value_type5 v5_ ;
};
Why would while hashing the denomator which is I guess is getting created from the second value of the pair which is a valid value will become zero and generate an arithmetic exception.
Thanks

Delete list element from class

I try to delete list element and always get error message.
Firstly I have this class structure :
Class X
{ public:
ID;
name;
}
then I have list container.
Now, I want to delete specific element from container.
void delete ( list<X> a , X b )
{
list<X>::iterator itr;
for ( itr =a.begin() ; itr != a.end() ; itr++ )
{
if ( *itr.ID == b.ID )
{ a.erase(itr) ; }
}
}
But, I get error:
" iterator doesn't have ID ".
How can I fix it?
This is because the dot operator . is "stronger" than the dereference operator *. According to this table, dot has a precedence of two, while dereference asterisk * has a precedence of three (lower is stronger). This means that without the parentheses the compiler tries to get iterator's member ID, and then dereference the value of ID. This is not possible, because the iterator has no member called ID.
You need to put parentheses to enforce the precedence that you want:
if ( (*itr).ID == b.ID )
Alternatively, you can use the -> operator to dereference the iterator:
if ( itr->ID == b.ID )
Note: C++ Standard Library provides a way to do this without a loop:
a.erase(
remove_if(
a.begin()
, a.end()
, [](X x){ return x.ID == b.ID; }
)
, a.end()
);
Demo on ideone.
i prefer to use itr->ID instead of *itr.ID. the later is a little confusing sometime.

RPAREN EQALOP and unbound variable and constructor errors in SML/ML

Said I have got 2 CNF logical phrases a,b and my distrib function should return the CNF form of a|b (a OR b).
Replacing rules that I've got are:
1) Replace p|(q&r) by (p|q)&(p|r)
2) Replace (q&r)|p by (q|p)&(r|p)
A prop defined this way:
datatype prop = Atom of string | Not of prop | And of prop*prop | Or of prop*prop;
The function:
local
fun doOr(prop1,prop2) = (Or(prop1,prop2))
fun distrib1 (Or(Atom(sName1),Atom(sName2) ) ) = Or(Atom(sName1), Atom(sName2) )
|distrib1 (Or(Not(Atom(sName1) ),Atom(sName2) ) ) = Or(Not(Atom(sName1) ), Atom(sName2) )
| distrib1 (Or(Atom(sName1),Not(Atom(sName2) ) ) ) = Or(Atom(sName1), Not(Atom(sName2) ) )
| distrib1 (Or(Not(Atom(sName1)),Not(Atom(sName2) ) ) ) = Or(Not(Atom(sName1)), Not(Atom(sName2) ) )
| distrib1 (Or(prop1,And(prop2,prop3) ) ) = And( distrib1(Or(prop1,prop2) ), distrib1(Or(prop1,prop3) ) )
| distrib1 (Or(And(prop1, prop2), prop3) ) ) = And( distrib1(Or(prop1,prop3) ), distrib1(Or(prop2,prop3) ) )
in
fun distrib (prop1,prop2) = distrib1(doOr(prop1,prop2) );
end;
Well, I don't know if the function itself is right although I just went through all the base options and the replacing rules but for now I get the above errors when the EQALOP appear after the distrib1 function and the constructors error appear the distrib function.
Why I get those errors? I am not sure but maybe I am supposed to use let and not local but then how can I transform it to a let structure?
Thanks.
In the last case of distrib1 you have a total of 3 opening parentheses, but 4 closing:
| distrib1 (Or(And(prop1, prop2), prop3) ) ) =
Which is why you get the the syntax error about the RPAREN.
You're getting an error in distrib because distrib1 has not been defined due to the syntax errors and thus it is an unknown variable. Fixing the syntax error in distrib1 will fix this too.