Mocking a non-virtual function using Google Mock? - c++

I am trying to mock the function sub so that I can test the function add.I am using non-virtual function,
//Non_virtual function
class baseclass {
public:
int add(int a, int b) {
return (a + sub(a, b));
}
int sub(int c, int d) {
return (c - d);
}
};
class mockclass {
public:
MOCK_METHOD2(sub, int(int a, int b));
};
TEST(sample_test, testmain) {
mockclass mo;
int c = 12;
int d = 4;
EXPECT_CALL(mo, sub(c, d))
.WillOnce(testing::Return(8));
EXPECT_EQ(mo.add(c, d), 20);
}
I don't know how to make the relationship between the add and sub and don't know where I was making mistake.
I can do it with virtual function but I want to do it in non-virtual function.
Thanks in advance

Possible way without virtual:
struct MySub
{
int sub(int c, int d) const { return c - d; }
};
template <typename Sub>
class baseclassT : public Sub
{
public:
int add(int a, int b) {
return (a + this->sub(a, b));
}
};
using baseclass = baseclassT<MySub>; // For prod
And then, for test:
class MockSub {
public:
MOCK_METHOD2(sub, int(int a, int b));
};
TEST(sample_test, testmain)
{
baseclassT<MockSub> mo;
int c = 12;
int d = 4;
EXPECT_CALL(mo, sub(c, d)).WillOnce(testing::Return(8));
EXPECT_EQ(mo.add(c, d), 20);
}

Related

Can you construct after instantiation? Avoiding empty constructor

I have a class which has a member attribute consisting of an object defined elsewhere. In the code below, A contains a public attribute var which is a B:
class B {
public:
int x, y;
std::vector<int> z;
B(int a, int b, std::vector<int> c) {
x = a; y = b; z = c;
}
};
class A {
public:
B var;
A(int i, int j) {
std::vector<int> someVector;
B(i, j, someVector);
}
};
int main() {
A foo(5, 3);
return 0;
}
This (obviously) doesn't compile as var is instantiated upon an instantiation of A, too late for it to be constructed.
The best way I can do something similar is modify some code:
class B {
public:
int x, y;
std::vector<int> z;
B() {}
void setAttributes(int a, int b, std::vector<int> c) {
x = a; y = b; z = c;
}
};
class A {
public:
B var;
A(int i, int j) {
std::vector<int> someVector;
B.setAttributes(i, j, someVector);
}
};
This does compile because attributes are set after instantiation.
But is there a way to remain closer to the first code snippet?
A(int i, int j) : var(i, j, {}) {}
Also, in your code B(i, j, someVector); does not initialize member variable var, and B.setAttributes(i, j, someVector); wouldn't compile at all.
if you cannot define a useful default constructor and don't want the ugly two step initialization, I guess there is no way around a pointer to B. Something like
#include <memory>
class B {
public:
int x, y;
std::vector<int> z;
B(int a, int b, std::vector<int> c) {
x = a; y = b; z = c;
}
};
class A {
public:
std::unique_ptr<B> var;
A() {
std::vector<int> someVector;
var = std::make_unique<B>(5, 2, someVector);
}
};
int main() {
A foo();
return 0;
}
should do the trick.

Fill map in Command pattern

#include <map>
class ICommand
{
public:
virtual double execute(double, double);
~ICommand();
};
class Add: public ICommand
{
public:
double execute(double a, double b) override
{
return a + b;
}
double operator()(double a, double b){
return a + b;
}
};
class Sub : public ICommand
{
public:
double execute(double a, double b) override
{
return a - b;
}
double operator()(double a, double b) {
return a - b;
}
};
class Mul : public ICommand
{
public:
double execute(double a, double b) override
{
return a * b;
}
double operator()(double a, double b) {
return a * b;
}
};
class Div : public ICommand
{
public:
double execute(double a, double b) override
{
return a / b;
}
double operator()(double a, double b) {
return a / b;
}
};
class RequestHundler
{
std::map<int, ICommand*> commands;
Add* add;
Sub* sub;
Mul* mul;
Div* div;
public:
RequestHundler()
{
commands[1] = add;
commands[2] = sub;
commands[3] = mul;
commands[4] = div;
}
double HandleRequest(int action, double a, double b)
{
ICommand* command = commands[action];
return command->execute(a, b);
}
};
int main(double argc, char* argv[])
{
RequestHundler* handler = new RequestHundler();
double result = handler->HandleRequest(2, 4, 6);
return 0;
}
I have access violation in command->execute(a, b);
, because map contains only null pointer, after filling. What is right way to store and filling map?
I think I should use factory for creating classes, but even in this case I must to fill map, and I not very want to use global variable for saving map. Maybe any good idea about this code?

Operator +/- overloading as a non member function in C++

So I have written some code that implements this hierarchy:
class A
{
int x;
int y;
public:
A () { }
void setX(int x) { this->x = x;}
void setY(int y) { this->y = y;}
int getX(void) { return x;}
int getY(void) { return y;}
virtual int somefunc() = 0;
friend B operator- ( B b1, B b2);
};
class B : public A
{
int somefunc() {return 0;}
};
class C : public A
{
int somefunc() {return 1;}
};
class D : public C
{
int somefunc() {return 2;}
};
/*
// 1 st attempt - fail
A operator- (const A& a_inst, const A& a_inst2)
{
A a_temp;
a_temp.setX( a_inst.getX() - a_inst2.getX() );
a_temp.setY( a_inst.getY() - a_inst2.getY() );
return a_temp;
}
// 2nd attempt - FAIL
const A* operator- (const A* a1, const A* a2)
{
a1.setX( a1.getX() - a2.getX() );
a1.setY( a1.getY() - a2.getY() );
return a1;
}
//*/
//3rd attempt
B operator- ( B b1, B b2)
{
int temp1x = b1.getX();
int temp2x = b2.getX();
b1.setX( temp1x - temp2x );
int temp1y = b1.getY();
int temp2y = b2.getY();
b2.setY( temp1y - temp2y );
return b1;
}
int main()
{
B b();
C c();
b = b - dynamic_cast<B*>(c) ;
}
I understand that since A is an abstract class it can't be instantiated, so I can't do it with Class A instances.
Is it possible to overload both +/- once (each) and make it apply for every instance that belongs to a class in this hierarchy? Also I want to point out that I want to be able to do the same thing with objects of different classes at the same time like so:
C c;
B b;
b = b - c;
EDIT 1~ added a second version of the overloading that I am currently trying to get to work.
EDIT 2~ Corrected mistaken call of setters
EDIT 3~ added a 3rd version, still getting errors
There are some problems in you code:
1. You should specify the inheritance type: public
class B : public A {
...
}
2. B operator- (const B& a_inst, const B& a_inst2)
{
B a_temp;
a_temp.setX() = a.getX() - a_inst2.getX();
a_temp.setY() = a.getY() - a_inst2.getY();
return a_temp;
}
You can't use like that, since setX and setY returns value not pointer or reference.

Derived method in not called in base class

I don't know how to put a right title. sorry by my problem is very hard with me.
I have a class myCal.h:
class myCal
{
public:
myCal();
int add(int a, int b);
int sub(int a, int b);
int expresstion(int a, int b, int c);
};
and myCal.cpp:
myCal::myCal()
{
}
int myCal::add(int a, int b)
{
return a+b;
}
int myCal::sub(int a, int b)
{
return a-b;
}
int myCal::expresstion(int a, int b, int c)
{
return add(sub(a, b), c);
}
in main.cpp, i have class mockcal like this:
class mockcal : public myCal
{
public:
int sub(int a, int b)
{
return 100;
}
int expresstion(int a, int b, int c)
{
return myCal::expresstion(a,b,c);
}
};
if I run myCal.expresstion(3,2,1), the return value is 2, that's OK,
but when I run mockCal.expresstion(3,2,1), the return values still is 2, I want it return 101.
please help me to do this, but do not change anything in mockCal::expresstion.
Thanks so much.
You need to make the method int sub(int a, int b) virtual in the base class ( myCal class in your code), if you want to override it in the mockcal class
class myCal
{
public:
//...
virtual int sub(int a, int b);
//..
};

Use reference instead of pointers in google test fixture?

Here is the code:
#include <gtest/gtest.h>
double sq(const double x) {
return x*x;
}
class Sqtest : public ::testing::Test {
protected:
virtual void SetUp() {
a = new int(1);
b = new int(2);
c = new int(3);
}
virtual void TearDown() {
delete a;
delete b;
delete c;
}
int *a, *b, *c;
};
TEST_F (Sqtest, posnos) {
EXPECT_EQ(1, sq(*a));
EXPECT_EQ(4, sq(*b));
EXPECT_EQ(9, sq(*c));
}
This is all nice, but what if in the function Sq I use reference, i.e.
double sq(const double& x) {
return x*x;
}
How should I modify the fixture accordingly?
There's no really need to use pointer for this particular example. Let Sqtest members be of type int and you're done:
#include <gtest/gtest.h>
int sq(int x)
{
return x * x;
}
class Sqtest : public ::testing::Test
{
protected:
virtual void SetUp() override
{
a = 1;
b = 2;
c = 3;
}
int a, b, c;
};
TEST_F(Sqtest, posnos)
{
EXPECT_EQ(1, sq(a));
EXPECT_EQ(4, sq(b));
EXPECT_EQ(9, sq(c));
}