Use reference instead of pointers in google test fixture? - c++

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));
}

Related

Wrong context when calling method

How can I achieve that CoroutineManager::Routine() calls Operator::Worker() ?
Worker() must be called by Routine() in this test scenario.
So the question is whether how C++ handle the context. The Routine() method must not implemented by the Operator class itself.
template <class T>
class CoroutineManager {
private:
T var;
int _a, _b;
public:
CoroutineManager(int a, int b);
T Worker();
void Routine();
};
template <class T>
CoroutineManager<T>::CoroutineManager(int a, int b) {
this->_a = a;
this->_b = b;
}
template <class T>
T CoroutineManager<T>::Worker() {
std::cout << "wrong method" << std::endl;
return var;
}
template <class T>
void CoroutineManager<T>::Routine() {
std::cout << this->Worker() << std::endl;
}
class Operator : public CoroutineManager<double> {
using CoroutineManager::CoroutineManager;
public:
Operator(int a, int b) : CoroutineManager(a,b) {};
virtual double Worker();
};
double Operator::Worker() {
return 3.141;
}
// MARK: -
int main(int argc, const char * argv[]) {
Operator *op = new Operator(3,4);
op->Routine();
return 0;
}
I've changed the code to fulfill my requirements, but maybe there are exists more straight forward solutions(?). It's only about Worker and Worker2, two different methods in two objects which can be called by the derived Routine method without the boundaries of inheritance context:
// MARK: -
template <typename T, typename V>
class CoroutineManager {
private:
V _a, _b;
V (T::*workerPtr)();
T *cm;
public:
CoroutineManager(V a, V b) {
this->_a = a;
this->_b = b;
}
void Routine() {
std::cout << (*cm.*workerPtr)() << std::endl;
}
void SetWorker(T *cm, V (T::*ptr)()) {
this->workerPtr = ptr;
this->cm = cm;
}
V getA() {
return this->_a;
}
V getB() {
return this->_b;
}
};
// MARK: -
class Operator : public CoroutineManager<Operator,int> {
private:
int xx;
public:
Operator(int a, int b) : CoroutineManager(a,b) {
this->xx = a*2 + b*2;
};
int Worker();
};
int Operator::Worker() {
return getA() * getB() + this->xx;
}
// MARK: -
class Operator2 : public CoroutineManager<Operator2,double> {
public:
Operator2(double a, double b) : CoroutineManager(a,b) {};
double Worker2();
};
double Operator2::Worker2() {
return getA() + getB();
}
// MARK: -
int main(int argc, const char * argv[]) {
Operator *op = new Operator(4,4);
int (Operator::*workerPtr)() = &Operator::Worker;
op->SetWorker(op, workerPtr);
op->Routine();
Operator2 *op2 = new Operator2(3.14,2.78);
double (Operator2::*workerPtr2)() = &Operator2::Worker2;
op2->SetWorker(op2, workerPtr2);
op2->Routine();
return 0;
}
Output:
32
5.92
Program ended with exit code: 0

Mocking a non-virtual function using Google Mock?

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);
}

Pointer to a member-function

I would like to do the following:
I have two classes, A and B, and want to bind a function from A to a function from B so that whenever something calls the function in B, the function from A is called.
So basically, this is the scenario:
(important A and B should be independent classes)
This would be class A:
class A {
private:
// some needed variables for "doStuff"
public:
void doStuff(int param1, float *param2);
}
This is class B
class B {
private:
void callTheFunction();
public:
void setTheFunction();
}
And this is how I would like to work with these classes:
B *b = new B();
A *a = new A();
b->setTheFunction(a->doStuff); // obviously not working :(
I've read that this could be achieved with std::function, how would this work? Also, does this have an impact in the performance whenever callTheFunction() is called? In my example, its a audio-callback function which should call the sample-generating function of another class.
Solution based on usage C++11 std::function and std::bind.
#include <functional>
#include <stdlib.h>
#include <iostream>
using functionType = std::function <void (int, float *)>;
class A
{
public:
void doStuff (int param1, float * param2)
{
std::cout << param1 << " " << (param2 ? * param2 : 0.0f) << std::endl;
};
};
class B
{
public:
void callTheFunction ()
{
function (i, f);
};
void setTheFunction (const functionType specificFunction)
{
function = specificFunction;
};
functionType function {};
int i {0};
float * f {nullptr};
};
int main (int argc, char * argv [])
{
using std::placeholders::_1;
using std::placeholders::_2;
A a;
B b;
b.setTheFunction (std::bind (& A::doStuff, & a, _1, _2) );
b.callTheFunction ();
b.i = 42;
b.f = new float {7.0f};
b.callTheFunction ();
delete b.f;
return EXIT_SUCCESS;
}
Compile:
$ g++ func.cpp -std=c++11 -o func
Output:
$ ./func
0 0
42 7
Here's a basic skeleton:
struct B
{
A * a_instance;
void (A::*a_method)(int, float *);
B() : a_instance(nullptr), a_method(nullptr) {}
void callTheFunction(int a, float * b)
{
if (a_instance && a_method)
{
(a_instance->*a_method)(a, b);
}
}
};
Usage:
A a;
B b;
b.a_instance = &a;
b.a_method = &A::doStuff;
b.callTheFunction(10, nullptr);
This i basic a solution
class A {
private:
// some needed variables for "doStuff"
public:
void doStuff(int param1, float *param2)
{
}
};
typedef void (A::*TMethodPtr)(int param1, float *param2);
class B {
private:
TMethodPtr m_pMethod;
A* m_Obj;
void callTheFunction()
{
float f;
(m_Obj->*m_pMethod)(10, &f);
}
public:
void setTheFunction(A* Obj, TMethodPtr pMethod)
{
m_pMethod = pMethod;
m_Obj = Obj;
}
};
void main()
{
B *b = new B();
A *a = new A();
b->setTheFunction(a, A::doStuff); // now work :)
}

How to get rid of the ugly record

class c {
private:
int n[10];
public:
c();
~c();
int operator()(int i) { return n[i];};
};
class cc {
private:
public:
c *mass;
cc();
~cc();
c& operator*() const {return *mass;};
};
int somfunc() {
c *c1 = new c();
cc * cc1 = new cc();
(*cc1->mass)(1);
delete c1;
}
I've got a pointer into class cc to class c.
Is there any way to get rid of record like this:
(*cc1->mass)(1);
and write somethink like that:
cc1->mass(1);
is it impossible?
When I saw the tags "c++" and "operator overloading", my mind alarm turns ON.
C++ operator overloading is complex, and some operators like "()" or "->" make it more difficult.
I suggest, before overloading operators, making either a global function or method with the same purpouse, test it works, and later replace it with the operator.
Global friend function example:
class c {
private:
int n[10];
public:
c();
~c();
// int operator()(int i) { return n[i]; }
// there is a friend global function, that when receives a "c" object,
// as a parameter, or declares a "c" object, as a local variable,
// this function, will have access to the "public" members of "c" objects,
// the "thisref" will be removed, when turned into a method
friend int c_subscript(c thisref, int i) ;
};
int c_subscript(c* thisref, int i)
{
return c->n[i];
}
int main()
{
c* objC() = new c();
// do something with "objcC"
int x = c_subscript(objC, 3);
// do something with "x"
return 0;
} // int main(...)
Local function ( "method" ) example:
class c {
private:
int n[10];
public:
c();
~c();
// int operator()(int i) { return n[i]; }
int subscript(int i) ;
};
int c::subscript(int i)
{
return this.n[i];
}
int main()
{
c* objC() = new c();
// do something with "objcC"
int x = c->subscript(objC, 3);
// do something with "x"
return 0;
} // int main(...)
And, finally use the overloaded operator:
class c {
private:
int n[10];
public:
c();
~c();
int subscript(int i) ;
int operator()(int i) { return this.subscript(i); }
};
int c::subscript(int i)
{
return this.n[i];
}
int main()
{
c* objC() = new c();
// do something with "objcC"
int x = c->subscript(3);
// do something with "x"
int x = c(3);
// do something with "x"
return 0;
} // int main(...)
Note that in the final example, I keep the method with a unique identifier.
Cheers.
Could always do this:
class cc {
private:
c *_mass;
public:
c& mass() const {return *_mass;};
};
Now..
cc1->mass()(1);
If mass were an object, not a pointer, you could use the syntax you want:
class cc {
private:
public:
c mass;
cc();
~cc();
const c& operator*() const {return mass;};
};
…
cc1->mass(1);
You can with
(*(*cc1))(1)
because operator() is applied to an object, not a pointer.
You can use
(**cc1)(1);
Or
cc1->mass->operator()(1);

Is there a way to forbid casting to subclass that is non-const in C++?

Here is a complete example.
I want to forbid using A::set from objects casted from B to A by allowing only casting
B to const A.
How to do it?
(I can't use virtual functions)
#include <iostream>
#include <cassert>
using namespace std;
class A {
public:
int get() const { return i_; }
void set(int i) { i_ = i; }
protected:
int i_;
};
class B : public A {
public:
int ok() const { return A::get() == copy_i_; }
void set(int i) { A::set(i); copy_i_ = i; }
protected:
int copy_i_;
};
void test2() {
A a;
a.set(3); // ok here
cout << a.get() << endl;
B b;
b.set(5);
A& aa = b;
assert(b.ok());
aa.set(3); // not ok here
assert(b.ok()); // fail-here
}
int main() {
test2();
return 0;
}
You could make the inheritance private and provide a member function in B to use instead of casting.
const A& B::convert_to_A() const { return *this; }
Why casting? Making void A::set(int i) protected will work in your case.
There is no need for forbidding non-const casts. You can solve your problem by using the template method design pattern.
#include "stdafx.h"
#include <iostream>
#include <cassert>
using namespace std;
class A {
public:
int get() const { return i_; }
void set(int i) { assert(i_ = i); copy_i();}
protected:
int i_;
virtual void copy_i(){};
};
class B : public A {
public:
int ok() const { return A::get() == copy_i_; }
protected:
int copy_i_;
void copy_i(){copy_i_ = i_; }
};
void test2() {
B b;
b.set(5);
A& a = b;
assert(b.ok());
a.set(3);
assert(b.ok()); // success!
}
int main() {
test2();
return 0;
}