Is MOCK_METHOD* not enough? - googletest

Inspired by that Google doc "ForDummies" :), I am trying a simple Google Mock with Google Test example as follows:
#include <gtest/gtest.h>
#include <gmock/gmock.h>
using ::testing::_;
class A
{
public:
A()
{
std::cout << "A()" << std::endl;
}
virtual ~A()
{
std::cout << "~A()" << std::endl;
}
virtual int incVirtual(int i)
{
return i + 1;
}
};
class MockA: public A
{
public:
MOCK_METHOD1(incVirtual, int(int));
};
TEST(Test, IncTest) {
MockA a;
EXPECT_CALL(a, incVirtual(_));
printf("n == %d\n", a.incVirtual(0));
}
int main(int argc, char **argv)
{
::testing::InitGoogleMock(&argc, argv);
return RUN_ALL_TESTS();
}
When I run it, I get n == 0:
[==========] Running 1 test from 1 test case.
[----------] Global test environment set-up.
[----------] 1 test from Test
[ RUN ] Test.IncTest
A()
n == 0
~A()
whereas I expect it to be n == 1. So I wonder if just defining MOCK_METHODx in the mock class is not enough for mocking the base class method and something additional needs to be done to make MockA::incVirtual call A::incVirtual?

This is behaving the way it should. By mocking your function, the MockA class has essentially overridden the A class implementation with a "do nothing" implementation.
If you want to call A::incVirtual() when calling MockA::incVirtual(), try the following
EXPECT_CALL(a, incVirtual(_)).WillOnce(Invoke(&a, &A::incVirtual));

Related

How to make variable in companion object protected

I want to make a variable in companion object protected. I think when I make the variable protected, it would be accessible only from another class which inherit the variable's class, not from other classes or functions. But It doesn't work.
Here's my code
open class Population{
companion object{
#JvmStatic // Though I added it because of recommendation of IntelliJ, It still doesn't work.
protected var population_quantity: Int = 0
}
}
class Marin : Population(){
init{
population_quantity += 1 // I want to make it possible
}
}
class Checker : Population() {
fun printPopulationQuantity()
{
println(population_quantity) // I also want to make it possible
}
}
fun main(){
var m1 = Marin()
var checker = Checker()
// But I want to make codes below here impossible
// Population.population_quantity += 1
// println(Population.population_quantity)
}
Below is what I want to make which is made via C++. I hope below would clarify what I'm saying.
#include <iostream>
class Population
{
protected:
static int population_quantity;
};
int Population::population_quantity = 0;
class Marin : Population
{
public:
Marin()
{
std::cout << "Marin is generated" << std::endl;
population_quantity += 1;
}
~Marin()
{
std::cout << "Marin is dead" << std::endl;
population_quantity -= 1;
}
};
class Checker : Population
{
public:
void printPopulationQuantity()
{
std::cout << population_quantity << std::endl;
}
};
int main()
{
Checker checker;
checker.printPopulationQuantity();
Marin *m1 = new Marin();
checker.printPopulationQuantity();
// std::cout << "population : " << Population::population_quantity << std::endl;
// Can't access population_quantity from main which doesn't inherite Population
// because Population::population_qantity is protected.
// But it is possible to access Population:population_quantity from Marin which inherits Population.
delete m1;
checker.printPopulationQuantity();
return 0;
}
When you help me solve this problem, I'm sure that I would feel sincere thankful from bottom of my heart.
That is probably because in class Checker : Population you have a void function which is in public.
Try changing it to this:
class Checker : Population
{
protected:
void printPopulationQuantity()
{
std::cout << population_quantity << std::endl;
}
};

Member of b object can't write in SystemC

I got this error on SystemC, and I don't understand why. The error is:
'write': is not a member of
'sc_core::sc_in' ConsoleApplication1
'write': is not a member of 'sc_core::sc_in'
class "sc_core::sc_in" has no member "write"
class "sc_core::sc_in" has no member "write"
Here I put together the code.
#include<systemc.h>
SC_MODULE(prin) {
sc_in<bool> a;
void print() {
cout << "Hello World\n";
}
SC_CTOR(prin) {
SC_METHOD(print);
sensitive << a;
}
};
SC_MODULE(input) {
prin b;
void in() {
b.a.write(false);
wait();
b.a.write(true);
wait();
}
SC_CTOR(input) : b("sds"){
SC_THREAD(in);
}
};
int sc_main(int argc, char* argv[]) {
input prin1("pint");
sc_start();
return 0;
}
If the error seems confusing, here I put together the picture of my error:
The port "a" is an input port so cannot be written to. If you make it an output port then you can write to it. Also, the port is not bound so you will also get an error for that so I have bound a signal to it just so it compiles.
#include <systemc.h>
SC_MODULE(prin) {
sc_out<bool> a; //output port
sc_signal<bool> sig; //something to bind port a to
void print() {
cout << "Hello World\n";
}
SC_CTOR(prin) {
SC_METHOD(print);
sensitive << a;
a(sig); //bind port a to s signal
}
};
SC_MODULE(input) {
prin b;
void in() {
b.a.write(false);
wait();
b.a.write(true);
wait();
}
SC_CTOR(input) : b("sds"){
SC_THREAD(in);
}
};
int sc_main(int argc, char* argv[]) {
input prin1("pint");
sc_start();
return 0;
}
Then
g++ -file.cpp -lsystemc
./a.out
Gives me the output
SystemC 2.3.2-Accellera --- Apr 16 2018 00:15:03
Copyright (c) 1996-2017 by all Contributors,
ALL RIGHTS RESERVED
Hello World

How to check if the test failed in Google Test TearDown()?

At the end of every "file based integration" test, I want to clear a temp folder of associated files.
If the test fails, I want to leave the files there, so I can review the unexpected output.
Is there a way in the Google Test TearDown to check if the test has failed?
Is there a way in the Google Test TearDown to check if the test has failed?
Yes, you can do that be querying ::testing::Test::HasFailure() in
the test cases of your fixture and using the result to tally failures in a counter member of
the fixture that can be queried in its TearDown(). An elementary example:
#include <gtest/gtest.h>
#include <iostream>
struct Fixture : public ::testing::Test {
virtual void SetUp() {
fails = 0;
}
virtual void TearDown() {
if (fails > 0) {
std::cerr << "Fixture::TearDown sees failures" << std::endl;
}
}
unsigned fails;
};
TEST_F(Fixture, foo) {
EXPECT_EQ(1,0);
fails += ::testing::Test::HasFailure();
}
TEST_F(Fixture, bar) {
EXPECT_EQ(1,1);
fails += ::testing::Test::HasFailure();
}
int main(int argc, char **argv) {
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
And output:
[==========] Running 2 tests from 1 test case.
[----------] Global test environment set-up.
[----------] 2 tests from Fixture
[ RUN ] Fixture.foo
/home/imk/dev/so/gtest/main.cpp:19: Failure
Expected: 1
To be equal to: 0
Fixture::TearDown sees failures
[ FAILED ] Fixture.foo (0 sec)
[ RUN ] Fixture.bar
[ OK ] Fixture.bar (0 sec)
[----------] 2 tests from Fixture (0.001 sec total)
[----------] Global test environment tear-down
[==========] 2 tests from 1 test case ran. (1 ms total)
[ PASSED ] 1 test.
[ FAILED ] 1 test, listed below:
[ FAILED ] Fixture.foo
1 FAILED TEST
Simplified and updated version of #mike-kinghan's answer.
Still using the ::testing::Test::HasFailure() but without the counter (it will always evaluate to 1 since the HasFailure() retuens a boolean)
#include <gtest/gtest.h>
#include <iostream>
struct Fixture : public ::testing::Test {
virtual void SetUp() {}
virtual void TearDown() {
if (::testing::Test::HasFailure()) {
std::cerr << "Fixture::TearDown sees failures" << std::endl;
} else {
std::cout << "Fixture::TearDown sees NO failures" << std::endl;
}
}
};
TEST_F(Fixture, foo) { EXPECT_EQ(0, 0); }
TEST_F(Fixture, bar) { EXPECT_EQ(1, 0); }
TEST_F(Fixture, car) { EXPECT_EQ(2, 2); }
TEST_F(Fixture, tar) { EXPECT_EQ(3, 0); }
Output:

boost::serialize segfaults

For boost::serialize I am trying to define a custom class with its own serialize function, similar to http://www.boost.org/doc/libs/1_53_0/libs/serialization/doc/tutorial.html#simplecase However, the program just segfaults. Why?
class Test {
public:
unsigned short testid;
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & testid;
}
};
int main() {
Test mytest = {100};
std::ofstream ofsx("test.tmp");
boost::archive::binary_oarchive oax(ofsx);
oax << mytest;
cout << "Exported";
exit(1);
}
I also tried the non-intrusive version with the same result.
Am I missing something?
The problem was caused by linking against outdated libraries.

Multiple dispatch and multi-methods

What are they, what's the different between them?
Many sources, like Wikipedia, claim they're the same thing, but others explicitly say the opposite, like sbi in this question:
First: "Visitor Pattern is a way to simulate Double Dispatching in C++." This is, erm, not fully right. Actually, double dispatch is one form of multiple dispatch, which is a way to simulate (the missing) multi-methods in C++.
They are the same.
When you call a virtual method in C++, the actual method to run is based on the runtime type of the object them method is invoked on. This is called "single dispatch" because it depends on the type of a single argument (in this case, the implicit 'this' argument). So, for example, the following:
class Base {
public:
virtual int Foo() { return 3; }
}
class Derived : public Base {
public:
virtual int Foo() { return 123; }
}
int main(int argc, char *argv[]) {
Base* base = new Derived;
cout << "The result is " << base->Foo();
delete base;
return 0;
}
When run, the above program prints 123, not 3. So far so good.
Multiple-dispatch is the ability of a language or runtime to dispatch on both the type of the 'this' pointer and the type of the arguments to the method. Consider (sticking with C++ syntax for the moment):
class Derived;
class Base {
public:
virtual int Foo(Base *b) { cout << "Called Base::Foo with a Base*"; }
virtual int Foo(Derived *d) { cout << "Called Base::Foo with a Derived*"; }
}
class Derived : public Base {
public:
virtual int Foo(Base *b) { cout << "Called Derived::Foo with a Base*"; }
virtual int Foo(Derived *d) { cout << "Called Derived::Foo with a Derived*"; }
}
int main(int argc, char *argv[]) {
Base* base = new Derived;
Base* arg = new Derived;
base->Foo(arg);
delete base;
delete arg;
return 0;
}
If C++ had multiple-dispatch, the program would print out "Called Derived::Foo with a Dervied*". (Sadly, C++ does not have multiple-dispatch, and so the program prints out "Called Derived::Foo with a Base*".)
Double-dispatch is a special case of multiple-dispatch, often easier to emulate, but not terribly common as a language feature. Most languages do either single-dispatch or multiple-dispatch.